I've been using jelly for EAI testing and found that I was frequently using setUp and 
tearDown-type scripts. I thought it would be neat and JUnit-like if these scripts 
could be added as attributes to the test:suite which could then arrange for them to be 
called for each contained test:case in the test:case context.

Attached is the patch file for the both the test cases and code.

Here's a snippet to show how it works ...
 
<define:script var="setUpScript">   
   <!-- fixture setUp goes here -->
</define:script> 

<define:script var="tearDownScript">
    <!-- fixture tear-down goes here -->
</define:script>
 
<!--
   || @setUp is called before each test:case.
   || @tearDown is called after each test:case.
   || @setUp and @tearDown run in the same context as each test:case.
-->
<test:suite setUp="${setUpScript}" tearDown="${tearDownScript}">
  <test:case name="testcase1">
    <!-- test case   ..... -->
  </test:case>
</test:suite>
 

If there's a better approach that i've missed i'd be happy to discuss/code it/try it 
out.
 
cheers
 
Darren McGrath
-- 
__________________________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup

Meet Singles
http://corp.mail.com/lavalife

Index: src/java/org/apache/commons/jelly/tags/junit/SuiteTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/SuiteTag.java,v

retrieving revision 1.2
diff -u -r1.2 SuiteTag.java
--- src/java/org/apache/commons/jelly/tags/junit/SuiteTag.java  30 Oct 2002 19:16:30 
-0000      1.2
+++ src/java/org/apache/commons/jelly/tags/junit/SuiteTag.java  20 Dec 2002 11:29:05 
+-0000
@@ -66,6 +66,7 @@
 
 import org.apache.commons.jelly.TagSupport;
 import org.apache.commons.jelly.XMLOutput;
+import org.apache.commons.jelly.Script;
 
 /** 
  * Represents a collection of TestCases.. This tag is analagous to
@@ -85,6 +86,12 @@
     /** the name of the test suite to create */
     private String name;
 
+    /** the fixture setup script to run before each test case */
+    private Script setUp;
+
+    /** the fixture tear down script to run after each test case */
+    private Script tearDown;
+
     public SuiteTag() {
     }
     
@@ -141,7 +148,35 @@
     public void setName(String name) {
         this.name = name;
     }
-    
+
+    /**
+     * Sets the fixture setup script to be run before each test case in this test 
+suite
+     */
+    public void setSetUp(Script setUp) {
+        this.setUp = setUp;
+    }
+
+     /**
+     * Sets the fixture tear-down script to be run after each test case in this test 
+suite
+     */
+    public void setTearDown(Script tearDown) {
+        this.tearDown = tearDown;
+    }
+
+    /**
+     * @return the fixture setup script to be run before each test case in this test 
+suite
+     */
+    public Script getSetUp() {
+        return setUp;
+    }
+
+    /**
+     * @return the fixture tear-down script to be run after each test case in this 
+test suite
+     */
+    public Script getTearDown() {
+        return tearDown;
+    }
+
     // Implementation methods
     //-------------------------------------------------------------------------       
         
     
Index: src/java/org/apache/commons/jelly/tags/junit/CaseTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/CaseTag.java,v

retrieving revision 1.4
diff -u -r1.4 CaseTag.java
--- src/java/org/apache/commons/jelly/tags/junit/CaseTag.java   30 Oct 2002 19:16:30 
-0000      1.4
+++ src/java/org/apache/commons/jelly/tags/junit/CaseTag.java   20 Dec 2002 11:29:06 
+-0000
@@ -100,9 +100,15 @@
                 // disable inheritence of variables and tag libraries
                 newContext.setExportLibraries(false);
                 newContext.setExport(false);
-                
+
+                // setup the test case fixture
+                runSetUp(newContext, output);
+
                 // invoke the test case
                 getBody().run(newContext, output);
+
+                // tear-down the test case fixture
+                runTearDown(newContext, output);
             }
         };
         
