Author: epbernard
Date: 2008-05-07 03:22:26 -0400 (Wed, 07 May 2008)
New Revision: 14646
Added:
validator/trunk/src/java/org/hibernate/validator/interpolator/DefaultMessageInterpolatorAggregator.java
Modified:
validator/trunk/src/java/org/hibernate/validator/ClassValidator.java
validator/trunk/src/test/org/hibernate/validator/test/ValidatorTest.java
Log:
test serialiazibility. Make one DefaultMessageInterporatorAggregator transient, fix class
typo
Modified: validator/trunk/src/java/org/hibernate/validator/ClassValidator.java
===================================================================
--- validator/trunk/src/java/org/hibernate/validator/ClassValidator.java 2008-05-07
01:30:40 UTC (rev 14645)
+++ validator/trunk/src/java/org/hibernate/validator/ClassValidator.java 2008-05-07
07:22:26 UTC (rev 14646)
@@ -39,7 +39,7 @@
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.util.IdentitySet;
-import org.hibernate.validator.interpolator.DefaultMessageInterpolatorAggerator;
+import org.hibernate.validator.interpolator.DefaultMessageInterpolatorAggregator;
/**
@@ -77,7 +77,7 @@
private transient List<Validator> memberValidators;
private transient List<XMember> memberGetters;
private transient List<XMember> childGetters;
- private transient DefaultMessageInterpolatorAggerator defaultInterpolator;
+ private transient DefaultMessageInterpolatorAggregator defaultInterpolator;
private transient MessageInterpolator userInterpolator;
private static final Filter GET_ALL_FILTER = new Filter() {
public boolean returnStatic() {
@@ -192,7 +192,7 @@
memberValidators = new ArrayList<Validator>();
memberGetters = new ArrayList<XMember>();
childGetters = new ArrayList<XMember>();
- defaultInterpolator = new DefaultMessageInterpolatorAggerator();
+ defaultInterpolator = new DefaultMessageInterpolatorAggregator();
defaultInterpolator.initialize( messageBundle, defaultMessageBundle );
//build the class hierarchy to look for members in
Copied:
validator/trunk/src/java/org/hibernate/validator/interpolator/DefaultMessageInterpolatorAggregator.java
(from rev 14609,
validator/trunk/src/java/org/hibernate/validator/interpolator/DefaultMessageInterpolatorAggerator.java)
===================================================================
---
validator/trunk/src/java/org/hibernate/validator/interpolator/DefaultMessageInterpolatorAggregator.java
(rev 0)
+++
validator/trunk/src/java/org/hibernate/validator/interpolator/DefaultMessageInterpolatorAggregator.java 2008-05-07
07:22:26 UTC (rev 14646)
@@ -0,0 +1,57 @@
+//$Id: $
+package org.hibernate.validator.interpolator;
+
+import java.lang.annotation.Annotation;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.ResourceBundle;
+import java.io.Serializable;
+
+import org.hibernate.validator.MessageInterpolator;
+import org.hibernate.validator.Validator;
+import org.hibernate.AssertionFailure;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class DefaultMessageInterpolatorAggregator implements MessageInterpolator,
Serializable {
+ private transient Map<Validator, DefaultMessageInterpolator> interpolators = new
HashMap<Validator, DefaultMessageInterpolator>();
+ //transient but repopulated by the object owing a reference to the interpolator
+ private transient ResourceBundle messageBundle;
+ //transient but repopulated by the object owing a reference to the interpolator
+ private transient ResourceBundle defaultMessageBundle;
+
+ //not an interface method
+ public void initialize(ResourceBundle messageBundle, ResourceBundle
defaultMessageBundle) {
+ this.messageBundle = messageBundle;
+ this.defaultMessageBundle = defaultMessageBundle;
+ //useful when we deserialize
+ for ( DefaultMessageInterpolator interpolator : interpolators.values() ) {
+ interpolator.initialize( messageBundle, defaultMessageBundle );
+ }
+ }
+
+ public void addInterpolator(Annotation annotation, Validator validator) {
+ DefaultMessageInterpolator interpolator = new DefaultMessageInterpolator();
+ interpolator.initialize(messageBundle, defaultMessageBundle );
+ interpolator.initialize( annotation, null );
+ interpolators.put( validator, interpolator );
+ }
+
+ public String interpolate(String message, Validator validator, MessageInterpolator
defaultInterpolator) {
+ DefaultMessageInterpolator defaultMessageInterpolator = interpolators.get( validator
);
+ if (defaultMessageInterpolator == null) {
+ return message;
+ }
+ else {
+ return defaultMessageInterpolator.interpolate( message, validator, defaultInterpolator
);
+ }
+ }
+
+ public String getAnnotationMessage(Validator validator) {
+ DefaultMessageInterpolator defaultMessageInterpolator = interpolators.get( validator
);
+ String message = defaultMessageInterpolator != null ?
defaultMessageInterpolator.getAnnotationMessage() : null;
+ if (message == null) throw new AssertionFailure("Validator not registred to the
messageInterceptorAggregator");
+ return message;
+ }
+}
Modified: validator/trunk/src/test/org/hibernate/validator/test/ValidatorTest.java
===================================================================
--- validator/trunk/src/test/org/hibernate/validator/test/ValidatorTest.java 2008-05-07
01:30:40 UTC (rev 14645)
+++ validator/trunk/src/test/org/hibernate/validator/test/ValidatorTest.java 2008-05-07
07:22:26 UTC (rev 14646)
@@ -2,6 +2,11 @@
package org.hibernate.validator.test;
import java.io.Serializable;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ObjectInputStream;
import java.math.BigInteger;
import java.util.Date;
import java.util.Locale;
@@ -140,6 +145,47 @@
}
}
+ public void testSerialization() throws Exception {
+
+ Tv tv = new Tv();
+ tv.serial = "FS";
+ tv.name = "TooLong";
+ tv.expDate = new Date( new Date().getTime() + 1000 * 60 * 60 * 24 * 10 );
+ ClassValidator<Tv> classValidator = new ClassValidator<Tv>( Tv.class );
+
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ ObjectOutput out = new ObjectOutputStream( stream );
+ out.writeObject( classValidator );
+ out.close();
+ byte[] serialized = stream.toByteArray();
+ stream.close();
+ ByteArrayInputStream byteIn = new ByteArrayInputStream( serialized );
+ ObjectInputStream in = new ObjectInputStream( byteIn );
+ classValidator = (ClassValidator<Tv>) in.readObject();
+ in.close();
+ byteIn.close();
+
+
+ InvalidValue[] invalidValues = classValidator.getInvalidValues( tv );
+ assertEquals( 1, invalidValues.length );
+ Locale loc = Locale.getDefault();
+ if ( loc.toString().startsWith( "en" ) ) {
+ assertEquals( "length must be between 0 and 2",
invalidValues[0].getMessage() );
+ }
+ else if ( loc.toString().startsWith( "fr" ) ) {
+ String message = invalidValues[0].getMessage();
+ String message2 ="la longueur doit �tre entre 0 et 2";
+ assertEquals( message2, message );
+ }
+ else if ( loc.toString().startsWith( "da" ) ) {
+ assertEquals( "l�ngden skal v�re mellem 0 og 2",
invalidValues[0].getMessage() );
+ }
+ else {
+ // if default not found then it must be english
+ assertEquals( "length must be between 0 and 2",
invalidValues[0].getMessage() );
+ }
+ }
+
public class PrefixMessageInterpolator implements MessageInterpolator, Serializable {
private String prefix;
Show replies by date