bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject [48/50] bval git commit: test refactoring
Date Tue, 16 Oct 2018 17:31:52 GMT
test refactoring


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

Branch: refs/heads/bv2
Commit: 210beabf1b1e8009fbfca805247f1b9805468a3e
Parents: 0bfc830
Author: Matt Benson <mbenson@apache.org>
Authored: Wed Oct 10 13:57:56 2018 -0500
Committer: Matt Benson <mbenson@apache.org>
Committed: Tue Oct 16 12:28:21 2018 -0500

----------------------------------------------------------------------
 .../jsr/DefaultMessageInterpolatorTest.java     | 150 +++++++------------
 1 file changed, 58 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bval/blob/210beabf/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java
b/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java
index a6c8a5b..6bd42c5 100644
--- a/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java
+++ b/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java
@@ -17,15 +17,20 @@
 package org.apache.bval.jsr;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 
+import java.lang.annotation.Annotation;
 import java.util.Locale;
+import java.util.Objects;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
 
 import javax.validation.MessageInterpolator;
 import javax.validation.Validator;
+import javax.validation.constraints.Digits;
 import javax.validation.constraints.Pattern;
 import javax.validation.metadata.ConstraintDescriptor;
 
+import org.apache.bval.constraints.NotEmpty;
 import org.apache.bval.jsr.example.Author;
 import org.apache.bval.jsr.example.PreferredGuest;
 import org.junit.Before;
