You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pe...@apache.org on 2010/11/24 18:18:19 UTC

svn commit: r1038706 - in /wicket/trunk/wicket/src/test/java/org/apache/wicket: BehaviorUrlTest.java ComponentWithLazyModelCreationTest.java

Author: pedro
Date: Wed Nov 24 17:18:18 2010
New Revision: 1038706

URL: http://svn.apache.org/viewvc?rev=1038706&view=rev
Log:
test cases preventing that the behavior index at component data get changed on component model assignment
Issue: WICKET-3142

Added:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentWithLazyModelCreationTest.java
Modified:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/BehaviorUrlTest.java

Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/BehaviorUrlTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/BehaviorUrlTest.java?rev=1038706&r1=1038705&r2=1038706&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/BehaviorUrlTest.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/BehaviorUrlTest.java Wed Nov 24 17:18:18 2010
@@ -24,6 +24,7 @@ import org.apache.wicket.markup.Componen
 import org.apache.wicket.markup.IMarkupResourceStreamProvider;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.model.Model;
 import org.apache.wicket.util.resource.IResourceStream;
 import org.apache.wicket.util.resource.StringResourceStream;
 import org.apache.wicket.util.tester.WicketTester;
@@ -52,6 +53,26 @@ public class BehaviorUrlTest extends Tes
 
 	}
 
+	/**
+	 * Asserting that the component model assigning don't affect the behavior data index
+	 * 
+	 * @see https://issues.apache.org/jira/browse/WICKET-3142
+	 */
+	public void testUrlRemainsStableAfterComponentReceiveAnModel()
+	{
+		TestPage page = new TestPage();
+
+		int indexBeforeRender = page.container.getBehaviorId(page.callbackBehavior);
+
+		page.container.setDefaultModel(Model.of(""));
+
+		int indexAfterRender = page.container.getBehaviorId(page.callbackBehavior);
+
+		assertEquals("index of behavior in the raw list should not have changed",
+			indexBeforeRender, indexAfterRender);
+
+	}
+
 	public static class TestPage extends WebPage implements IMarkupResourceStreamProvider
 	{
 		private WebMarkupContainer container;

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentWithLazyModelCreationTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentWithLazyModelCreationTest.java?rev=1038706&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentWithLazyModelCreationTest.java (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentWithLazyModelCreationTest.java Wed Nov 24 17:18:18 2010
@@ -0,0 +1,119 @@
+/*
+ * 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/LICENSE2.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 org.apache.wicket.behavior.AbstractBehavior;
+import org.apache.wicket.behavior.IBehaviorListener;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.model.CompoundPropertyModel;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.request.IRequestHandler;
+import org.apache.wicket.request.handler.ListenerInterfaceRequestHandler;
+import org.apache.wicket.request.handler.PageAndComponentProvider;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+
+/**
+ * @see https://issues.apache.org/jira/browse/WICKET3142
+ */
+public class ComponentWithLazyModelCreationTest extends WicketTestCase
+{
+	private static final String LABEL_VALUE = "some value";
+	/** just an property to be reached by the CompoundPropertyModel */
+	public String label = LABEL_VALUE;
+
+	/**
+	 * Simulates the reported problem at the ticket: an behavior at the position 0 on the component
+	 * data has its position incremented after its ULR get encoded.
+	 */
+	public void testUrlReferingSomeBehavior()
+	{
+		TestPage page = new TestPage(new CompoundPropertyModel(this));
+		tester.startPage(page);
+
+		tester.executeUrl(page.mainCallbackBehavior.statefullUrl);
+
+		tester.assertLabel("label", LABEL_VALUE);
+		assertTrue("mainCallbackBehavior was called", page.mainCallbackBehavior.requested);
+	}
+
+	/**
+	 * If the index used to encode the behavior URL is no longer valid, an possible problem is this
+	 * URL invoking the wrong component behavior
+	 */
+	public void testUrlDontCallOtherBehavior()
+	{
+		TestPage page = new TestPage(new CompoundPropertyModel(this));
+		tester.startPage(page);
+
+		tester.executeUrl(page.brotherCallbackBehavior.statefullUrl);
+
+		tester.assertLabel("label", LABEL_VALUE);
+		assertTrue("brotherCallbackBehavior was requested", page.brotherCallbackBehavior.requested);
+		assertFalse("mainCallbackBehavior was not requested", page.mainCallbackBehavior.requested);
+	}
+
+	public static class TestPage extends WebPage implements IMarkupResourceStreamProvider
+	{
+		private TestCallbackBehavior mainCallbackBehavior;
+		private TestCallbackBehavior brotherCallbackBehavior;
+
+		public TestPage(IModel model)
+		{
+			super(model);
+			mainCallbackBehavior = new TestCallbackBehavior();
+			brotherCallbackBehavior = new TestCallbackBehavior();
+			// returning an component that will force the model initialization
+			Label label = new Label("label");
+			label.add(mainCallbackBehavior);
+			label.add(brotherCallbackBehavior);
+			add(label);
+		}
+
+		public IResourceStream getMarkupResourceStream(MarkupContainer container,
+			Class<?> containerClass)
+		{
+			return new StringResourceStream("<html><a wicket:id=\"label\"></a></html>");
+		}
+	}
+
+	private static class TestCallbackBehavior extends AbstractBehavior implements IBehaviorListener
+	{
+		private boolean requested;
+		// simulating the callback URL generated for component aware behaviors
+		private String statefullUrl;
+
+		@Override
+		public void onComponentTag(Component component, ComponentTag tag)
+		{
+			super.onComponentTag(component, tag);
+			int index = component.getBehaviorId(this);
+			IRequestHandler handler = new ListenerInterfaceRequestHandler(
+				new PageAndComponentProvider(component.getPage(), component),
+				IBehaviorListener.INTERFACE, index);
+			statefullUrl = component.getRequestCycle().mapUrlFor(handler).toString();
+		}
+
+		public void onRequest()
+		{
+			requested = true;
+		}
+	}
+}