KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > tags > junit > CaseTag


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
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 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 JavaDoc name;
36
37
38     // Tag interface
39
//-------------------------------------------------------------------------
40
public void doTag(final XMLOutput output) throws JellyTagException {
41         String JavaDoc name = getName();
42         if ( name == null ) {
43             name = toString();
44         }
45
46         // #### we need to redirect the output to a TestListener
47
// or something?
48
TestCase testCase = new TestCase(name) {
49             protected void runTest() throws Throwable JavaDoc {
50                 // create a new child context so that each test case
51
// will have its own variable scopes
52
JellyContext newContext = new JellyContext( context );
53
54                 // disable inheritence of variables and tag libraries
55
newContext.setExportLibraries(false);
56                 newContext.setExport(false);
57
58                 // invoke the test case
59
getBody().run(newContext, output);
60             }
61         };
62
63         // lets find the test suite
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     // Properties
72
//-------------------------------------------------------------------------
73

74     /**
75      * @return the name of this test case
76      */

77     public String JavaDoc getName() {
78         return name;
79     }
80
81     /**
82      * Sets the name of this test case
83      */

84     public void setName(String JavaDoc name) {
85         this.name = name;
86     }
87
88     // Implementation methods
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 }
103
Popular Tags