@@ -35,63 +40,60 @@ import org.junit.Test;
  * MessageResolverImpl Tester.
  */
 public class DefaultMessageInterpolatorTest {
-
-    private DefaultMessageInterpolator interpolator;
-
-    @Before
-    public void setUp() throws Exception {
-        interpolator = new DefaultMessageInterpolator();
-        interpolator.setLocale(Locale.ENGLISH);
+    private static Predicate<ConstraintDescriptor<?>> forConstraintType(Class<?
extends Annotation> type) {
+        return d -> Objects.equals(type, d.getAnnotation().annotationType());
     }
 
-    @Test
-    public void testCreateResolver() {
-
-        final Validator gvalidator = getValidator();
-
-        assertFalse(gvalidator.getConstraintsForClass(PreferredGuest.class)
-            .getConstraintsForProperty("guestCreditCardNumber").getConstraintDescriptors().isEmpty());
-
-        MessageInterpolator.Context ctx = new MessageInterpolator.Context() {
-
+    private static MessageInterpolator.Context context(Object validatedValue, Supplier<ConstraintDescriptor<?>>
descriptor){
+        return new MessageInterpolator.Context() {
+            
             @Override
-            public ConstraintDescriptor<?> getConstraintDescriptor() {
-                return gvalidator.getConstraintsForClass(PreferredGuest.class)
-                    .getConstraintsForProperty("guestCreditCardNumber").getConstraintDescriptors().iterator().next();
+            public <T> T unwrap(Class<T> type) {
+                return null;
             }
-
+            
             @Override
             public Object getValidatedValue() {
-                return "12345678";
+                return validatedValue;
             }
-
+            
             @Override
-            public <T> T unwrap(Class<T> type) {
-                return null;
+            public ConstraintDescriptor<?> getConstraintDescriptor() {
+                return descriptor.get();
             }
         };
-        String msg = interpolator.interpolate("{validator.creditcard}", ctx);
-        assertEquals("credit card is not valid", msg);
+    }
 
-        ctx = new MessageInterpolator.Context() {
-            @Override
-            public ConstraintDescriptor<?> getConstraintDescriptor() {
-                return gvalidator.getConstraintsForClass(Author.class).getConstraintsForProperty("lastName")
-                    .getConstraintDescriptors().iterator().next();
-            }
+    private DefaultMessageInterpolator interpolator;
+    private Validator validator;
 
-            @Override
-            public Object getValidatedValue() {
-                return "";
-            }
+    @Before
+    public void setUp() throws Exception {
+        interpolator = new DefaultMessageInterpolator();
+        interpolator.setLocale(Locale.ENGLISH);
+        validator = ApacheValidatorFactory.getDefault().getValidator();
+    }
 
-            @Override
-            public <T> T unwrap(Class<T> type) {
-                return null;
-            }
-        };
+    @Test
+    public void testInterpolateFromValidationResources() {
+        String msg = interpolator.interpolate("{validator.creditcard}",
+            context("12345678",
+                () -> validator.getConstraintsForClass(PreferredGuest.class)
+                    .getConstraintsForProperty("guestCreditCardNumber").getConstraintDescriptors().stream()
+                    .filter(forConstraintType(Digits.class)).findFirst()
+                    .orElseThrow(() -> new AssertionError("expected constraint missing"))));
+
+        assertEquals("credit card is not valid", msg);
+    }
+
+    @Test
+    public void testInterpolateFromDefaultResources() {
+        String msg = interpolator.interpolate("{org.apache.bval.constraints.NotEmpty.message}",
+            context("",
+                () -> validator.getConstraintsForClass(Author.class).getConstraintsForProperty("lastName")
+                    .getConstraintDescriptors().stream().filter(forConstraintType(NotEmpty.class)).findFirst()
+                    .orElseThrow(() -> new AssertionError("expected constraint missing"))));
 
-        msg = interpolator.interpolate("{org.apache.bval.constraints.NotEmpty.message}",
ctx);
         assertEquals("may not be empty", msg);
     }
 
@@ -101,57 +103,25 @@ public class DefaultMessageInterpolatorTest {
      */
     @Test
     public void testReplacementWithSpecialChars() {
-
-        final Validator validator = getValidator();
-        MessageInterpolator.Context ctx;
-
         // Try to interpolate an annotation attribute containing $
-        ctx = new MessageInterpolator.Context() {
-
-            @Override
-            public ConstraintDescriptor<?> getConstraintDescriptor() {
-                return validator.getConstraintsForClass(Person.class).getConstraintsForProperty("idNumber")
-                    .getConstraintDescriptors().iterator().next();
-            }
-
-            @Override
-            public Object getValidatedValue() {
-                return "12345678";
-            }
-
-            @Override
-            public <T> T unwrap(Class<T> type) {
-                return null;
-            }
-        };
+        String idNumberResult = this.interpolator.interpolate("Id number should match {regexp}",
+            context("12345678",
+                () -> validator.getConstraintsForClass(Person.class).getConstraintsForProperty("idNumber")
+                    .getConstraintDescriptors().stream().filter(forConstraintType(Pattern.class)).findFirst()
+                    .orElseThrow(() -> new AssertionError("expected constraint missing"))));
 
-        String result = this.interpolator.interpolate("Id number should match {regexp}",
ctx);
         assertEquals("Incorrect message interpolation when $ is in an attribute", "Id number
should match ....$",
-            result);
+            idNumberResult);
 
         // Try to interpolate an annotation attribute containing \
-        ctx = new MessageInterpolator.Context() {
-
-            @Override
-            public ConstraintDescriptor<?> getConstraintDescriptor() {
-                return validator.getConstraintsForClass(Person.class).getConstraintsForProperty("otherId")
-                    .getConstraintDescriptors().iterator().next();
-            }
+        String otherIdResult = this.interpolator.interpolate("Other id should match {regexp}",
+            context("12345678",
+                () -> validator.getConstraintsForClass(Person.class).getConstraintsForProperty("otherId")
+                    .getConstraintDescriptors().stream().filter(forConstraintType(Pattern.class)).findFirst()
+                    .orElseThrow(() -> new AssertionError("expected constraint missing"))));
 
-            @Override
-            public Object getValidatedValue() {
-                return "12345678";
-            }
-
-            @Override
-            public <T> T unwrap(Class<T> type) {
-                return null;
-            }
-        };
-
-        result = this.interpolator.interpolate("Other id should match {regexp}", ctx);
         assertEquals("Incorrect message interpolation when \\ is in an attribute value",
"Other id should match .\\n",
-            result);
+            otherIdResult);
     }
 
     public static class Person {
@@ -163,8 +133,4 @@ public class DefaultMessageInterpolatorTest {
         public String otherId;
 
     }
-
-    private Validator getValidator() {
-        return ApacheValidatorFactory.getDefault().getValidator();
-    }
 }


Mime
View raw message