001: /*
002: * Copyright 2006,2007 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 junit.framework.TestCase;
020:
021: import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
022: import org.springunit.framework.tests.OneDeepTransactionalTest;
023: import org.springunit.framework.tests.OneDeepUnitTest;
024: import org.springunit.framework.tests.ThreeDeepTransactionalTest;
025: import org.springunit.framework.tests.ThreeDeepUnitTest;
026: import org.springunit.framework.tests.TwoDeepTransactionalTest;
027: import org.springunit.framework.tests.TwoDeepUnitTest;
028:
029: public class HierarchicalSpringUnitContextTest extends TestCase {
030:
031: public void testConstructor() {
032: HierarchicalSpringUnitContext subject = createSubject(1, "unit");
033: }
034:
035: public void testGetConfigLocations1() throws Exception {
036: HierarchicalSpringUnitContext subject = createSubject(1, "unit");
037: String[] actual = subject.getConfigLocations();
038: String[] expected = new String[] { "classpath:OneDeepUnitTest.xml" };
039: assertTrue(areEqual(expected, actual));
040: }
041:
042: public void testGetConfigLocations2() throws Exception {
043: HierarchicalSpringUnitContext subject = createSubject(2, "unit");
044: String[] actual = subject.getConfigLocations();
045: String[] expected = new String[] {
046: "classpath:org/springunit/framework/tests/TwoDeepUnitTest.xml",
047: "classpath:OneDeepUnitTest.xml" };
048: assertTrue(areEqual(expected, actual));
049: }
050:
051: public void testGetConfigLocations3() throws Exception {
052: HierarchicalSpringUnitContext subject = createSubject(3, "unit");
053: String[] actual = subject.getConfigLocations();
054: String[] expected = new String[] {
055: "classpath:org/springunit/framework/tests/ThreeDeepUnitTest.xml",
056: "classpath:org/springunit/framework/tests/TwoDeepUnitTest.xml",
057: "classpath:OneDeepUnitTest.xml" };
058: assertTrue(areEqual(expected, actual));
059: }
060:
061: public void testGetConfigLocations4() throws Exception {
062: HierarchicalSpringUnitContext subject = createSubject(1,
063: "transaction");
064: String[] actual = subject.getConfigLocations();
065: String[] expected = new String[] { "classpath:OneDeepTransactionalTest.xml" };
066: assertTrue(areEqual(expected, actual));
067: }
068:
069: public void testGetConfigLocations5() throws Exception {
070: HierarchicalSpringUnitContext subject = createSubject(2,
071: "transaction");
072: String[] actual = subject.getConfigLocations();
073: String[] expected = new String[] {
074: "classpath:org/springunit/framework/tests/TwoDeepTransactionalTest.xml",
075: "classpath:OneDeepTransactionalTest.xml" };
076: assertTrue(areEqual(expected, actual));
077: }
078:
079: public void testGetConfigLocations6() throws Exception {
080: HierarchicalSpringUnitContext subject = createSubject(3,
081: "transaction");
082: String[] actual = subject.getConfigLocations();
083: String[] expected = new String[] {
084: "classpath:org/springunit/framework/tests/ThreeDeepTransactionalTest.xml",
085: "classpath:org/springunit/framework/tests/TwoDeepTransactionalTest.xml",
086: "classpath:OneDeepTransactionalTest.xml" };
087: assertTrue(areEqual(expected, actual));
088: }
089:
090: protected HierarchicalSpringUnitContext createSubject(int depth,
091: String type) {
092: AbstractDependencyInjectionSpringContextTests test = createTest(
093: depth, type);
094: HierarchicalSpringUnitContext result = new HierarchicalSpringUnitContext(
095: test.getClass());
096: return result;
097: }
098:
099: protected class ClassTest extends SpringUnitTest {
100: public ClassTest() {
101: }
102:
103: public ClassTest(String fName) {
104: super (fName);
105: }
106: }
107:
108: protected AbstractDependencyInjectionSpringContextTests createTest(
109: int depth, String type) {
110: if ("unit".equals(type)) {
111: switch (depth) {
112: case 1:
113: return new OneDeepUnitTest();
114: case 2:
115: return new TwoDeepUnitTest();
116: case 3:
117: return new ThreeDeepUnitTest();
118: default:
119: throw new IllegalArgumentException(
120: "depth must be an integer between 1 and 3");
121: }
122: } else if ("transaction".equals(type)) {
123: switch (depth) {
124: case 1:
125: return new OneDeepTransactionalTest();
126: case 2:
127: return new TwoDeepTransactionalTest();
128: case 3:
129: return new ThreeDeepTransactionalTest();
130: default:
131: throw new IllegalArgumentException(
132: "depth must be an integer between 1 and 3");
133: }
134: } else {
135: throw new IllegalArgumentException(
136: "type must be either \"unit\" or \"transaction\"");
137: }
138: }
139:
140: protected boolean areEqual(String[] left, String[] right) {
141: boolean result = left.length == right.length;
142: if (result) {
143: for (int i = 0; i < left.length; i++) {
144: result &= ((left[i] == null && right[i] == null) || left[i] != null
145: && left[i].equals(right[i]));
146: if (!result) {
147: break;
148: }
149: }
150: }
151: return result;
152: }
153:
154: }
|