001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springunit.framework;
018:
019: import java.util.Map;
020:
021: /**
022: * A bean that holds data for a single SpringUnitTest.<br/>
023: * The bean contains a single attribute, data,
024: * that is a map of strings to objects. The string keys
025: * are either the names of tests or arbitrary identifiers
026: * for data values. For keys that name tests,
027: * the corresponding value is itself a map of string keys
028: * to object values.<br/>
029: *
030: * @author <a href="mailto:ted@velkoff.com">Ted Velkoff</a>
031: *
032: */
033: public class SpringUnitContext<T> {
034:
035: /**
036: * Test data values associated with a TestCase.
037: * By convention, keys of the form "testXxx"
038: * have associated values of the type
039: * Map<? extends String, ? extends T>.
040: * All such elements comprise data items
041: * in the scope of the test having the same
042: * name as the key.
043: * Keys of any other form have values of type T,
044: * and represent data in the scope of the test case.<br/>
045: * @return Map<? extends String, ? extends T>
046: */
047: public Map<? extends String, ? extends T> getData() {
048: return this .data;
049: }
050:
051: /**
052: * Find the data value identified by <code>key</code>,
053: * searching first in the scope of the name
054: * of the test (<code>fName</code>), then outward
055: * until found or <code>null</code> is returned.<br/>
056: * @param key Identifier of data value to be found
057: * @param fName Name of test in whose scope search
058: * should begin
059: * @return Object of type T if found, null otherwise
060: */
061: public T getObject(String key, String fName) {
062: return getObject(key, fName, getData());
063: }
064:
065: /**
066: * Make <code>data</code> the test data values
067: * associcated with TestCase.<br/>
068: * @param data Data values indexed by string names
069: */
070: public void setData(Map<? extends String, ? extends T> data) {
071: this .data = data;
072: }
073:
074: /**
075: * Find the data value identified by <code>key</code>
076: * in the <code>map</code> by searching first
077: * in the scope of the test (<code>fName</code>).
078: * If <code>fName</code> is the name of a map
079: * <code>testMap</code>
080: * contained in <code>map</code>, then return
081: * the value in <code>testMap</code> associated
082: * with <code>key</code>. If there is no
083: * value in <code>testMap</code> associated with
084: * <code>key</code>, then return the value
085: * associated with <code>key</code> in <code>map</code>,
086: * which could be <code>null</code>.
087: * @param key Identifier of data value
088: * @param fName Identifier of test; must exactly match
089: * the name of a test procedure in a SpringUnitTest.
090: * @param map Map of string identifiers to objects
091: * where the string is either an fName or a key
092: * @return Object of type T if found, otherwise null
093: */
094: protected T getObject(String key, String fName,
095: Map<? extends String, ? extends T> map) {
096: T testData = map.get(fName);
097: if ((testData != null) && testData instanceof Map) {
098: Map<? extends String, ? extends T> testMap = (Map<? extends String, ? extends T>) testData;
099: T obj = testMap.get(key);
100: if (obj != null) {
101: return obj;
102: }
103: }
104: return map.get(key);
105: }
106:
107: /**
108: * Map containing data that comprises the test context.<br/>
109: */
110: private Map<? extends String, ? extends T> data;
111:
112: }
|