01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 org.springframework.test.AbstractDependencyInjectionSpringContextTests;
20:
21: /**
22: * Extends Spring's test framework to support data-driven tests.
23: * Data-driven tests separate data values from test logic,
24: * keeping data values in external files and test logic in Java code.
25: * Every descendent of SpringUnitTest is required by convention
26: * to have a bean called <code><i>Classname</i></code> of type
27: * SpringUnitContext, where <code><i>Classname</i></code> is
28: * the simple name of the subclass of SpringUnitTest.
29: * Note that the simple names of subclasses must be unique,
30: * that is, they must not be distinguished solely by different
31: * package qualifiers.
32: *
33: * @author <a href="mailto:ted@velkoff.com">Ted Velkoff</a>
34: *
35: */
36: public abstract class SpringUnitTest extends
37: AbstractDependencyInjectionSpringContextTests {
38:
39: /**
40: * Default constructor.
41: */
42: protected SpringUnitTest() {
43: this (null);
44: }
45:
46: /**
47: * Constructor with JUnit name.
48: * Sets AutowireMode to "by name" (overriding the default, which is "by type").
49: * Creates a context contexts that stores the
50: * hierarchy of test contexts.
51: * @param fName Name of JUnit test
52: */
53: protected SpringUnitTest(String fName) {
54: super (fName);
55: setAutowireMode(AUTOWIRE_BY_NAME);
56: this .contexts = new HierarchicalSpringUnitContext<SpringUnitTest>(
57: getClass());
58: }
59:
60: /**
61: * Return list of file names that the
62: * Spring test framework uses in order to create beans for testing.
63: * @return Array of string filenames
64: */
65: protected final String[] getConfigLocations() {
66: return this .contexts.getConfigLocations();
67: }
68:
69: /**
70: * Search for object identified by <code>key</code>
71: * in the hierarchy of contexts.
72: * @param key Identifier of data value to find
73: * @return Object if found or null
74: * @throws Exception if errors occur when using reflection
75: * to access the SpringUnitContext for any
76: * class in the list
77: */
78: protected final <T> T getObject(String key) throws Exception {
79: return (T) this .contexts.getObject(key, getName(), this );
80: }
81:
82: /**
83: * Container of the hierarchy of contexts for the test.<br/>
84: */
85: private HierarchicalSpringUnitContext<SpringUnitTest> contexts;
86:
87: }
|