click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r1037191 - in /click/trunk/click/extras: src/org/apache/click/extras/cayenne/CayenneForm.java test/org/apache/click/extras/control/CayenneFormTest.java
Date Sat, 20 Nov 2010 12:32:13 GMT
Author: sabob
Date: Sat Nov 20 12:32:12 2010
New Revision: 1037191

URL: http://svn.apache.org/viewvc?rev=1037191&view=rev
Log:
don't store FO_CLASS in session as it doesn't change between requests

Added:
    click/trunk/click/extras/test/org/apache/click/extras/control/CayenneFormTest.java
Modified:
    click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java

Modified: click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java?rev=1037191&r1=1037190&r2=1037191&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java Sat Nov
20 12:32:12 2010
@@ -20,6 +20,7 @@ package org.apache.click.extras.cayenne;
 
 import java.math.BigDecimal;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.cayenne.BaseContext;
 import org.apache.click.control.Checkbox;
@@ -470,6 +471,28 @@ public class CayenneForm extends Form {
     }
 
     /**
+     * Return the CayenneForm state.
+     *
+     * @see org.apache.click.control.Form
+     *
+     * @return the state of input Fields and FieldSets contained in the form
+     */
+    @Override
+    public Object getState() {
+        Map stateMap = (Map) super.getState();
+
+        if (stateMap == null || stateMap.isEmpty()) {
+            return null;
+        }
+
+        // Ensure the FO_CLASS HiddenField isn't stored as it's value does not change
+        // between requests
+        stateMap.remove(FO_CLASS);
+
+        return stateMap;
+    }
+
+    /**
      * Save the object to the database committing all changes in the
      * <tt>DataContext</tt> and return true.
      * If a <tt>ValidationException</tt>

Added: click/trunk/click/extras/test/org/apache/click/extras/control/CayenneFormTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/test/org/apache/click/extras/control/CayenneFormTest.java?rev=1037191&view=auto
==============================================================================
--- click/trunk/click/extras/test/org/apache/click/extras/control/CayenneFormTest.java (added)
+++ click/trunk/click/extras/test/org/apache/click/extras/control/CayenneFormTest.java Sat
Nov 20 12:32:12 2010
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.click.extras.control;
+
+import java.util.HashMap;
+import java.util.Map;
+import junit.framework.TestCase;
+import org.apache.click.MockContext;
+import org.apache.click.control.Field;
+import org.apache.click.control.TextField;
+import org.apache.click.extras.cayenne.CayenneForm;
+
+/**
+ * Provides test cases for CayenneForm.
+ */
+public class CayenneFormTest extends TestCase {
+
+
+    /**
+     * Test that CayenneForm.getState contains the state of all the Fields in
+     * the CayenneForm. Also test that Immutable HiddenFields are not saved as
+     * it is unnecessary state to keep in memory.
+
+     * CLK-715
+     */
+    public void testGetState() {
+        MockContext.initContext();
+
+        // Setup CayenneForm and Fields
+        String foOid = "100";
+        String foClass = "com.mycorp.Person";
+        String name = "Steve";
+
+        CayenneForm form = new CayenneForm();
+        form.setName("form");
+        Field nameField  = new TextField("name");
+        nameField.setValue(name);
+        form.add(nameField);
+        Field foOidField = form.getField(CayenneForm.FO_ID);
+        foOidField.setValue(foOid);
+        Field foClassField = form.getField(CayenneForm.FO_CLASS);
+        foClassField.setValue(foClass);
+
+        // Retrieve state
+        Object state = form.getState();
+        Map formStateMap = (Map) state;
+
+        // Perform tests
+        assertEquals(formStateMap.get(nameField.getName()), name);
+        assertEquals(formStateMap.get(foOidField.getName()), foOid);
+
+        // Check that the FO_CLASS isn't persisted. FO_CLASS will always be the
+        // same so no need store it in the session
+        assertNull(formStateMap.get(foClassField.getName()));
+
+        // Check that only the fields defined above are returned
+        assertEquals(2, formStateMap.size());
+    }
+
+    /**
+     * Test that CayenneForm.setState correctly set the state of the Fields in
+     * the CayenneForm.
+     *
+     * CLK-715
+     */
+    public void testSetState() {
+        MockContext.initContext();
+
+        // Setup CayenneForm and Fields
+        CayenneForm form = new CayenneForm();
+        form.setName("form");
+        Field nameField  = new TextField("name");
+        form.add(nameField);
+        Field foOidField = form.getField(CayenneForm.FO_ID);
+
+        // Setup state
+        Map state = new HashMap();
+        String foOid = "100";
+        state.put(CayenneForm.FO_ID, foOid);
+        String name = "Steve";
+        state.put("name", name);
+        // Set state
+        form.setState(state);
+
+        // Perform tests
+        assertEquals(name, nameField.getValue());
+        assertEquals(foOid, foOidField.getValue());
+    }
+}



Mime
View raw message