Updated Branches:
  refs/heads/wicket-1.5.x 9f4a9d2d0 -> 31f4ac4d4

WICKET-4361
Markup Inheritance & getVariation(): markup loading (and merging) order depends 
on whether the super constructor adds components or not.


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/31f4ac4d
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/31f4ac4d
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/31f4ac4d

Branch: refs/heads/wicket-1.5.x
Commit: 31f4ac4d4f34f7c2d0924894dcc7fe5187558f2e
Parents: 9f4a9d2
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Tue Jan 31 17:56:29 2012 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Tue Jan 31 18:07:23 2012 +0200

----------------------------------------------------------------------
 .../src/main/java/org/apache/wicket/Component.java |   11 ++-
 .../java/org/apache/wicket/MarkupContainer.java    |   42 -------
 .../org/apache/wicket/OnMarkupAttachedTest.java    |   96 +++++++++++++++
 3 files changed, 106 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/31f4ac4d/wicket-core/src/main/java/org/apache/wicket/Component.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/Component.java 
b/wicket-core/src/main/java/org/apache/wicket/Component.java
index a528f05..c11dcef 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Component.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Component.java
@@ -743,7 +743,7 @@ public abstract class Component
         * 
         * @return false, if it was called the first time
         */
-       final boolean internalOnMarkupAttached()
+       private boolean internalOnMarkupAttached()
        {
                boolean rtn = getFlag(FLAG_MARKUP_ATTACHED);
                if (rtn == false)
@@ -886,6 +886,15 @@ public abstract class Component
        protected void onInitialize()
        {
                setRequestFlag(RFLAG_INITIALIZE_SUPER_CALL_VERIFIED, true);
+
+               try
+               {
+                       internalOnMarkupAttached();
+               }
+               catch (WicketRuntimeException exception)
+               {
+                       // ignore
+               }
        }
 
        /**

http://git-wip-us.apache.org/repos/asf/wicket/blob/31f4ac4d/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java 
b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
index e585d6c..96829a9 100644
--- a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
@@ -174,48 +174,6 @@ public abstract class MarkupContainer extends Component 
implements Iterable<Comp
                                throw new 
IllegalArgumentException(exceptionMessage("A child with id '" +
                                        child.getId() + "' already exists"));
                        }
-
-                       // One of the key pre-requisites to successfully load 
markup, is the availability of the
-                       // file extension. Which in turn is part of MarkupType 
which by default requires the
-                       // Page.
-                       if (getMarkupType() != null)
-                       {
-                               // Check if the markup is available after the 
child has been added to the parent
-                               try
-                               {
-                                       // If not yet triggered, then do now 
(e.g. Pages)
-                                       if (getMarkup() != null)
-                                       {
-                                               internalOnMarkupAttached();
-                                       }
-
-                                       if (child.getMarkup() != null)
-                                       {
-                                               
child.internalOnMarkupAttached();
-
-                                               // Tell all children of 
"component" as well
-                                               if (child instanceof 
MarkupContainer)
-                                               {
-                                                       MarkupContainer 
container = (MarkupContainer)child;
-                                                       
container.visitChildren(new IVisitor<Component, Void>()
-                                                       {
-                                                               public void 
component(final Component component,
-                                                                       final 
IVisit<Void> visit)
-                                                               {
-                                                                       if 
(component.internalOnMarkupAttached())
-                                                                       {
-                                                                               
visit.dontGoDeeper();
-                                                                       }
-                                                               }
-                                                       });
-                                               }
-                                       }
-                               }
-                               catch (WicketRuntimeException exception)
-                               {
-                                       // ignore
-                               }
-                       }
                }
                return this;
        }

http://git-wip-us.apache.org/repos/asf/wicket/blob/31f4ac4d/wicket-core/src/test/java/org/apache/wicket/OnMarkupAttachedTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/OnMarkupAttachedTest.java 
b/wicket-core/src/test/java/org/apache/wicket/OnMarkupAttachedTest.java
new file mode 100644
index 0000000..82a0344
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/OnMarkupAttachedTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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 java.util.concurrent.atomic.AtomicInteger;
+
+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.junit.Test;
+
+/**
+ * Verifies that each Component's onMarkupAttached() is called exactly once
+ * for its lifecycle.
+ *
+ * https://issues.apache.org/jira/browse/WICKET-4361
+ *
+ * @since 1.5.5
+ */
+public class OnMarkupAttachedTest extends WicketTestCase
+{
+       @Test
+       public void onMarkupAttached()
+       {
+               AtomicInteger counter = new AtomicInteger(0);
+               OnMarkupAttachedPage page = new OnMarkupAttachedPage(counter);
+               tester.startPage(page);
+
+               assertEquals(3, counter.get());
+       }
+       
+       private static class OnMarkupAttachedPage extends WebPage implements 
IMarkupResourceStreamProvider
+       {
+               private final AtomicInteger counter;
+
+               private OnMarkupAttachedPage(AtomicInteger counter)
+               {
+                       this.counter = counter;
+
+                       WebMarkupContainer comp1 = new WebMarkupContainer("one")
+                       {
+                               @Override
+                               protected void onMarkupAttached()
+                               {
+                                       super.onMarkupAttached();
+                                       assertEquals(1, 
getCounter().getAndIncrement());
+                               }
+                       };
+                       
+                       WebMarkupContainer comp2 = new WebMarkupContainer("two")
+                       {
+                               @Override
+                               protected void onMarkupAttached()
+                               {
+                                       super.onMarkupAttached();
+                                       assertEquals(2, 
getCounter().getAndIncrement());
+                               }
+                       };
+                       comp1.add(comp2);
+                       add(comp1);
+               }
+
+               @Override
+               protected void onMarkupAttached()
+               {
+                       super.onMarkupAttached();
+                       assertEquals(0, counter.getAndIncrement());
+               }
+
+               private AtomicInteger getCounter()
+               {
+                       return counter;
+               }
+
+               public IResourceStream getMarkupResourceStream(MarkupContainer 
container, Class<?> containerClass)
+               {
+                       return new StringResourceStream("<html><body><div 
wicket:id='one'><div wicket:id='two'></div></div></body></html>");
+               }
+       }
+}

Reply via email to