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/02/03 10:03:41 UTC

git commit: WICKET-4379 org.apache.wicket.validation.ValidatorAdapter class causes problem with validator properties to be loaded

Updated Branches:
  refs/heads/wicket-1.5.x 8a5fc63fb -> a54735cd4


WICKET-4379
org.apache.wicket.validation.ValidatorAdapter class causes problem with validator properties to be loaded


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

Branch: refs/heads/wicket-1.5.x
Commit: a54735cd49dd3323291bfc07202d7be93592d618
Parents: 8a5fc63
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Fri Feb 3 11:03:34 2012 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Fri Feb 3 11:03:34 2012 +0200

----------------------------------------------------------------------
 .../loader/ValidatorStringResourceLoader.java      |   18 ++-
 ...ingResourceLoaderTest$ClassValidator.properties |    1 +
 ...esourceLoaderTest$InterfaceValidator.properties |    1 +
 .../loader/ValidatorStringResourceLoaderTest.java  |  115 +++++++++++++++
 4 files changed, 134 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/a54735cd/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java b/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
index f3f03a2..ad7a610 100644
--- a/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
+++ b/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
@@ -21,6 +21,7 @@ import java.util.Locale;
 import org.apache.wicket.Component;
 import org.apache.wicket.markup.html.form.FormComponent;
 import org.apache.wicket.validation.IValidator;
+import org.apache.wicket.validation.ValidatorAdapter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -81,7 +82,8 @@ public class ValidatorStringResourceLoader extends ComponentStringResourceLoader
 		FormComponent<?> fc = (FormComponent<?>)component;
 		for (IValidator<?> validator : fc.getValidators())
 		{
-			String resource = loadStringResource(validator.getClass(), key, locale, style,
+			Class<?> scope = getScope(validator);
+			String resource = loadStringResource(scope, key, locale, style,
 				variation);
 			if (resource != null)
 			{
@@ -92,4 +94,18 @@ public class ValidatorStringResourceLoader extends ComponentStringResourceLoader
 		// not found
 		return null;
 	}
+
+	private Class<? extends IValidator> getScope(IValidator<?> validator)
+	{
+		Class<? extends IValidator> scope;
+		if (validator instanceof ValidatorAdapter)
+		{
+			scope = ((ValidatorAdapter) validator).getValidator().getClass();
+		}
+		else
+		{
+			scope = validator.getClass();
+		}
+		return scope;
+	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/a54735cd/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$ClassValidator.properties
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$ClassValidator.properties b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$ClassValidator.properties
new file mode 100644
index 0000000..7d099e0
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$ClassValidator.properties
@@ -0,0 +1 @@
+testError=Interface error loaded OK
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/a54735cd/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$InterfaceValidator.properties
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$InterfaceValidator.properties b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$InterfaceValidator.properties
new file mode 100644
index 0000000..7d099e0
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$InterfaceValidator.properties
@@ -0,0 +1 @@
+testError=Interface error loaded OK
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/a54735cd/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
new file mode 100644
index 0000000..5a5513f
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.resource.loader;
+
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.PasswordTextField;
+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.FormTester;
+import org.apache.wicket.validation.IValidatable;
+import org.apache.wicket.validation.IValidator;
+import org.apache.wicket.validation.ValidationError;
+import org.apache.wicket.validation.validator.AbstractValidator;
+import org.junit.Test;
+
+/**
+ * Tests for ValidatorStringResourceLoader
+ *
+ * @since 1.5.5
+ */
+public class ValidatorStringResourceLoaderTest extends WicketTestCase
+{
+	/**
+	 * Tests that resource bundle is properly loaded for implementations of
+	 * IValidator need to be wrapped in ValidatorAdapter
+	 *
+	 * https://issues.apache.org/jira/browse/WICKET-4379
+	 */
+	@Test
+	public void interfaceValidator()
+	{
+		tester.startPage(new ValidatorLoaderPage(new InterfaceValidator()));
+		FormTester formTester = tester.newFormTester("form");
+		formTester.setValue("passwd", "anything");
+		formTester.submit();
+
+		tester.assertErrorMessages("Interface error loaded OK");
+	}
+
+	/**
+	 * Tests that resource bundle is properly loaded for implementations of
+	 * AbstractValidator
+	 *
+	 * https://issues.apache.org/jira/browse/WICKET-4379
+	 */
+	@Test
+	public void classValidator()
+	{
+		tester.startPage(new ValidatorLoaderPage(new ClassValidator()));
+		FormTester formTester = tester.newFormTester("form");
+		formTester.setValue("passwd", "anything");
+		formTester.submit();
+
+		tester.assertErrorMessages("Interface error loaded OK");
+	}
+	
+	private static class ValidatorLoaderPage extends WebPage implements IMarkupResourceStreamProvider
+	{
+		private ValidatorLoaderPage(IValidator<String> validator)
+		{
+			Form<Void> form = new Form<Void>("form");
+			add(form);
+
+			PasswordTextField passwordTextField = new PasswordTextField("passwd", Model.of(""));
+			form.add(passwordTextField);
+			passwordTextField.add(validator);
+		}
+
+		public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass)
+		{
+			return new StringResourceStream("<html><body><form wicket:id='form'><input type='password' wicket:id='passwd' /></form></body></html>");
+		}
+	}
+
+	private static class ClassValidator extends AbstractValidator<String>
+	{
+		@Override
+		protected void onValidate(IValidatable<String> validatable)
+		{
+			ValidationError error = new ValidationError();
+			error.addMessageKey("testError");
+			validatable.error(error);
+		}
+	}
+
+	private static class InterfaceValidator implements IValidator<String>
+	{
+
+		public void validate(IValidatable<String> validatable)
+		{
+			ValidationError error = new ValidationError();
+			error.addMessageKey("testError");
+			validatable.error(error);
+		}
+	}
+}