Author: mgrigorov
Date: Thu Jul 28 13:13:40 2011
New Revision: 1151831

URL: http://svn.apache.org/viewvc?rev=1151831&view=rev
Log:
WICKET-3931 Component markup caching inconsistencies


Added:
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_one.html
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_two.html
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest.java
Modified:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/internal/InlineEnclosure.java
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/internal/AjaxEnclosureTest.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java?rev=1151831&r1=1151830&r2=1151831&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java 
(original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.java Thu 
Jul 28 13:13:40 2011
@@ -36,6 +36,7 @@ import org.apache.wicket.feedback.Feedba
 import org.apache.wicket.feedback.IFeedback;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.IMarkupFragment;
+import org.apache.wicket.markup.MarkupCache;
 import org.apache.wicket.markup.MarkupElement;
 import org.apache.wicket.markup.MarkupException;
 import org.apache.wicket.markup.MarkupNotFoundException;
@@ -1170,6 +1171,8 @@ public abstract class Component
 
                requestFlags = 0;
 
+               internalDetach();
+
                // notify any detach listener
                IDetachListener detachListener = 
getApplication().getFrameworkSettings()
                        .getDetachListener();
@@ -1180,6 +1183,15 @@ public abstract class Component
        }
 
        /**
+        * Removes the cached markup at the end of the request. For the next 
request it will be get
+        * either from the parent's markup or from {@link MarkupCache}.
+        */
+       private void internalDetach()
+       {
+               markup = null;
+       }
+
+       /**
         * Detaches all models
         */
        public void detachModels()
@@ -3065,7 +3077,7 @@ public abstract class Component
                        {
                                setFlag(FLAG_PLACEHOLDER, false);
                                // I think it's better to not setOutputMarkupId 
to false...
-                               // user can do it if we want
+                               // user can do it if she want
                        }
                }
                return this;

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/internal/InlineEnclosure.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/internal/InlineEnclosure.java?rev=1151831&r1=1151830&r2=1151831&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/internal/InlineEnclosure.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/internal/InlineEnclosure.java
 Thu Jul 28 13:13:40 2011
@@ -17,6 +17,8 @@
 package org.apache.wicket.markup.html.internal;
 
 import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.IMarkupFragment;
+import org.apache.wicket.markup.Markup;
 import org.apache.wicket.markup.parser.filter.InlineEnclosureHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,6 +44,8 @@ public class InlineEnclosure extends Enc
 
        private static final Logger log = 
LoggerFactory.getLogger(InlineEnclosure.class);
 
+       private String enclosureMarkupAsString;
+
        /**
         * Construct.
         * 
@@ -53,8 +57,9 @@ public class InlineEnclosure extends Enc
        {
                super(id, childId);
 
+               enclosureMarkupAsString = null;
+
                // ensure that the Enclosure is ready for ajax updates
-               setOutputMarkupId(true);
                setOutputMarkupPlaceholderTag(true);
                setMarkupId(getId());
        }
@@ -79,4 +84,32 @@ public class InlineEnclosure extends Enc
                setVisible(visible);
                return visible;
        }
+
+       /**
+        * {@link InlineEnclosure}s keep their own cache of their markup 
because Component#markup is
+        * detached and later during Ajax request it is hard to re-lookup 
{@link InlineEnclosure}'s
+        * markup from its parent.
+        * 
+        * @see org.apache.wicket.Component#getMarkup()
+        */
+       @Override
+       public IMarkupFragment getMarkup()
+       {
+               IMarkupFragment enclosureMarkup = null;
+               if (enclosureMarkupAsString == null)
+               {
+                       IMarkupFragment markup = super.getMarkup();
+                       if (markup != null && markup != Markup.NO_MARKUP)
+                       {
+                               enclosureMarkup = markup;
+                               enclosureMarkupAsString = markup.toString(true);
+                       }
+               }
+               else
+               {
+                       enclosureMarkup = Markup.of(enclosureMarkupAsString);
+               }
+
+               return enclosureMarkup;
+       }
 }

Added: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_one.html
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest%24VariationPanel_one.html?rev=1151831&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_one.html
 (added)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_one.html
 Thu Jul 28 13:13:40 2011
