1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2003 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id: SourceGeneratorTestCase.java 7006 2007-06-03 15:03:12Z wguttmn $
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 * This class encapsulate all the logic to run the tests patterns for the source
60 * generator. It is able to run the source generator by itself and then compile
61 * the file that have been generated.
62 *
63 * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
64 * @author <a href="mailto:gignoux@kernelcenter.org">Sebastien Gignoux</a>
65 * @version $Revision: 7006 $ $Date: 2005-02-28 17:22:46 -0700 (Mon, 28 Feb 2005) $
66 */
67 public class SourceGeneratorTestCase extends XMLTestCase {
68
69 /** Contains the information for the configuration for all the tests in this jar. */
70 protected final SourceGeneratorTest _sourceGenConf;
71 /** If true, the randomize() function has been implemented in the root class. */
72 protected final boolean _hasRandom;
73 /** Generates and compiles source in a test harness, but does nothing else. */
74 private final TestSourceGenerator _sourceGenerator;
75
76 /**
77 * Creates a new test case for the given setup.
78 *
79 * @param test the reference to the jar/directory
80 * @param unit the UnitTestCase that wraps the configuration for this XML
81 * Test case.
82 * @param sourceGen the Source Generator test to be executed
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 * Create a new SourceGeneratorTestCase with the given name.
118 * @param name name for the test case
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 * Returns the test suite for this given test setup.
127 * @return the test suite for this given test setup.
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 * Sets up this test suite.
148 * @throws java.lang.Exception if anything goes wrong
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 // Set up and run the source generator so we can test using the generated source
156 _sourceGenerator.setXMLContext(getXMLContext());
157 _sourceGenerator.setUp();
158 _sourceGenerator.runTest();
159
160 // Set up the root class
161 _rootClass = _test.getClassLoader().loadClass(_rootClassName);
162 }
163
164 /**
165 * Cleans up after this unit test (nothing to do except provide output).
166 * @throws java.lang.Exception never
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 }