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