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 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 AbstractDependencyInjectionSpringContextTests {
37
38 /**
39 * Default constructor.
40 */
41 protected SpringUnitTest() {
42 this(null);
43 }
44
45 /**
46 * Constructor with JUnit name.
47 * Sets AutowireMode to "by name" (overriding the default, which is "by type").
48 * Creates a context contexts that stores the
49 * hierarchy of test contexts.
50 * @param fName Name of JUnit test
51 */
52 protected SpringUnitTest(String fName) {
53 super(fName);
54 setAutowireMode(AUTOWIRE_BY_NAME);
55 this.contexts = new HierarchicalSpringUnitContext<SpringUnitTest>(getClass());
56 }
57
58 /**
59 * Return list of file names that the
60 * Spring test framework uses in order to create beans for testing.
61 * @return Array of string filenames
62 */
63 protected final String[] getConfigLocations() {
64 return this.contexts.getConfigLocations();
65 }
66
67 /**
68 * Search for object identified by <code>key</code>
69 * in the hierarchy of contexts.
70 * @param key Identifier of data value to find
71 * @return Object if found or null
72 * @throws Exception if errors occur when using reflection
73 * to access the SpringUnitContext for any
74 * class in the list
75 */
76 protected final <T> T getObject(String key) throws Exception {
77 return (T)this.contexts.getObject(key, getName(), this);
78 }
79
80 /**
81 * Container of the hierarchy of contexts for the test.<br/>
82 */
83 private HierarchicalSpringUnitContext<SpringUnitTest> contexts;
84
85 }