1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.castor.xmlctf;
17
18 import java.util.Iterator;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
23 import org.exolab.castor.tests.framework.testDescriptor.ConfigurationType;
24 import org.exolab.castor.tests.framework.testDescriptor.CustomTest;
25 import org.exolab.castor.tests.framework.testDescriptor.FailureType;
26 import org.exolab.castor.tests.framework.testDescriptor.types.FailureStepType;
27 import org.exolab.castor.xml.XMLContext;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 class TestWithCustomTest extends TestCase {
52
53 private static final String CUSTOM = "_CustomTest";
54
55
56 private final XMLTestCase _delegate;
57
58
59 protected final FailureType _failure;
60
61 protected final boolean _failureExpected;
62
63
64
65
66
67
68
69 TestWithCustomTest(final String name) {
70 super(name + CUSTOM);
71 throw new IllegalArgumentException("You cannot use the name-only constructor");
72 }
73
74
75
76
77
78
79
80 TestWithCustomTest(final String name, final XMLTestCase tc) {
81 super(name + CUSTOM);
82 _delegate = tc;
83 _failure = tc._failure;
84 _failureExpected = _failure != null && _failure.getContent();
85 }
86
87
88
89
90
91
92
93 protected void setUp() throws Exception {
94 _delegate.setXMLContext(new XMLContext());
95
96 if (_delegate instanceof MarshallingFrameworkTestCase) {
97 ((MarshallingFrameworkTestCase) _delegate).setUp();
98 } else if (_delegate instanceof SourceGeneratorTestCase) {
99 ((SourceGeneratorTestCase) _delegate).setUp();
100 }
101 }
102
103
104
105
106
107
108
109 protected void tearDown() throws Exception {
110 if (_delegate instanceof MarshallingFrameworkTestCase) {
111 ((MarshallingFrameworkTestCase) _delegate).tearDown();
112 } else if (_delegate instanceof SourceGeneratorTestCase) {
113 ((SourceGeneratorTestCase) _delegate).tearDown();
114 }
115 }
116
117
118
119
120
121
122 public void runTest() {
123 verbose("\n------------------------------");
124 verbose("Run the custom test case");
125 verbose("------------------------------\n");
126 if (_delegate._skip) {
127 verbose("-->Skipping the test");
128 return;
129 }
130
131 List returnValues = null;
132
133 try {
134 CustomTest customTest = _delegate._unitTest.getCustomTest();
135 ConfigurationType testConfig = customTest.getMethods();
136 Object object = getTestObject(customTest.getTestClass());
137 returnValues = _delegate.invokeEnumeratedMethods(object, testConfig);
138 } catch (Exception e) {
139 if (!_delegate.checkExceptionWasExpected(e, FailureStepType.CUSTOM_TEST)) {
140 fail("Exception running the custom test " + e);
141 }
142 return;
143 }
144
145
146 int count = 0;
147 boolean testFailed = false;
148
149 for (Iterator i = returnValues.iterator(); i.hasNext(); count++) {
150 Object o = i.next();
151
152
153 if (o instanceof Boolean && !((Boolean) o).booleanValue() && !_failureExpected) {
154
155 testFailed = true;
156 System.out
157 .println("Custom test #" + count + " was expected to succeed, but returned false");
158 }
159
160
161 if (o instanceof Throwable && !_failureExpected) {
162 testFailed = true;
163 System.out
164 .println("Custom test #" + count + " was expected to succeed, but returned Throwable");
165 if (XMLTestCase._printStack) {
166 ((Throwable) o).printStackTrace();
167 }
168 }
169 }
170
171
172 if (testFailed ^ _failureExpected) {
173 if (testFailed) {
174 fail("The custom test failed");
175 } else {
176 fail("The custom test was expected to fail, but succeeded");
177 }
178 }
179 }
180
181
182
183
184
185
186
187
188
189
190 protected Object getTestObject(final String testClassName)
191 throws ClassNotFoundException, IllegalAccessException, InstantiationException {
192 Class testObject = null;
193 if (_delegate._test.getClassLoader() != null) {
194 testObject = _delegate._test.getClassLoader().loadClass(testClassName);
195 } else {
196 testObject = this.getClass().getClassLoader().loadClass(testClassName);
197 }
198 return testObject.newInstance();
199 }
200
201
202
203
204
205
206 private void verbose(final String message) {
207 _delegate.verbose(message);
208 }
209
210 }