[hibernate-commits] Hibernate SVN: r15838 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validation/util and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Jan 30 10:50:46 EST 2009


Author: hardy.ferentschik
Date: 2009-01-30 10:50:46 -0500 (Fri, 30 Jan 2009)
New Revision: 15838

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeConstraintValidator.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationDescriptor.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java
Log:
- Refactored package structure to untangle inter packages references
- Refactored access to MessageResolver and ConstraintValidatorFactory


Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeConstraintValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeConstraintValidator.java	2009-01-30 15:49:57 UTC (rev 15837)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeConstraintValidator.java	2009-01-30 15:50:46 UTC (rev 15838)
@@ -17,8 +17,8 @@
 */
 package org.hibernate.validation.constraints;
 
+import java.lang.reflect.Array;
 import java.util.Collection;
-import java.lang.reflect.Array;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
 import javax.validation.constraints.Size;

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java	2009-01-30 15:49:57 UTC (rev 15837)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java	2009-01-30 15:50:46 UTC (rev 15838)
@@ -38,8 +38,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import javax.validation.Constraint;
 import javax.validation.ConstraintValidator;
-import javax.validation.Constraint;
 import javax.validation.ValidationException;
 
 import org.slf4j.Logger;

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationDescriptor.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationDescriptor.java	2009-01-30 15:49:57 UTC (rev 15837)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationDescriptor.java	2009-01-30 15:50:46 UTC (rev 15838)
@@ -18,8 +18,8 @@
 package org.hibernate.validation.util.annotationfactory;
 
 import java.lang.annotation.Annotation;
+import java.util.HashMap;
 import java.util.Map;
