1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.castor.xmlctf;
46
47 import java.util.ArrayList;
48 import java.util.List;
49
50 import junit.framework.Test;
51 import junit.framework.TestSuite;
52
53 import org.exolab.castor.tests.framework.testDescriptor.ExpectedSources;
54 import org.exolab.castor.tests.framework.testDescriptor.RootType;
55 import org.exolab.castor.tests.framework.testDescriptor.SourceGeneratorTest;
56 import org.exolab.castor.tests.framework.testDescriptor.UnitTestCase;
57
58
59
60
61
62
63
64
65
66
67 public class SourceGeneratorTestCase extends XMLTestCase {
68
69
70 protected final SourceGeneratorTest _sourceGenConf;
71
72 protected final boolean _hasRandom;
73
74 private final TestSourceGenerator _sourceGenerator;
75
76
77
78
79
80
81
82
83
84 public SourceGeneratorTestCase(final CastorTestCase test, final UnitTestCase unit,
85 final SourceGeneratorTest sourceGen) {
86 super(test, unit);
87 _sourceGenConf = sourceGen;
88 _sourceGenerator = new TestSourceGenerator(test, unit, sourceGen);
89
90 RootType rootType = _sourceGenConf.getRoot_Object();
91 if (rootType == null) {
92 throw new IllegalArgumentException("You must give a root object for a Source Generator Test"
93 + _outputRootFile + ", " + getName());
94 }
95
96 _rootClassName = rootType.getContent();
97 if (_rootClassName == null) {
98 throw new IllegalArgumentException("You must give a root object for a Source Generator Test"
99 + _outputRootFile + ", " + getName());
100 }
101
102 _hasRandom = rootType.getRandom();
103 _hasDump = rootType.getDump();
104
105 ExpectedSources expectedSources = _sourceGenConf.getExpectedSources();
106 if (expectedSources != null) {
107 String[] expectedSource = expectedSources.getExpectedSource();
108 List expectedSourcesList = new ArrayList();
109 for (int i = 0; i < expectedSource.length; i++) {
110 expectedSourcesList.add(expectedSource[i]);
111 }
112 _sourceGenerator.setExpectedSources(expectedSourcesList);
113 }
114 }
115
116
117
118
119
120 public SourceGeneratorTestCase(final String name) {
121 super(name);
122 throw new IllegalArgumentException("You cannot use the name-only constructor");
123 }
124
125
126
127
128
129 public Test suite() {
130 TestSuite suite = new TestSuite(_name);
131
132 String name = getTestSuiteName();
133 name = (name != null) ? name + "#" + _name : _name;
134 if (_unitTest.getCustomTest() != null) {
135 suite.addTest(new TestWithCustomTest(name, this));
136 } else {
137 suite.addTest(new TestWithReferenceDocument(name, this));
138 if (_hasRandom) {
139 suite.addTest(new TestWithRandomObject(name, this));
140 }
141 }
142
143 return suite;
144 }
145
146
147
148
149
150 protected void setUp() throws java.lang.Exception {
151 verbose("\n================================================");
152 verbose("Test suite '"+_test.getName()+"': setting up test '" + _name+"'");
153 verbose("================================================\n");
154
155
156 _sourceGenerator.setXMLContext(getXMLContext());
157 _sourceGenerator.setUp();
158 _sourceGenerator.runTest();
159
160
161 _rootClass = _test.getClassLoader().loadClass(_rootClassName);
162 }
163
164
165
166
167
168 protected void tearDown() throws java.lang.Exception {
169 verbose("\n================================================");
170 verbose("Test suite '"+_test.getName()+"': test '" + _name+"' complete.");
171 verbose("================================================\n");
172 _sourceGenerator.tearDown();
173 }
174
175 }