You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2014/01/14 17:00:42 UTC

git commit: WICKET-5466 watch out for null component; break early from method

Updated Branches:
  refs/heads/master 5de39f4ff -> c1c1f7940


WICKET-5466 watch out for null component; break early from method

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

Branch: refs/heads/master
Commit: c1c1f79408052235339c8696a8c3564caac8a111
Parents: 5de39f4
Author: svenmeier <sv...@meiers.net>
Authored: Tue Jan 14 16:59:42 2014 +0100
Committer: svenmeier <sv...@meiers.net>
Committed: Tue Jan 14 16:59:42 2014 +0100

----------------------------------------------------------------------
 .../ListenerInterfaceRequestHandler.java        | 91 ++++++++++----------
 .../ListenerInterfaceRequestHandlerTest.java    | 31 ++++++-
 2 files changed, 74 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/c1c1f794/wicket-core/src/main/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandler.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandler.java b/wicket-core/src/main/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandler.java
index 84592d7..1e24206 100644
--- a/wicket-core/src/main/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandler.java
+++ b/wicket-core/src/main/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandler.java
@@ -172,65 +172,62 @@ public class ListenerInterfaceRequestHandler
 			component = null;
 		}
 
-		if ((component == null && freshPage) ||
-			(component != null && getComponent().getPage() == page))
+		if ((component == null && !freshPage) || (component != null && component.getPage() != page))
 		{
-			if (page instanceof Page)
-			{
-				// initialize the page to be able to check whether it is stateless
-				((Page)page).internalInitialize();
-			}
-			final boolean isStateless = page.isPageStateless();
+			throw new WicketRuntimeException("Component '" + getComponentPath()
+				+ "' has been removed from page.");
+		}
 
-			RedirectPolicy policy = isStateless ? RedirectPolicy.NEVER_REDIRECT
-				: RedirectPolicy.AUTO_REDIRECT;
-			final IPageProvider pageProvider = new PageProvider(page);
+		if (page instanceof Page)
+		{
+			// initialize the page to be able to check whether it is stateless
+			((Page)page).internalInitialize();
+		}
+		final boolean isStateless = page.isPageStateless();
+
+		RedirectPolicy policy = isStateless
+			? RedirectPolicy.NEVER_REDIRECT
+			: RedirectPolicy.AUTO_REDIRECT;
+		final IPageProvider pageProvider = new PageProvider(page);
 
-			if (freshPage && (isStateless == false || component == null))
+		if (freshPage && (isStateless == false || component == null))
+		{
+			// A listener interface is invoked on an expired page.
+
+			// If the page is stateful then we cannot assume that the listener interface is
+			// invoked on its initial state (right after page initialization) and that its
+			// component and/or behavior will be available. That's why the listener interface
+			// should be ignored and the best we can do is to re-paint the newly constructed
+			// page.
+
+			if (LOG.isDebugEnabled())
 			{
-				// A listener interface is invoked on an expired page.
-
-				// If the page is stateful then we cannot assume that the listener interface is
-				// invoked on its initial state (right after page initialization) and that its
-				// component and/or behavior will be available. That's why the listener interface
-				// should be ignored and the best we can do is to re-paint the newly constructed
-				// page.
-
-				if (LOG.isDebugEnabled())
-				{
-					LOG.debug(
-						"A ListenerInterface '{}' assigned to '{}' is executed on an expired stateful page. "
-							+ "Scheduling re-create of the page and ignoring the listener interface...",
-						listenerInterface, getComponentPath());
-				}
-
-				if (isAjax)
-				{
-					policy = RedirectPolicy.ALWAYS_REDIRECT;
-				}
-
-				requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(
-					pageProvider, policy));
-				return;
+				LOG.debug(
+					"A ListenerInterface '{}' assigned to '{}' is executed on an expired stateful page. "
+						+ "Scheduling re-create of the page and ignoring the listener interface...",
+					listenerInterface, getComponentPath());
 			}
 
-			if (isAjax == false && listenerInterface.isRenderPageAfterInvocation())
+			if (isAjax)
 			{
-				// schedule page render after current request handler is done. this can be
-				// overridden during invocation of listener
-				// method (i.e. by calling RequestCycle#setResponsePage)
-				requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(
-					pageProvider, policy));
+				policy = RedirectPolicy.ALWAYS_REDIRECT;
 			}
 
-			invokeListener();
-
+			requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(
+				pageProvider, policy));
+			return;
 		}
-		else
+
+		if (isAjax == false && listenerInterface.isRenderPageAfterInvocation())
 		{
-			throw new WicketRuntimeException("Component " + getComponent() +
-				" has been removed from page.");
+			// schedule page render after current request handler is done. this can be
+			// overridden during invocation of listener
+			// method (i.e. by calling RequestCycle#setResponsePage)
+			requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(
+				pageProvider, policy));
 		}
+
+		invokeListener();
 	}
 
 	private void invokeListener()

http://git-wip-us.apache.org/repos/asf/wicket/blob/c1c1f794/wicket-core/src/test/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandlerTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandlerTest.java b/wicket-core/src/test/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandlerTest.java
index 9cd6905..cc03296 100644
--- a/wicket-core/src/test/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandlerTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/core/request/handler/ListenerInterfaceRequestHandlerTest.java
@@ -23,13 +23,16 @@ import org.apache.wicket.MarkupContainer;
 import org.apache.wicket.Page;
 import org.apache.wicket.RequestListenerInterface;
 import org.apache.wicket.Session;
+import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.WicketTestCase;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.AjaxLink;
 import org.apache.wicket.markup.IMarkupResourceStreamProvider;
 import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.IOnChangeListener;
 import org.apache.wicket.markup.html.link.ILinkListener;
 import org.apache.wicket.request.Url;
+import org.apache.wicket.resource.DummyPage;
 import org.apache.wicket.util.resource.IResourceStream;
 import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
 import org.apache.wicket.util.resource.StringResourceStream;
@@ -42,8 +45,34 @@ public class ListenerInterfaceRequestHandlerTest extends WicketTestCase
 {
 
 	/**
+	 * WICKET-5466
+	 */
+	@Test
+	public void removedComponent()
+	{
+		// non-existing component on fresh page is ignored
+		PageAndComponentProvider freshPage = new PageAndComponentProvider(DummyPage.class, null,
+			"foo");
+		new ListenerInterfaceRequestHandler(freshPage, IOnChangeListener.INTERFACE).respond(tester
+			.getRequestCycle());
+
+		// non-existing component on old page fails
+		PageAndComponentProvider oldPage = new PageAndComponentProvider(new DummyPage(), "foo");
+		try
+		{
+			new ListenerInterfaceRequestHandler(oldPage, IOnChangeListener.INTERFACE)
+				.respond(tester.getRequestCycle());
+			fail();
+		}
+		catch (WicketRuntimeException ex)
+		{
+			assertEquals("Component 'foo' has been removed from page.", ex.getMessage());
+		}
+	}
+
+	/**
 	 * https://issues.apache.org/jira/browse/WICKET-4116
-	 *
+	 * 
 	 * @throws Exception
 	 */
 	@Test