Author: ivaynberg
Date: Sat Mar 26 05:49:20 2011
New Revision: 1085649

URL: http://svn.apache.org/viewvc?rev=1085649&view=rev
Log:
delay first onInitialize cascade just before render so pages can use 
onInitialize() as well
Issue: WICKET-3218

Added:
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationIntegrationTest.java
   (with props)
Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Page.java
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationTest.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java?rev=1085649&r1=1085648&r2=1085649&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java 
(original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java 
Sat Mar 26 05:49:20 2011
@@ -924,7 +924,10 @@ public abstract class MarkupContainer ex
                        page.componentAdded(child);
 
                        // initialie the component
-                       child.internalInitialize();
+                       if (page.getFlag(FLAG_INITIALIZED))
+                       {
+                               child.internalInitialize();
+                       }
                }
 
                // if the PREPARED_FOR_RENDER flag is set, we have already 
called

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Page.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Page.java?rev=1085649&r1=1085648&r2=1085649&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Page.java 
(original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Page.java Sat Mar 
26 05:49:20 2011
@@ -572,17 +572,6 @@ public abstract class Page extends Marku
                return stateless;
        }
 
-       @Override
-       protected final void onInitialize()
-       {
-               /*
-                * final in the page because: *** doesnt make sense for pages 
*** can be called from page's
-                * constructor - which is bad *** if delayed can lead to page 
not being initialized after
-                * its constructor call - which is bad
-                */
-               super.onInitialize();
-       }
-
        /**
         * Redirect to this page.
         * 
@@ -1017,12 +1006,6 @@ public abstract class Page extends Marku
         */
        final void componentAdded(final Component component)
        {
-               if (!getFlag(FLAG_INITIALIZED))
-               {
-                       // initialize the page if not yet initialized
-                       internalInitialize();
-               }
-
                if (!component.isAuto())
                {
                        dirty();

Added: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationIntegrationTest.java?rev=1085649&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationIntegrationTest.java
 (added)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationIntegrationTest.java
 Sat Mar 26 05:49:20 2011
@@ -0,0 +1,113 @@
+/*
+ * 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.wicket;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ComponentInitializationIntegrationTest
+{
+       WicketTester tester;
+
+
+       @Before
+       public void setup()
+       {
+               tester = new WicketTester();
+       }
+
+       @After
+       public void destroy()
+       {
+               tester.destroy();
+       }
+
+       @Test
+       public void initialization()
+       {
+               TestPage page = new TestPage();
+               TestComponent t1 = new TestComponent("t1");
+               page.add(t1);
+
+               assertEquals(0, page.getCount());
+               assertEquals(0, t1.getCount());
+               tester.startPage(page);
+               assertEquals(1, page.getCount());
+               assertEquals(1, t1.getCount());
+       }
+
+
+       public static class TestPage extends WebPage implements 
IMarkupResourceStreamProvider
+       {
+               private int count;
+
+               public IResourceStream getMarkupResourceStream(MarkupContainer 
container,
+                       Class<?> containerClass)
+               {
+                       return new StringResourceStream("<html><body><div 
wicket:id='t1'></div></body></html>");
+               }
+
+               @Override
+               protected void onInitialize()
+               {
+                       super.onInitialize();
+                       count++;
+               }
+
+               public int getCount()
+               {
+                       return count;
+               }
+
+       }
+
+       private static class TestComponent extends WebMarkupContainer
+       {
+               private static final long serialVersionUID = 1L;
+
+               private int count = 0;
+
+               public TestComponent(String id)
+               {
+                       super(id);
+               }
+
+               @Override
+               protected void onInitialize()
+               {
+                       super.onInitialize();
+                       count++;
+               }
+
+               public int getCount()
+               {
+                       return count;
+               }
+
+
+       }
+
+}

Propchange: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationIntegrationTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationTest.java?rev=1085649&r1=1085648&r2=1085649&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationTest.java
 (original)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ComponentInitializationTest.java
 Sat Mar 26 05:49:20 2011
@@ -43,8 +43,15 @@ public class ComponentInitializationTest
                TestComponent t3 = new TestComponent("t3");
                TestComponent t4 = new TestComponent("t4");
 
-               // as soon as we add to page child should be initialized
+               // component is not initialized until the page has been
                page.add(t1);
+               assertEquals(0, page.getCount());
+               assertEquals(0, t1.getCount());
+
+               // initialize the page which will initialize t1 and make 
subsequent component
+               // initializations immediate on add
+               page.internalInitialize();
+               assertEquals(1, page.getCount());
                assertEquals(1, t1.getCount());
 
                // unless the page is available no initialization takes place
@@ -79,6 +86,7 @@ public class ComponentInitializationTest
                t1.add(t2);
                t2.add(t3);
 
+               page.internalInitialize();
                page.add(t1);
 
                assertEquals(1, t1.getCount());
@@ -100,6 +108,8 @@ public class ComponentInitializationTest
        public void testOnInitializeSuperVerified()
        {
                TestPage page = new TestPage();
+               page.internalInitialize();
+
                boolean illegalState = false;
                try
                {
@@ -122,6 +132,8 @@ public class ComponentInitializationTest
                WebPage page = new WebPage()
                {
                };
+               page.internalInitialize();
+
                TestComponent t1 = new TestComponent("t1");
                TestComponent t2 = new TestComponent("t2");
 
@@ -144,6 +156,9 @@ public class ComponentInitializationTest
                WebPage page = new WebPage()
                {
                };
+
+               page.internalInitialize();
+
                TestComponent t1 = new TestComponent("t1");
                TestComponent t2 = new TestComponent("t2");
                TestComponent t3 = new TestComponent("t3");
@@ -164,15 +179,25 @@ public class ComponentInitializationTest
 
        public static class TestPage extends WebPage implements 
IMarkupResourceStreamProvider
        {
-               public TestPage()
-               {
-               }
+               private int count;
 
                public IResourceStream getMarkupResourceStream(MarkupContainer 
container,
                        Class<?> containerClass)
                {
                        return new 
StringResourceStream("<html><body></body></html>");
                }
+
+               @Override
+               protected void onInitialize()
+               {
+                       super.onInitialize();
+                       count++;
+               }
+
+               public int getCount()
+               {
+                       return count;
+               }
        }
 
        private static class TestComponent extends WebMarkupContainer


Reply via email to