@@ -0,0 +1,25 @@
+<wicket:panel>
+
+    <a wicket:id="l">Change variation</a>
+
+    <br/>One
+
+    <span wicket:id="simpleLabel">Text</span>
+    <br/>One after Label
+    <form action="#" wicket:id="a_form">
+        <br/>in One Form
+        <input type="submit" value="Delete" name="delete"/>
+    </form>
+    
+    <div wicket:enclosure="child">
+        Enclosure One
+        <span wicket:id="child"></span>
+    
+        <div wicket:enclosure="nestedChild">
+            Nested Enclosure One
+            <span wicket:id="nestedChild"></span>
+        </div>
+    </div>
+    
+    <br/>One
+</wicket:panel>
\ No newline at end of file

Added: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_two.html
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest%24VariationPanel_two.html?rev=1151831&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_two.html
 (added)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest$VariationPanel_two.html
 Thu Jul 28 13:13:40 2011
@@ -0,0 +1,25 @@
+<wicket:panel>
+
+    <a wicket:id="l">Change variation</a>
+
+    <br/>Two
+
+    <span wicket:id="simpleLabel">Text</span>
+    <br/>Two after Label
+    <form action="#" wicket:id="a_form">
+        <br/>in Two Form
+        <input type="submit" value="Delete" name="delete"/>
+    </form>
+    
+    <div wicket:enclosure="child">
+        Enclosure Two
+        <span wicket:id="child"></span>
+    
+        <div wicket:enclosure="nestedChild">
+            Nested Enclosure Two
+            <span wicket:id="nestedChild"></span>
+        </div>
+    </div>
+    
+    <br/>Two
+</wicket:panel>
\ No newline at end of file

Added: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest.java?rev=1151831&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest.java
 (added)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/MarkupVariationTest.java
 Thu Jul 28 13:13:40 2011
@@ -0,0 +1,106 @@
+/*
+ * 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.markup;
+
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.markup.html.AjaxLink;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+import org.junit.Test;
+
+/**
+ * Tests that changing component's variation will use the correct markup
+ */
+public class MarkupVariationTest extends WicketTestCase
+{
+
+       /**
+        * https://issues.apache.org/jira/browse/WICKET-3931
+        */
+       @Test
+       public void changeVariation()
+       {
+               tester.startPage(new VariationPage());
+               tester.assertContainsNot("Two");
+               tester.clickLink("p:l");
+
+               tester.assertContainsNot("One");
+               tester.clickLink("p:l");
+
+               tester.assertContainsNot("Two");
+               tester.clickLink("p:l");
+       }
+
+       private static class VariationPage extends WebPage implements 
IMarkupResourceStreamProvider
+       {
+               private VariationPage()
+               {
+                       add(new VariationPanel("p"));
+               }
+
+               public IResourceStream getMarkupResourceStream(MarkupContainer 
container,
+                       Class<?> containerClass)
+               {
+
+                       return new StringResourceStream("<html><body><div 
wicket:id='p'></div></body></html>");
+               }
+       }
+
+       private static class VariationPanel extends Panel
+       {
+               private String variation;
+
+               public VariationPanel(String id)
+               {
+                       super(id);
+
+                       setOutputMarkupId(true);
+                       variation = "one";
+
+                       add(new AjaxLink<Void>("l")
+                       {
+
+                               @Override
+                               public void onClick(AjaxRequestTarget target)
+                               {
+                                       variation = "one".equals(variation) ? 
"two" : "one";
+                                       target.add(VariationPanel.this);
+                               }
+                       });
+
+                       add(new Label("simpleLabel", "Label"));
+
+                       add(new Form<Void>("a_form"));
+
+                       add(new Label("child", "Inline Enclosure child text"));
+                       add(new Label("nestedChild", "Nested Inline Enclosure 
child text"));
+
+               }
+
+               @Override
+               public String getVariation()
+               {
+                       return variation;
+               }
+       }
+}

Modified: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/internal/AjaxEnclosureTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/internal/AjaxEnclosureTest.java?rev=1151831&r1=1151830&r2=1151831&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/internal/AjaxEnclosureTest.java
 (original)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/internal/AjaxEnclosureTest.java
 Thu Jul 28 13:13:40 2011
@@ -218,7 +218,7 @@ public class AjaxEnclosureTest extends W
 
                {
                        // enclosure On
-                       AjaxEnclosurePage_3 ajaxPage = 
(AjaxEnclosurePage_3)tester.startPage(AjaxEnclosurePage_3.class);
+                       AjaxEnclosurePage_3 ajaxPage = 
tester.startPage(AjaxEnclosurePage_3.class);
                        assertVisible(ajaxPage.getLabel1(), true);
                        ensureEnclosureIsVisible(enclosurePath, ajaxPage);
                        
tester.clickLink(ajaxPage.getToggleLabel1Link().getPageRelativePath());


Reply via email to