You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2005/09/28 22:57:23 UTC

svn commit: r292297 - in /struts/shale/trunk/core-library/src: java/org/apache/shale/dialog/ java/org/apache/shale/dialog/impl/ test/org/apache/shale/dialog/impl/

Author: craigmcc
Date: Wed Sep 28 13:57:01 2005
New Revision: 292297

URL: http://svn.apache.org/viewcvs?rev=292297&view=rev
Log:
Add accessors that allow you to iterate over the states and transitions
associated with a Dialog, or the transitions associated with a state.  Also,
add some unit tests to validate the behavior of the configuration parsing
(including the functionality of these new methods).

Added:
    struts/shale/trunk/core-library/src/test/org/apache/shale/dialog/impl/
    struts/shale/trunk/core-library/src/test/org/apache/shale/dialog/impl/ImplClassesTestCase.java
Modified:
    struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/Dialog.java
    struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/State.java
    struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/AbstractState.java
    struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/DialogImpl.java

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/Dialog.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/Dialog.java?rev=292297&r1=292296&r2=292297&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/Dialog.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/Dialog.java Wed Sep 28 13:57:01 2005
@@ -16,6 +16,8 @@
 
 package org.apache.shale.dialog;
 
+import java.util.Iterator;
+
 /**
  * <p>Overall configuration of an individual dialog.  During application
  * execution, this information is immutable (so that simultaneous execution
@@ -56,6 +58,23 @@
      * {@link Dialog}.</p>
      */
     public String getStart();
+
+
+    /**
+     * <p>Return an <code>Iterator</code> over the names of {@link State}s
+     * that are owned by this {@link Dialog}.  If there are no such
+     * {@link State}s, an empty <code>Iterator</code> is returned.</p>
+     */
+    public Iterator getStateIds();
+
+
+    /**
+     * <p>Return an <code>Iterator</code> over the logical outcomes of
+     * global {@link Transition}s for this {@link Dialog}.  If there are
+     * no such {@link Transition}s, an empty <code>Iterator</code> is
+     * returned.</p>
+     */
+    public Iterator getTransitionOutcomes();
 
 
     // ---------------------------------------------------------- Public Methods

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/State.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/State.java?rev=292297&r1=292296&r2=292297&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/State.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/State.java Wed Sep 28 13:57:01 2005
@@ -16,6 +16,8 @@
 
 package org.apache.shale.dialog;
 
+import java.util.Iterator;
+
 /**
  * <p>A {@link State} is an executable entity, within the scope of an
  * owning {@link Dialog}.  Execution of a {@link State} returns a logical
@@ -55,6 +57,15 @@
      * among the {@link State}s owned by the same {@link Dialog}.</p>
      */
     public String getName();
