Updated Branches:
  refs/heads/master 6a4971045 -> 7a162f775

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/7a162f77
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/7a162f77
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/7a162f77

Branch: refs/heads/master
Commit: 7a162f7753b5fe79737e60c7a13d29ba480ec823
Parents: 6a49710
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Thu Feb 9 15:52:26 2012 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Thu Feb 9 15:52:26 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/7a162f77/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/7a162f77/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..cf2ef64
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$ClassValidator.properties
@@ -0,0 +1 @@
+testError=Class error loaded OK
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/7a162f77/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/7a162f77/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..144b5b1
--- /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("Class 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);
+               }
+       }
+}

Reply via email to