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