001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.junit;
017:
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: import org.apache.commons.jelly.JellyContext;
022: import org.apache.commons.jelly.JellyTagException;
023: import org.apache.commons.jelly.TagSupport;
024: import org.apache.commons.jelly.XMLOutput;
025:
026: /**
027: * Represents a single test case in a test suite; this tag is analagous to
028: * JUnit's TestCase class.
029: *
030: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
031: * @version $Revision: 155420 $
032: */
033: public class CaseTag extends TagSupport {
034:
035: private String name;
036:
037: // Tag interface
038: //-------------------------------------------------------------------------
039: public void doTag(final XMLOutput output) throws JellyTagException {
040: String name = getName();
041: if (name == null) {
042: name = toString();
043: }
044:
045: // #### we need to redirect the output to a TestListener
046: // or something?
047: TestCase testCase = new TestCase(name) {
048: protected void runTest() throws Throwable {
049: // create a new child context so that each test case
050: // will have its own variable scopes
051: JellyContext newContext = new JellyContext(context);
052:
053: // disable inheritence of variables and tag libraries
054: newContext.setExportLibraries(false);
055: newContext.setExport(false);
056:
057: // invoke the test case
058: getBody().run(newContext, output);
059: }
060: };
061:
062: // lets find the test suite
063: TestSuite suite = getSuite();
064: if (suite == null) {
065: throw new JellyTagException(
066: "Could not find a TestSuite to add this test to. This tag should be inside a <test:suite> tag");
067: }
068: suite.addTest(testCase);
069: }
070:
071: // Properties
072: //-------------------------------------------------------------------------
073:
074: /**
075: * @return the name of this test case
076: */
077: public String getName() {
078: return name;
079: }
080:
081: /**
082: * Sets the name of this test case
083: */
084: public void setName(String name) {
085: this .name = name;
086: }
087:
088: // Implementation methods
089: //-------------------------------------------------------------------------
090:
091: /**
092: * Strategy method to find the corrent TestSuite to add a new Test case to
093: */
094: protected TestSuite getSuite() {
095: SuiteTag tag = (SuiteTag) findAncestorWithClass(SuiteTag.class);
096: if (tag != null) {
097: return tag.getSuite();
098: }
099: return (TestSuite) context
100: .getVariable("org.apache.commons.jelly.junit.suite");
101: }
102:
103: }
|