Author: alexsmirnov
Date: 2010-10-14 17:35:43 -0400 (Thu, 14 Oct 2010)
New Revision: 19574
Added:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/BeanValidatorService.java
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/FacesValidatorService.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ValidatorDescriptor.java
Removed:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptLookupService.java
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ValidatorLookup.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/AjaxBehaviorWrapper.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ValidatorDescriptor.java
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorBehavior.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorImpl.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetConverterTest.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetValidatorTest.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorTestBase.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/GetScriptTest.java
Log:
RESOLVED - issue RF-9506: ClientValidatorBehavior unit tests and implementation.
https://jira.jboss.org/browse/RF-9506
Copied:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/BeanValidatorService.java
(from rev 19570,
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ValidatorLookup.java)
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/BeanValidatorService.java
(rev 0)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/BeanValidatorService.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -0,0 +1,27 @@
+package org.richfaces.validator;
+
+import java.util.Collection;
+
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+
+
+/**
+ * <p class="changed_added_4_0">This interface describes service that
gets Bean Validator constrains for EL-expressions</p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface BeanValidatorService {
+
+ /**
+ * <p class="changed_added_4_0">Get all constrains for given
EL-expression</p>
+ * @param context
+ * @param expression
+ * @return
+ */
+ Collection<ValidatorDescriptor> getConstrains(FacesContext context,
ValueExpression expression,Class<?> ...groups);
+
+ Collection<String> validateExpression(FacesContext context,ValueExpression
expression, Object newValue,Class<?> ...groups);
+
+ Collection<String> validateObject(FacesContext context,Object object, Object
newValue,Class<?> ...groups);
+}
Property changes on:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/BeanValidatorService.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptLookupService.java
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptLookupService.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptLookupService.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,42 +0,0 @@
-/*
- * $Id$
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.richfaces.validator;
-
-/**
- * <p class="changed_added_4_0">This interface describes service that
determines JavaScript module and function
- * for Java corresponded version ( both Converter and Validator )</p>
- * @author asmirnov(a)exadel.com
- *
- */
-public interface ClientScriptLookupService {
-
- /**
- * <p class="changed_added_4_0">Get description for client-side
version of Java implementation</p>
- * @param javaClass either Converter or Validator class.
- * @return description of client-side script.
- * @throws ScriptNotFoundException if no JavaScript code associated with Java class.
- */
- ClientScript getScript(Class<?> javaClass) throws ScriptNotFoundException;
-
-}
Copied:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java
(from rev 19570,
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptLookupService.java)
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java
(rev 0)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -0,0 +1,42 @@
+/*
+ * $Id$
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.validator;
+
+/**
+ * <p class="changed_added_4_0">This interface describes service that
determines JavaScript module and function
+ * for Java corresponded version ( both Converter and Validator )</p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface ClientScriptService {
+
+ /**
+ * <p class="changed_added_4_0">Get description for client-side
version of Java implementation</p>
+ * @param javaClass either Converter or Validator class.
+ * @return description of client-side script.
+ * @throws ScriptNotFoundException if no JavaScript code associated with Java class.
+ */
+ ClientScript getScript(Class<?> javaClass) throws ScriptNotFoundException;
+
+}
Property changes on:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/FacesValidatorService.java
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/FacesValidatorService.java
(rev 0)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/FacesValidatorService.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -0,0 +1,10 @@
+package org.richfaces.validator;
+
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+
+
+public interface FacesValidatorService {
+
+ ValidatorDescriptor getValidatorDescription(FacesContext context,Validator
validator);
+}
Property changes on:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/FacesValidatorService.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Deleted:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ValidatorLookup.java
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ValidatorLookup.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ValidatorLookup.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,25 +0,0 @@
-package org.richfaces.validator;
-
-import java.util.Collection;
-
-import javax.el.ValueExpression;
-import javax.faces.context.FacesContext;
-import javax.validation.metadata.ConstraintDescriptor;
-
-/**
- * <p class="changed_added_4_0">This interface describes service that
gets Bean Validator constrains for EL-expressions</p>
- * @author asmirnov(a)exadel.com
- *
- */
-public interface ValidatorLookup {
-
- /**
- * <p class="changed_added_4_0">Get all constrains for given
EL-expression</p>
- * @param context
- * @param expression
- * @return
- */
- Iterable<ConstraintDescriptor<?>> getConstrains(FacesContext context,
ValueExpression expression);
-
- Collection<String> validateExpression(FacesContext context,ValueExpression
expression, Object newValue);
-}
Copied:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ValidatorDescriptor.java
(from rev 19570,
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ValidatorDescriptor.java)
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ValidatorDescriptor.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ValidatorDescriptor.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -0,0 +1,42 @@
+package org.richfaces.validator;
+
+import java.util.Map;
+
+/**
+ * <p class="changed_added_4_0">
+ * Inmlementations of this interface describe JSF or JSR-303 validators. Because JSF
supports 2 types of validators,
+ * this interface unifies access to them.
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface ValidatorDescriptor {
+
+ /**
+ * <p class="changed_added_4_0">
+ * Returns JSF {@link javax.faces.validator.Validator} implementation class or
JSR-303 annotation class.
+ * </p>
+ *
+ * @return
+ */
+ Class<?> getValidatorClass();
+
+ /**
+ * <p class="changed_added_4_0">
+ * Concrete validator parameters
+ * </p>
+ *
+ * @return non null map with validator instance parameters.
+ */
+ Map<String, Object> getValidatorParameters();
+
+ /**
+ * <p class="changed_added_4_0">
+ * Localized validator message
+ * </p>
+ *
+ * @return
+ */
+ String getMessage();
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ValidatorDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/AjaxBehaviorWrapper.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/AjaxBehaviorWrapper.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/AjaxBehaviorWrapper.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,17 +0,0 @@
-package org.richfaces.component.behavior;
-
-import javax.faces.component.behavior.ClientBehaviorBase;
-import javax.faces.component.behavior.ClientBehaviorContext;
-
-public class AjaxBehaviorWrapper extends ClientBehaviorBase {
-
- public AjaxBehaviorWrapper() {
- super();
- }
-
- public String getAjaxScript(ClientBehaviorContext context) {
- // TODO Auto-generated method stub
- return null;
- }
-
-}
\ No newline at end of file
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorBehavior.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorBehavior.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorBehavior.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -6,6 +6,8 @@
import javax.faces.component.behavior.ClientBehaviorContext;
import javax.faces.convert.Converter;
+import org.richfaces.validator.ValidatorDescriptor;
+
/**
* <p class="changed_added_4_0">Interface for JSF Behavior that creates
scripts for client-side validation</p>
* @author asmirnov(a)exadel.com
@@ -38,5 +40,9 @@
* @return
*/
Collection<ValidatorDescriptor> getValidators(ClientBehaviorContext context);
+
+ Class<?>[] getGroups();
+
+ void setGroups(Class<?> ...groups);
}
\ No newline at end of file
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorImpl.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorImpl.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ClientValidatorImpl.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -22,24 +22,34 @@
*/
package org.richfaces.component.behavior;
+import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
+import javax.el.ValueExpression;
import javax.faces.FacesException;
+import javax.faces.application.Application;
import javax.faces.component.ActionSource;
import javax.faces.component.EditableValueHolder;
+import javax.faces.component.UIComponent;
import javax.faces.component.behavior.AjaxBehavior;
import javax.faces.component.behavior.ClientBehaviorContext;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.render.ClientBehaviorRenderer;
import javax.faces.render.RenderKit;
+import javax.faces.validator.Validator;
+import org.richfaces.application.ServiceTracker;
import org.richfaces.log.Logger;
import org.richfaces.log.RichfacesLogger;
import org.richfaces.renderkit.html.ClientValidatorRenderer;
import org.richfaces.renderkit.html.FormClientValidatorRenderer;
+import org.richfaces.validator.BeanValidatorService;
+import org.richfaces.validator.FacesValidatorService;
+import org.richfaces.validator.ValidatorDescriptor;
+import com.google.common.collect.Lists;
+
/**
* <p class="changed_added_4_0">
* </p>
@@ -47,19 +57,21 @@
* @author asmirnov(a)exadel.com
*
*/
-public class ClientValidatorImpl extends AjaxBehaviorWrapper implements
ClientValidatorBehavior {
+public class ClientValidatorImpl extends AjaxBehavior implements ClientValidatorBehavior
{
+ private static final String VALUE = "value";
+
private static final Logger LOG = RichfacesLogger.COMPONENTS.getLogger();
-
- private AjaxBehavior ajaxBehavior;
+ private Class<?>[] groups;
+
@Override
public String getScript(ClientBehaviorContext behaviorContext) {
if (behaviorContext.getComponent() instanceof EditableValueHolder) {
return super.getScript(behaviorContext);
} else if (behaviorContext.getComponent() instanceof ActionSource) {
- ClientBehaviorRenderer renderer =
- getRenderer(behaviorContext.getFacesContext(),
FormClientValidatorRenderer.RENDERER_TYPE);
+ ClientBehaviorRenderer renderer =
getRenderer(behaviorContext.getFacesContext(),
+ FormClientValidatorRenderer.RENDERER_TYPE);
return renderer.getScript(behaviorContext, this);
} else {
throw new FacesException("Invalid target for client-side validator
behavior");
@@ -71,6 +83,18 @@
return ClientValidatorRenderer.RENDERER_TYPE;
}
+ @Override
+ public void decode(FacesContext context, UIComponent component) {
+ if (null == context || null == component) {
+ throw new NullPointerException();
+ }
+ ClientBehaviorRenderer renderer = getRenderer(context,
AjaxBehavior.BEHAVIOR_ID);
+ if (null != renderer) {
+ renderer.decode(context, component, this);
+ // TODO - setup phase and render area to rerender.
+ }
+ }
+
/**
* <p class="changed_added_4_0">
* Look up for {@link ClientBehaviorRenderer} instence
@@ -100,7 +124,6 @@
return renderer;
}
-
/*
* (non-Javadoc)
*
@@ -108,7 +131,30 @@
* ClientBehaviorContext)
*/
public Converter getConverter(ClientBehaviorContext context) throws
ConverterNotFoundException {
- return null;
+ UIComponent component = context.getComponent();
+ if (component instanceof EditableValueHolder) {
+ EditableValueHolder input = (EditableValueHolder) component;
+ Converter converter = input.getConverter();
+ if (null == converter) {
+ Class<?> valueType;
+ ValueExpression valueExpression = component.getValueExpression(VALUE);
+ if (null != valueExpression) {
+ FacesContext facesContext = context.getFacesContext();
+ valueType = valueExpression.getType(facesContext.getELContext());
+ if (valueType != null && valueType != Object.class) {
+ Application application = facesContext.getApplication();
+ converter = application.createConverter(valueType);
+ if (null == converter && valueType != String.class) {
+ throw new ConverterNotFoundException("No converter
registered for type "
+ + valueType.getName());
+ }
+ }
+ }
+ }
+ return converter;
+ } else {
+ throw new ConverterNotFoundException("Component does not implement
EditableValueHolder" + component);
+ }
}
/*
@@ -118,6 +164,96 @@
* ClientBehaviorContext)
*/
public Collection<ValidatorDescriptor> getValidators(ClientBehaviorContext
context) {
- return Collections.emptySet();
+ UIComponent component = context.getComponent();
+ if (component instanceof EditableValueHolder) {
+ ArrayList<ValidatorDescriptor> validators = Lists.newArrayList();
+ EditableValueHolder input = (EditableValueHolder) component;
+ Validator[] facesValidators = input.getValidators();
+ FacesContext facesContext = context.getFacesContext();
+ if (facesValidators.length > 0) {
+ FacesValidatorService facesValidatorService =
ServiceTracker.getService(facesContext,
+ FacesValidatorService.class);
+ for (Validator validator : facesValidators) {
+
validators.add(facesValidatorService.getValidatorDescription(facesContext, validator));
+ }
+ }
+ ValueExpression valueExpression = component.getValueExpression(VALUE);
+ if (null != valueExpression) {
+ BeanValidatorService beanValidatorService =
ServiceTracker.getService(facesContext,
+ BeanValidatorService.class);
+ validators.addAll(beanValidatorService.getConstrains(facesContext,
valueExpression, getGroups()));
+ }
+ return validators;
+ } else {
+ throw new FacesException("Component " +
component.getClass().getName()
+ + " does not implement EditableValueHolder interface");
+ }
}
+
+ public Class<?>[] getGroups() {
+ if (groups != null) {
+ return groups;
+ }
+ ValueExpression expression = getValueExpression("groups");
+ if (expression != null) {
+ FacesContext ctx = FacesContext.getCurrentInstance();
+ return (Class<?>[]) expression.getValue(ctx.getELContext());
+ }
+ return null;
+ }
+
+ public void setGroups(Class<?>... groups) {
+ this.groups = groups;
+ clearInitialState();
+ }
+
+ public String getAjaxScript(ClientBehaviorContext context) {
+ return getRenderer(context.getFacesContext(), BEHAVIOR_ID).getScript(context,
this);
+ }
+
+ @Override
+ public Object saveState(FacesContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ Object[] values;
+
+ Object superState = super.saveState(context);
+
+ if (initialStateMarked()) {
+ if (superState == null) {
+ values = null;
+ } else {
+ values = new Object[] { superState };
+ }
+ } else {
+ values = new Object[2];
+
+ values[0] = superState;
+ values[1] = groups;
+ }
+
+ return values;
+ }
+
+ @Override
+ public void restoreState(FacesContext context, Object state) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ if (state != null) {
+
+ Object[] values = (Object[]) state;
+ super.restoreState(context, values[0]);
+
+ if (values.length != 1) {
+ groups = (Class<?>[]) values[1];
+ // If we saved state last time, save state again next time.
+ clearInitialState();
+ }
+ }
+ }
+
}
Deleted:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ValidatorDescriptor.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ValidatorDescriptor.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/behavior/ValidatorDescriptor.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,42 +0,0 @@
-package org.richfaces.component.behavior;
-
-import java.util.Map;
-
-/**
- * <p class="changed_added_4_0">
- * Inmlementations of this interface describe JSF or JSR-303 validators. Because JSF
supports 2 types of validators,
- * this interface unifies access to them.
- * </p>
- *
- * @author asmirnov(a)exadel.com
- *
- */
-public interface ValidatorDescriptor {
-
- /**
- * <p class="changed_added_4_0">
- * Returns JSF {@link javax.faces.validator.Validator} implementation class or
JSR-303 annotation class.
- * </p>
- *
- * @return
- */
- Class<?> getValidatorClass();
-
- /**
- * <p class="changed_added_4_0">
- * Concrete validator parameters
- * </p>
- *
- * @return non null map with validator instance parameters.
- */
- Map<String, Object> getValidatorParameters();
-
- /**
- * <p class="changed_added_4_0">
- * Localized validator message
- * </p>
- *
- * @return
- */
- String getMessage();
-}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetConverterTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetConverterTest.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetConverterTest.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,6 +1,7 @@
package org.richfaces.component.behavior;
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.*;
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import javax.faces.convert.Converter;
import javax.faces.convert.NumberConverter;
@@ -63,6 +64,7 @@
expect(input.getConverter()).andReturn(null);
expect(input.getValueExpression("value")).andReturn(expression);
expect((Class)(expression.getType(environment.getElContext()))).andReturn(String.class);
+
expect(environment.getApplication().createConverter(String.class)).andReturn(null);
checkConverter(null);
}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetValidatorTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetValidatorTest.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorGetValidatorTest.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,51 +1,147 @@
package org.richfaces.component.behavior;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import javax.el.ValueExpression;
import javax.faces.validator.Validator;
+import javax.validation.groups.Default;
import org.jboss.test.faces.mock.Mock;
import org.jboss.test.faces.mock.MockTestRunner;
+import org.jboss.test.faces.mock.Stub;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.richfaces.application.ServiceTracker;
+import org.richfaces.application.ServicesFactory;
+import org.richfaces.validator.BeanValidatorService;
+import org.richfaces.validator.FacesValidatorService;
+import org.richfaces.validator.ValidatorDescriptor;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
/**
- * <p class="changed_added_4_0">This class tests client validator
behavior.
- * as it described at
https://community.jboss.org/wiki/ClientSideValidation # Server-side
rendering algorithm</p>
+ * <p class="changed_added_4_0">
+ * This class tests client validator behavior. as it described at
https://community.jboss.org/wiki/ClientSideValidation
+ * # Server-side rendering algorithm
+ * </p>
+ *
* @author asmirnov(a)exadel.com
- *
+ *
*/
@RunWith(MockTestRunner.class)
public class BehaviorGetValidatorTest extends BehaviorTestBase {
-
+ private static final String VALIDATION_ERROR = "Validation Error";
+
+ private static final Class<?>[] DEFAULT_GROUP = { Default.class };
+
@Mock
private Validator validator;
+
+ @Mock
+ private ValidatorDescriptor beanValidatorDescriptor;
+
+ @Mock
+ private BeanValidatorService validatorService;
+
+ @Mock
+ private FacesValidatorService facesValidatorService;
+
+ @Stub
+ private ServicesFactory factory;
+
+ @Stub
+ private ValueExpression expression;
+
+ @Before
+ public void setupService() {
+
expect(factory.getInstance(BeanValidatorService.class)).andStubReturn(validatorService);
+
expect(factory.getInstance(FacesValidatorService.class)).andStubReturn(facesValidatorService);
+ ServiceTracker.setFactory(factory);
+ setupBehaviorContext(input);
+ behavior.setGroups(DEFAULT_GROUP);
+ expect(input.getValueExpression("value")).andStubReturn(expression);
+ }
+
+ @After
+ public void releaseService() {
+ ServiceTracker.release();
+ }
+
/**
- * <p class="changed_added_4_0">Component does not define any
validators.</p>
+ * <p class="changed_added_4_0">
+ * Component does not define any validators.
+ * </p>
+ *
* @throws Exception
*/
@Test
public void testEmptyValidators() throws Exception {
- setupComponentValidator(null);
- setupBeanValidator(null);
- checkValidator();
+ setupComponentValidator();
+ setupBeanValidator();
+ assertTrue(checkValidator().isEmpty());
+ controller.verify();
}
- private void setupComponentValidator(Validator validator) {
- // TODO Auto-generated method stub
-
+ private Collection<ValidatorDescriptor> checkValidator() {
+ controller.replay();
+ Collection<ValidatorDescriptor> validators =
behavior.getValidators(behaviorContext);
+ // controller.verify();
+ return validators;
}
+ private void setupBeanValidator(ValidatorDescriptor... validators) {
+ expect(validatorService.getConstrains(environment.getFacesContext(), expression,
DEFAULT_GROUP)).andStubReturn(
+ Lists.newArrayList(validators));
+
+ }
+
+ private void setupComponentValidator(Validator... validators) {
+ expect(input.getValidators()).andStubReturn(validators);
+ for (Validator validator : validators) {
+ ValidatorDescriptor validatorDescriptor =
environment.createMock(ValidatorDescriptor.class);
+ expect((Class)
validatorDescriptor.getValidatorClass()).andStubReturn(validator.getClass());
+ expect(validatorDescriptor.getMessage()).andStubReturn(VALIDATION_ERROR);
+
expect(facesValidatorService.getValidatorDescription(environment.getFacesContext(),
validator))
+ .andStubReturn(validatorDescriptor);
+ }
+ }
+
/**
- * <p class="changed_added_4_0">Component defines JSF validator
only.</p>
+ * <p class="changed_added_4_0">
+ * Component defines JSF validator only.
+ * </p>
+ *
* @throws Exception
*/
@Test
public void testComponentValidator() throws Exception {
-
+ setupComponentValidator(validator);
+ setupBeanValidator();
+ Collection<ValidatorDescriptor> validators = checkValidator();
+ assertEquals(1, validators.size());
+ ValidatorDescriptor validatorDescriptor = Iterables.getOnlyElement(validators);
+ assertSame(validator.getClass(), validatorDescriptor.getValidatorClass());
+ assertEquals(VALIDATION_ERROR, validatorDescriptor.getMessage());
+ controller.verify();
}
-
+
@Test
public void testBeanValidators() throws Exception {
-
+ setupComponentValidator();
+ setupBeanValidator(beanValidatorDescriptor);
+ Collection<ValidatorDescriptor> validators = checkValidator();
+ assertEquals(1, validators.size());
+ ValidatorDescriptor validatorDescriptor = Iterables.getOnlyElement(validators);
+ assertSame(beanValidatorDescriptor, validatorDescriptor);
+ controller.verify();
}
}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorTestBase.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorTestBase.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/BehaviorTestBase.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,6 +1,6 @@
package org.richfaces.component.behavior;
-import static org.easymock.EasyMock.*;
+import static org.easymock.EasyMock.expect;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
@@ -18,7 +18,7 @@
public class BehaviorTestBase {
@Mock()
- @Environment({ Feature.APPLICATION, Feature.RENDER_KIT })
+ @Environment({ Feature.APPLICATION, Feature.RENDER_KIT,Feature.EL_CONTEXT })
protected MockFacesEnvironment environment;
@Mock
protected UIInput input;
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/GetScriptTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/GetScriptTest.java 2010-10-14
16:22:29 UTC (rev 19573)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/behavior/GetScriptTest.java 2010-10-14
21:35:43 UTC (rev 19574)
@@ -1,11 +1,12 @@
package org.richfaces.component.behavior;
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.*;
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
import javax.faces.FacesException;
import javax.faces.component.UICommand;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
+import javax.faces.component.behavior.AjaxBehavior;
import javax.faces.component.behavior.ClientBehaviorContext;
import javax.faces.render.RenderKit;
@@ -65,12 +66,27 @@
testGetScriptDelegate(FormClientValidatorRenderer.RENDERER_TYPE, command);
}
+ @Test
+ public void testGetAjaxScript() throws Exception {
+ setupRenderer(AjaxBehavior.BEHAVIOR_ID);
+ ClientBehaviorContext clientBehaviorContext = setupBehaviorContext(input);
+ controller.replay();
+ String script = behavior.getAjaxScript(clientBehaviorContext);
+ controller.verify();
+ assertEquals(CLIENT_VALIDATION_FUNCTION,script);
+
+ }
private void testGetScriptDelegate(String rendererType, UIComponent component) {
+ setupRenderer(rendererType);
+ String script = getScript(component);// ajax-only validator.
+ assertEquals(CLIENT_VALIDATION_FUNCTION,script);
+ }
+
+
+ private void setupRenderer(String rendererType) {
RenderKit renderKit = environment.getRenderKit();
expect(renderKit.getClientBehaviorRenderer(rendererType)).andReturn(behaviorRenderer);
expect(behaviorRenderer.getScript(behaviorContext,
behavior)).andReturn(CLIENT_VALIDATION_FUNCTION);
- String script = getScript(component);// ajax-only validator.
- assertEquals(CLIENT_VALIDATION_FUNCTION,script);
}
private String getScript(UIComponent component) {