You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by da...@apache.org on 2011/04/26 18:02:56 UTC

svn commit: r1096794 - in /wicket/trunk/wicket-core/src: main/java/org/apache/wicket/ test/java/org/apache/wicket/markup/html/

Author: dashorst
Date: Tue Apr 26 16:02:55 2011
New Revision: 1096794

URL: http://svn.apache.org/viewvc?rev=1096794&view=rev
Log:
Fixed a regression between Wicket 1.4 and 1.5, where markup ID generation/retrieval caused runtime exceptions in cases where the component was not added to the Page yet. Improved the semantics of Component#getMarkupId() and encased it in a couple unit tests.
Issue: WICKET-3647

Added:
    wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTest.java
    wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.html
    wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.java
Modified:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/Component.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=1096794&r1=1096793&r2=1096794&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 Tue Apr 26 16:02:55 2011
@@ -1499,18 +1499,20 @@ public abstract class Component
 	 */
 	public final Object getMarkupIdImpl()
 	{
-		String id = getMarkupIdFromMarkup();
-		if (id != null)
-		{
-			return id;
-		}
-
 		if (generatedMarkupId != -1)
 		{
 			return generatedMarkupId;
 		}
 
-		return getMetaData(MARKUP_ID_KEY);
+		String id = getMetaData(MARKUP_ID_KEY);
+
+		// if still no markup id is found, and the component has been attached to a page, try to
+		// retrieve the id from the markup file.
+		if (id == null && findPage() != null)
+		{
+			id = getMarkupIdFromMarkup();
+		}
+		return id;
 	}
 
 	/**

Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTest.java?rev=1096794&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTest.java (added)
+++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTest.java Tue Apr 26 16:02:55 2011
@@ -0,0 +1,162 @@
+/*
+ * 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.html;
+
+import junit.framework.Assert;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.panel.EmptyPanel;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.Test;
+
+/**
+ * Unit test to ensure that calling {@link Component#getMarkupId()} still works as advertised in
+ * Wicket 1.4.
+ */
+public class ComponentMarkupIdTest
+{
+	/** */
+	@Test
+	public void idGeneratedWhenComponentNotAddedToPage()
+	{
+		WicketTester tester = new WicketTester();
+
+		// In wicket 1.4 the following sequence would not cause an exception
+		Label label = new Label("bla", "Hello, World!");
+
+		// however in 1.5 the following statement generated a MarkupNotFoundException
+		String markupId = label.getMarkupId();
+
+		// instead 1.4 would just generate the missing markup identifier
+		Assert.assertEquals("bla1", markupId);
+	}
+
+	/** */
+	@Test
+	public void idGeneratedWhenPanelNotAddedToPage()
+	{
+		WicketTester tester = new WicketTester();
+
+		// In wicket 1.4 the following sequence would not cause an exception
+		Panel panel = new EmptyPanel("bla");
+
+		// however in 1.5 the following statement generated a WicketRuntimeException
+		// that the markup type could not be determined
+		String markupId = panel.getMarkupId();
+
+		// instead 1.4 would just generate the missing markup identifier
+		Assert.assertEquals("bla1", markupId);
+	}
+
+	/**
+	 * This tests the expected behavior where the DOM id for the component is retrieved from the
+	 * markup file, when the component has been added to the page.
+	 */
+	@Test
+	public void idFromMarkupRetrievedWhenPanelAddedToPage()
+	{
+		WicketTester tester = new WicketTester();
+
+		ComponentMarkupIdTestPage page = new ComponentMarkupIdTestPage();
+		tester.startPage(page);
+		tester.assertRenderedPage(ComponentMarkupIdTestPage.class);
+
+		Assert.assertEquals("markupPanel", page.markupPanel.getMarkupId());
+	}
+
+	/**
+	 * This tests the expected behavior where the DOM id for the component is retrieved from the
+	 * markup file, when the component has been added to the page.
+	 */
+	@Test
+	public void idFromMarkupRetrievedWhenLabelAddedToPage()
+	{
+		WicketTester tester = new WicketTester();
+
+		ComponentMarkupIdTestPage page = new ComponentMarkupIdTestPage();
+		tester.startPage(page);
+		tester.assertRenderedPage(ComponentMarkupIdTestPage.class);
+
+		Assert.assertEquals("markupLabel", page.markupLabel.getMarkupId());
+	}
+
+	/**
+	 * Tests that a generated ID is kept, even if an identifier in the markup was set.
+	 */
+	@Test
+	public void generatedIdOverridesIdFromMarkupWhenLabelAddedToPage()
+	{
+		WicketTester tester = new WicketTester();
+
+		ComponentMarkupIdTestPage page = new ComponentMarkupIdTestPage();
+		tester.startPage(page);
+		tester.assertRenderedPage(ComponentMarkupIdTestPage.class);
+
+		Assert.assertEquals("generatedLabel1", page.generatedLabelMarkupId);
+		Assert.assertEquals("generatedLabel1", page.generatedLabel.getMarkupId());
+	}
+
+	/**
+	 * Tests that a generated ID is kept, even if an identifier in the markup was set.
+	 */
+	@Test
+	public void generatedIdOverridesIdFromMarkup()
+	{
+		WicketTester tester = new WicketTester();
+
+		ComponentMarkupIdTestPage page = new ComponentMarkupIdTestPage();
+		tester.startPage(page);
+		tester.assertRenderedPage(ComponentMarkupIdTestPage.class);
+
+		Assert.assertEquals("generatedPanel2", page.generatedPanelMarkupId);
+		Assert.assertEquals("generatedPanel2", page.generatedPanel.getMarkupId());
+	}
+
+	/**
+	 * Tests that a ID set from Java code using {@link Component#setOutputMarkupId(boolean)} is
+	 * kept, even if an identifier in the markup was set.
+	 */
+	@Test
+	public void fixedIdFromJavaForLabelOverridesIdFromMarkup()
+	{
+		WicketTester tester = new WicketTester();
+
+		ComponentMarkupIdTestPage page = new ComponentMarkupIdTestPage();
+		tester.startPage(page);
+		tester.assertRenderedPage(ComponentMarkupIdTestPage.class);
+
+		Assert.assertEquals("javaLabel", page.fixedLabel.getMarkupId());
+	}
+
+	/**
+	 * Tests that a ID set from Java code using {@link Component#setOutputMarkupId(boolean)} is
+	 * kept, even if an identifier in the markup was set.
+	 */
+	@Test
+	public void fixedIdFromJavaForPanelOverridesIdFromMarkup()
+	{
+		WicketTester tester = new WicketTester();
+
+		ComponentMarkupIdTestPage page = new ComponentMarkupIdTestPage();
+		tester.startPage(page);
+		tester.assertRenderedPage(ComponentMarkupIdTestPage.class);
+
+		Assert.assertEquals("javaPanel", page.fixedPanel.getMarkupId());
+	}
+}

Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.html?rev=1096794&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.html (added)
+++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.html Tue Apr 26 16:02:55 2011
@@ -0,0 +1,13 @@
+<html xmlns:wicket>
+<head><title></title></head>
+<body>
+	<h1 id="markupLabel" wicket:id="markupLabel">Label with markup ID defined in markup</h1>
+	<div id="markupPanel" wicket:id="markupPanel">Panel with markup ID defined in markup</div>
+
+	<h1 id="generatedLabel" wicket:id="generatedLabel">Label with markup ID generated</h1>
+	<div id="generatedLabel" wicket:id="generatedPanel">Panel with markup ID generated</div>
+
+	<h1 id="fixedLabel" wicket:id="fixedLabel">Label with markup ID set in Java</h1>
+	<div id="fixedLabel" wicket:id="fixedPanel">Panel with markup ID set in Java</div>
+</body>
+</html>
\ No newline at end of file

Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.java?rev=1096794&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.java (added)
+++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/ComponentMarkupIdTestPage.java Tue Apr 26 16:02:55 2011
@@ -0,0 +1,57 @@
+/*
+ * 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.html;
+
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.panel.EmptyPanel;
+
+/** */
+public class ComponentMarkupIdTestPage extends WebPage
+{
+	private static final long serialVersionUID = 1L;
+
+	final Label markupLabel = new Label("markupLabel", "Hello, World!");
+	final EmptyPanel markupPanel = new EmptyPanel("markupPanel");
+
+	final Label generatedLabel = new Label("generatedLabel", "Hello, World!");
+	final EmptyPanel generatedPanel = new EmptyPanel("generatedPanel");
+
+	final String generatedLabelMarkupId;
+	final String generatedPanelMarkupId;
+
+	final Label fixedLabel = new Label("fixedLabel", "Hello, World!");
+	final EmptyPanel fixedPanel = new EmptyPanel("fixedPanel");
+
+	/** */
+	public ComponentMarkupIdTestPage()
+	{
+		add(markupLabel);
+		add(markupPanel);
+
+		generatedLabelMarkupId = generatedLabel.getMarkupId();
+		generatedPanelMarkupId = generatedPanel.getMarkupId();
+
+		add(generatedLabel);
+		add(generatedPanel);
+
+		fixedLabel.setMarkupId("javaLabel");
+		fixedPanel.setMarkupId("javaPanel");
+
+		add(fixedLabel);
+		add(fixedPanel);
+	}
+}