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.Test;
19  import junit.framework.TestSuite;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  
25  /***
26   * Represents a collection of TestCases.. This tag is analagous to
27   * JUnit's TestSuite class.
28   *
29   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30   * @version $Revision: 201960 $
31   */
32  public class SuiteTag extends TagSupport {
33  
34      /*** the test suite this tag created */
35      private TestSuite suite;
36  
37      /*** the name of the variable of the test suite */
38      private String var;
39  
40      /*** the name of the test suite to create */
41      private String name;
42  
43      public SuiteTag() {
44      }
45  
46      /***
47       * Adds a new Test to this suite
48       */
49      public void addTest(Test test) {
50          getSuite().addTest(test);
51      }
52  
53      // Tag interface
54      //-------------------------------------------------------------------------
55      public void doTag(XMLOutput output) throws JellyTagException {
56          suite = createSuite();
57  
58          TestSuite parent = (TestSuite) context.getVariable("org.apache.commons.jelly.junit.suite");
59          if ( parent == null ) {
60              context.setVariable("org.apache.commons.jelly.junit.suite", suite );
61          }
62          else {
63              parent.addTest( suite );
64          }
65  
66          invokeBody(output);
67  
68          if ( var != null ) {
69              context.setVariable(var, suite);
70          }
71      }
72  
73      // Properties
74      //-------------------------------------------------------------------------
75      public TestSuite getSuite() {
76          return suite;
77      }
78  
79      /***
80       * Sets the name of the test suite whichi is exported
81       */
82      public void setVar(String var) {
83          this.var = var;
84      }
85  
86      /***
87       * @return the name of this test suite
88       */
89      public String getName() {
90          return name;
91      }
92  
93      /***
94       * Sets the name of this test suite
95       */
96      public void setName(String name) {
97          this.name = name;
98      }
99  
100     // Implementation methods
101     //-------------------------------------------------------------------------
102 
103     /***
104      * Factory method to create a new TestSuite
105      */
106     protected TestSuite createSuite() {
107         if ( name == null ) {
108             return new TestSuite();
109         }
110         return new TestSuite(name);
111     }
112 }