[hibernate-commits] Hibernate SVN: r17115 - in validator/trunk/hibernate-validator/src/test: java/org/hibernate/validation/engine/messageinterpolation and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Jul 17 07:14:33 EDT 2009


Author: hardy.ferentschik
Date: 2009-07-17 07:14:32 -0400 (Fri, 17 Jul 2009)
New Revision: 17115

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/MessageInterpolationTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ConstraintHelperTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/Engine.java
Removed:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintHelperTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Engine.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Order.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java
Modified:
   validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml
Log:
HV-184 added more tests for message interpolation

Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintHelperTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintHelperTest.java	2009-07-16 18:00:21 UTC (rev 17114)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintHelperTest.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -1,73 +0,0 @@
-// $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.lang.annotation.Annotation;
-import java.lang.reflect.Field;
-import java.util.List;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Pattern;
-
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.ReflectionHelper;
-import org.hibernate.validation.metadata.ConstraintHelper;
-
-/**
- * @author Hardy Ferentschik
- */
-public class ConstraintHelperTest {
-
-	private static ConstraintHelper constraintHelper;
-
-	@BeforeClass
-	public static void init() {
-		constraintHelper = new ConstraintHelper();
-	}
-
-	@Test
-	public void testGetMultiValueConstraints() throws Exception {
-		Engine engine = new Engine();
-		Field[] fields = engine.getClass().getDeclaredFields();
-		assertNotNull( fields );
-		assertTrue( fields.length == 1 );
-		ReflectionHelper.setAccessibility( fields[0] );
-
-		Annotation annotation = fields[0].getAnnotation( Pattern.List.class );
-		assertNotNull( annotation );
-		List<Annotation> multiValueConstraintAnnotations = constraintHelper.getMultiValueConstraints( annotation );
-		assertTrue( multiValueConstraintAnnotations.size() == 2, "There should be two constraint annotations" );
-		assertTrue( multiValueConstraintAnnotations.get( 0 ) instanceof Pattern, "Wrong constraint annotation" );
-		assertTrue( multiValueConstraintAnnotations.get( 1 ) instanceof Pattern, "Wrong constraint annotation" );
-
-
-		Order order = new Order();
-		fields = order.getClass().getDeclaredFields();
-		assertNotNull( fields );
-		assertTrue( fields.length == 1 );
-		ReflectionHelper.setAccessibility( fields[0] );
-
-		annotation = fields[0].getAnnotation( NotNull.class );
-		assertNotNull( annotation );
-		multiValueConstraintAnnotations = constraintHelper.getMultiValueConstraints( annotation );
-		assertTrue( multiValueConstraintAnnotations.size() == 0, "There should be no constraint annotations" );
-	}
-}

Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Engine.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Engine.java	2009-07-16 18:00:21 UTC (rev 17114)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Engine.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -1,41 +0,0 @@
-// $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 javax.validation.constraints.Pattern;
-
-
-/**
- * @author Hardy Ferentschik
- */
-public class Engine {
-	@Pattern.List({
-			@Pattern(regexp = "^[A-Z0-9-]+$",
-					message = "must contain alphabetical characters only"),
-			@Pattern(regexp = "^....-....-....$", message = "must match ....-....-....")
-	})
-	private String serialNumber;
-
-	public String getSerialNumber() {
-		return serialNumber;
-	}
-
-	public void setSerialNumber(String serialNumber) {
-		this.serialNumber = serialNumber;
-	}
-}

Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Order.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Order.java	2009-07-16 18:00:21 UTC (rev 17114)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Order.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -1,36 +0,0 @@
-// $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 javax.validation.constraints.NotNull;
-
-/**
- * @author Hardy Ferentschik
- */
-public class Order {
-	@NotNull
-	Integer orderNumber;
-
-	public Integer getOrderNumber() {
-		return orderNumber;
-	}
-
-	public void setOrderNumber(Integer orderNumber) {
-		this.orderNumber = orderNumber;
-	}
-}

Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java	2009-07-16 18:00:21 UTC (rev 17114)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -1,268 +0,0 @@
-// $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.Locale;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.ResourceBundle;
-import javax.validation.MessageInterpolator;
-import javax.validation.constraints.Max;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
-
-import static org.testng.Assert.assertEquals;
-import org.testng.annotations.BeforeTest;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.metadata.ConstraintDescriptorImpl;
-import org.hibernate.validation.metadata.ConstraintHelper;
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-
-/**
- * Tests for message resolution.
- *
- * @author Hardy Ferentschik
- */
-public class ResourceBundleMessageInterpolatorTest {
-
-	private ResourceBundleMessageInterpolator interpolator;
-	private NotNull notNull;
-	private ConstraintDescriptorImpl<NotNull> notNullDescriptor;
-	private Size size;
-	private ConstraintDescriptorImpl<Size> sizeDescriptor;
-
-	@BeforeTest
-	public void setUp() {
-		// Create some annotations for testing using AnnotationProxies
-		AnnotationDescriptor<NotNull> descriptor = new AnnotationDescriptor<NotNull>( NotNull.class );
-		notNull = AnnotationFactory.create( descriptor );
-		notNullDescriptor = new ConstraintDescriptorImpl<NotNull>(
-				notNull, new ConstraintHelper()
-		);
-
-		AnnotationDescriptor<Size> sizeAnnotationDescriptor = new AnnotationDescriptor<Size>( Size.class );
-		size = AnnotationFactory.create( sizeAnnotationDescriptor );
-		sizeDescriptor = new ConstraintDescriptorImpl<Size>(
-				size, new ConstraintHelper()
-		);
-	}
-
-	@Test
-	public void testSuccessfulInterpolation() {
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-		String expected = "replacement worked";
-		String actual = interpolator.interpolate( "{foo}", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "replacement worked replacement worked";
-		actual = interpolator.interpolate( "{foo} {foo}", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "This replacement worked just fine";
-		actual = interpolator.interpolate( "This {foo} just fine", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "{} { replacement worked }";
-		actual = interpolator.interpolate( "{} { {foo} }", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testMessageLiterals() {
-
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-
-		String expected = "{";
-		String actual = interpolator.interpolate( "\\{", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "}";
-		actual = interpolator.interpolate( "\\}", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "\\";
-		actual = interpolator.interpolate( "\\", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testUnSuccessfulInterpolation() {
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-
-		String expected = "foo";  // missing {}
-		String actual = interpolator.interpolate( "foo", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "#{foo  {}";
-		actual = interpolator.interpolate( "#{foo  {}", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testUnkownTokenInterpolation() {
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-
-		String expected = "{bar}";  // unkown token {}
-		String actual = interpolator.interpolate( "{bar}", context );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testDefaultInterpolation() {
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-
-		String expected = "may not be null";
-		String actual = interpolator.interpolate( notNull.message(), context );
-		assertEquals( actual, expected, "Wrong substitution" );
-
-		expected = "size must be between 0 and 2147483647";  // unkown token {}
-		context = new MessageInterpolatorContext( sizeDescriptor, null );
-		actual = interpolator.interpolate( size.message(), context );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testMessageInterpolationWithLocale() {
-		interpolator = new ResourceBundleMessageInterpolator();
-
-		String expected = "kann nicht null sein";
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-		String actual = interpolator.interpolate( notNull.message(), context, Locale.GERMAN );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testFallbackToDefaultLocale() {
-		interpolator = new ResourceBundleMessageInterpolator();
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-
-		String expected = "may not be null";
-		String actual = interpolator.interpolate( notNull.message(), context, Locale.JAPAN );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	@Test
-	public void testUserResourceBundle() {
-		interpolator = new ResourceBundleMessageInterpolator();
-		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
-
-		String expected = "no puede ser null";
-		String actual = interpolator.interpolate( notNull.message(), context, new Locale( "es", "ES" ) );
-		assertEquals( actual, expected, "Wrong substitution" );
-	}
-
-	/**
-	 * HV-102
-	 */
-	@Test
-	public void testRecursiveMessageInterpoliation() {
-		AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
-		descriptor.setValue( "message", "{replace.in.user.bundle1}" );
-		descriptor.setValue( "value", 10l );
-		Max max = AnnotationFactory.create( descriptor );
-
-
-		ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(
-				max, new ConstraintHelper()
-		);
-
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( constraintDescriptor, null );
-
-		String expected = "{replace.in.default.bundle2}";
-		String actual = interpolator.interpolate( max.message(), context );
-		assertEquals(
-				actual, expected, "Within default bundle replacement parameter evauation should not be recursive!"
-		);
-	}
-
-	/**
-	 * HV-182
-	 */
-	@Test
-	public void testCorrectMessageInterpolationIfParameterCannotBeReplaced() {
-		AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
-		String message = "Message should stay unchanged since {fubar} is not replacable";
-		descriptor.setValue( "message", message );
-		descriptor.setValue( "value", 10l );
-		Max max = AnnotationFactory.create( descriptor );
-
-
-		ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(
-				max, new ConstraintHelper()
-		);
-
-		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
-		MessageInterpolator.Context context = new MessageInterpolatorContext( constraintDescriptor, null );
-
-		String actual = interpolator.interpolate( max.message(), context );
-		assertEquals(
-				actual, message, "The message should not have changed."
-		);
-	}
-
-	/**
-	 * A dummy resource bundle which can be passed to the constructor of ResourceBundleMessageInterpolator to replace
-	 * the user specified resource bundle.
-	 */
-	class TestResourceBundle extends ResourceBundle implements Enumeration<String> {
-		private Map<String, String> testResources;
-		Iterator<String> iter;
-
-		public TestResourceBundle() {
-			testResources = new HashMap<String, String>();
-			// add some test messages
-			testResources.put( "foo", "replacement worked" );
-			testResources.put( "replace.in.user.bundle1", "{replace.in.user.bundle2}" );
-			testResources.put( "replace.in.user.bundle2", "{replace.in.default.bundle1}" );
-
-			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();
-			}
-		}
-	}
-}

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/MessageInterpolationTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/MessageInterpolationTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/MessageInterpolationTest.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -0,0 +1,109 @@
+// $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.messageinterpolation;
+
+import java.io.ByteArrayInputStream;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
+import javax.validation.Configuration;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+
+import static org.testng.Assert.assertEquals;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.engine.ResourceBundleMessageInterpolator;
+
+/**
+ * Tests for HV-184
+ *
+ * @author Hardy Ferentschik
+ */
+public class MessageInterpolationTest {
+	private Validator validator;
+
+	@BeforeClass
+	public void createValidator() throws Exception {
+		final StringBuilder lines = new StringBuilder();
+		lines.append( "bar=Message is \\\\{escaped\\\\}" ).append( "\r\n" );
+		lines.append( "baz=Message is US$ {value}" ).append( "\r\n" );
+		lines.append( "qux=Message is {missing}" ).append( "\r\n" );
+		lines.append( "escaped=wrong" ).append( "\r\n" );
+		final ResourceBundle bundle = new PropertyResourceBundle(
+				new ByteArrayInputStream( lines.toString().getBytes() )
+		);
+		Configuration<?> config = ( Configuration<?> ) Validation.byDefaultProvider()
+				.configure()
+				.messageInterpolator(
+						new ResourceBundleMessageInterpolator( bundle )
+				);
+
+		ValidatorFactory factory = config.buildValidatorFactory();
+		validator = factory.getValidator();
+	}
+
+	@Test
+	public void testCurlyBracesEscapingShouldBeRespected() {
+		final ConstraintViolation<Foo> violation = validator.validate( new Foo(), Bar.class ).iterator().next();
+		assertEquals( violation.getMessage(), "Message is {escaped}" );
+	}
+
+	@Test
+	public void testAppendReplacementNeedsToEscapeBackslashAndDollarSign() {
+		final ConstraintViolation<Foo> violation = validator.validate( new Foo(), Baz.class ).iterator().next();
+		assertEquals( violation.getMessage(), "Message is US$ 5" );
+	}
+
+	@Test
+	public void testUnknownParametersShouldBePreserved() {
+		final ConstraintViolation<Foo> violation = validator.validate( new Foo(), Qux.class ).iterator().next();
+		assertEquals( violation.getMessage(), "Message is {missing}" );
+	}
+
+	public static interface Bar {
+	}
+
+	public static interface Baz {
+	}
+
+	public static interface Qux {
+	}
+
+	public static class Foo {
+		@NotNull(message = "{bar}", groups = { Bar.class })
+		public String getBar() {
+			return null;
+		}
+
+		@Min(value = 5, message = "{baz}", groups = { Baz.class })
+		public int getBaz() {
+			return 0;
+		}
+
+
+		@NotNull(message = "{qux}", groups = { Qux.class })
+		public String getQux() {
+			return null;
+		}
+	}
+}

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java (from rev 17088, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -0,0 +1,270 @@
+// $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.messageinterpolation;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.ResourceBundle;
+import javax.validation.MessageInterpolator;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+import static org.testng.Assert.assertEquals;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.metadata.ConstraintDescriptorImpl;
+import org.hibernate.validation.metadata.ConstraintHelper;
+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.MessageInterpolatorContext;
+
+/**
+ * Tests for message resolution.
+ *
+ * @author Hardy Ferentschik
+ */
+public class ResourceBundleMessageInterpolatorTest {
+
+	private ResourceBundleMessageInterpolator interpolator;
+	private NotNull notNull;
+	private ConstraintDescriptorImpl<NotNull> notNullDescriptor;
+	private Size size;
+	private ConstraintDescriptorImpl<Size> sizeDescriptor;
+
+	@BeforeTest
+	public void setUp() {
+		// Create some annotations for testing using AnnotationProxies
+		AnnotationDescriptor<NotNull> descriptor = new AnnotationDescriptor<NotNull>( NotNull.class );
+		notNull = AnnotationFactory.create( descriptor );
+		notNullDescriptor = new ConstraintDescriptorImpl<NotNull>(
+				notNull, new ConstraintHelper()
+		);
+
+		AnnotationDescriptor<Size> sizeAnnotationDescriptor = new AnnotationDescriptor<Size>( Size.class );
+		size = AnnotationFactory.create( sizeAnnotationDescriptor );
+		sizeDescriptor = new ConstraintDescriptorImpl<Size>(
+				size, new ConstraintHelper()
+		);
+	}
+
+	@Test
+	public void testSuccessfulInterpolation() {
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+		String expected = "replacement worked";
+		String actual = interpolator.interpolate( "{foo}", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "replacement worked replacement worked";
+		actual = interpolator.interpolate( "{foo} {foo}", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "This replacement worked just fine";
+		actual = interpolator.interpolate( "This {foo} just fine", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "{} { replacement worked }";
+		actual = interpolator.interpolate( "{} { {foo} }", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testMessageLiterals() {
+
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "{";
+		String actual = interpolator.interpolate( "\\{", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "}";
+		actual = interpolator.interpolate( "\\}", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "\\";
+		actual = interpolator.interpolate( "\\", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testUnSuccessfulInterpolation() {
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "foo";  // missing {}
+		String actual = interpolator.interpolate( "foo", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "#{foo  {}";
+		actual = interpolator.interpolate( "#{foo  {}", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testUnkownTokenInterpolation() {
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "{bar}";  // unkown token {}
+		String actual = interpolator.interpolate( "{bar}", context );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testDefaultInterpolation() {
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "may not be null";
+		String actual = interpolator.interpolate( notNull.message(), context );
+		assertEquals( actual, expected, "Wrong substitution" );
+
+		expected = "size must be between 0 and 2147483647";  // unkown token {}
+		context = new MessageInterpolatorContext( sizeDescriptor, null );
+		actual = interpolator.interpolate( size.message(), context );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testMessageInterpolationWithLocale() {
+		interpolator = new ResourceBundleMessageInterpolator();
+
+		String expected = "kann nicht null sein";
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+		String actual = interpolator.interpolate( notNull.message(), context, Locale.GERMAN );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testFallbackToDefaultLocale() {
+		interpolator = new ResourceBundleMessageInterpolator();
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "may not be null";
+		String actual = interpolator.interpolate( notNull.message(), context, Locale.JAPAN );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	@Test
+	public void testUserResourceBundle() {
+		interpolator = new ResourceBundleMessageInterpolator();
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "no puede ser null";
+		String actual = interpolator.interpolate( notNull.message(), context, new Locale( "es", "ES" ) );
+		assertEquals( actual, expected, "Wrong substitution" );
+	}
+
+	/**
+	 * HV-102
+	 */
+	@Test
+	public void testRecursiveMessageInterpoliation() {
+		AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
+		descriptor.setValue( "message", "{replace.in.user.bundle1}" );
+		descriptor.setValue( "value", 10l );
+		Max max = AnnotationFactory.create( descriptor );
+
+
+		ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(
+				max, new ConstraintHelper()
+		);
+
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( constraintDescriptor, null );
+
+		String expected = "{replace.in.default.bundle2}";
+		String actual = interpolator.interpolate( max.message(), context );
+		assertEquals(
+				actual, expected, "Within default bundle replacement parameter evauation should not be recursive!"
+		);
+	}
+
+	/**
+	 * HV-182
+	 */
+	@Test
+	public void testCorrectMessageInterpolationIfParameterCannotBeReplaced() {
+		AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
+		String message = "Message should stay unchanged since {fubar} is not replacable";
+		descriptor.setValue( "message", message );
+		descriptor.setValue( "value", 10l );
+		Max max = AnnotationFactory.create( descriptor );
+
+
+		ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(
+				max, new ConstraintHelper()
+		);
+
+		interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+		MessageInterpolator.Context context = new MessageInterpolatorContext( constraintDescriptor, null );
+
+		String actual = interpolator.interpolate( max.message(), context );
+		assertEquals(
+				actual, message, "The message should not have changed."
+		);
+	}
+
+	/**
+	 * A dummy resource bundle which can be passed to the constructor of ResourceBundleMessageInterpolator to replace
+	 * the user specified resource bundle.
+	 */
+	class TestResourceBundle extends ResourceBundle implements Enumeration<String> {
+		private Map<String, String> testResources;
+		Iterator<String> iter;
+
+		public TestResourceBundle() {
+			testResources = new HashMap<String, String>();
+			// add some test messages
+			testResources.put( "foo", "replacement worked" );
+			testResources.put( "replace.in.user.bundle1", "{replace.in.user.bundle2}" );
+			testResources.put( "replace.in.user.bundle2", "{replace.in.default.bundle1}" );
+
+			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/messageinterpolation/ResourceBundleMessageInterpolatorTest.java
___________________________________________________________________
Name: svn:keywords
   + Id

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ConstraintHelperTest.java (from rev 17057, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintHelperTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ConstraintHelperTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ConstraintHelperTest.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -0,0 +1,72 @@
+// $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.metadata;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.List;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.ReflectionHelper;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintHelperTest {
+
+	private static ConstraintHelper constraintHelper;
+
+	@BeforeClass
+	public static void init() {
+		constraintHelper = new ConstraintHelper();
+	}
+
+	@Test
+	public void testGetMultiValueConstraints() throws Exception {
+		Engine engine = new Engine();
+		Field[] fields = engine.getClass().getDeclaredFields();
+		assertNotNull( fields );
+		assertTrue( fields.length == 1 );
+		ReflectionHelper.setAccessibility( fields[0] );
+
+		Annotation annotation = fields[0].getAnnotation( Pattern.List.class );
+		assertNotNull( annotation );
+		List<Annotation> multiValueConstraintAnnotations = constraintHelper.getMultiValueConstraints( annotation );
+		assertTrue( multiValueConstraintAnnotations.size() == 2, "There should be two constraint annotations" );
+		assertTrue( multiValueConstraintAnnotations.get( 0 ) instanceof Pattern, "Wrong constraint annotation" );
+		assertTrue( multiValueConstraintAnnotations.get( 1 ) instanceof Pattern, "Wrong constraint annotation" );
+
+
+		Order order = new Order();
+		fields = order.getClass().getDeclaredFields();
+		assertNotNull( fields );
+		assertTrue( fields.length == 1 );
+		ReflectionHelper.setAccessibility( fields[0] );
+
+		annotation = fields[0].getAnnotation( NotNull.class );
+		assertNotNull( annotation );
+		multiValueConstraintAnnotations = constraintHelper.getMultiValueConstraints( annotation );
+		assertTrue( multiValueConstraintAnnotations.size() == 0, "There should be no constraint annotations" );
+	}
+}


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

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/Engine.java (from rev 17057, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Engine.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/Engine.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/Engine.java	2009-07-17 11:14:32 UTC (rev 17115)
@@ -0,0 +1,41 @@
+// $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.metadata;
+
+import javax.validation.constraints.Pattern;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Engine {
+	@Pattern.List({
+			@Pattern(regexp = "^[A-Z0-9-]+$",
+					message = "must contain alphabetical characters only"),
+			@Pattern(regexp = "^....-....-....$", message = "must match ....-....-....")
+	})
+	private String serialNumber;
+
+	public String getSerialNumber() {
+		return serialNumber;
+	}
+
+	public void setSerialNumber(String serialNumber) {
+		this.serialNumber = serialNumber;
+	}
+}


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

Modified: validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml
===================================================================
--- validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml	2009-07-16 18:00:21 UTC (rev 17114)
+++ validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml	2009-07-17 11:14:32 UTC (rev 17115)
@@ -7,6 +7,7 @@
             <package name="org.hibernate.validation.constraints.impl"/>
             <package name="org.hibernate.validation.constraints.composition"/>
             <package name="org.hibernate.validation.engine"/>
+            <package name="org.hibernate.validation.engine.messageinterpolation"/>
             <package name="org.hibernate.validation.engine.groups"/>
             <package name="org.hibernate.validation.engine.traversableresolver"/>
             <package name="org.hibernate.validation.util"/>



More information about the hibernate-commits mailing list