struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject svn commit: r209993 - in /struts/shale/trunk/use-cases/src: java/org/apache/shale/usecases/rolodex/ test/org/apache/shale/usecases/rolodex/ web/WEB-INF/ web/rolodex/
Date Sun, 10 Jul 2005 04:55:24 GMT
Author: craigmcc
Date: Sat Jul  9 21:55:23 2005
New Revision: 209993

URL: http://svn.apache.org/viewcvs?rev=209993&view=rev
Log:
Commit updates to the Rolodex example for Clay, so that it now illustrates
all of the possible mechanisms.

PR:  Bugzilla #35639
Submitted by: Gary VanMatre <gvanmatre AT comcast.net>

Added:
    struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/rolodex/
    struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/rolodex/RolodexTestCase.java
    struts/shale/trunk/use-cases/src/web/rolodex/address.html
Modified:
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Address.java
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Contact.java
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Rolodex.java
    struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/RolodexDao.java
    struts/shale/trunk/use-cases/src/web/WEB-INF/clay-config.xml
    struts/shale/trunk/use-cases/src/web/rolodex/rolodex.jsp

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Address.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Address.java?rev=209993&r1=209992&r2=209993&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Address.java (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Address.java Sat
Jul  9 21:55:23 2005
@@ -131,8 +131,6 @@
      * <p>
      * Gets the Zip Code.
      * </p>
-     * 
-     * @return
      */
     public int getZip() {
         return zip;
@@ -145,6 +143,27 @@
      */
     public void setZip(int zip) {
         this.zip = zip;
+    }
+
+    
+    /**
+     * <p>
+     * Gets the Zip Code.
+     * </p>
+     */
+    public String getZipAsString() {
+        return String.valueOf(zip);
+    }
+
+    /**
+     * <p>
+     * Sets the Zip Code.
+     * </p>
+     */
+    public void setZipAsString(String zip) {
+        try {
+            this.zip = Integer.parseInt(zip);
+        } catch (NumberFormatException e) {}
     }
 
 }

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Contact.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Contact.java?rev=209993&r1=209992&r2=209993&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Contact.java (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Contact.java Sat
Jul  9 21:55:23 2005
@@ -1,5 +1,8 @@
 package org.apache.shale.usecases.rolodex;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
 /**
  * <p>
  * Represents an entity in the rolodex.
@@ -143,7 +146,6 @@
 
     /**
      * <p>Returns the <code>name</code> in upper case.</p>
-     * @return
      */
     public String getSortName() {
        if (name != null)
@@ -151,5 +153,20 @@
        
        return null;
     }
+    
+    /**
+     * <p>Returns the <code>name</code> encoded.</p>
+     */
+    public String getEncodedName() {
+        String encName = null;
+
+        if (name != null) {
+          try {
+            encName =  URLEncoder.encode(name, "UTF-8");
+          } catch (UnsupportedEncodingException e) {}          
+       }
+       return encName;
+    }
+    
     
 }

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Rolodex.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Rolodex.java?rev=209993&r1=209992&r2=209993&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Rolodex.java (original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/Rolodex.java Sat
Jul  9 21:55:23 2005
@@ -1,5 +1,7 @@
 package org.apache.shale.usecases.rolodex;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.util.List;
 import java.util.Map;
 
@@ -12,7 +14,6 @@
 import org.apache.shale.clay.config.beans.AttributeBean;
 import org.apache.shale.clay.config.beans.ComponentBean;
 import org.apache.shale.clay.config.beans.ElementBean;
-import org.apache.shale.clay.parser.builder.Builder;
 import org.apache.shale.usecases.view.BaseViewController;
 
 /**
@@ -349,6 +350,11 @@
             // find the dao cached in application scope
             RolodexDao dao = (RolodexDao) getBean("rolodexDao");
 
+            //decode value 
+            try {
+                name = URLDecoder.decode(name, "UTF-8");
+            } catch (UnsupportedEncodingException e) {}
+            
             // finds the selected contact by name
             setSelectedContact(dao.findContact(name));
         }

Modified: struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/RolodexDao.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/RolodexDao.java?rev=209993&r1=209992&r2=209993&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/RolodexDao.java
(original)
+++ struts/shale/trunk/use-cases/src/java/org/apache/shale/usecases/rolodex/RolodexDao.java
Sat Jul  9 21:55:23 2005
@@ -175,7 +175,16 @@
 
         Object config = new Object() {
             public void addState(State state) {
-                SelectItem item = new SelectItem();
+                SelectItem item = null;
+                // add a blank option
+                if (stateDataStore.size() == 0) {
+                    item = new SelectItem();
+                    item.setLabel("");
+                    item.setValue("");
+                    stateDataStore.add(item);
+                }
+                
+                item = new SelectItem();
                 item.setLabel(state.getState());
                 item.setValue(state.getAbbrState());
                 stateDataStore.add(item);
@@ -213,8 +222,8 @@
                 "setCity", 0);
         digester.addCallMethod("dex/contacts/contact/residentialAddress/state",
                 "setState", 0);
-        //digester.addCallMethod("dex/contacts/contact/residentialAddress/zip",
-        //        "setZip", 0);
+        digester.addCallMethod("dex/contacts/contact/residentialAddress/zip",
+                "setZipAsString", 0);
         digester.addCallMethod(
                 "dex/contacts/contact/residentialAddress/province",
                 "setProvince", 0);
@@ -235,8 +244,8 @@
                 "setCity", 0);
         digester.addCallMethod("dex/contacts/contact/businessAddress/state",
                 "setState", 0);
-        //digester.addCallMethod("dex/contacts/contact/businessAddress/zip",
-        //        "setZip", 0);
+        digester.addCallMethod("dex/contacts/contact/businessAddress/zip",
+                "setZipAsString", 0);
         digester.addCallMethod("dex/contacts/contact/businessAddress/province",
                 "setProvince", 0);
         digester.addCallMethod("dex/contacts/contact/businessAddress/country",

Added: struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/rolodex/RolodexTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/rolodex/RolodexTestCase.java?rev=209993&view=auto
==============================================================================
--- struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/rolodex/RolodexTestCase.java
(added)
+++ struts/shale/trunk/use-cases/src/test/org/apache/shale/usecases/rolodex/RolodexTestCase.java
Sat Jul  9 21:55:23 2005
@@ -0,0 +1,252 @@
+package org.apache.shale.usecases.rolodex;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import javax.faces.el.ValueBinding;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.shale.clay.component.Clay;
+import org.apache.shale.clay.config.beans.ComponentBean;
+import org.apache.shale.test.base.AbstractViewControllerTestCase;
+import org.apache.shale.test.mock.MockExternalContext;
+
+public class RolodexTestCase extends AbstractViewControllerTestCase {
+
+    // Construct a new instance of this test case.
+    public RolodexTestCase(String name) {
+        super(name);
+    }
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(RolodexTestCase.class));
+
+    }
+
+    private Rolodex viewController = null;
+
+    // setup the test case
+    public void setUp() {
+        super.setUp();
+
+        // add some more mock support
+        externalContext = new MockExternalContext(servletContext, request,
+                response) {
+            public Map getRequestParameterMap() {
+                Map targetMap = new TreeMap();
+                Map params = request.getParameterMap();
+                Iterator pi = params.entrySet().iterator();
+                while (pi.hasNext()) {
+                    Map.Entry e = (Map.Entry) pi.next();
+                    // holds multiple params with the same name
+                    String[] values = (String[]) e.getValue();
+                    // Simulates clearing the HTTP request. This test
+                    // will only require single parameters but I don't
+                    // want to create a new request each itteration
+                    targetMap.put(e.getKey(), values[values.length - 1]);
+                }
+
+                return targetMap;
+            }
+
+        };
+        // hookup an new reference
+        facesContext.setExternalContext(externalContext);
+
+        // create the target view controller to test
+        viewController = new Rolodex();
+
+        // simulate a managed bean
+        ValueBinding vb = application.createValueBinding("rolodex");
+        vb.setValue(facesContext, viewController);
+
+        // simulate a managed bean
+        vb = application.createValueBinding("rolodexDao");
+        vb.setValue(facesContext, new RolodexDao());
+
+    }
+
+    // tear down test case
+    public void tearDown() {
+        super.tearDown();
+
+        viewController = null;
+    }
+
+    // test the runtime method from creating a clay subtree
+    public void testCreateTabs() {
+
+        // simulate a method binding event being invoked from the
+        // "shapeValidator" event fired on the Clay component
+        //
+        // <clay:clay id="tabs" jsfid="RUNTIME"
+        // shapeValidator="#{rolodex.createTabs}" managedBeanName="rolodex" />
+
+        // create a fake clay root display element bean
+        ComponentBean displayElementRoot = new ComponentBean();
+
+        // create a mock component for the validator style/signature
+        // of method binding
+        Clay component = new Clay();
+        component.setId("RUNTIME");
+        component.setManagedBeanName("rolodex");
+        component.setShapeValidator("#{rolodex.createTabs}");
+
+        // make the last tab active
+        viewController.setSelectedTab(RolodexDao.TAB_INDEX.length - 1);
+
+        // simulate the the "shapeValidator" event is fired from the
+        // beginEncode method of the Clay component on the view controller.
+        viewController.createTabs(facesContext, component, displayElementRoot);
+
+        // Check the number of children. Each tab has 6 nodes multiplied times
+        // the
+        // number of tabs plus two for the unordered list tags.
+        int n = (RolodexDao.TAB_INDEX.length * 6) + 2;
+
+        assertEquals("#Children", n, displayElementRoot.getChildren().size());
+
+    }
+
+    // simulate clicking a tab
+    public void testChangeTab() {
+
+        for (int t = 0; t < RolodexDao.TAB_INDEX.length; t++) {
+
+            // tab links add a param of the tab index
+            request.addParameter("tabIndex", String.valueOf(t));
+
+            // add a dummy contact
+            viewController.setSelectedContact(new Contact());
+
+            // simulate a action method binding event fired from a commandLink
+            // component
+            String forward = viewController.changeTab();
+            assertEquals("forward", "rolodex$test", forward);
+
+            // selected contact should be null after changing tabs
+            assertNull("selectedContact", viewController.getSelectedContact());
+
+            // make sure the tab is selected
+            assertEquals("selectedTab", t, viewController.getSelectedTab());
+
+        }
+
+    }
+
+    // test populating contacts for each tab index
+    public void testContactsForTab() {
+
+        // number of contacts per page
+        int[] knownGoodState = { 4, 1, 0, 0, 0, 0, 0, 0, 0 };
+
+        for (int i = 0; i < RolodexDao.TAB_INDEX.length; i++) {
+            viewController.setSelectedTab(i);
+            List contacts = viewController.getContactsForTab();
+            assertEquals("contacts on page", contacts.size(), knownGoodState[i]);
+        }
+
+    }
+
+    // simulates selecting a contact for edit on the page
+    public void testSelectContact() {
+        // force selection of the first tab
+        viewController.setSelectedTab(0);
+        // return the contacts on the first page
+        List contacts = viewController.getContactsForTab();
+
+        Iterator ci = contacts.iterator();
+        while (ci.hasNext()) {
+            Contact contact = (Contact) ci.next();
+            String nameParam = null;
+            // simulate encoded commandLink parameter
+            try {
+                nameParam = URLEncoder.encode(contact.getName(), "UTF-8");
+            } catch (UnsupportedEncodingException e) {
+            }
+            // clear out the selected contact
+            viewController.setSelectedContact(null);
+
+            request.addParameter("selectedName", nameParam);
+
+            // invoke the method on the view controller as if it was fired
+            // via an action method binding event on the name collumn in the
+            // data table
+            String forward = viewController.selectContact();
+            assertEquals("forward", "rolodex$test", forward);
+
+            // make sure something was selected
+            assertNotNull("selectedContact", viewController
+                    .getSelectedContact());
+
+            // compare the source name with the target name
+            assertEquals("contact.name", contact.getName(), viewController
+                    .getSelectedContact().getName());
+
+        }
+    }
+
+    // simulates an new/add/delete operation
+    public void testNewAddDelete() {
+
+        // initializes a new contact for binding with the form
+        viewController.setSelectedContact(null);
+        viewController.newContact();
+        assertNotNull("newContact", viewController.getSelectedContact());
+
+        // name is the only PK
+        viewController.getSelectedContact().setName("VanMatre, Gary M.");
+        viewController.saveContact();
+
+        // should be on the last tab after the save
+        assertTrue(
+                "last tab",
+                viewController.getSelectedTab() == RolodexDao.TAB_INDEX.length - 1);
+
+        List contacts = viewController.getContactsForTab();
+        assertNotNull(contacts);
+
+        // should be 1 contact on this tab
+        assertTrue("#contacts", contacts.size() == 1);
+        Contact contact = (Contact) contacts.get(0);
+        String nameParam = null;
+        try {
+            nameParam = URLEncoder.encode(contact.getName(), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+        }
+
+        request.addParameter("selectedName", nameParam);
+
+        // invoke the method on the view controller as if it was fired
+        // via an action method binding event on the name collumn in the
+        // data table
+        String forward = viewController.selectContact();
+        assertEquals("forward", "rolodex$test", forward);
+
+        // make sure something was selected
+        assertNotNull("selectedContact", viewController.getSelectedContact());
+
+        // compare the source name with the target name
+        assertEquals("contact.name", contact.getName(), viewController
+                .getSelectedContact().getName());
+
+        // delete the selectedContact
+        viewController.deleteContact();
+
+        // get the contacts on the page after the delete
+        contacts = viewController.getContactsForTab();
+
+        // should be 0 contact on this tab
+        assertTrue("#contacts", contacts.size() == 0);
+
+    }
+
+}

Modified: struts/shale/trunk/use-cases/src/web/WEB-INF/clay-config.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/WEB-INF/clay-config.xml?rev=209993&r1=209992&r2=209993&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/WEB-INF/clay-config.xml (original)
+++ struts/shale/trunk/use-cases/src/web/WEB-INF/clay-config.xml Sat Jul  9 21:55:23 2005
@@ -18,7 +18,7 @@
       <element renderId="1" jsfid="param">
          <attributes>
              <set name="name" value="selectedName"/>
-             <set name="value" useValueLateBinding="true" value="#{e.name}"/>
+             <set name="value" useValueLateBinding="true" value="#{e.encodedName}"/>
          </attributes>    
       </element>
 
@@ -100,7 +100,7 @@
 			  <set name="for"   value="state" />
 		   </attributes>
 	</component>				
-	<component jsfid="state" extends="selectOneMenu" id="state"> 
+	<component jsfid="state" extends="selectOneMenu" id="state" allowBody="false"> 
        <attributes>
 	      <set name="value" useValueLateBinding="true" value="#{managed-bean-name.state}"
/>
 		  <set name="required" value="true" />	      

Added: struts/shale/trunk/use-cases/src/web/rolodex/address.html
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/rolodex/address.html?rev=209993&view=auto
==============================================================================
--- struts/shale/trunk/use-cases/src/web/rolodex/address.html (added)
+++ struts/shale/trunk/use-cases/src/web/rolodex/address.html Sat Jul  9 21:55:23 2005
@@ -0,0 +1,34 @@
+<table border=0>
+   <tr class="contactsHeader">
+      <td rowspan="6">H<br>T<br>M<br>L<BR></td>
+   </tr>
+   <tr>
+        <td><label jsfid=street1Label>Mock Address 1:</label></td>
+        <td><input jsfid="street1" type=text></td>
+        <td><span jsfid="street1Message">Mock Error Address 1</span></td>
+   </tr>
+   <tr>
+        <td><label jsfid=street2Label>Mock Address 2:</label></td>
+        <td><input jsfid="street2" type=text></td>
+        <td><span jsfid="street2Message">Mock Error Address 2</span></td>
+   </tr>
+   <tr>
+        <td><label jsfid="cityLabel">Mock City:</label></td>
+        <td><input jsfid="city" type=text></td>
+        <td><span jsfid="cityMessage">Mock Error City</span></td>
+   </tr>
+   <tr>
+        <td><label jsfid="stateLabel">Mock State:</label></td>
+        <td><select jsfid="state">
+               <option>Mock State 1
+               <option>Mock State 2
+            </select>
+        </td>
+        <td><span jsfid="stateMessage">Mock Error State</span></td>
+   </tr>
+   <tr>
+        <td><label jsfid="zipLabel">Mock Zip:</label></td>
+        <td><input jsfid="zip" type=text/></td>
+        <td><span jsfid="zipMessage">Mock Error Zip</span></td>
+   </tr>
+</table>

Modified: struts/shale/trunk/use-cases/src/web/rolodex/rolodex.jsp
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/use-cases/src/web/rolodex/rolodex.jsp?rev=209993&r1=209992&r2=209993&view=diff
==============================================================================
--- struts/shale/trunk/use-cases/src/web/rolodex/rolodex.jsp (original)
+++ struts/shale/trunk/use-cases/src/web/rolodex/rolodex.jsp Sat Jul  9 21:55:23 2005
@@ -140,7 +140,9 @@
 					managedBeanName="rolodex" />
 				<h:outputText value="Residential Address:" style="color:#66B9CC"/>
 				<h:outputText value="Business Address:" style="color:#66B9CC"/>
-				<clay:clay id="address1" jsfid="addressPanel"
+				<!-- clay:clay id="address1" jsfid="addressPanel"
+					managedBeanName="rolodex.selectedContact.residentialAddress" / -->
+				<clay:clay id="address1" jsfid="rolodex/address.html"
 					managedBeanName="rolodex.selectedContact.residentialAddress" />
 				<clay:clay id="address2" jsfid="foreignAddressPanel"
 					managedBeanName="rolodex.selectedContact.businessAddress" />



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


Mime
View raw message