1 /*
2 * Copyright 2006 Edward Kuns
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * $Id: OnlySourceGenerationTestCase.java 0000 2006-10-05 22:00:00Z ekuns $
17 */
18 package org.castor.xmlctf;
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.exolab.castor.tests.framework.testDescriptor.OnlySourceGenerationTest;
24 import org.exolab.castor.tests.framework.testDescriptor.UnitTestCase;
25 import org.exolab.castor.xml.XMLContext;
26
27 /**
28 * This class encapsulate all the logic to run the tests patterns for the source
29 * generator. It is able to run the source generator by itself and then compile
30 * the file that have been generated. This class does not do anything additional.
31 * It only runs the source generator and ensures that the generated source will
32 * compile without error.
33 *
34 * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a>
35 * @version $Revision: 0000 $ $Date: $
36 */
37 public class OnlySourceGenerationTestCase extends XMLTestCase {
38
39 /** We add this fixed string to the end of our testcase name. */
40 private static final String ONLY_GENERATION = "_OnlySourceGeneration";
41
42 /** Generates and compiles source in a test harness, but does nothing else. */
43 private final TestSourceGenerator _sourceGenerator;
44
45 /**
46 * Creates a new test case for the given setup.
47 *
48 * @param test the reference to the jar/directory
49 * @param unit the UnitTestCase that wraps the configuration for this XML
50 * Test case.
51 * @param sourceGen the Source Generator test to be executed
52 * @param outputRoot the directory that contains the files needed for the
53 * test
54 */
55 public OnlySourceGenerationTestCase(final CastorTestCase test, final UnitTestCase unit,
56 final OnlySourceGenerationTest sourceGen) {
57 super(test, unit);
58 _sourceGenerator = new TestSourceGenerator(test, unit, sourceGen);
59 }
60
61 /**
62 * Create a new SourceGeneratorTestCase with the given name.
63 * @param name name for the test case
64 */
65 public OnlySourceGenerationTestCase(final String name) {
66 super(name);
67 _sourceGenerator = null;
68 }
69
70 /**
71 * Returns the test suite for this given test setup.
72 * @return the test suite for this given test setup.
73 */
74 public Test suite() {
75 TestSuite suite = new TestSuite(_name);
76
77 String name = getTestSuiteName();
78 name = (name != null) ? name + "#" + _name : _name;
79 this.setName(name + ONLY_GENERATION);
80
81 suite.addTest(this);
82 return suite;
83 }
84
85 /**
86 * Sets up this test suite.
87 * @throws java.lang.Exception if anything goes wrong
88 */
89 protected void setUp() throws java.lang.Exception {
90 verbose("\n================================================");
91 verbose("Test suite '"+_test.getName()+"': setting up test '" + _name+"'");
92 verbose("================================================\n");
93 if (getXMLContext() == null) {
94 // not wrapped inside a TestWithXy test!
95 setXMLContext(new XMLContext());
96 }
97 _sourceGenerator.setXMLContext(getXMLContext());
98 _sourceGenerator.setUp();
99 }
100
101 public void runTest() {
102 _sourceGenerator.runTest();
103 verbose("-->Done");
104 }
105
106 /**
107 * Cleans up after this unit test (nothing to do except provide output).
108 * @throws java.lang.Exception never
109 */
110 protected void tearDown() throws java.lang.Exception {
111 verbose("\n================================================");
112 verbose("Test suite '"+_test.getName()+"': test '" + _name+"' complete.");
113 verbose("================================================\n");
114 _sourceGenerator.tearDown();
115 }
116
117 }