-import java.util.HashMap;
 
 /**
  * Encapsulates the data you need to create an annotation. In

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java	2009-01-30 15:49:57 UTC (rev 15837)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java	2009-01-30 15:50:46 UTC (rev 15838)
@@ -18,10 +18,10 @@
 package org.hibernate.validation.util.annotationfactory;
 
 import java.lang.annotation.Annotation;
-import java.lang.reflect.Proxy;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Constructor;
+import java.lang.reflect.Proxy;
 
 
 /**

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java (from rev 15834, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/impl/ResourceBundleMessageInterpolatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java	2009-01-30 15:50:46 UTC (rev 15838)
@@ -0,0 +1,148 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.engine;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.ResourceBundle;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+import org.hibernate.validation.engine.ResourceBundleMessageInterpolator;
+import org.hibernate.validation.engine.ConstraintDescriptorImpl;
+
+/**
+ * Tests for message resolution.
+ *
+ * @author Hardy Ferentschik
+ */
+public class ResourceBundleMessageInterpolatorTest {
+
+	private ResourceBundleMessageInterpolator interpolator;
+	private NotNull notNull;
+	private Size size;
+
+	@Before
+	public void setUp() {
+		interpolator = new ResourceBundleMessageInterpolator( new TestResources() );
+
+		AnnotationDescriptor descriptor = new AnnotationDescriptor( NotNull.class );
+		notNull = AnnotationFactory.create( descriptor );
+
+		descriptor = new AnnotationDescriptor( Size.class );
+		size = AnnotationFactory.create( descriptor );
+	}
+
+	@Test
+	public void testSuccessfulInterpolation() {
+		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl( notNull, new Class<?>[] { } );
+
+		String expected = "replacement worked";
+		String actual = interpolator.interpolate( "{foo}", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+
+		expected = "replacement worked replacement worked";
+		actual = interpolator.interpolate( "{foo} {foo}", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+
+		expected = "This replacement worked just fine";
+		actual = interpolator.interpolate( "This {foo} just fine", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+
+		expected = "{} { replacement worked }";
+		actual = interpolator.interpolate( "{} { {foo} }", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+	}
+
+	@Test
+	public void testUnSuccessfulInterpolation() {
+		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl( notNull, new Class<?>[] { } );
+		String expected = "foo";  // missing {}
+		String actual = interpolator.interpolate( "foo", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+
+		expected = "#{foo  {}";
+		actual = interpolator.interpolate( "#{foo  {}", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+	}
+
+	@Test
+	public void testUnkownTokenInterpolation() {
+		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl( notNull, new Class<?>[] { } );
+		String expected = "{bar}";  // unkown token {}
+		String actual = interpolator.interpolate( "{bar}", desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+	}
+
+	@Test
+	public void testDefaultInterpolation() {
+		ConstraintDescriptorImpl desciptor = new ConstraintDescriptorImpl( notNull, new Class<?>[] { } );
+		String expected = "may not be null";
+		String actual = interpolator.interpolate( notNull.message(), desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+
+		desciptor = new ConstraintDescriptorImpl( size, new Class<?>[] { } );
+		expected = "size must be between -2147483648 and 2147483647";  // unkown token {}
+		actual = interpolator.interpolate( size.message(), desciptor, null );
+		assertEquals( "Wrong substitution", expected, actual );
+	}
+
+
+	class TestResources extends ResourceBundle implements Enumeration<String> {
+		private Map<String, String> testResources;
+		Iterator<String> iter;
+
+		public TestResources() {
+			testResources = new HashMap<String, String>();
+			// add some test messages
+			testResources.put( "foo", "replacement worked" );
+
+			iter = testResources.keySet().iterator();
+		}
+
+		public Object handleGetObject(String key) {
+			return testResources.get( key );
+		}
+
+		public Enumeration<String> getKeys() {
+			return this;
+		}
+
+		public boolean hasMoreElements() {
+			return iter.hasNext();
+		}
+
+		public String nextElement() {
+			if ( hasMoreElements() ) {
+				return iter.next();
+			}
+			else {
+				throw new NoSuchElementException();
+			}
+		}
+	}
+}


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java
___________________________________________________________________
Name: svn:keywords
   + Id

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java (from rev 15834, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/impl/ValidatorTypeTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java	2009-01-30 15:50:46 UTC (rev 15838)
@@ -0,0 +1,75 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Map;
+import java.util.Set;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.NotNull;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+import org.hibernate.validation.constraints.composition.FrenchZipcodeConstraintValidator;
+import org.hibernate.validation.util.ValidatorTypeHelper;
+
+/**
+ * Tests for message resolution.
+ *
+ * @author Emmanuel Bernard
+ */
+public class ValidatorTypeTest {
+
+	@Test
+	public void testTypeDiscovery() {
+		Map<Class<?>, Class<? extends ConstraintValidator<? extends Annotation, ?>>> validatorsTypes = ValidatorTypeHelper
+				.getValidatorsTypes( new Class[] { FrenchZipcodeConstraintValidator.class } );
+		assertEquals( FrenchZipcodeConstraintValidator.class, validatorsTypes.get( String.class ) );
+
+		Type type = ValidatorTypeHelper
+				.extractTypeLoose( TestValidator.class );
+
+
+		Type type2 = ValidatorTypeHelper
+				.extractTypeLoose( TestValidator2.class  );
+		assertEquals( type, type2 );
+
+	}
+
+	public static class TestValidator implements ConstraintValidator<NotNull, Set<String>> {
+		public void initialize(NotNull constraintAnnotation) {
+			//To change body of implemented methods use File | Settings | File Templates.
+		}
+
+		public boolean isValid(Set<String> object, ConstraintValidatorContext constraintValidatorContext) {
+			return false;  //To change body of implemented methods use File | Settings | File Templates.
+		}
+}
+
+	public static class TestValidator2 implements ConstraintValidator<NotNull, Set<String>> {
+		public void initialize(NotNull constraintAnnotation) {
+		}
+
+		public boolean isValid(Set<String> object, ConstraintValidatorContext constraintValidatorContext) {
+			return true;
+		}
+	}
+}
\ No newline at end of file


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:mergeinfo
   + 




More information about the hibernate-commits mailing list