@@ -145,4 +151,29 @@
         return (TestSuite) context.getVariable( 
"org.apache.commons.jelly.junit.suite" );
     }
 
+    /**
+     * Method to invoke the fixture setup script (if there is one) of the
+     * enclosing SuiteTag (if there is one)
+     */
+     protected void runSetUp(JellyContext newContext, XMLOutput output)  throws 
+Exception {
+        SuiteTag tag = (SuiteTag) findAncestorWithClass( SuiteTag.class );
+        if ( tag != null ) {
+            if (tag.getSetUp() != null) {
+                tag.getSetUp().run(newContext, output);
+            }
+        }
+    }
+
+    /**
+     * Method to invoke the fixture tear-down script (if there is one) of the
+     * enclosing SuiteTag (if there is one)
+     */
+     protected void runTearDown(JellyContext newContext, XMLOutput output)  throws 
+Exception {
+        SuiteTag tag = (SuiteTag) findAncestorWithClass( SuiteTag.class );
+        if ( tag != null ) {
+            if (tag.getTearDown() != null) {
+                tag.getTearDown().run(newContext, output);
+            }
+        }
+    }
 }
Index: src/test/org/apache/commons/jelly/junit/suite.jelly
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/junit/suite.jelly,v

retrieving revision 1.11
diff -u -r1.11 suite.jelly
--- src/test/org/apache/commons/jelly/junit/suite.jelly 16 Dec 2002 10:46:42 -0000     
 1.11
+++ src/test/org/apache/commons/jelly/junit/suite.jelly 20 Dec 2002 11:29:07 -0000
@@ -1,5 +1,7 @@
 <?xml version="1.0"?>
-<test:suite xmlns:j="jelly:core" xmlns:test="jelly:junit" xmlns:x="jelly:xml" 
xmlns:log="jelly:log">
+<j:jelly xmlns:j="jelly:core" xmlns:test="jelly:junit" xmlns:x="jelly:xml" 
+xmlns:log="jelly:log" xmlns:define="jelly:define">
+
+<test:suite>
 
   <test:case name="assertTests">
   
@@ -113,3 +115,59 @@
 -->
 
 </test:suite>
+
+<!-- Define the fixture which consists of a setUp and tearDown scripts -->
+<define:script var="setUpScript">
+  <j:set var="foo" value="one"/>
+
+  <!-- check that tearDown removes bar from the parent context -->
+  <test:assert test='${context.getParent().findVariable("bar") == null}'/>
+  <j:set var="bar" value="uno" scope="parent"/>
+</define:script>
+
+<define:script var="tearDownScript">
+  <j:set var="bar" value="${null}" scope="parent"/>
+</define:script>
+
+<!--
+  || @setUp is called before each test:case.
+  || @tearDown is called after each test:case.
+  || @setUp and @tearDown run in the same scope as the test:case.
+-->
+<test:suite setUp="${setUpScript}" tearDown="${tearDownScript}">
+
+  <test:case name="testFixtureCase1">
+
+    <!-- make sure foo is in this context but not parent context -->
+    <test:assertEquals expected="one" actual="${foo}"/>
+    <test:assert test='${context.getParent().findVariable("foo") == null}'/>
+    <j:set var="foo" value="two"/>
+
+    <test:assert test='${context.getParent().findVariable("bar") == "uno"}'/>
+
+    <j:set var="local" value="tres"/>
+
+  </test:case>
+
+  <test:case name="testFixtureCase2">
+
+    <test:assertEquals expected="one" actual="${foo}"/>
+    <test:assert test='${context.getParent().findVariable("foo") == null}'/>
+
+    <test:assert test='${context.getParent().findVariable("bar") == "uno"}'/>
+
+    <test:assert test="${local == null}"/>
+
+  </test:case>
+
+</test:suite>
+
+<test:suite>
+
+  <test:case name="testNoFixture">
+    <test:assertEquals expected="${null}" actual="${foo}"/>
+  </test:case>
+
+</test:suite>
+
+</j:jelly>
\ No newline at end of file


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to