click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r1034931 - in /click/trunk/click/mock: src/org/apache/click/MockContext.java test/org/apache/click/MockContextTest.java
Date Sun, 14 Nov 2010 03:53:58 GMT
Author: sabob
Date: Sun Nov 14 03:53:57 2010
New Revision: 1034931

URL: http://svn.apache.org/viewvc?rev=1034931&view=rev
Log:
improved MockContext to cleanup lingering ThreadLocals of the Mock stack

Modified:
    click/trunk/click/mock/src/org/apache/click/MockContext.java
    click/trunk/click/mock/test/org/apache/click/MockContextTest.java

Modified: click/trunk/click/mock/src/org/apache/click/MockContext.java
URL: http://svn.apache.org/viewvc/click/trunk/click/mock/src/org/apache/click/MockContext.java?rev=1034931&r1=1034930&r2=1034931&view=diff
==============================================================================
--- click/trunk/click/mock/src/org/apache/click/MockContext.java (original)
+++ click/trunk/click/mock/src/org/apache/click/MockContext.java Sun Nov 14 03:53:57 2010
@@ -181,6 +181,10 @@ public class MockContext extends Context
      */
     public static MockContext initContext(HttpServletRequest request) {
         MockContext mockContext = new MockContext(request);
+
+        // Remove lingering ThreadLocal variables of the Mock stack
+        mockContext.cleanup();
+
         Context.pushThreadLocalContext(mockContext);
         return (MockContext) Context.getThreadLocalContext();
     }
@@ -291,6 +295,9 @@ public class MockContext extends Context
                 controlRegistry = new ControlRegistry(configService);
             }
 
+            // Remove lingering ThreadLocal variables of the Mock stack
+        mockContext.cleanup();
+
             ActionEventDispatcher.pushThreadLocalDispatcher(actionEventDispatcher);
             ControlRegistry.pushThreadLocalRegistry(controlRegistry);
             Context.pushThreadLocalContext(mockContext);
@@ -373,10 +380,26 @@ public class MockContext extends Context
      * method will remove any references to objects, thus freeing up memory.
      */
     public void reset() {
-        ControlRegistry registry = ControlRegistry.getThreadLocalRegistry();
-        registry.clear();
+        if (ControlRegistry.hasThreadLocalRegistry()) {
+            ControlRegistry registry = ControlRegistry.getThreadLocalRegistry();
+            registry.clear();
+        }
 
-        ActionEventDispatcher actionEventDispatcher = ActionEventDispatcher.getThreadLocalDispatcher();
-        actionEventDispatcher.clear();
+        if (ActionEventDispatcher.hasThreadLocalDispatcher()) {
+            ActionEventDispatcher actionEventDispatcher = ActionEventDispatcher.getThreadLocalDispatcher();
+            actionEventDispatcher.clear();
+        }
+    }
+
+    /**
+     * Cleanup the MockContext.
+     * <p/>
+     * This method removes any lingering ThreadLocal variables from the Mock stack.
+     */
+    void cleanup() {
+        // Cleanup ThreadLocals
+        Context.getContextStack().clear();
+        ControlRegistry.getRegistryStack().clear();
+        ActionEventDispatcher.getDispatcherStack().clear();
     }
 }

Modified: click/trunk/click/mock/test/org/apache/click/MockContextTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/mock/test/org/apache/click/MockContextTest.java?rev=1034931&r1=1034930&r2=1034931&view=diff
==============================================================================
--- click/trunk/click/mock/test/org/apache/click/MockContextTest.java (original)
+++ click/trunk/click/mock/test/org/apache/click/MockContextTest.java Sun Nov 14 03:53:57
2010
@@ -385,4 +385,20 @@ public class MockContextTest extends Tes
         // Assert that the behavior was removed after reset
         assertEquals(0, registry.getAjaxTargetControls().size());
     }
+
+    /**
+     * Test that initContext removes previous Contexts from the ThreadLocal.
+     */
+    public void testInitContextCleanup() {
+        MockContext.initContext();
+        assertEquals(1, Context.getContextStack().size());
+        assertEquals(1, ActionEventDispatcher.getDispatcherStack().size());
+        assertEquals(1, ControlRegistry.getRegistryStack().size());
+
+        MockContext.initContext();
+        assertEquals(1, Context.getContextStack().size());
+        assertEquals(1, ActionEventDispatcher.getDispatcherStack().size());
+        assertEquals(1, ControlRegistry.getRegistryStack().size());
+        
+    }
 }



Mime
View raw message