1 /*
2 * Copyright 2006 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.springunit.framework;
18
19 import java.util.Map;
20
21 /**
22 * A bean that holds data for a single SpringUnitTest.<br/>
23 * The bean contains a single attribute, data,
24 * that is a map of strings to objects. The string keys
25 * are either the names of tests or arbitrary identifiers
26 * for data values. For keys that name tests,
27 * the corresponding value is itself a map of string keys
28 * to object values.<br/>
29 *
30 * @author <a href="mailto:ted@velkoff.com">Ted Velkoff</a>
31 *
32 */
33 public class SpringUnitContext<T> {
34
35 /**
36 * Test data values associated with a TestCase.
37 * By convention, keys of the form "testXxx"
38 * have associated values of the type
39 * Map<? extends String, ? extends T>.
40 * All such elements comprise data items
41 * in the scope of the test having the same
42 * name as the key.
43 * Keys of any other form have values of type T,
44 * and represent data in the scope of the test case.<br/>
45 * @return Map<? extends String, ? extends T>
46 */
47 public Map<? extends String, ? extends T> getData() {
48 return this.data;
49 }
50
51 /**
52 * Find the data value identified by <code>key</code>,
53 * searching first in the scope of the name
54 * of the test (<code>fName</code>), then outward
55 * until found or <code>null</code> is returned.<br/>
56 * @param key Identifier of data value to be found
57 * @param fName Name of test in whose scope search
58 * should begin
59 * @return Object of type T if found, null otherwise
60 */
61 public T getObject(String key, String fName) {
62 return getObject(key, fName, getData());
63 }
64
65 /**
66 * Make <code>data</code> the test data values
67 * associcated with TestCase.<br/>
68 * @param data Data values indexed by string names
69 */
70 public void setData(Map<? extends String, ? extends T> data) {
71 this.data = data;
72 }
73
74 /**
75 * Find the data value identified by <code>key</code>
76 * in the <code>map</code> by searching first
77 * in the scope of the test (<code>fName</code>).
78 * If <code>fName</code> is the name of a map
79 * <code>testMap</code>
80 * contained in <code>map</code>, then return
81 * the value in <code>testMap</code> associated
82 * with <code>key</code>. If there is no
83 * value in <code>testMap</code> associated with
84 * <code>key</code>, then return the value
85 * associated with <code>key</code> in <code>map</code>,
86 * which could be <code>null</code>.
87 * @param key Identifier of data value
88 * @param fName Identifier of test; must exactly match
89 * the name of a test procedure in a SpringUnitTest.
90 * @param map Map of string identifiers to objects
91 * where the string is either an fName or a key
92 * @return Object of type T if found, otherwise null
93 */
94 protected T getObject(String key, String fName, Map<? extends String, ? extends T> map) {
95 T testData = map.get(fName);
96 if ((testData != null) && testData instanceof Map) {
97 Map<? extends String, ? extends T> testMap = (Map<? extends String, ? extends T>)testData;
98 T obj = testMap.get(key);
99 if (obj != null) {
100 return obj;
101 }
102 }
103 return map.get(key);
104 }
105
106 /**
107 * Map containing data that comprises the test context.<br/>
108 */
109 private Map<? extends String, ? extends T> data;
110
111 }