+
+
+    /**
+     * <p>Return an <code>Iterator</code> over the logical outcomes of
+     * local {@link Transition}s for this {@link State}.  If there are
+     * no such {@link Transition}s, an empty <code>Iterator</code> is
+     * returned.</p>
+     */
+    public Iterator getTransitionOutcomes();
 
 
     // ---------------------------------------------------------- Public Methods

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/AbstractState.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/AbstractState.java?rev=292297&r1=292296&r2=292297&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/AbstractState.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/AbstractState.java Wed Sep 28 13:57:01 2005
@@ -17,6 +17,7 @@
 package org.apache.shale.dialog.impl;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -87,6 +88,16 @@
     public String getName() {
 
         return this.name;
+
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Iterator getTransitionOutcomes() {
+
+        return this.transitions.keySet().iterator();
 
     }
 

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/DialogImpl.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/DialogImpl.java?rev=292297&r1=292296&r2=292297&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/DialogImpl.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/dialog/impl/DialogImpl.java Wed Sep 28 13:57:01 2005
@@ -17,6 +17,7 @@
 package org.apache.shale.dialog.impl;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -90,6 +91,31 @@
     public String getStart() {
 
         return this.start;
+
+    }
+
+
+    /**
+     * <p>Return an <code>Iterator</code> over the names of {@link State}s
+     * that are owned by this {@link Dialog}.  If there are no such
+     * {@link State}s, an empty <code>Iterator</code> is returned.</p>
+     */
+    public Iterator getStateIds() {
+
+        return this.states.keySet().iterator();
+
+    }
+
+
+    /**
+     * <p>Return an <code>Iterator</code> over the logical outcomes of
+     * global {@link Transition}s for this {@link Dialog}.  If there are
+     * no such {@link Transition}s, an empty <code>Iterator</code> is
+     * returned.</p>
+     */
+    public Iterator getTransitionOutcomes() {
+
+        return this.transitions.keySet().iterator();
 
     }
 

Added: struts/shale/trunk/core-library/src/test/org/apache/shale/dialog/impl/ImplClassesTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/dialog/impl/ImplClassesTestCase.java?rev=292297&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/test/org/apache/shale/dialog/impl/ImplClassesTestCase.java (added)
+++ struts/shale/trunk/core-library/src/test/org/apache/shale/dialog/impl/ImplClassesTestCase.java Wed Sep 28 13:57:01 2005
@@ -0,0 +1,294 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.dialog.impl;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.commons.chain.Command;
+import org.apache.commons.chain.Context;
+import org.apache.commons.chain.impl.ContextBase;
+import org.apache.shale.dialog.ActionState;
+import org.apache.shale.dialog.Dialog;
+import org.apache.shale.dialog.EndState;
+import org.apache.shale.dialog.Globals;
+import org.apache.shale.dialog.State;
+import org.apache.shale.dialog.config.ConfigurationInit;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+import org.apache.shale.test.mock.MockNavigationHandler;
+import org.apache.shale.test.mock.MockPropertyResolver;
+
+/**
+ * <p>Test case for implementation classes in package
+ * <code>org.apache.shale.dialog.impl</code>.  Note that we are
+ * sharing a dialog configuration file with package
+ * <code>org.apache.shale.dialog.faces</code> as well.</p>
+ *
+ * $Id$
+ */
+
+public class ImplClassesTestCase extends AbstractJsfTestCase {
+
+
+    // ------------------------------------------------------------ Constructors
+
+
+    // Construct a new instance of this test case.
+    public ImplClassesTestCase(String name) {
+        super(name);
+    }
+
+
+    // ---------------------------------------------------- Overall Test Methods
+
+
+    // Set up instance variables required by this test case.
+    public void setUp() {
+
+        super.setUp();
+
+        // Configure the test dialogs we will be using
+        servletContext.addInitParameter
+          (Globals.CONFIGURATION_PARAM,
+           "/org/apache/shale/dialog/faces/dialog-config.xml");
+        Context context = new ContextBase();
+        context.put("context", servletContext);
+        Command command = new ConfigurationInit();
+        try {
+            command.execute(context);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        dialogMap = (Map) externalContext.getApplicationMap().get(Globals.DIALOGS);
+
+    }
+
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(ImplClassesTestCase.class));
+
+    }
+
+
+    // Tear down instance variables required by this test case.
+    public void tearDown() {
+
+        dialogMap = null;
+        super.tearDown();
+
+    }
+
+
+    // ------------------------------------------------------ Instance Variables
+
+
+    // Map of configured Dialog instances for this test case,
+    // keyed by dialog id
+    Map dialogMap = null;
+
+
+    // ------------------------------------------------------------ Test Methods
+
+
+    // Test the contents of the "testExplicitTransitions" dialog
+    public void testExplicitTransitions() {
+
+        Dialog dialog = (Dialog) dialogMap.get("testExplicitTransitions");
+        assertNotNull(dialog);
+        assertTrue(dialog instanceof DialogImpl);
+        assertEquals("testExplicitTransitions", dialog.getName());
+        assertEquals("first", dialog.getStart());
+        checkItems("testExplicitTransitions states",
+                   dialog.getStateIds(),
+                   new String[] { "first", "previous", "next", "last", "finish" });
+        checkItems("textExplicitTransitions transitions", 
+                   dialog.getTransitionOutcomes(),
+                   new String[0]);
+        State state = null;
+
+        state = dialog.findState("first");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("first", state.getName());
+        checkItems("testExplicitTransitions.first transitions",
+                   state.getTransitionOutcomes(),
+                   new String[] { "first" });
+        
+        state = dialog.findState("previous");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("previous", state.getName());
+        checkItems("testExplicitTransitions.previous transitions",
+                   state.getTransitionOutcomes(),
+                   new String[] { "previous" });
+        
+        state = dialog.findState("next");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("next", state.getName());
+        checkItems("testExplicitTransitions.next transitions",
+                   state.getTransitionOutcomes(),
+                   new String[] { "next" });
+
+        state = dialog.findState("last");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("last", state.getName());
+        checkItems("testExplicitTransitions.last transitions",
+                   state.getTransitionOutcomes(),
+                   new String[] { "last" });
+
+        state = dialog.findState("finish");
+        assertNotNull(state);
+        assertTrue(state instanceof EndState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("finish", state.getName());
+        checkItems("testExplicitTransitions.finish transitions",
+                   state.getTransitionOutcomes(),
+                   new String[0]);
+
+    }
+
+
+    // Test the contents of the "testImplicitTransitions" dialog
+    public void testImplicitTransitions() {
+
+        Dialog dialog = (Dialog) dialogMap.get("testImplicitTransitions");
+        assertNotNull(dialog);
+        assertTrue(dialog instanceof DialogImpl);
+        assertEquals("testImplicitTransitions", dialog.getName());
+        assertEquals("first", dialog.getStart());
+        checkItems("testImplicitTransitions states",
+                   dialog.getStateIds(),
+                   new String[] { "first", "previous", "next", "last", "finish" });
+        checkItems("textImplicitTransitions transitions", 
+                   dialog.getTransitionOutcomes(),
+                   new String[] { "first", "previous", "next", "last" });
+        State state = null;
+
+        state = dialog.findState("first");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("first", state.getName());
+        checkItems("testImplicitTransitions.first transitions",
+                   state.getTransitionOutcomes(),
+                   new String[0]);
+        
+        state = dialog.findState("previous");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("previous", state.getName());
+        checkItems("testImplicitTransitions.previous transitions",
+                   state.getTransitionOutcomes(),
+                   new String[0]);
+        
+        state = dialog.findState("next");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("next", state.getName());
+        checkItems("testImplicitTransitions.next transitions",
+                   state.getTransitionOutcomes(),
+                   new String[0]);
+
+        state = dialog.findState("last");
+        assertNotNull(state);
+        assertTrue(state instanceof ActionState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("last", state.getName());
+        checkItems("testImplicitTransitions.last transitions",
+                   state.getTransitionOutcomes(),
+                   new String[0]);
+
+        state = dialog.findState("finish");
+        assertNotNull(state);
+        assertTrue(state instanceof EndState);
+        assertEquals(dialog, state.getDialog());
+        assertEquals("finish", state.getName());
+        checkItems("testImplicitTransitions.finish transitions",
+                   state.getTransitionOutcomes(),
+                   new String[0]);
+
+    }
+
+
+    // Test that the configured Map contains exactly the expected dialogs
+    public void testPristine() {
+
+        assertNotNull(dialogMap);
+        assertEquals(5, dialogMap.size());
+        assertNotNull(dialogMap.get("testExplicitTransitions"));
+        assertNotNull(dialogMap.get("testImplicitTransitions"));
+        assertNotNull(dialogMap.get("testMixedTransitions"));
+        assertNotNull(dialogMap.get("testNestedSubdialog"));
+        assertNotNull(dialogMap.get("testViewStates"));
+
+    }
+
+
+    // --------------------------------------------------------- Support Methods
+
+
+    // Check the contents of an iterator against the specified
+    // array of expected values.  Order does not matter, but exactly
+    // matching content does matter
+    private void checkItems(String message, Iterator iterator, String expected[]) {
+        Set set = new HashSet();
+        for (int i = 0; i < expected.length; i++) {
+            set.add(expected[i]);
+        }
+        while (iterator.hasNext()) {
+            String next = (String) iterator.next();
+            assertTrue(message + ": value '" + next + "' is legal", set.contains(next));
+            set.remove(next);
+        }
+        Iterator leftovers = set.iterator();
+        while (leftovers.hasNext()) {
+            String leftover = (String) leftovers.next();
+            fail(message + ": value '" + leftover + "' is missing");
+        }
+    }
+
+
+    // Check the count of items returned by the specified Iterator
+    private void checkIterator(String message, Iterator iterator, int expected) {
+        int actual = 0;
+        while (iterator.hasNext()) {
+            iterator.next();
+            actual++;
+        }
+        assertEquals(message, expected, actual);
+    }
+
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org