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 junit.framework.TestCase;
19 import junit.framework.TestSuite;
20
21 import org.apache.commons.jelly.JellyContext;
22 import org.apache.commons.jelly.JellyTagException;
23 import org.apache.commons.jelly.TagSupport;
24 import org.apache.commons.jelly.XMLOutput;
25
26 /***
27 * Represents a single test case in a test suite; this tag is analagous to
28 * JUnit's TestCase class.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public class CaseTag extends TagSupport {
34
35 private String name;
36
37
38
39
40 public void doTag(final XMLOutput output) throws JellyTagException {
41 String name = getName();
42 if ( name == null ) {
43 name = toString();
44 }
45
46
47
48 TestCase testCase = new TestCase(name) {
49 protected void runTest() throws Throwable {
50
51
52 JellyContext newContext = new JellyContext( context );
53
54
55 newContext.setExportLibraries(false);
56 newContext.setExport(false);
57
58
59 getBody().run(newContext, output);
60 }
61 };
62
63
64 TestSuite suite = getSuite();
65 if ( suite == null ) {
66 throw new JellyTagException( "Could not find a TestSuite to add this test to. This tag should be inside a <test:suite> tag" );
67 }
68 suite.addTest(testCase);
69 }
70
71
72
73
74 /***
75 * @return the name of this test case
76 */
77 public String getName() {
78 return name;
79 }
80
81 /***
82 * Sets the name of this test case
83 */
84 public void setName(String name) {
85 this.name = name;
86 }
87
88
89
90
91 /***
92 * Strategy method to find the corrent TestSuite to add a new Test case to
93 */
94 protected TestSuite getSuite() {
95 SuiteTag tag = (SuiteTag) findAncestorWithClass( SuiteTag.class );
96 if ( tag != null ) {
97 return tag.getSuite();
98 }
99 return (TestSuite) context.getVariable( "org.apache.commons.jelly.junit.suite" );
100 }
101
102 }