1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.junit;
17
18 import java.net.URL;
19
20 import junit.framework.TestSuite;
21
22 import org.apache.commons.jelly.JellyContext;
23 import org.apache.commons.jelly.XMLOutput;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /***
28 * An abstract base class for creating a TestSuite via a Jelly script.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public abstract class JellyTestSuite {
34
35 /*** The Log to which logging calls will be made. */
36 private static final Log log = LogFactory.getLog(JellyTestSuite.class);
37
38
39 /***
40 * Helper method to create a test suite from a file name on the class path
41 * in the package of the given class.
42 * For example a test could call
43 * <code>
44 * createTestSuite( Foo.class, "suite.jelly" );
45 * </code>
46 * which would loaad the 'suite.jelly script from the same package as the Foo
47 * class on the classpath.
48 *
49 * @param testClass is the test class used to load the script via the classpath
50 * @param script is the name of the script, which is typically just a name, no directory.
51 * @return a newly created TestSuite
52 */
53 public static TestSuite createTestSuite(Class testClass, String script) throws Exception {
54 URL url = testClass.getResource(script);
55 if ( url == null ) {
56 throw new Exception(
57 "Could not find Jelly script: " + script
58 + " in package of class: " + testClass.getName()
59 );
60 }
61 return createTestSuite( url );
62 }
63
64 /***
65 * Helper method to create a test suite from the given Jelly script
66 *
67 * @param script is the URL to the script which should create a TestSuite
68 * @return a newly created TestSuite
69 */
70 public static TestSuite createTestSuite(URL script) throws Exception {
71 JellyContext context = new JellyContext(script);
72 XMLOutput output = XMLOutput.createXMLOutput(System.out);
73 context = context.runScript(script, output);
74 TestSuite answer = (TestSuite) context.getVariable("org.apache.commons.jelly.junit.suite");
75 if ( answer == null ) {
76 log.warn( "Could not find a TestSuite created by Jelly for the script:" + script );
77
78 return new TestSuite();
79 }
80 return answer;
81 }
82 }