Author: alexsmirnov
Date: 2010-10-27 20:20:19 -0400 (Wed, 27 Oct 2010)
New Revision: 19720
Added:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/FacesValidatorServiceTest.java
Modified:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/application/ValidatorModule.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
Log:
RESOLVED - issue RF-9519: FacesValidatorService tests and implementation
https://jira.jboss.org/browse/RF-9519
Modified:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/application/ValidatorModule.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/application/ValidatorModule.java 2010-10-27
23:57:24 UTC (rev 19719)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/application/ValidatorModule.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -28,6 +28,8 @@
import org.richfaces.validator.BeanValidator;
import org.richfaces.validator.ConverterServiceImpl;
import org.richfaces.validator.FacesConverterService;
+import org.richfaces.validator.FacesValidatorService;
+import org.richfaces.validator.FacesValidatorServiceImpl;
import org.richfaces.validator.NullValidator;
import org.richfaces.validator.ObjectValidator;
@@ -47,8 +49,9 @@
* @see
org.richfaces.application.Module#configure(org.richfaces.application.ServicesFactory)
*/
public void configure(ServicesFactory factory) {
- factory.setInstance(ObjectValidator.class, createInstance());
+ factory.setInstance(ObjectValidator.class, createObjectValidator());
factory.setInstance(FacesConverterService.class, new ConverterServiceImpl());
+ factory.setInstance(FacesValidatorService.class, new
FacesValidatorServiceImpl());
}
/**
@@ -56,7 +59,7 @@
*
* @return
*/
- static ObjectValidator createInstance() {
+ static ObjectValidator createObjectValidator() {
ObjectValidator validator = null;
try {
validator = new BeanValidator();
Added:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -0,0 +1,42 @@
+package org.richfaces.validator;
+
+import java.util.Map;
+
+import javax.faces.application.FacesMessage;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+
+public class BaseFacesObjectDescriptor<T> {
+
+ private final Class<? extends T> converterClass;
+ private Map<String, Object> parameters = Maps.newHashMap();
+ private final FacesMessage message;
+
+
+ public BaseFacesObjectDescriptor(Class<? extends T> converterClass,
FacesMessage message) {
+ this.converterClass = converterClass;
+ this.message = message;
+ }
+
+ public Class<?> getImplementationClass() {
+ return converterClass;
+ }
+
+ public Map<String, ? extends Object> getAdditionalParameters() {
+ return parameters;
+ }
+
+ public FacesMessage getMessage() {
+ return message;
+ }
+
+ protected void addParameter(String name, Object value) {
+ parameters.put(name, value);
+ }
+
+ protected void makeImmutable() {
+ parameters = ImmutableMap.copyOf(parameters);
+ }
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java 2010-10-27
23:57:24 UTC (rev 19719)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -3,59 +3,20 @@
*/
package org.richfaces.validator;
-import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.convert.Converter;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
/**
* @author asmirnov
*
*/
-public class ConverterDescriptorImpl implements ConverterDescriptor {
+public class ConverterDescriptorImpl extends BaseFacesObjectDescriptor<Converter>
implements ConverterDescriptor {
- private final Class<? extends Converter> converterClass;
-
- private Map<String, Object> parameters = Maps.newHashMap();
-
- private final FacesMessage message;
-
ConverterDescriptorImpl(Class<? extends Converter> converterClass,FacesMessage
message) {
- this.converterClass = converterClass;
- this.message = message;
-
+ super(converterClass,message);
}
- /* (non-Javadoc)
- * @see org.richfaces.validator.FacesObjectDescriptor#getImplementationClass()
- */
- public Class<?> getImplementationClass() {
- return converterClass;
- }
-
- /* (non-Javadoc)
- * @see org.richfaces.validator.FacesObjectDescriptor#getAdditionalParameters()
- */
- public Map<String, ? extends Object> getAdditionalParameters() {
- return parameters;
- }
-
- /* (non-Javadoc)
- * @see org.richfaces.validator.FacesObjectDescriptor#getMessage()
- */
- public FacesMessage getMessage() {
- return message;
- }
-
- void addParameter(String name, Object value){
- parameters.put(name, value);
- }
- void makeImmutable(){
- parameters = ImmutableMap.copyOf(parameters);
- }
-
}
Modified:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java 2010-10-27
23:57:24 UTC (rev 19719)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -3,11 +3,6 @@
*/
package org.richfaces.validator;
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
-import java.lang.reflect.InvocationTargetException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIInput;
@@ -27,18 +22,15 @@
import javax.faces.convert.NumberConverter;
import javax.faces.convert.ShortConverter;
-import com.google.common.collect.ImmutableSet;
/**
* @author asmirnov
*
*/
-public class ConverterServiceImpl implements FacesConverterService {
+public class ConverterServiceImpl extends FacesServiceBase<Converter> implements
FacesConverterService {
private static final String DEFAULT_CONVERTER_MESSAGE_ID =
UIInput.CONVERSION_MESSAGE_ID;
- private static final ImmutableSet<String> HIDDEN_PROPERTIES =
ImmutableSet.of("class", "transient");
-
/*
* (non-Javadoc)
*
@@ -47,53 +39,15 @@
*/
public ConverterDescriptor getConverterDescription(FacesContext context, Converter
converter) {
// determine converter message.
- FacesMessage message = getConverterMessage(context, converter);
+ FacesMessage message = getMessage(context, converter);
ConverterDescriptorImpl descriptor = new
ConverterDescriptorImpl(converter.getClass(), message);
- fillConverterParameters(descriptor, converter);
+ fillParameters(descriptor, converter);
descriptor.makeImmutable();
return descriptor;
}
- private void fillConverterParameters(ConverterDescriptorImpl descriptor, Converter
converter) {
- // get bean attributes for converter, put them into parameters.
- try {
- BeanInfo beanInfo = Introspector.getBeanInfo(converter.getClass());
- PropertyDescriptor[] propertyDescriptors =
beanInfo.getPropertyDescriptors();
- for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
- String name = propertyDescriptor.getName();
- if (!HIDDEN_PROPERTIES.contains(name)) {
- try {
- Object value =
propertyDescriptor.getReadMethod().invoke(converter);
- if (null != value) {
- descriptor.addParameter(name, value);
- }
- } catch (IllegalArgumentException e) {
- // Ignore
- } catch (IllegalAccessException e) {
- // Ignore
- } catch (InvocationTargetException e) {
- // Ignore
- }
- }
- }
- } catch (IntrospectionException e) {
- // Ignore.
- }
- }
-
- /**
- * Creates message for converter, using current locale.
- *
- * @param context
- * @param converter
- * @return
- */
- private FacesMessage getConverterMessage(FacesContext context, Converter converter)
{
- String messageId = getMessageId(converter);
- return MessageFactory.createMessage(context, messageId);
- }
-
- private String getMessageId(Converter converter) {
+ @Override
+ protected String getMessageId(Converter converter) {
String messageId;
if (converter instanceof BigDecimalConverter) {
messageId = BigDecimalConverter.DECIMAL_ID;
Added:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -0,0 +1,60 @@
+package org.richfaces.validator;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
+
+import com.google.common.collect.ImmutableSet;
+
+public abstract class FacesServiceBase<T> {
+
+ private static final ImmutableSet<String> HIDDEN_PROPERTIES =
ImmutableSet.of("class", "transient");
+
+ protected abstract String getMessageId(T component);
+
+
+ protected void fillParameters(BaseFacesObjectDescriptor<T> descriptor, T
component) {
+ // get bean attributes for converter, put them into parameters.
+ try {
+ BeanInfo beanInfo = Introspector.getBeanInfo(component.getClass());
+ PropertyDescriptor[] propertyDescriptors =
beanInfo.getPropertyDescriptors();
+ for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
+ String name = propertyDescriptor.getName();
+ if (!HIDDEN_PROPERTIES.contains(name)) {
+ try {
+ Object value =
propertyDescriptor.getReadMethod().invoke(component);
+ if (null != value) {
+ descriptor.addParameter(name, value);
+ }
+ } catch (IllegalArgumentException e) {
+ // Ignore
+ } catch (IllegalAccessException e) {
+ // Ignore
+ } catch (InvocationTargetException e) {
+ // Ignore
+ }
+ }
+ }
+ } catch (IntrospectionException e) {
+ // Ignore.
+ }
+ }
+
+ /**
+ * Creates message for converter, using current locale.
+ *
+ * @param context
+ * @param component
+ * @return
+ */
+ protected FacesMessage getMessage(FacesContext context, T component) {
+ String messageId = getMessageId(component);
+ return MessageFactory.createMessage(context, messageId);
+ }
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -0,0 +1,12 @@
+package org.richfaces.validator;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.validator.Validator;
+
+public class FacesValidatorDescriptor extends BaseFacesObjectDescriptor<Validator>
implements ValidatorDescriptor {
+
+ public FacesValidatorDescriptor(Class<? extends Validator> validatorClass,
FacesMessage message) {
+ super(validatorClass, message);
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -0,0 +1,55 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.DoubleRangeValidator;
+import javax.faces.validator.LengthValidator;
+import javax.faces.validator.LongRangeValidator;
+import javax.faces.validator.RegexValidator;
+import javax.faces.validator.RequiredValidator;
+import javax.faces.validator.Validator;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class FacesValidatorServiceImpl extends FacesServiceBase<Validator>
implements FacesValidatorService {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.validator.FacesValidatorService#getValidatorDescription(javax.faces.context.FacesContext,
+ * javax.faces.validator.Validator)
+ */
+ public ValidatorDescriptor getValidatorDescription(FacesContext context, Validator
validator) {
+ FacesMessage message = getMessage(context, validator);
+ FacesValidatorDescriptor descriptor = new
FacesValidatorDescriptor(validator.getClass(), message);
+ fillParameters(descriptor, validator);
+ descriptor.makeImmutable();
+ return descriptor;
+ }
+
+ @Override
+ protected String getMessageId(Validator component) {
+ String messageId;
+ if (component instanceof DoubleRangeValidator) {
+ messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ } else if (component instanceof LengthValidator) {
+ messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ } else if (component instanceof LongRangeValidator) {
+ messageId = LongRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ } else if (component instanceof RegexValidator) {
+ messageId = RegexValidator.NOT_MATCHED_MESSAGE_ID;
+ } else if (component instanceof RequiredValidator) {
+ messageId = UIInput.REQUIRED_MESSAGE_ID;
+ } else {
+ messageId = UIInput.UPDATE_MESSAGE_ID;
+ }
+ return messageId;
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added:
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/FacesValidatorServiceTest.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/FacesValidatorServiceTest.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/FacesValidatorServiceTest.java 2010-10-28
00:20:19 UTC (rev 19720)
@@ -0,0 +1,88 @@
+package org.richfaces.validator;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.HashMap;
+import java.util.Locale;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIInput;
+import javax.faces.component.UIViewRoot;
+import javax.faces.validator.LengthValidator;
+import javax.faces.validator.RequiredValidator;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+
+import org.jboss.test.faces.mock.Environment;
+import org.jboss.test.faces.mock.Environment.Feature;
+import org.jboss.test.faces.mock.Mock;
+import org.jboss.test.faces.mock.MockController;
+import org.jboss.test.faces.mock.MockFacesEnvironment;
+import org.jboss.test.faces.mock.MockTestRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(MockTestRunner.class)
+public class FacesValidatorServiceTest {
+
+ @Mock()
+ @Environment({ Feature.APPLICATION })
+ protected MockFacesEnvironment environment;
+ protected FacesValidatorService serviceImpl;
+ @Mock
+ protected UIViewRoot viewRoot;
+ @Mock
+ protected UIInput input;
+ protected MockController controller;
+ protected Validator validator;
+
+
+ @Before
+ public void setUp() {
+ // create service impl.
+ serviceImpl = new FacesValidatorServiceImpl();
+ expect(environment.getFacesContext().getViewRoot()).andStubReturn(viewRoot);
+ expect(viewRoot.getLocale()).andStubReturn(Locale.ENGLISH);
+
expect(environment.getApplication().getMessageBundle()).andStubReturn("com.sun.faces.resources.Messages");
+ HashMap<String, Object> attributes = new HashMap<String, Object>();
+ attributes.put("label", "foo");
+ expect(input.getAttributes()).andStubReturn(attributes);
+ expect(input.getRequiredMessage()).andStubReturn(null);
+ }
+
+ @After
+ public void tearDown() {
+ controller.verify();
+ serviceImpl = null;
+ }
+
+ @Test
+ public void getConverterClass() throws Exception {
+ validator = new LengthValidator();
+ controller.replay();
+ ValidatorDescriptor validatorDescription =
serviceImpl.getValidatorDescription(environment.getFacesContext(), validator);
+ assertEquals(validator.getClass(),
validatorDescription.getImplementationClass());
+ }
+
+ @Test
+ public void getValidatorMessage() throws Exception {
+ validator = new RequiredValidator();
+ FacesMessage facesMessage = null;
+ controller.replay();
+ try{
+ validator.validate(environment.getFacesContext(), input, null);
+ } catch(ValidatorException e){
+ facesMessage = e.getFacesMessage();
+ }
+ assertNotNull(facesMessage);
+ ValidatorDescriptor validatorDescription =
serviceImpl.getValidatorDescription(environment.getFacesContext(), validator);
+ String summary = validatorDescription.getMessage().getSummary();
+ summary = summary.replace("{0}", "foo");
+ assertEquals(facesMessage.getSummary(), summary);
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/FacesValidatorServiceTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native