craigmcc 01/08/16 22:15:48 Modified: workflow build.xml workflow/src/java/org/apache/commons/workflow Context.java workflow/src/java/org/apache/commons/workflow/base BaseContext.java Added: workflow/src/test/org/apache/commons/workflow/base BaseContextTestCase.java Log: Add unit test for BaseContext and BaseScope - covers all methods that do not involve an assigned Activity. This is also a suitable base class for unit tests of more sophisticated Context implementations. Revision Changes Path 1.2 +6 -7 jakarta-commons-sandbox/workflow/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/workflow/build.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- build.xml 2001/08/13 21:12:13 1.1 +++ build.xml 2001/08/17 05:15:48 1.2 @@ -3,7 +3,7 @@ <!-- "Workflow" component of the Jakarta Commons Subproject - $Id: build.xml,v 1.1 2001/08/13 21:12:13 craigmcc Exp $ + $Id: build.xml,v 1.2 2001/08/17 05:15:48 craigmcc Exp $ --> @@ -237,20 +237,19 @@ </target> - <target name="test" depends="compile.tests" + <target name="test" depends="compile.tests, + test.BaseContext" description="Run all unit test cases"> </target> -<!-- - <target name="test.property"> - <echo message="Running PropertyUtils tests ..."/> + <target name="test.BaseContext"> + <echo message="Running BaseContext tests ..."/> <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}"> - <arg value="org.apache.commons.beanutils.PropertyUtilsTestCase"/> + <arg value="org.apache.commons.workflow.base.BaseContextTestCase"/> <classpath refid="test.classpath"/> </java> </target> ---> <!-- ========== Demonstration Program Target ============================== --> 1.2 +19 -4 jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/Context.java Index: Context.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/Context.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Context.java 2001/08/13 21:15:03 1.1 +++ Context.java 2001/08/17 05:15:48 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/Context.java,v 1.1 2001/08/13 21:15:03 craigmcc Exp $ - * $Revision: 1.1 $ - * $Date: 2001/08/13 21:15:03 $ + * $Header: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/Context.java,v 1.2 2001/08/17 05:15:48 craigmcc Exp $ + * $Revision: 1.2 $ + * $Date: 2001/08/17 05:15:48 $ * * ==================================================================== * @@ -87,7 +87,7 @@ * the Step implementations that are executed to maintain the stack's * integrity.</p> * - * @version $Revision: 1.1 $ $Date: 2001/08/13 21:15:03 $ + * @version $Revision: 1.2 $ $Date: 2001/08/17 05:15:48 $ * @author Craig R. McClanahan */ @@ -245,6 +245,15 @@ /** + * Return the Scope implementation registered for the specified + * name, if any; otherwise, return <code>null</code>. + * + * @param name Scope name to select + */ + public Scope getScope(String name); + + + /** * Return the Scope identifier registered for the specified * name, if any; otherwise, return <code>null</code>. * @@ -260,6 +269,12 @@ * Clear the evaluation stack. */ public void clear(); + + + /** + * Is the evaluation stack currently empty? + */ + public boolean isEmpty(); /** 1.2 +31 -5 jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java Index: BaseContext.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- BaseContext.java 2001/08/13 21:16:01 1.1 +++ BaseContext.java 2001/08/17 05:15:48 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java,v 1.1 2001/08/13 21:16:01 craigmcc Exp $ - * $Revision: 1.1 $ - * $Date: 2001/08/13 21:16:01 $ + * $Header: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java,v 1.2 2001/08/17 05:15:48 craigmcc Exp $ + * $Revision: 1.2 $ + * $Date: 2001/08/17 05:15:48 $ * * ==================================================================== * @@ -80,7 +80,7 @@ * class. If it is used in a multiple thread environment, callers must * take suitable precations.</p> * - * @version $Revision: 1.1 $ $Date: 2001/08/13 21:16:01 $ + * @version $Revision: 1.2 $ $Date: 2001/08/17 05:15:48 $ * @author Craig R. McClanahan */ @@ -352,12 +352,28 @@ */ public Scope getScope(int scope) { - return (scopes[scope]); + if ((scope >= 0) && (scope < MAX_SCOPES)) + return (scopes[scope]); + else + return (null); } /** + * Return the Scope implementation registered for the specified + * name, if any; otherwise, return <code>null</code>. + * + * @param name Scope name to select + */ + public Scope getScope(String name) { + + return (getScope(getScopeId(name))); + + } + + + /** * Return the Scope identifier registered for the specified * name, if any; otherwise, return <code>-1</code>. * @@ -385,6 +401,16 @@ public void clear() { stack.clear(); + + } + + + /** + * Is the evaluation stack currently empty? + */ + public boolean isEmpty() { + + return (stack.size() == 0); } 1.1 jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/base/BaseContextTestCase.java Index: BaseContextTestCase.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/base/BaseContextTestCase.java,v 1.1 2001/08/17 05:15:48 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2001/08/17 05:15:48 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.workflow.base; import java.util.EmptyStackException; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.commons.workflow.Context; import org.apache.commons.workflow.Scope; /** * <p>Test Case for the BaseContext class. These tests exercise those * functions that are not dependent upon an associated Activity.</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2001/08/17 05:15:48 $ */ public class BaseContextTestCase extends TestCase { // ----------------------------------------------------- Instance Variables /** * The digester instance we will be processing. */ protected Context context = null; // ----------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public BaseContextTestCase(String name) { super(name); } // -------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() { context = new BaseContext(); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(BaseContextTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { context = null; } // ------------------------------------------------ Individual Test Methods /** * Add a new scope and test direct access to it. */ public void testAddScope() { assertNull("Should not find 'extra' scope by id", context.getScope(Context.LOCAL_SCOPE + 1)); assertNull("Should not find 'extra' scope by name", context.getScope("extra")); Scope scope = new BaseScope(); context.addScope(Context.LOCAL_SCOPE + 1, "extra", scope); assertEquals("Should look up 'extra' scope id", context.getScopeId("extra"), Context.LOCAL_SCOPE + 1); assertNotNull("Should find 'extra' scope by id", context.getScope(Context.LOCAL_SCOPE + 1)); assertNotNull("Should find 'extra' scope by name", context.getScope("extra")); assertEquals("Should match 'extra' scope by id", scope, context.getScope(Context.LOCAL_SCOPE + 1)); assertEquals("Should match 'extra' scope by name", scope, context.getScope("extra")); commonDirectScope(Context.LOCAL_SCOPE + 1); } /** * Test default access to LOCAL scope. */ public void testDefaultScope() { assertTrue("Should not contain A #1", !context.contains("BaseContextTestCase.A")); context.put("BaseContextTestCase.A", "A Value"); assertTrue("Should contain A #1", context.contains("BaseContextTestCase.A")); assertEquals("Should get A #1", "A Value", (String) context.get("BaseContextTestCase.A")); assertTrue("Should not contain B #2", !context.contains("BaseContextTestCase.B")); context.put("BaseContextTestCase.B", "B Value"); assertTrue("Should contain B #2", context.contains("BaseContextTestCase.B")); assertEquals("Should get B #2", "B Value", (String) context.get("BaseContextTestCase.B")); assertTrue("Should contain A #2", context.contains("BaseContextTestCase.A")); assertEquals("Should get A #2", "A Value", (String) context.get("BaseContextTestCase.A")); context.remove("BaseContextTestCase.B"); assertTrue("Should not contain B #3", !context.contains("BaseContextTestCase.B")); assertTrue("Should contain A #3", context.contains("BaseContextTestCase.A")); assertEquals("Should get A #3", "A Value", (String) context.get("BaseContextTestCase.A")); context.remove("BaseContextTestCase.A"); assertTrue("Should not contain A #4", !context.contains("BaseContextTestCase.A")); assertTrue("Should not contain B #4", !context.contains("BaseContextTestCase.B")); } /** * Test direct access to LOCAL scope. */ public void testDirectScope() { // Common directed tests commonDirectScope(Context.LOCAL_SCOPE); // Implicit --> Explicit visibility assertTrue("Should not contain C #1", !context.contains("BaseContextTestCase.C", Context.LOCAL_SCOPE)); context.put("BaseContextTestCase.C", "C Value"); assertTrue("Should contain C #1", context.contains("BaseContextTestCase.C", Context.LOCAL_SCOPE)); assertEquals("Should get C #1", "C Value", (String) context.get("BaseContextTestCase.C", Context.LOCAL_SCOPE)); context.remove("BaseContextTestCase.C"); assertTrue("Should not contain C #1", !context.contains("BaseContextTestCase.C", Context.LOCAL_SCOPE)); // Explicit --> Implicit visibility assertTrue("Should not contain D #2", !context.contains("BaseContextTestCase.D")); context.put("BaseContextTestCase.D", "D Value", Context.LOCAL_SCOPE); assertTrue("Should contain D #2", context.contains("BaseContextTestCase.D")); assertEquals("Should get D #2", "D Value", (String) context.get("BaseContextTestCase.D")); context.remove("BaseContextTestCase.D", Context.LOCAL_SCOPE); assertTrue("Should not contain D #2", !context.contains("BaseContextTestCase.D")); } /** * Test the basic manipulation of the associated LOCAL scope. */ public void testLocalScope() { commonExplicitScope("local", Context.LOCAL_SCOPE); } /** * Test the basic stack mechanisms. */ public void testStackMethods() { Object value = null; // New stack must be empty assertTrue("New stack is empty", context.isEmpty()); try { value = context.peek(); fail("Did not throw EmptyStackException on empty peek()"); } catch (EmptyStackException e) { ; // Expected result } try { value = context.pop(); fail("Did not throw EmptyStackException on empty pop()"); } catch (EmptyStackException e) { ; // Expected result } // Test pushing and popping activities context.push("First Item"); value = context.peek(); assertNotNull("Peeked first item is not null", value); assertEquals("Peeked first item value", "First Item", (String) value); context.push("Second Item"); value = context.peek(); assertNotNull("Peeked second item is not null", value); assertEquals("Peeked second item value", "Second Item", (String) value); value = context.pop(); assertNotNull("Popped second item is not null", value); assertEquals("Popped second item value", "Second Item", (String) value); value = context.peek(); assertNotNull("Remaining item is not null", value); assertEquals("Remaining item value", "First Item", (String) value); // Cleared stack is empty context.push("Dummy Item"); context.clear(); assertTrue("Cleared stack is empty", context.isEmpty()); try { value = context.peek(); fail("Did not throw EmptyStackException on empty peek()"); } catch (EmptyStackException e) { ; // Expected result } try { value = context.pop(); fail("Did not throw EmptyStackException on empty pop()"); } catch (EmptyStackException e) { ; // Expected result } } // ------------------------------------------------------ Protected Methods /** * Common base tests for direct access to a Scope associated with * this Context. * * @param id Scope identifier */ protected void commonDirectScope(int id) { assertTrue("Should not contain A #1", !context.contains("BaseContextTestCase.A", id)); context.put("BaseContextTestCase.A", "A Value", id); assertTrue("Should contain A #1", context.contains("BaseContextTestCase.A", id)); assertEquals("Should get A #1", "A Value", (String) context.get("BaseContextTestCase.A", id)); assertTrue("Should not contain B #2", !context.contains("BaseContextTestCase.B", id)); context.put("BaseContextTestCase.B", "B Value", id); assertTrue("Should contain B #2", context.contains("BaseContextTestCase.B", id)); assertEquals("Should get B #2", "B Value", (String) context.get("BaseContextTestCase.B", id)); assertTrue("Should contain A #2", context.contains("BaseContextTestCase.A", id)); assertEquals("Should get A #2", "A Value", (String) context.get("BaseContextTestCase.A", id)); context.remove("BaseContextTestCase.B", id); assertTrue("Should not contain B #3", !context.contains("BaseContextTestCase.B", id)); assertTrue("Should contain A #3", context.contains("BaseContextTestCase.A", id)); assertEquals("Should get A #3", "A Value", (String) context.get("BaseContextTestCase.A", id)); context.remove("BaseContextTestCase.A", id); assertTrue("Should not contain A #4", !context.contains("BaseContextTestCase.A", id)); assertTrue("Should not contain B #4", !context.contains("BaseContextTestCase.B", id)); } /** * Common base tests for a Scope associated with this Context. * * @param name Name of the scope to be tested * @param id Expected scope identifier */ protected void commonExplicitScope(String name, int id) { // Verify that we can retrieve the specified scope int scopeId = context.getScopeId(name); assertEquals("Scope identifier for " + name, id, scopeId); if (scopeId < 0) return; Scope scope = context.getScope(scopeId); assertNotNull("Found scope " + name, scope); if (scope == null) return; assertNull("get(null) returns null", scope.get(null)); scope.clear(); // Perform explicit tests on this scope assertTrue("Should not contain A #1", !scope.containsKey("BaseContextTestCase.A")); scope.put("BaseContextTestCase.A", "A Value"); assertTrue("Should contain A #1", scope.containsKey("BaseContextTestCase.A")); assertEquals("Should get A #1", "A Value", (String) scope.get("BaseContextTestCase.A")); assertTrue("Should not contain B #2", !scope.containsKey("BaseContextTestCase.B")); scope.put("BaseContextTestCase.B", "B Value"); assertTrue("Should contain B #2", scope.containsKey("BaseContextTestCase.B")); assertEquals("Should get B #2", "B Value", (String) scope.get("BaseContextTestCase.B")); assertTrue("Should contain A #2", scope.containsKey("BaseContextTestCase.A")); assertEquals("Should get A #2", "A Value", (String) scope.get("BaseContextTestCase.A")); scope.remove("BaseContextTestCase.B"); assertTrue("Should not contain B #3", !scope.containsKey("BaseContextTestCase.B")); assertTrue("Should contain A #3", scope.containsKey("BaseContextTestCase.A")); assertEquals("Should get A #3", "A Value", (String) scope.get("BaseContextTestCase.A")); scope.remove("BaseContextTestCase.A"); assertTrue("Should not contain A #4", !scope.containsKey("BaseContextTestCase.A")); assertTrue("Should not contain B #4", !scope.containsKey("BaseContextTestCase.B")); } }