click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r963272 - in /click/trunk/click/framework: src/org/apache/click/control/AbstractControl.java test/org/apache/click/control/AbstractControlTest.java
Date Mon, 12 Jul 2010 12:56:34 GMT
Author: sabob
Date: Mon Jul 12 12:56:34 2010
New Revision: 963272

URL: http://svn.apache.org/viewvc?rev=963272&view=rev
Log:
added a guard and test against registering control more than once

Added:
    click/trunk/click/framework/test/org/apache/click/control/AbstractControlTest.java
Modified:
    click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java

Modified: click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java?rev=963272&r1=963271&r2=963272&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/AbstractControl.java Mon Jul
12 12:56:34 2010
@@ -175,6 +175,9 @@ public abstract class AbstractControl im
     /** The listener method name. */
     protected String listenerMethod;
 
+    /** Flag indicating whether the control has been registered as an AJAX target. */
+    boolean registeredAsAjaxTarget = false;
+
     // Constructors -----------------------------------------------------------
 
     /**
@@ -266,14 +269,8 @@ public abstract class AbstractControl im
             throw new IllegalArgumentException("Behavior cannot be null");
         }
 
-        // If the control has behaviors yet, register the control as an AJAX target
-        if (!hasBehaviors()) {
-            // Register control here in case behavior was added *after* the onInit event.
-            // This can occur if the behavior is added in a listener event or during
-            // onRender.
-            ControlRegistry.registerAjaxTarget(this);
-        }
         getBehaviors().add(behavior);
+        registerAsAjaxTarget();
     }
 
     /**
@@ -599,25 +596,24 @@ public abstract class AbstractControl im
      * @see org.apache.click.Control#onInit()
      */
     public void onInit() {
-        // Q: Why does onInit need to register callback instead of addBehavior?
-        // A: Because on stateful pages addBehavior might only be called once
-        //    when the control was created so we ensure the behavior is registered
-        //    before onProcess
-        //    TODO might want to extract the code below into ClickServlet itself
-        //    to ensure this code is called *before* onProcess. Leaving the code
-        //    here opens problems if subclass does not call super.onInit
-        if (hasBehaviors()) {
-           ControlRegistry.registerAjaxTarget(this);
-        }
+        // FAQ: Why does onInit need to register callback instead of addBehavior?
+        //   A: Because on stateful pages addBehavior might only be called once
+        //      when the control was created so we ensure the behavior is registered
+        //      before onProcess
+        registerAsAjaxTarget();
     }
 
     /**
-     * This method does nothing. Subclasses may override this method to perform
-     * clean up any resources.
+     * Cleans up the AbstractControl state.
+     * <p/>
+     * <b>Please note:</b> a common problem when overriding onDestroy in
+     * subclasses is forgetting to call <em>super.onDestroy()</em>. Consider
+     * carefully whether you should call <em>super.onDestroy()</em> or not.
      *
      * @see org.apache.click.Control#onDestroy()
      */
     public void onDestroy() {
+        registeredAsAjaxTarget = false;
     }
 
     /**
@@ -1026,6 +1022,23 @@ public abstract class AbstractControl im
         return size;
     }
 
+    // Package Private Methods ------------------------------------------------
+
+    /**
+     * Register the control as an AJAX target with the ControlRegistry. If the
+     * control is already registered, this method returns silently.
+     */
+    void registerAsAjaxTarget() {
+        if (registeredAsAjaxTarget) {
+            return;
+        }
+
+        if (hasBehaviors()) {
+            ControlRegistry.registerAjaxTarget(this);
+            registeredAsAjaxTarget = true;
+        }
+    }
+
     // Private Methods --------------------------------------------------------
 
     /**

Added: click/trunk/click/framework/test/org/apache/click/control/AbstractControlTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/control/AbstractControlTest.java?rev=963272&view=auto
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/control/AbstractControlTest.java (added)
+++ click/trunk/click/framework/test/org/apache/click/control/AbstractControlTest.java Mon
Jul 12 12:56:34 2010
@@ -0,0 +1,79 @@
+/*
+ * 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.control;
+
+import junit.framework.TestCase;
+import org.apache.click.MockContext;
+import org.apache.click.ajax.AjaxBehavior;
+
+/**
+ * Test AbstractLink behavior.
+ */
+public class AbstractControlTest extends TestCase {
+
+    /**
+     * Check that AbstractControl#addBehavior registers the control as an AJAX
+     * target.
+     */
+    public void testRegisterAsAjaxTargetThroughAddBehavior() {
+        MockContext.initContext();
+
+        AbstractControl control = new AbstractControl() {
+            @Override
+            public String getTag() {
+                return null;
+            }
+        };
+
+        assertFalse(control.registeredAsAjaxTarget);
+        control.addBehavior(new AjaxBehavior());
+        assertTrue(control.registeredAsAjaxTarget);
+
+        control.onInit();
+    }
+
+    /**
+     * Check that AbstractControl#onInit registers the control as an AJAX target
+     * if it has behaviors.
+     */
+    public void testRegisterAsAjaxTargetThroughOnInit() {
+        MockContext.initContext();
+
+        AbstractControl control = new AbstractControl() {
+            @Override
+            public String getTag() {
+                return null;
+            }
+        };
+
+        // Since no behaviors have been added yet, check that onInit does not
+        // register the control as an AJAX target
+        assertFalse(control.registeredAsAjaxTarget);
+        control.onInit();
+        assertFalse(control.registeredAsAjaxTarget);
+
+        // Bypass addBehavior which would register the control with the ControlRegistry
+        control.getBehaviors().add(new AjaxBehavior());
+        assertFalse(control.registeredAsAjaxTarget);
+
+        // Check that onInit does register the control as an AJAX target
+        control.onInit();
+        assertTrue(control.registeredAsAjaxTarget);
+    }
+}



Mime
View raw message