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 2017/10/13 19:58:44 UTC

wicket git commit: WICKET-6482 delegate to nested behaviors

Repository: wicket
Updated Branches:
  refs/heads/master cfcfc26d7 -> 123c10a20


WICKET-6482 delegate to nested behaviors


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

Branch: refs/heads/master
Commit: 123c10a20f5152ff2b236ae5fe6dcbe6e9ba060f
Parents: cfcfc26
Author: Sven Meier <sv...@apache.org>
Authored: Fri Oct 13 21:57:58 2017 +0200
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Oct 13 21:58:24 2017 +0200

----------------------------------------------------------------------
 .../wicket/validation/CompoundValidator.java    | 124 +++++++++++++++++++
 .../validation/CompoundValidatorTest.java       |  72 +++++++++++
 2 files changed, 196 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/123c10a2/wicket-core/src/main/java/org/apache/wicket/validation/CompoundValidator.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/validation/CompoundValidator.java b/wicket-core/src/main/java/org/apache/wicket/validation/CompoundValidator.java
index 3d9ef8f..77b2c60 100644
--- a/wicket-core/src/main/java/org/apache/wicket/validation/CompoundValidator.java
+++ b/wicket-core/src/main/java/org/apache/wicket/validation/CompoundValidator.java
@@ -21,7 +21,11 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.wicket.Component;
 import org.apache.wicket.behavior.Behavior;
+import org.apache.wicket.event.IEvent;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.head.IHeaderResponse;
 import org.apache.wicket.util.lang.Args;
 
 /**
@@ -83,4 +87,124 @@ public class CompoundValidator<T> extends Behavior implements IValidator<T>
 	{
 		return Collections.unmodifiableList(validators);
 	}
+
+	@Override
+	public void beforeRender(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).beforeRender(component);
+			}
+		}
+	}
+
+	@Override
+	public void afterRender(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).afterRender(component);
+			}
+		}
+	}
+
+	@Override
+	public void bind(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).bind(component);
+			}
+		}
+	}
+
+	@Override
+	public void unbind(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).unbind(component);
+			}
+		}
+	}
+	
+	@Override
+	public void detach(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).detach(component);
+			}
+		}
+	}
+
+	@Override
+	public void onException(Component component, RuntimeException exception)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).onException(component, exception);
+			}
+		}
+	}
+
+	public boolean getStatelessHint(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior && ((Behavior)validator).getStatelessHint(component) == false) {
+				return false;
+			}
+		}
+		return super.getStatelessHint(component);
+	}
+
+	@Override
+	public void onComponentTag(Component component, ComponentTag tag)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).onComponentTag(component, tag);
+			}
+		}
+	}
+
+	@Override
+	public void renderHead(Component component, IHeaderResponse response)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).renderHead(component, response);
+			}
+		}
+	}
+
+	@Override
+	public void onConfigure(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).onConfigure(component);
+			}
+		}
+	}
+
+	@Override
+	public void onEvent(Component component, IEvent<?> event)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).onEvent(component, event);
+			}
+		}
+	}
+
+	@Override
+	public void onRemove(Component component)
+	{
+		for (IValidator<T> validator : validators) {
+			if (validator instanceof Behavior) {
+				((Behavior)validator).onRemove(component);
+			}
+		}
+	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/123c10a2/wicket-core/src/test/java/org/apache/wicket/validation/CompoundValidatorTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/validation/CompoundValidatorTest.java b/wicket-core/src/test/java/org/apache/wicket/validation/CompoundValidatorTest.java
new file mode 100644
index 0000000..22091e5
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/validation/CompoundValidatorTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.validation;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.validation.validator.RangeValidator;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Test for {@link CompoundValidator}.
+ */
+public class CompoundValidatorTest
+{
+	/**
+	 * WICKET-6482 delegate to nested behaviors
+	 */
+	@SuppressWarnings("unchecked")
+	@Test
+	public void delegate() {
+		CompoundValidator<String> compound = new CompoundValidator<>();
+		
+		compound.add(new IValidator<String>()
+		{
+			@Override
+			public void validate(IValidatable<String> validatable)
+			{
+			}
+		});
+		
+		RangeValidator<String> validator = Mockito.mock(RangeValidator.class);
+		compound.add(validator);
+
+		Component component = null;
+
+		compound.bind(component);
+		compound.onConfigure(component);
+		compound.renderHead(component, null);
+		compound.beforeRender(component);
+		compound.onComponentTag(component, null);
+		compound.afterRender(component);
+		compound.onEvent(component, null);
+		compound.onException(component, null);
+		compound.onRemove(component);
+		compound.detach(component);
+		
+		Mockito.verify(validator).bind(component);
+		Mockito.verify(validator).onConfigure(component);
+		Mockito.verify(validator).renderHead(component, null);
+		Mockito.verify(validator).beforeRender(component);
+		Mockito.verify(validator).onComponentTag(component, null);
+		Mockito.verify(validator).afterRender(component);
+		Mockito.verify(validator).onEvent(component, null);
+		Mockito.verify(validator).onException(component, null);
+		Mockito.verify(validator).onRemove(component);
+		Mockito.verify(validator).detach(component);
+	}
+}
\ No newline at end of file