You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2012/01/31 13:49:41 UTC

git commit: WICKET-4369 Allow user supplied behaviors to override automatically created ones

Updated Branches:
  refs/heads/wicket-1.5.x 28b70d748 -> 9f4a9d2d0


WICKET-4369
Allow user supplied behaviors to override automatically created ones


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

Branch: refs/heads/wicket-1.5.x
Commit: 9f4a9d2d0b356e60c0a70400a5e397edd45aa8ec
Parents: 28b70d7
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Tue Jan 31 14:49:34 2012 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Tue Jan 31 14:49:34 2012 +0200

----------------------------------------------------------------------
 .../src/main/java/org/apache/wicket/Component.java |   30 +++---
 ...ponentBehaviorOverComponentTagBehaviorTest.java |   83 +++++++++++++++
 2 files changed, 98 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/9f4a9d2d/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 648ed95..a528f05 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Component.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Component.java
@@ -3925,34 +3925,34 @@ public abstract class Component
 	{
 		if (needToRenderTag(tag))
 		{
-			// Apply behavior modifiers
-			List<? extends Behavior> behaviors = getBehaviors();
-			if ((behaviors != null) && !behaviors.isEmpty() && !tag.isClose() &&
-				(isIgnoreAttributeModifier() == false))
+			// apply behaviors that are attached to the component tag.
+			if (tag.hasBehaviors())
 			{
-				tag = tag.mutable();
-				for (Behavior behavior : behaviors)
+				Iterator<? extends Behavior> tagBehaviors = tag.getBehaviors();
+				while (tagBehaviors.hasNext())
 				{
-					// Components may reject some behavior components
-					if (isBehaviorAccepted(behavior))
+					final Behavior behavior = tagBehaviors.next();
+					if (behavior.isEnabled(this))
 					{
 						behavior.onComponentTag(this, tag);
 					}
+					behavior.detach(this);
 				}
 			}
 
-			// apply behaviors that are attached to the component tag.
-			if (tag.hasBehaviors())
+			// Apply behavior modifiers
+			List<? extends Behavior> behaviors = getBehaviors();
+			if ((behaviors != null) && !behaviors.isEmpty() && !tag.isClose() &&
+					(isIgnoreAttributeModifier() == false))
 			{
-				Iterator<? extends Behavior> tagBehaviors = tag.getBehaviors();
-				while (tagBehaviors.hasNext())
+				tag = tag.mutable();
+				for (Behavior behavior : behaviors)
 				{
-					final Behavior behavior = tagBehaviors.next();
-					if (behavior.isEnabled(this))
+					// Components may reject some behavior components
+					if (isBehaviorAccepted(behavior))
 					{
 						behavior.onComponentTag(this, tag);
 					}
-					behavior.detach(this);
 				}
 			}
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/9f4a9d2d/wicket-core/src/test/java/org/apache/wicket/ComponentBehaviorOverComponentTagBehaviorTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/ComponentBehaviorOverComponentTagBehaviorTest.java b/wicket-core/src/test/java/org/apache/wicket/ComponentBehaviorOverComponentTagBehaviorTest.java
new file mode 100644
index 0000000..8df5d91
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/ComponentBehaviorOverComponentTagBehaviorTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.Locale;
+
+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.resource.loader.IStringResourceLoader;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+import org.junit.Test;
+
+/**
+ * Tests that behaviors added to a Component has precedence
+ * over similar behavior added on the ComponentTag for this Component
+ *
+ * https://issues.apache.org/jira/browse/WICKET-4369
+ *
+ * @since 1.5.5
+ */
+public class ComponentBehaviorOverComponentTagBehaviorTest extends WicketTestCase
+{
+	/**
+	 * Verifies that the Label's title attribute comes from the
+	 * Component behavior instead of ComponentTag's behavior (added by
+	 * WicketMessageTagHandler)
+	 */
+	@Test
+	public void runtimeBehaviorOverMarkupBehavior()
+	{
+		tester.getApplication().getResourceSettings().getStringResourceLoaders().add(new TestStringResourceLoader());
+		WicketMessage page = new WicketMessage();
+		tester.startPage(page);
+
+		tester.assertContains("title=\"Component behavior title\"");
+	}
+
+	private static class TestStringResourceLoader implements IStringResourceLoader
+	{
+		public String loadStringResource(Class<?> clazz, String key, Locale locale, String style, String variation)
+		{
+			return "markupTitle".equals(key) ? "ComponentTag behavior title" : null;
+		}
+
+		public String loadStringResource(Component component, String key, Locale locale, String style, String variation)
+		{
+			return loadStringResource(component.getClass(), key, locale, style, variation);
+		}
+	}
+	
+	private static class WicketMessage extends WebPage implements IMarkupResourceStreamProvider
+	{
+		private WicketMessage() {
+
+			Label label = new Label("l", "Label");
+			label.add(AttributeModifier.replace("title", "Component behavior title"));
+			add(label);
+		}
+
+		public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass)
+		{
+			return new StringResourceStream("<html><body>" +
+					"<span wicket:id=\"l\" wicket:message=\"title:markupTitle\">Test</span>" +
+					"</body></html>");
+		}
+	}
+}