Author: alexsmirnov
Date: 2009-07-27 20:21:54 -0400 (Mon, 27 Jul 2009)
New Revision: 15018
Added:
branches/community/3.3.X/samples/beanValidatorSample/src/main/java/org/richfaces/LocaleBean.java
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/ObjectValidatorTest.java
branches/community/3.3.X/ui/beanValidator/src/test/resources/ValidatorMessages_ru.properties
Modified:
branches/community/3.3.X/samples/beanValidatorSample/pom.xml
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/WEB-INF/faces-config.xml
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr299src/org/richfaces/example/Bean.java
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/beanValidation.xhtml
branches/community/3.3.X/ui/beanValidator/pom.xml
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/HibernateValidator.java
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/NullValidator.java
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/BeanValidatorTest.java
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/HibernateValidatorTest.java
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/MockValidationProvider.java
Log:
https://jira.jboss.org/jira/browse/RF-4428
Handle localization for HibernateValidator, test localization issues.
Modified: branches/community/3.3.X/samples/beanValidatorSample/pom.xml
===================================================================
--- branches/community/3.3.X/samples/beanValidatorSample/pom.xml 2009-07-24 23:45:04 UTC
(rev 15017)
+++ branches/community/3.3.X/samples/beanValidatorSample/pom.xml 2009-07-28 00:21:54 UTC
(rev 15018)
@@ -126,12 +126,12 @@
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
- <version>1.0.CR2</version>
+ <version>1.0.CR3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
- <version>4.0.0.Beta1</version>
+ <version>4.0.0.Beta2</version>
</dependency>
</dependencies>
</profile>
@@ -142,7 +142,7 @@
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
- <version>1.0.CR2</version>
+ <version>1.0.CR3</version>
</dependency>
</dependencies>
</profile>
Added:
branches/community/3.3.X/samples/beanValidatorSample/src/main/java/org/richfaces/LocaleBean.java
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/java/org/richfaces/LocaleBean.java
(rev 0)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/java/org/richfaces/LocaleBean.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -0,0 +1,53 @@
+/**
+ *
+ */
+package org.richfaces;
+
+import java.util.Locale;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class LocaleBean {
+
+ private Locale current = Locale.ENGLISH;
+
+ private String language = "en";
+
+ /**
+ * @return the current
+ */
+ public Locale getCurrent() {
+ return current;
+ }
+
+ /**
+ * @param current the current to set
+ */
+ public void setCurrent(Locale current) {
+ this.current = current;
+ }
+
+ /**
+ * @return the language
+ */
+ public String getLanguage() {
+ return language;
+ }
+
+ /**
+ * @param language the language to set
+ */
+ public void setLanguage(String language) {
+ this.language = language;
+ }
+
+
+ public String select(){
+ setCurrent(new Locale(getLanguage()));
+ return null;
+ }
+
+
+}
Property changes on:
branches/community/3.3.X/samples/beanValidatorSample/src/main/java/org/richfaces/LocaleBean.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/WEB-INF/faces-config.xml
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/WEB-INF/faces-config.xml 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/WEB-INF/faces-config.xml 2009-07-28
00:21:54 UTC (rev 15018)
@@ -1,7 +1,16 @@
-<?xml version="1.0"?>
-<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces
Config 1.1//EN"
-
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
-<faces-config>
+<?xml version="1.0" encoding="UTF-8"?>
+<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
+ version="1.2">
+ <application>
+ <locale-config>
+ <default-locale>en</default-locale>
+ <supported-locale>en</supported-locale>
+ <supported-locale>de</supported-locale>
+ <supported-locale>fr</supported-locale>
+ </locale-config>
+ </application>
<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>org.richfaces.example.Bean</managed-bean-class>
@@ -28,6 +37,11 @@
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
+ <managed-bean-name>locale</managed-bean-name>
+ <managed-bean-class>org.richfaces.LocaleBean</managed-bean-class>
+ <managed-bean-scope>session</managed-bean-scope>
+ </managed-bean>
+ <managed-bean>
<managed-bean-name>contentBean</managed-bean-name>
<managed-bean-class>org.richfaces.ContentBean</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr299src/org/richfaces/example/Bean.java
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr299src/org/richfaces/example/Bean.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr299src/org/richfaces/example/Bean.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -58,7 +58,7 @@
*/
@NotEmpty
// @Email
- @Pattern(regexp="^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9](a)[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$")
+// @Pattern(regexp="^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9](a)[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$")
public String getEmail() {
return email;
}
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml 2009-07-28
00:21:54 UTC (rev 15018)
@@ -1,10 +1,11 @@
-<a:page
xmlns:ui="http://java.sun.com/jsf/facelets"
-
xmlns:h="http://java.sun.com/jsf/html"
-
xmlns:f="http://java.sun.com/jsf/core"
-
xmlns:s="http://jboss.com/products/seam/taglib"
-
xmlns:a="http://richfaces.org/a4j"
-
xmlns:rich="http://richfaces.org/rich"
- pageTitle="#{title}" format="xhtml">
+<f:view
xmlns:ui="http://java.sun.com/jsf/facelets"
+
xmlns:h="http://java.sun.com/jsf/html"
+
xmlns:f="http://java.sun.com/jsf/core"
+
xmlns:s="http://jboss.com/products/seam/taglib"
+
xmlns:a="http://richfaces.org/a4j"
+
xmlns:rich="http://richfaces.org/rich"
+ locale="#{locale.current}">
+<a:page pageTitle="#{title}" format="xhtml">
<a:loadStyle src="/css/grids.css" />
<a:loadStyle src="resource://css/panel.xcss" />
<a:loadStyle src="resource://css/table.xcss" />
@@ -16,6 +17,14 @@
<div class="yui-b">
<!-- YOUR NAVIGATION GOES HERE -->
<ui:include src="/include/pagesList.xhtml" />
+ <h:form>
+ <h:selectOneMenu value="#{locale.language}">
+ <f:selectItem itemLabel="English" itemValue="en"/>
+ <f:selectItem itemLabel="Deutsch" itemValue="de"/>
+ <f:selectItem itemLabel="Francais" itemValue="fr"/>
+</h:selectOneMenu>
+<h:commandButton action="#{locale.select}"
value="ChangeLanguage"/>
+</h:form>
</div>
<div class="yui-main">
<div class="yui-b">
@@ -46,4 +55,5 @@
</div>
</div>
</div>
-</a:page>
\ No newline at end of file
+</a:page>
+</f:view>
\ No newline at end of file
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/beanValidation.xhtml
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/beanValidation.xhtml 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/beanValidation.xhtml 2009-07-28
00:21:54 UTC (rev 15018)
@@ -20,7 +20,7 @@
<h:panelGrid columns="3">
<h:outputLabel for="email" value="Email Address:" />
<h:inputText id="email" value="#{bean.email}"
label="Email">
- <rich:beanValidator summary="Invalid Email address"
profiles="javax.validation.groups.Default"/>
+ <rich:beanValidator summary="Invalid Email address" />
</h:inputText>
<rich:message for="email"/>
<h:outputLabel for="card" value="Credit card number:" />
Modified: branches/community/3.3.X/ui/beanValidator/pom.xml
===================================================================
--- branches/community/3.3.X/ui/beanValidator/pom.xml 2009-07-24 23:45:04 UTC (rev 15017)
+++ branches/community/3.3.X/ui/beanValidator/pom.xml 2009-07-28 00:21:54 UTC (rev 15018)
@@ -70,7 +70,7 @@
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
- <version>1.0.CR2</version>
+ <version>1.0.CR3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -42,10 +42,10 @@
* java.lang.String, java.lang.Object, java.util.Locale)
*/
@Override
- protected String[] validate(Object base, String property, Object value,
- Locale locale, Set<String> profiles) {
+ protected String[] validate(FacesContext facesContext, Object base, String property,
+ Object value, Set<String> profiles) {
Class beanType = base.getClass();
- Set<ConstraintViolation<Object>> constrains = getValidator(locale)
+ Set<ConstraintViolation<Object>> constrains = getValidator(facesContext)
.validateValue(beanType, property, value, getGroups(profiles));
return extractMessages(constrains);
}
@@ -62,7 +62,7 @@
Set<String> profiles) {
Class<?>[] groups = getGroups(profiles);
Set<ConstraintViolation<Object>> violations = getValidator(
- calculateLocale(context)).validate(value, groups);
+ context).validate(value, groups);
String[] messages = extractMessages(violations);
return messages;
}
@@ -106,10 +106,10 @@
return messages;
}
- protected Validator getValidator(Locale locale) {
+ protected Validator getValidator(FacesContext facesContext) {
ValidatorContext validatorContext = validatorFactory.usingContext();
MessageInterpolator jsfMessageInterpolator = new JsfMessageInterpolator(
- locale, validatorFactory.getMessageInterpolator());
+ calculateLocale(facesContext), validatorFactory.getMessageInterpolator());
validatorContext.messageInterpolator(jsfMessageInterpolator);
Validator beanValidator = validatorContext.getValidator();
return beanValidator;
@@ -138,7 +138,12 @@
public String interpolate(String messageTemplate, Context context,
Locale locale) {
- return delegate.interpolate(messageTemplate, context, locale);
+ if (null != locale) {
+ return delegate.interpolate(messageTemplate, context,
+ this.locale);
+ } else {
+ return delegate.interpolate(messageTemplate, context, locale);
+ }
}
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/HibernateValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/HibernateValidator.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/HibernateValidator.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -22,10 +22,13 @@
import java.beans.FeatureDescriptor;
import java.util.Collection;
+import java.util.Collections;
import java.util.EmptyStackException;
+import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
+import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Stack;
@@ -37,6 +40,8 @@
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
+import org.hibernate.validator.MessageInterpolator;
+import org.hibernate.validator.Validator;
/**
* Perform validation by Hibernate Validator annotations
@@ -46,6 +51,9 @@
*/
public class HibernateValidator extends ObjectValidator {
+ static final String DEFAULT_VALIDATOR_MESSAGES =
"org.hibernate.validator.resources.DefaultValidatorMessages";
+ static final String VALIDATOR_MESSAGES = "ValidatorMessages";
+
private Map<ValidatorKey, ClassValidator<? extends Object>> classValidators
= new ConcurrentHashMap<ValidatorKey, ClassValidator<? extends Object>>();
HibernateValidator() {
@@ -64,7 +72,7 @@
String validationMessages[] = null;
if (null != value) {
ClassValidator<Object> validator = (ClassValidator<Object>) getValidator(
- value.getClass(), calculateLocale(context));
+ context, value.getClass());
if (validator.hasValidationRules()) {
InvalidValue[] invalidValues = validator
.getInvalidValues(value);
@@ -83,16 +91,16 @@
/**
* Validate bean property in the base class aganist new value.
- *
* @param beanClass
* @param property
* @param value
+ *
* @return
*/
- protected InvalidValue[] validateClass(Class<? extends Object> beanClass,
- String property, Object value, Locale locale) {
+ protected InvalidValue[] validateClass(FacesContext facesContext,
+ Class<? extends Object> beanClass, String property, Object value) {
ClassValidator<? extends Object> classValidator =
- getValidator(beanClass, locale);
+ getValidator(facesContext, beanClass);
InvalidValue[] invalidValues = classValidator
.getPotentialInvalidValues(property, value);
@@ -101,18 +109,18 @@
/**
* Get ( or create ) {@link ClassValidator} for a given bean class.
+ * @param beanClass
*
- * @param beanClass
* @return
*/
@SuppressWarnings("unchecked")
protected ClassValidator<? extends Object> getValidator(
- Class<? extends Object> beanClass, Locale locale) {
+ FacesContext facesContext, Class<? extends Object> beanClass) {
// TODO - localization support.
- ValidatorKey key = new ValidatorKey(beanClass, locale);
+ ValidatorKey key = new ValidatorKey(beanClass, calculateLocale(facesContext));
ClassValidator result = classValidators.get(key);
if (null == result) {
- result = createValidator(beanClass, locale);
+ result = createValidator(facesContext, beanClass);
classValidators.put(key, result);
}
return result;
@@ -126,17 +134,32 @@
*/
@SuppressWarnings("unchecked")
protected ClassValidator<? extends Object> createValidator(
- Class<? extends Object> beanClass, Locale locale) {
- ResourceBundle bundle = getCurrentResourceBundle(locale);
+ FacesContext facesContext, Class<? extends Object> beanClass) {
+ ResourceBundle bundle = createHibernateMessages(facesContext);
return bundle == null ? new ClassValidator(beanClass)
: new ClassValidator(beanClass, bundle);
}
+ /**
+ * @param facesContext
+ * @return
+ */
+ protected ResourceBundle createHibernateMessages(FacesContext facesContext) {
+ ResourceBundle bundle = getResourceBundle(facesContext, VALIDATOR_MESSAGES);
+ ResourceBundle defaultMessagesBundle = getResourceBundle(facesContext,
DEFAULT_VALIDATOR_MESSAGES);
+ if(null != bundle && defaultMessagesBundle != null){
+ bundle = new ResourceBundleChain(bundle, defaultMessagesBundle);
+ } else if(null != defaultMessagesBundle){
+ bundle = defaultMessagesBundle;
+ }
+ return bundle;
+ }
+
@Override
- protected String[] validate(Object base, String property, Object value,
- Locale locale, Set<String> profiles) {
- InvalidValue[] invalidValues = validateBean(base, property, value,
- locale);
+ protected String[] validate(FacesContext facesContext, Object base, String property,
+ Object value, Set<String> profiles) {
+ InvalidValue[] invalidValues = validateBean(facesContext, base, property,
+ value);
if (null == invalidValues) {
return null;
} else {
@@ -151,19 +174,68 @@
/**
* Validate bean property of the base object aganist new value
- *
* @param base
* @param property
* @param value
+ *
* @return
*/
- protected InvalidValue[] validateBean(Object base, String property, Object value,
- Locale locale) {
+ protected InvalidValue[] validateBean(FacesContext facesContext, Object base, String
property,
+ Object value) {
Class<? extends Object> beanClass = base.getClass();
- InvalidValue[] invalidValues = validateClass(beanClass, property, value, locale);
+ InvalidValue[] invalidValues = validateClass(facesContext, beanClass, property,
value);
return invalidValues;
}
+ private static class JsfMessageInterpolator implements MessageInterpolator {
+
+ private Locale locale;
+ private MessageInterpolator delegate;
+
+ public JsfMessageInterpolator(Locale locale,
+ MessageInterpolator delegate) {
+ this.locale = locale;
+ this.delegate = delegate;
+ }
+
+
+ public String interpolate(String message, Validator validator,
+ MessageInterpolator defaultInterpolator) {
+ return delegate.interpolate(message, validator, defaultInterpolator);
+ }
+
+ }
+ static class ResourceBundleChain extends ResourceBundle {
+
+ private final ResourceBundle delegate;
+
+ public ResourceBundleChain(ResourceBundle delegate, ResourceBundle parent) {
+ this.delegate = delegate;
+ setParent(parent);
+ }
+
+ @Override
+ public Enumeration<String> getKeys() {
+ // TODO Auto-generated method stub
+ return
null!=delegate?delegate.getKeys():Collections.<String>enumeration(Collections.<String>emptyList());
+ }
+
+ @Override
+ protected Object handleGetObject(String key) {
+ try {
+ return null != delegate ? delegate.getObject(key) : null;
+
+ } catch (MissingResourceException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public Locale getLocale() {
+ return null!=delegate?delegate.getLocale():null;
+ }
+ }
+
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/NullValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/NullValidator.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/NullValidator.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -3,7 +3,6 @@
*/
package org.richfaces.validator;
-import java.util.Locale;
import java.util.Set;
import javax.faces.context.FacesContext;
@@ -18,8 +17,8 @@
* @see org.richfaces.validator.ObjectValidator#validate(java.lang.Object,
java.lang.String, java.lang.Object, java.util.Locale)
*/
@Override
- protected String[] validate(Object base, String property, Object value,
- Locale locale, Set<String> profiles) {
+ protected String[] validate(FacesContext facesContext, Object base, String property,
+ Object value, Set<String> profiles) {
// do nothing.
return null;
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -6,6 +6,7 @@
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
+import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Stack;
@@ -15,6 +16,7 @@
import javax.el.ELResolver;
import javax.el.ValueExpression;
import javax.faces.FacesException;
+import javax.faces.application.Application;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
@@ -113,7 +115,7 @@
if (null != target) {
ELContext elContext = context.getELContext();
ValidationResolver validationResolver = createValidationResolver(
- elContext.getELResolver(), calculateLocale(context),profiles);
+ context, elContext.getELResolver(),profiles);
ELContextWrapper wrappedElContext = new ELContextWrapper(elContext,
validationResolver);
// TODO - handle ELExceptions ?
@@ -142,7 +144,7 @@
/**
* Validate bean property for a new value.
- *
+ * @param facesContext TODO
* @param base
* - bean
* @param property
@@ -150,39 +152,46 @@
* @param value
* new value.
* @param profiles TODO
+ *
* @return null for a valid value, array of the validation messages
* othervise.
*/
- protected abstract String[] validate(Object base, String property,
- Object value, Locale locale, Set<String> profiles);
+ protected abstract String[] validate(FacesContext facesContext, Object base,
+ String property, Object value, Set<String> profiles);
- protected ResourceBundle getCurrentResourceBundle(Locale locale) {
- if (null == FacesContext.getCurrentInstance()
- || null == FacesContext.getCurrentInstance().getApplication()) {
- throw new FacesException(FACES_CONTEXT_IS_NULL);
+ protected ResourceBundle getResourceBundle(FacesContext facesContext, String name) {
+ // TODO - cache resource bundles.
+ ResourceBundle bundle = null;
+ if (null != facesContext) {
+ Application application = facesContext.getApplication();
+ try {
+ bundle = application.getResourceBundle(facesContext,
+ name);
+
+ } catch (Exception e) {
+ // Let one more attempt to load resource
+ }
}
- String appBundle = FacesContext.getCurrentInstance().getApplication()
- .getMessageBundle();
- if (null == appBundle || null == locale) {
- return null;
- }
+ if (null == bundle) {
+ ClassLoader classLoader = Thread.currentThread()
+ .getContextClassLoader();
+ if (null == classLoader) {
+ classLoader = this.getClass().getClassLoader();
+ }
+ try {
+ bundle = ResourceBundle.getBundle(name, calculateLocale(facesContext),
+ classLoader);
- ResourceBundle bundle;
-
- ClassLoader classLoader = Thread.currentThread()
- .getContextClassLoader();
- if (classLoader != null) {
- bundle = ResourceBundle.getBundle(appBundle, locale, classLoader);
- } else {
- bundle = ResourceBundle.getBundle(appBundle, locale);
+ } catch (MissingResourceException e) {
+ // Do nothing, use default bundle.
+ }
}
-
return bundle;
}
- protected ValidationResolver createValidationResolver(ELResolver parent,
- Locale locale, Set<String> profiles) {
- return new ValidationResolver(parent, locale, profiles);
+ protected ValidationResolver createValidationResolver(FacesContext context,
+ ELResolver parent, Set<String> profiles) {
+ return new ValidationResolver(parent, context, profiles);
}
/**
@@ -308,20 +317,21 @@
private String[] validationMessages = null;
- private Locale locale = null;
-
private Stack<BasePropertyPair> valuesStack;
private Set<String> profiles;
+ private FacesContext facesContext;
+
/**
* @param parent
+ * @param context
*/
- public ValidationResolver(ELResolver parent, Locale locale,Set<String> profiles)
{
+ public ValidationResolver(ELResolver parent, FacesContext context, Set<String>
profiles) {
this.parent = parent;
- this.locale = locale;
this.valuesStack = new Stack<BasePropertyPair>();
this.profiles = profiles;
+ this.facesContext = context;
}
public boolean isValid() {
@@ -412,8 +422,8 @@
// apache el looses locale information during value
// resolution,
// so we use our own
- validationMessages = validate(base, property.toString(),
- value, locale, profiles);
+ validationMessages = validate(facesContext, base,
+ property.toString(), value, profiles);
valid = null == validationMessages
|| 0 == validationMessages.length;
Modified:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/BeanValidatorTest.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/BeanValidatorTest.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/BeanValidatorTest.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -21,7 +21,6 @@
package org.richfaces.validator;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
@@ -50,7 +49,7 @@
public void testValidate() throws Exception {
BeanValidator validator = new BeanValidator();
- String[] validate = validator.validate(new Bean(), "property", null,
Locale.ENGLISH, null);
+ String[] validate = validator.validate(facesContext, new Bean(),
"property", null, null);
assertNotNull(validate);
assertEquals(1, validate.length);
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/HibernateValidatorTest.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/HibernateValidatorTest.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/HibernateValidatorTest.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -51,26 +51,26 @@
public void testGetValidator() throws Exception {
HibernateValidator beanValidator = new HibernateValidator();
- ClassValidator<? extends Object> validator =
beanValidator.getValidator(ValidableBean.class,Locale.ENGLISH);
+ ClassValidator<? extends Object> validator =
beanValidator.getValidator(facesContext,ValidableBean.class);
assertNotNull(validator);
assertTrue(validator.hasValidationRules());
- validator = beanValidator.getValidator(String.class,Locale.getDefault());
+ validator = beanValidator.getValidator(facesContext,String.class);
assertNotNull(validator);
assertFalse(validator.hasValidationRules());
}
public void testValidateClass() throws Exception {
HibernateValidator beanValidator = new HibernateValidator();
- InvalidValue[] invalidValues = beanValidator.validateClass(ValidableBean.class,
"integerProperty", new Integer(3),Locale.getDefault());
+ InvalidValue[] invalidValues = beanValidator.validateClass(facesContext,
ValidableBean.class, "integerProperty",new Integer(3));
assertNotNull(invalidValues);
assertEquals(0, invalidValues.length);
- invalidValues = beanValidator.validateClass(ValidableBean.class,
"integerProperty", new Integer(-1),Locale.getDefault());
+ invalidValues = beanValidator.validateClass(facesContext, ValidableBean.class,
"integerProperty",new Integer(-1));
assertNotNull(invalidValues);
assertEquals(1, invalidValues.length);
- invalidValues = beanValidator.validateClass(UnValidableBean.class,
"integerProperty", new Integer(-1),Locale.getDefault());
+ invalidValues = beanValidator.validateClass(facesContext, UnValidableBean.class,
"integerProperty",new Integer(-1));
assertNotNull(invalidValues);
assertEquals(0, invalidValues.length);
- invalidValues = beanValidator.validateClass(ValidableBean.class,
"nonExistentProperty", new Integer(-1),Locale.getDefault());
+ invalidValues = beanValidator.validateClass(facesContext, ValidableBean.class,
"nonExistentProperty",new Integer(-1));
assertNotNull(invalidValues);
assertEquals(0, invalidValues.length);
@@ -78,14 +78,14 @@
public void testValidateBean() throws Exception {
HibernateValidator beanValidator = new HibernateValidator();
- InvalidValue[] invalidValues = beanValidator.validateBean(new ValidableBean(),
"integerProperty", new Integer(-1),Locale.getDefault());
+ InvalidValue[] invalidValues = beanValidator.validateBean(facesContext, new
ValidableBean(), "integerProperty",new Integer(-1));
assertNotNull(invalidValues);
assertEquals(1, invalidValues.length);
}
public void testValidateArray() throws Exception {
HibernateValidator beanValidator = new HibernateValidator();
- InvalidValue[] invalidValues = beanValidator.validateBean(new ValidableBean(),
"array", "",Locale.getDefault());
+ InvalidValue[] invalidValues = beanValidator.validateBean(facesContext, new
ValidableBean(), "array","");
assertNotNull(invalidValues);
assertEquals(2, invalidValues.length);
System.out.println(invalidValues[0].getMessage());
@@ -94,14 +94,14 @@
public void testValidateList() throws Exception {
HibernateValidator beanValidator = new HibernateValidator();
- InvalidValue[] invalidValues = beanValidator.validateBean(new ValidableBean(),
"list", "",Locale.getDefault());
+ InvalidValue[] invalidValues = beanValidator.validateBean(facesContext, new
ValidableBean(), "list","");
assertNotNull(invalidValues);
assertEquals(1, invalidValues.length);
System.out.println(invalidValues[0].getMessage());
}
public void testValidateMap() throws Exception {
HibernateValidator beanValidator = new HibernateValidator();
- InvalidValue[] invalidValues = beanValidator.validateBean(new ValidableBean(),
"map", "",Locale.getDefault());
+ InvalidValue[] invalidValues = beanValidator.validateBean(facesContext, new
ValidableBean(), "map","");
assertNotNull(invalidValues);
assertEquals(1, invalidValues.length);
System.out.println(invalidValues[0].getMessage());
@@ -110,7 +110,7 @@
public void testValidationResolver() throws Exception {
ValidableBean bean = new ValidableBean();
HibernateValidator beanValidator = new HibernateValidator();
- ValidationResolver validationResolver =
beanValidator.createValidationResolver(facesContext.getELContext().getELResolver(),
Locale.US,null);
+ ValidationResolver validationResolver =
beanValidator.createValidationResolver(facesContext,
facesContext.getELContext().getELResolver(),null);
Object list = validationResolver.getValue(elContext, bean, "list");
assertNotNull(list);
assertTrue(list instanceof List);
@@ -121,7 +121,7 @@
public void testValidationResolverMap() throws Exception {
ValidableBean bean = new ValidableBean();
HibernateValidator beanValidator = new HibernateValidator();
- ValidationResolver validationResolver =
beanValidator.createValidationResolver(facesContext.getELContext().getELResolver(),
Locale.US,null);
+ ValidationResolver validationResolver =
beanValidator.createValidationResolver(facesContext,
facesContext.getELContext().getELResolver(),null);
Object list = validationResolver.getValue(elContext, bean, "map");
assertNotNull(list);
assertTrue(list instanceof Map);
@@ -129,4 +129,36 @@
assertFalse(validationResolver.isValid());
assertEquals(1, validationResolver.getValidationMessages().length);
}
+
+ public void testHibernateMessages() throws Exception {
+ HibernateValidator beanValidator = new HibernateValidator();
+ facesContext.getViewRoot().setLocale(Locale.FRANCE);
+ ResourceBundle hibernateMessages =
beanValidator.createHibernateMessages(facesContext);
+ assertEquals(Locale.FRANCE.getLanguage(),
hibernateMessages.getLocale().getLanguage());
+ String message = hibernateMessages.getString("validator.pattern");
+ assertEquals("doit suivre \"{regex}\"", message);
+
+ }
+
+ public void testLocalization() throws Exception {
+ HibernateValidator beanValidator = new HibernateValidator();
+ InvalidValue[] invalidValues = beanValidator.validateClass(facesContext,
ValidableBean.class, "integerProperty",new Integer(-1));
+ assertNotNull(invalidValues);
+ assertEquals(1, invalidValues.length);
+ System.out.println(invalidValues[0]);
+ assertTrue(invalidValues[0].getMessage().contains("must be greater than or
equal"));
+ facesContext.getViewRoot().setLocale(Locale.FRANCE);
+ invalidValues = beanValidator.validateClass(facesContext, ValidableBean.class,
"integerProperty",new Integer(-1));
+ assertNotNull(invalidValues);
+ assertEquals(1, invalidValues.length);
+ System.out.println(invalidValues[0]);
+ assertTrue(invalidValues[0].getMessage().contains("rieur ou"));
+ facesContext.getViewRoot().setLocale(new Locale("ru","RU"));
+ invalidValues = beanValidator.validateClass(facesContext, ValidableBean.class,
"integerProperty",new Integer(-1));
+ assertNotNull(invalidValues);
+ assertEquals(1, invalidValues.length);
+ System.out.println(invalidValues[0]);
+ assertTrue(invalidValues[0].getMessage().contains("i18n Test"));
+ }
+
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/MockValidationProvider.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/MockValidationProvider.java 2009-07-24
23:45:04 UTC (rev 15017)
+++
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/MockValidationProvider.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -7,16 +7,17 @@
import java.util.Collections;
import java.util.Set;
-import javax.validation.BeanDescriptor;
import javax.validation.Configuration;
-import javax.validation.ConstraintDescriptor;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.ConstraintViolation;
import javax.validation.MessageInterpolator;
+import javax.validation.Path;
import javax.validation.TraversableResolver;
import javax.validation.Validator;
import javax.validation.ValidatorContext;
import javax.validation.ValidatorFactory;
+import javax.validation.metadata.BeanDescriptor;
+import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;
import javax.validation.spi.ValidationProvider;
@@ -30,10 +31,6 @@
public <T> ConstraintViolation<T> getDefaultConstraint(){
return new ConstraintViolation<T>(){
- public ConstraintDescriptor<?> getConstraintDescriptor() {
- // TODO Auto-generated method stub
- return null;
- }
public Object getInvalidValue() {
// TODO Auto-generated method stub
@@ -55,7 +52,7 @@
return null;
}
- public String getPropertyPath() {
+ public Path getPropertyPath() {
// TODO Auto-generated method stub
return null;
}
@@ -69,6 +66,11 @@
// TODO Auto-generated method stub
return null;
}
+
+ public ConstraintDescriptor<?> getConstraintDescriptor() {
+ // TODO Auto-generated method stub
+ return null;
+ }
};
}
@@ -125,6 +127,12 @@
ConstraintViolation<T> constrain = getDefaultConstraint();
return Collections.singleton(constrain);
}
+
+ public <T> T unwrap(Class<T> type) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
};
}
@@ -143,6 +151,11 @@
};
}
+
+ public <T> T unwrap(Class<T> type) {
+ // TODO Auto-generated method stub
+ return null;
+ }
};
}
@@ -220,4 +233,9 @@
return false;
}
+ public Configuration createSpecializedConfiguration(BootstrapState state) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
}
Added:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/ObjectValidatorTest.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/ObjectValidatorTest.java
(rev 0)
+++
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/ObjectValidatorTest.java 2009-07-28
00:21:54 UTC (rev 15018)
@@ -0,0 +1,79 @@
+package org.richfaces.validator;
+
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.Set;
+
+import javax.faces.context.FacesContext;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+
+public class ObjectValidatorTest extends AbstractAjax4JsfTestCase {
+
+ public ObjectValidatorTest(String name) {
+ super(name);
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testCreateInstance() {
+ ObjectValidator objectValidator = ObjectValidator.createInstance();
+ assertEquals(BeanValidator.class, objectValidator.getClass());
+ }
+
+ public void testGetInstance() {
+ ObjectValidator objectValidator = ObjectValidator.getInstance(facesContext);
+ assertEquals(BeanValidator.class, objectValidator.getClass());
+ ObjectValidator objectValidator2 = ObjectValidator.getInstance(facesContext);
+ assertSame(objectValidator, objectValidator2);
+ }
+
+ public void testCalculateLocale() {
+ ObjectValidatorStub validator = new ObjectValidatorStub();
+ facesContext.getViewRoot().setLocale(Locale.FRANCE);
+ assertEquals(Locale.FRANCE, validator.calculateLocale(facesContext));
+ facesContext.getViewRoot().setLocale(Locale.US);
+ assertEquals(Locale.US, validator.calculateLocale(facesContext));
+ }
+
+ public void testGetResourceBundle() {
+ ObjectValidatorStub validator = new ObjectValidatorStub();
+ facesContext.getViewRoot().setLocale(Locale.US);
+ ResourceBundle bundle = validator.getResourceBundle(facesContext,
HibernateValidator.DEFAULT_VALIDATOR_MESSAGES);
+ assertNotNull(bundle);
+ assertEquals(Locale.US.getLanguage(), bundle.getLocale().getLanguage());
+ }
+
+ public void testGetResourceBundleNonExists() {
+ ObjectValidatorStub validator = new ObjectValidatorStub();
+ facesContext.getViewRoot().setLocale(Locale.US);
+ ResourceBundle bundle = validator.getResourceBundle(facesContext, "fooBar");
+ assertNull(bundle);
+ }
+
+ private static class ObjectValidatorStub extends ObjectValidator {
+
+ @Override
+ protected String[] validate(FacesContext facesContext, Object base, String property,
+ Object value, Set<String> profiles) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String[] validateGraph(FacesContext context, Object value,
+ Set<String> profiles) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ }
+}
Property changes on:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/ObjectValidatorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/community/3.3.X/ui/beanValidator/src/test/resources/ValidatorMessages_ru.properties
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/test/resources/ValidatorMessages_ru.properties
(rev 0)
+++
branches/community/3.3.X/ui/beanValidator/src/test/resources/ValidatorMessages_ru.properties 2009-07-28
00:21:54 UTC (rev 15018)
@@ -0,0 +1 @@
+validator.min=i18n Test
Property changes on:
branches/community/3.3.X/ui/beanValidator/src/test/resources/ValidatorMessages_ru.properties
___________________________________________________________________
Name: svn:mime-type
+ text/plain