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