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