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.AbstractTransactionalSpringContextTests;
20
21 /**
22 * Extends Spring's test framework to support transactional 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 SpringUnitTransactionalTest 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 SpringUnitTransactionalTest.
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 SpringUnitTransactionalTest extends AbstractTransactionalSpringContextTests {
37
38 /**
39 * Default constructor.
40 */
41 protected SpringUnitTransactionalTest() {
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 hierarchy of contexts.
49 * @param fName Name of JUnit test
50 */
51 protected SpringUnitTransactionalTest(String name) {
52 super(name);
53 setAutowireMode(AUTOWIRE_BY_NAME);
54 this.contexts = new HierarchicalSpringUnitContext<SpringUnitTransactionalTest>(getClass());
55 }
56
57 /**
58 * Return list of file names that the
59 * Spring test framework uses in order to create beans for testing.
60 * @return Array of string filenames
61 */
62 protected final String[] getConfigLocations() {
63 return this.contexts.getConfigLocations();
64 }
65
66 /**
67 * Search for object identified by <code>key</code>
68 * in the hierarchy of contexts.
69 * @param key Identifier of data value to find
70 * @return Object if found or null
71 * @throws Exception if errors occur when using reflection
72 * to access the SpringUnitContext for any
73 * class in the list
74 */
75 protected final <T> T getObject(String key) throws Exception {
76 return (T)this.contexts.getObject(key, getName(), this);
77 }
78
79 /**
80 * Subclasses should override this method to perform any setup operations,
81 * such as populating a database table, <i>within</i> the transaction
82 * created by this class.
83 * <p><b>NB:</b> Not called if there is no transaction management, due to no
84 * transaction manager being provided in the context.<br/>
85 * <b>Note:</b> This overriding of <code>onSetUpInTransaction</code>
86 * is deprecated in SpringUnit and will be removed in a future release.
87 * Subclasses that previously overrode <code>onSetUpInTransactionAtBeginning</code>
88 * should instead override this method.
89 * @throws Exception simply let any exception propagate
90 */
91 protected void onSetUpInTransaction() throws Exception {
92 onSetUpInTransactionAtBeginning();
93 }
94
95 /**
96 * @deprecated Subclasses should instead override <code>onSetUpInTransaction</code>
97 * directly.
98 * This method is maintained for backward compatibility but will be removed
99 * in a future release.
100 * @throws Exception
101 */
102 protected void onSetUpInTransactionAtBeginning() throws Exception {
103 }
104
105 /**
106 * Calls onTearDownAfterTransactionEnds so that subclasses can
107 * insert behavior at conclusion of transaction.
108 * The transaction is <i>not open anymore</i> at this point.
109 * Follows by calling setDirty so that all beans created
110 * from configuration files are refreshed.
111 * @throws Exception simply let any exception propagate
112 */
113 protected final void onTearDownAfterTransaction() throws Exception {
114 try {
115 onTearDownAfterTransactionEnds();
116 }
117 finally {
118 setDirty();
119 }
120 }
121
122 /**
123 * Subclasses can override this method to perform cleanup here.
124 * The transaction is <i>not open anymore</i> at this point.
125 * @throws Exception simply let any exception propagate
126 */
127 protected void onTearDownAfterTransactionEnds() throws Exception {
128 }
129
130 /**
131 * Container of the hierarchy of contexts for the test.<br/>
132 */
133 private HierarchicalSpringUnitContext<SpringUnitTransactionalTest> contexts;
134
135 }