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 org.springframework.test.AbstractTransactionalSpringContextTests;
020:
021: /**
022: * Extends Spring's test framework to support transactional data-driven tests.
023: * Data-driven tests separate data values from test logic,
024: * keeping data values in external files and test logic in Java code.
025: * Every descendent of SpringUnitTransactionalTest is required by convention
026: * to have a bean called <code><i>Classname</i></code> of type
027: * SpringUnitContext, where <code><i>Classname</i></code> is
028: * the simple name of the subclass of SpringUnitTransactionalTest.
029: * Note that the simple names of subclasses must be unique,
030: * that is, they must not be distinguished solely by different
031: * package qualifiers.
032: *
033: * @author <a href="mailto:ted@velkoff.com">Ted Velkoff</a>
034: *
035: */
036: public abstract class SpringUnitTransactionalTest extends
037: AbstractTransactionalSpringContextTests {
038:
039: /**
040: * Default constructor.
041: */
042: protected SpringUnitTransactionalTest() {
043: this (null);
044: }
045:
046: /**
047: * Constructor with JUnit name.
048: * Sets AutowireMode to "by name" (overriding the default, which is "by type").
049: * Creates a hierarchy of contexts.
050: * @param fName Name of JUnit test
051: */
052: protected SpringUnitTransactionalTest(String name) {
053: super (name);
054: setAutowireMode(AUTOWIRE_BY_NAME);
055: this .contexts = new HierarchicalSpringUnitContext<SpringUnitTransactionalTest>(
056: getClass());
057: }
058:
059: /**
060: * Return list of file names that the
061: * Spring test framework uses in order to create beans for testing.
062: * @return Array of string filenames
063: */
064: protected final String[] getConfigLocations() {
065: return this .contexts.getConfigLocations();
066: }
067:
068: /**
069: * Search for object identified by <code>key</code>
070: * in the hierarchy of contexts.
071: * @param key Identifier of data value to find
072: * @return Object if found or null
073: * @throws Exception if errors occur when using reflection
074: * to access the SpringUnitContext for any
075: * class in the list
076: */
077: protected final <T> T getObject(String key) throws Exception {
078: return (T) this .contexts.getObject(key, getName(), this );
079: }
080:
081: /**
082: * Subclasses should override this method to perform any setup operations,
083: * such as populating a database table, <i>within</i> the transaction
084: * created by this class.
085: * <p><b>NB:</b> Not called if there is no transaction management, due to no
086: * transaction manager being provided in the context.<br/>
087: * <b>Note:</b> This overriding of <code>onSetUpInTransaction</code>
088: * is deprecated in SpringUnit and will be removed in a future release.
089: * Subclasses that previously overrode <code>onSetUpInTransactionAtBeginning</code>
090: * should instead override this method.
091: * @throws Exception simply let any exception propagate
092: */
093: protected void onSetUpInTransaction() throws Exception {
094: onSetUpInTransactionAtBeginning();
095: }
096:
097: /**
098: * @deprecated Subclasses should instead override <code>onSetUpInTransaction</code>
099: * directly.
100: * This method is maintained for backward compatibility but will be removed
101: * in a future release.
102: * @throws Exception
103: */
104: protected void onSetUpInTransactionAtBeginning() throws Exception {
105: }
106:
107: /**
108: * Calls onTearDownAfterTransactionEnds so that subclasses can
109: * insert behavior at conclusion of transaction.
110: * The transaction is <i>not open anymore</i> at this point.
111: * Follows by calling setDirty so that all beans created
112: * from configuration files are refreshed.
113: * @throws Exception simply let any exception propagate
114: */
115: protected final void onTearDownAfterTransaction() throws Exception {
116: try {
117: onTearDownAfterTransactionEnds();
118: } finally {
119: setDirty();
120: }
121: }
122:
123: /**
124: * Subclasses can override this method to perform cleanup here.
125: * The transaction is <i>not open anymore</i> at this point.
126: * @throws Exception simply let any exception propagate
127: */
128: protected void onTearDownAfterTransactionEnds() throws Exception {
129: }
130:
131: /**
132: * Container of the hierarchy of contexts for the test.<br/>
133: */
134: private HierarchicalSpringUnitContext<SpringUnitTransactionalTest> contexts;
135:
136: }
|