Author: alexsmirnov
Date: 2010-10-29 19:11:21 -0400 (Fri, 29 Oct 2010)
New Revision: 19791
Added:
branches/RF-8742/ui/common/ui/src/main/java/org/richfaces/component/util/Strings.java
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientSideScript.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/ClientSideScripts.java
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/Component.java
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ClientScriptServiceTest.java
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ServiceConfigParserTest.java
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ValidatorWithFacesResource.java
branches/RF-8742/ui/validator/impl/src/test/resources/badcsv.xml
branches/RF-8742/ui/validator/impl/src/test/resources/csv.xml
Modified:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java
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/el/CapturingELContext.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientValidatorRenderer.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java
Log:
RESOLVED - issue RF-9510: BeanValidatorService tests and implementation
https://jira.jboss.org/browse/RF-9510
RESOLVED - issue RF-9509: ClientScriptLookupService unit tests and implementation
https://jira.jboss.org/browse/RF-9509
Added:
branches/RF-8742/ui/common/ui/src/main/java/org/richfaces/component/util/Strings.java
===================================================================
--- branches/RF-8742/ui/common/ui/src/main/java/org/richfaces/component/util/Strings.java
(rev 0)
+++
branches/RF-8742/ui/common/ui/src/main/java/org/richfaces/component/util/Strings.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,109 @@
+/*
+ * $Id: Strings.java 19714 2010-10-27 19:04:55Z alexsmirnov $
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.component.util;
+
+import java.util.NoSuchElementException;
+
+import com.google.common.base.Joiner;
+
+
+/**
+ * <p class="changed_added_4_0">String manipulation utils.</p>
+ *
+ * @author asmirnov(a)exadel.com
+ */
+public final class Strings {
+
+ public static final Joiner DOT_JOINER = Joiner.on('.');
+
+ private Strings() {
+
+ // this is utility class with static methods only.
+ }
+
+ /**
+ * <p class="changed_added_4_0">Remove characters from string
end</p>
+ *
+ * @param in input string
+ * @param size number of characters to remove.
+ * @return
+ */
+ public static String cut(String in, int size) {
+ if (size > 0) {
+ return in.substring(0, in.length() - size);
+ }
+
+ return in;
+ }
+
+ /**
+ * <p class="changed_added_4_0">Change case of the first character to
lower, as it required by the Java Beans property and setter/getter method name
conventions:</p>
+ * <p>"PropertyFoo" will be changed to
"propertyFoo"</p>
+ *
+ * @param in
+ * @return {@code in} with first character changed to lower case.
+ */
+ public static String firstToLowerCase(String in) {
+ if (!isEmpty(in)) {
+ in = in.substring(0, 1).toLowerCase() + in.substring(1);
+ }
+
+ return in;
+ }
+
+ /**
+ * <p class="changed_added_4_0">Change case of the first character to
upper, as it required by the Java Beans property and setter/getter method name
conventions:</p>
+ * <p>"propertyFoo" will be changed to
"PropertyFoo"</p>
+ *
+ * @param in
+ * @return {@code in} with first character changed to lower case.
+ */
+ public static String firstToUpperCase(String in) {
+ if (!isEmpty(in)) {
+ in = in.substring(0, 1).toUpperCase() + in.substring(1);
+ }
+
+ return in;
+ }
+
+ /**
+ * <p class="changed_added_4_0">Check string for null or empty
value</p>
+ *
+ * @param type
+ * @return true if {@code type} is null or zero-length string.
+ */
+ public static boolean isEmpty(String type) {
+ return type == null || type.length() == 0;
+ }
+
+ public static String firstNonEmpty(String... strings) {
+ for (String s : strings) {
+ if (!isEmpty(s)) {
+ return s;
+ }
+ }
+
+ throw new NoSuchElementException();
+ }
+}
Property changes on:
branches/RF-8742/ui/common/ui/src/main/java/org/richfaces/component/util/Strings.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified:
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/ClientScriptService.java 2010-10-29
19:15:30 UTC (rev 19790)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientScriptService.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -23,6 +23,8 @@
package org.richfaces.validator;
+import javax.faces.context.FacesContext;
+
/**
* <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>
@@ -33,10 +35,11 @@
/**
* <p class="changed_added_4_0">Get description for client-side
version of Java implementation</p>
+ * @param facesContext TODO
* @param javaClass either Converter or Validator class.
* @return description of client-side script.
* @throws ScriptNotFoundException if no JavaScript code associated with Java class.
*/
- LibraryFunction getScript(Class<?> javaClass) throws ScriptNotFoundException;
+ LibraryFunction getScript(FacesContext facesContext, Class<?> javaClass) throws
ScriptNotFoundException;
}
Added:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientSideScript.java
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientSideScript.java
(rev 0)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientSideScript.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,26 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * This annotation describes client-side version of Converter/validator.
+ * @author asmirnov
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+(a)Target(ElementType.TYPE)
+public @interface ClientSideScript {
+
+ String library() default "";
+
+ String resource();
+
+ String function();
+
+}
Property changes on:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/ClientSideScript.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
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-29
19:15:30 UTC (rev 19790)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/application/ValidatorModule.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -23,6 +23,8 @@
package org.richfaces.application;
+import java.util.Map;
+
import javax.validation.ValidationException;
import org.richfaces.el.ValueExpressionAnalayserImpl;
@@ -30,11 +32,15 @@
import org.richfaces.validator.BeanValidatorFactory;
import org.richfaces.validator.BeanValidatorService;
import org.richfaces.validator.BeanValidatorServiceImpl;
+import org.richfaces.validator.ClientScriptService;
+import org.richfaces.validator.ClientScriptServiceImpl;
+import org.richfaces.validator.ClientServiceConfigParser;
import org.richfaces.validator.ConverterServiceImpl;
import org.richfaces.validator.DummyBeanValidatorService;
import org.richfaces.validator.FacesConverterService;
import org.richfaces.validator.FacesValidatorService;
import org.richfaces.validator.FacesValidatorServiceImpl;
+import org.richfaces.validator.LibraryFunction;
import org.richfaces.validator.NullValidator;
import org.richfaces.validator.ObjectValidator;
import org.richfaces.validator.RichFacesBeanValidatorFactory;
@@ -58,9 +64,18 @@
configureBeanValidators(factory);
factory.setInstance(FacesConverterService.class, new ConverterServiceImpl());
factory.setInstance(FacesValidatorService.class, new
FacesValidatorServiceImpl());
+ ClientScriptServiceImpl clientScriptService = createClientScriptService();
+ factory.setInstance(ClientScriptService.class, clientScriptService);
}
+ private ClientScriptServiceImpl createClientScriptService() {
+ Map<Class<?>, LibraryFunction> config =
ClientServiceConfigParser.parseConfig("META-INF/csv.xml");
+ ClientScriptServiceImpl clientScriptService = new
ClientScriptServiceImpl(config);
+ return clientScriptService;
+ }
+
+
void configureBeanValidators(ServicesFactory factory){
BeanValidatorService service ;
ObjectValidator validator;
Modified:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/el/CapturingELContext.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/el/CapturingELContext.java 2010-10-29
19:15:30 UTC (rev 19790)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/el/CapturingELContext.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -106,8 +106,6 @@
@Override
public Object getValue(ELContext context, Object base, Object property) {
- // Null value for the base means new properties chain.
- // TODO - record each chain to the separate collection for compound
expressions.
reference = new ValueReference(base, property, reference);
return delegate.getValue(context, base, property);
}
Added:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,74 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.Map;
+
+import javax.faces.application.Resource;
+import javax.faces.application.ResourceHandler;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.component.util.Strings;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class ClientScriptServiceImpl implements ClientScriptService {
+
+ private static final String TEXT_JAVASCRIPT = "text/javascript";
+
+ private static final String ORG_RICHFACES_CSV = "org.richfaces.csv";
+
+ private final Map<Class<?>, LibraryFunction> defaultMapping;
+
+ public ClientScriptServiceImpl(Map<Class<?>, LibraryFunction>
defaultMapping) {
+ this.defaultMapping = defaultMapping;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.validator.ClientScriptService#getScript(java.lang.Class)
+ */
+ public LibraryFunction getScript(FacesContext facesContext, Class<?> javaClass)
throws ScriptNotFoundException {
+ if (null == facesContext || null == javaClass) {
+ throw new NullPointerException();
+ }
+ LibraryFunction function;
+ try {
+ function = getScriptResource(facesContext, javaClass);
+ } catch (ScriptNotFoundException e) {
+ if (defaultMapping.containsKey(javaClass)) {
+ function = defaultMapping.get(javaClass);
+ } else {
+ function = getScriptFromAnnotation(javaClass);
+ }
+ }
+ return function;
+ }
+
+ private LibraryFunction getScriptFromAnnotation(Class<?> javaClass) throws
ScriptNotFoundException {
+ if (javaClass.isAnnotationPresent(ClientSideScript.class)) {
+ ClientSideScript clientSideScript =
javaClass.getAnnotation(ClientSideScript.class);
+ return new LibraryFunctionImplementation(clientSideScript.library(),
clientSideScript.resource(), clientSideScript.function());
+ } else {
+ throw new ScriptNotFoundException();
+ }
+ }
+
+ private LibraryFunction getScriptResource(FacesContext facesContext, Class<?>
javaClass)
+ throws ScriptNotFoundException {
+ ResourceHandler resourceHandler =
facesContext.getApplication().getResourceHandler();
+ String resourceName = javaClass.getSimpleName() + ".js";
+ Resource facesResource = resourceHandler.createResource(resourceName,
ORG_RICHFACES_CSV, TEXT_JAVASCRIPT);
+ if (null != facesResource) {
+ final String functionName =
Strings.firstToLowerCase(javaClass.getSimpleName());
+ return new LibraryFunctionImplementation(ORG_RICHFACES_CSV,resourceName,
functionName);
+ } else {
+ throw new ScriptNotFoundException();
+ }
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.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/ClientServiceConfigParser.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,67 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Map;
+
+import javax.faces.FacesException;
+import javax.xml.bind.JAXB;
+
+import org.richfaces.validator.model.ClientSideScripts;
+import org.richfaces.validator.model.Component;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.Maps;
+
+/**
+ * @author asmirnov
+ *
+ */
+public final class ClientServiceConfigParser {
+
+ private ClientServiceConfigParser() {
+ }
+
+ public static Map<Class<?>, LibraryFunction> parseConfig(String name) {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (null == loader) {
+ loader = ClientServiceConfigParser.class.getClassLoader();
+ }
+ Builder<Class<?>, LibraryFunction> resultBuilder =
ImmutableMap.builder();
+ try {
+ Enumeration<URL> resources = loader.getResources(name);
+ while (resources.hasMoreElements()) {
+ URL url = (URL) resources.nextElement();
+ resultBuilder.putAll(parse(loader, url));
+ }
+ } catch (IOException e) {
+ return Collections.emptyMap();
+ }
+ return resultBuilder.build();
+ }
+
+ static Map<Class<?>, LibraryFunction> parse(ClassLoader loader, URL url)
{
+ Map<Class<?>, LibraryFunction> result = Maps.newHashMap();
+ try {
+ ClientSideScripts clientSideScripts = JAXB.unmarshal(url,
ClientSideScripts.class);
+ for (Component component : clientSideScripts.getComponent()) {
+ Class<?> componentClass = loader.loadClass(component.getType());
+ LibraryFunctionImplementation function = new
LibraryFunctionImplementation(component.getLibrary(),
+ component.getResource(), component.getFunction());
+ result.put(componentClass, function);
+ }
+ } catch (ClassNotFoundException e) {
+ throw new FacesException("Class for component not found",e);
+ } catch (Exception e) {
+ throw new FacesException("Error parsing config file "+url,e);
+ }
+ return result;
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.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/LibraryFunctionImplementation.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,24 @@
+package org.richfaces.validator;
+
+final class LibraryFunctionImplementation implements LibraryFunction {
+ private final LibraryResource library;
+ private final String functionName;
+
+ LibraryFunctionImplementation(LibraryResource library, String functionName) {
+ this.library = library;
+ this.functionName = functionName;
+ }
+
+ LibraryFunctionImplementation(String library, String resource, String functionName)
{
+ this.library = new LibraryResource(library, resource);
+ this.functionName = functionName;
+ }
+
+ public LibraryResource getResource() {
+ return library;
+ }
+
+ public String getName() {
+ return functionName;
+ }
+}
\ No newline at end of file
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.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/model/ClientSideScripts.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/ClientSideScripts.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/ClientSideScripts.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,31 @@
+/**
+ *
+ */
+package org.richfaces.validator.model;
+
+import java.util.Collection;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import com.google.common.collect.Lists;
+
+/**
+ * @author asmirnov
+ *
+ */
+@XmlRootElement(name="scripts")
+public class ClientSideScripts {
+
+ private Collection<Component> component = Lists.newArrayList();
+
+ public void setComponent(Collection<Component> component) {
+ this.component = component;
+ }
+
+ @XmlElement
+ public Collection<Component> getComponent() {
+ return component;
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/ClientSideScripts.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/model/Component.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/Component.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/Component.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,75 @@
+package org.richfaces.validator.model;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class Component {
+
+ private String type;
+
+ private String function;
+
+ private String library;
+
+ private String resource;
+
+ /**
+ * @return the type
+ */
+ @XmlElement
+ public String getType() {
+ return type;
+ }
+
+ /**
+ * @param type the type to set
+ */
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ /**
+ * @return the function
+ */
+ @XmlElement
+ public String getFunction() {
+ return function;
+ }
+
+ /**
+ * @param function the function to set
+ */
+ public void setFunction(String function) {
+ this.function = function;
+ }
+
+ /**
+ * @return the library
+ */
+ @XmlElement
+ public String getLibrary() {
+ return library;
+ }
+
+ /**
+ * @param library the library to set
+ */
+ public void setLibrary(String library) {
+ this.library = library;
+ }
+
+ /**
+ * @return the resource
+ */
+ @XmlElement
+ public String getResource() {
+ return resource;
+ }
+
+ /**
+ * @param resource the resource to set
+ */
+ public void setResource(String resource) {
+ this.resource = resource;
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/main/java/org/richfaces/validator/model/Component.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/ClientScriptServiceTest.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ClientScriptServiceTest.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ClientScriptServiceTest.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,121 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+import java.util.Map;
+
+import javax.faces.application.Resource;
+import javax.faces.application.ResourceHandler;
+import javax.validation.constraints.Max;
+
+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;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * @author asmirnov
+ *
+ */
+(a)RunWith(MockTestRunner.class)
+public class ClientScriptServiceTest {
+
+ private static final String TEXT_JAVASCRIPT = "text/javascript";
+
+ private static final String ORG_RICHFACES_CSV = "org.richfaces.csv";
+
+ private static final String RESOURCE_NAME =
ValidatorWithFacesResource.class.getSimpleName() + ".js";
+
+ @Mock
+ @Environment({ Feature.APPLICATION })
+ private MockFacesEnvironment environment;
+
+ @Mock
+ private ResourceHandler resourceHandler;
+
+ private MockController controller;
+
+ @Mock
+ private Resource resource;
+
+ @Mock
+ private LibraryFunction function;
+
+ private ClientScriptServiceImpl serviceImpl;
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+
expect(environment.getApplication().getResourceHandler()).andStubReturn(resourceHandler);
+ Map<Class<?>, LibraryFunction> defaultMapping = ImmutableMap
+ .<Class<?>, LibraryFunction> of(Max.class, function);
+ serviceImpl = new ClientScriptServiceImpl(defaultMapping);
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ controller.verify();
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.validator.ClientScriptServiceImpl#getScript(FacesContext,
java.lang.Class)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetScriptAsJsfResource() throws Exception {
+ LibraryFunction script = getScript(resource, ValidatorWithFacesResource.class);
+ assertEquals(RESOURCE_NAME, script.getResource().getResourceName());
+ assertEquals(ORG_RICHFACES_CSV, script.getResource().getLibrary());
+ assertEquals("validatorWithFacesResource", script.getName());
+ }
+
+ @Test
+ public void testGetScriptFromAnnotation() throws Exception {
+ LibraryFunction script = getScript(null, ValidatorWithFacesResource.class);
+ assertEquals("baz.js", script.getResource().getResourceName());
+ assertEquals("bar", script.getResource().getLibrary());
+ assertEquals("foo", script.getName());
+ }
+
+ @Test
+ public void testGetScriptFromDefaultMapping() throws Exception {
+ LibraryFunction script = getScript(null, Max.class);
+ assertSame(function, script);
+ }
+
+ @Test
+ public void testGetScriptOverrideAnnotation() throws Exception {
+ Map<Class<?>, LibraryFunction> defaultMapping =
ImmutableMap.<Class<?>, LibraryFunction> of(
+ ValidatorWithFacesResource.class, function);
+ serviceImpl = new ClientScriptServiceImpl(defaultMapping);
+ LibraryFunction script = getScript(null, ValidatorWithFacesResource.class);
+ assertSame(function, script);
+ }
+
+ private LibraryFunction getScript(Resource resource, Class<?> serverSideType)
throws ScriptNotFoundException {
+ expect(resourceHandler.createResource(serverSideType.getSimpleName() +
".js", ORG_RICHFACES_CSV, TEXT_JAVASCRIPT)).andReturn(resource);
+ controller.replay();
+ LibraryFunction script = serviceImpl.getScript(environment.getFacesContext(),
serverSideType);
+ return script;
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ClientScriptServiceTest.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/ServiceConfigParserTest.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ServiceConfigParserTest.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ServiceConfigParserTest.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,43 @@
+package org.richfaces.validator;
+
+import static org.junit.Assert.*;
+
+import java.util.Map;
+
+import javax.faces.FacesException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ServiceConfigParserTest {
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void testParseConfig() {
+ Map<Class<?>, LibraryFunction> parseConfig =
ClientServiceConfigParser.parseConfig("csv.xml");
+ assertEquals(2, parseConfig.size());
+ assertTrue(parseConfig.containsKey(String.class));
+ LibraryFunction libraryFunction = parseConfig.get(String.class);
+ assertEquals("stringConverter", libraryFunction.getName());
+ assertEquals("csv.js",
libraryFunction.getResource().getResourceName());
+ assertEquals("org.richfaces",
libraryFunction.getResource().getLibrary());
+ }
+
+ @Test(expected=FacesException.class)
+ public void testParseBadConfig() {
+ Map<Class<?>, LibraryFunction> parseConfig =
ClientServiceConfigParser.parseConfig("badcsv.xml");
+ }
+ @Test()
+ public void testParseNoConfig() {
+ Map<Class<?>, LibraryFunction> parseConfig =
ClientServiceConfigParser.parseConfig("non-exists-csv.xml");
+ assertEquals(0, parseConfig.size());
+ }
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ServiceConfigParserTest.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/ValidatorWithFacesResource.java
===================================================================
---
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ValidatorWithFacesResource.java
(rev 0)
+++
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ValidatorWithFacesResource.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -0,0 +1,6 @@
+package org.richfaces.validator;
+
+@ClientSideScript(function="foo",library="bar",resource="baz.js")
+public class ValidatorWithFacesResource {
+
+}
Property changes on:
branches/RF-8742/ui/validator/impl/src/test/java/org/richfaces/validator/ValidatorWithFacesResource.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: branches/RF-8742/ui/validator/impl/src/test/resources/badcsv.xml
===================================================================
--- branches/RF-8742/ui/validator/impl/src/test/resources/badcsv.xml
(rev 0)
+++ branches/RF-8742/ui/validator/impl/src/test/resources/badcsv.xml 2010-10-29 23:11:21
UTC (rev 19791)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scripts>
+ <component>
+ <type>non.existed.Class</type>
+ <library>org.richfaces</library>
+ <resource>csv.js</resource>
+ <function>stringConverter</function>
+ </component>
+</scripts>
\ No newline at end of file
Property changes on: branches/RF-8742/ui/validator/impl/src/test/resources/badcsv.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: branches/RF-8742/ui/validator/impl/src/test/resources/csv.xml
===================================================================
--- branches/RF-8742/ui/validator/impl/src/test/resources/csv.xml
(rev 0)
+++ branches/RF-8742/ui/validator/impl/src/test/resources/csv.xml 2010-10-29 23:11:21 UTC
(rev 19791)
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scripts>
+ <component>
+ <type>java.lang.String</type>
+ <library>org.richfaces</library>
+ <resource>csv.js</resource>
+ <function>stringConverter</function>
+ </component>
+ <component>
+ <type>java.lang.Integer</type>
+ <library>org.richfaces</library>
+ <resource>csv.js</resource>
+ <function>intConverter</function>
+ </component>
+</scripts>
\ No newline at end of file
Property changes on: branches/RF-8742/ui/validator/impl/src/test/resources/csv.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientValidatorRenderer.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientValidatorRenderer.java 2010-10-29
19:15:30 UTC (rev 19790)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientValidatorRenderer.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -197,12 +197,12 @@
ConverterDescriptor converter) throws ScriptNotFoundException {
ClientScriptService clientScriptService =
ServiceTracker.getService(facesContext, ClientScriptService.class);
- return createClientFunction(converter, VALUE_LITERAL, clientScriptService);
+ return createClientFunction(facesContext, converter, VALUE_LITERAL,
clientScriptService);
}
- private LibraryScriptString createClientFunction(FacesObjectDescriptor descriptor,
JSLiteral variable,
- ClientScriptService clientScriptService) throws ScriptNotFoundException {
- LibraryFunction script =
clientScriptService.getScript(descriptor.getImplementationClass());
+ private LibraryScriptString createClientFunction(FacesContext facesContext,
FacesObjectDescriptor descriptor,
+ JSLiteral variable, ClientScriptService clientScriptService) throws
ScriptNotFoundException {
+ LibraryFunction script = clientScriptService.getScript(facesContext,
descriptor.getImplementationClass());
return new LibraryScriptFunction(script, variable, descriptor.getMessage(),
descriptor.getAdditionalParameters());
}
@@ -223,7 +223,7 @@
List<LibraryScriptString> scripts = Lists.newArrayList();
for (FacesObjectDescriptor validator : validators) {
try {
- scripts.add(createClientFunction(validator, CONVERTED_VALUE_LITERAL,
clientScriptService));
+ scripts.add(createClientFunction(facesContext, validator,
CONVERTED_VALUE_LITERAL, clientScriptService));
} catch (ScriptNotFoundException e) {
// Skip this validator for AJAX call.
}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java 2010-10-29
19:15:30 UTC (rev 19790)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java 2010-10-29
23:11:21 UTC (rev 19791)
@@ -23,7 +23,7 @@
import org.richfaces.validator.LibraryFunction;
import org.richfaces.validator.LibraryScriptString;
import org.richfaces.validator.ScriptNotFoundException;
-import org.richfaces.validator.FacesObjectDescriptor;
+import org.richfaces.validator.ValidatorDescriptor;
import com.google.common.collect.Iterables;
@@ -32,7 +32,7 @@
public class RendererGetClientSideScriptTest extends RendererTestBase {
@Mock
- private FacesObjectDescriptor descriptor;
+ private ValidatorDescriptor descriptor;
@Mock
private ConverterDescriptor converterDescriptor;
@@ -43,7 +43,7 @@
@Mock
private LibraryFunction script;
- private Collection<FacesObjectDescriptor> descriptors;
+ private Collection<ValidatorDescriptor> descriptors;
@Before
public void setupService() {
@@ -60,7 +60,7 @@
@Test()
public void testGetClientSideScriptNotExists() throws Throwable {
expect((Class)
descriptor.getImplementationClass()).andReturn(RegexValidator.class);
- expect(scriptService.getScript(RegexValidator.class)).andThrow(new
ScriptNotFoundException());
+ expect(scriptService.getScript(environment.getFacesContext(),
RegexValidator.class)).andThrow(new ScriptNotFoundException());
controller.replay();
Collection<? extends LibraryScriptString> clientSideValidatorScript =
renderer.getClientSideValidatorScript(
@@ -72,7 +72,7 @@
@Test(expected = ScriptNotFoundException.class)
public void testGetClientSideConverterScriptNotExists() throws Throwable {
expect((Class)
converterDescriptor.getImplementationClass()).andReturn(NumberConverter.class);
- expect(scriptService.getScript(NumberConverter.class)).andThrow(new
ScriptNotFoundException());
+ expect(scriptService.getScript(environment.getFacesContext(),
NumberConverter.class)).andThrow(new ScriptNotFoundException());
controller.replay();
renderer.getClientSideConverterScript(environment.getFacesContext(),
converterDescriptor);
controller.verify();
@@ -84,7 +84,7 @@
expect(descriptor.getMessage()).andReturn(VALIDATOR_MESSAGE);
expect((Map<String, Object>)
descriptor.getAdditionalParameters()).andReturn(
(Map<String, Object>) VALIDATOR_PARAMS);
- expect(scriptService.getScript(RegexValidator.class)).andReturn(script);
+ expect(scriptService.getScript(environment.getFacesContext(),
RegexValidator.class)).andReturn(script);
expect(script.getName()).andReturn(REGEX_VALIDATOR).atLeastOnce();
expect(script.getResource()).andReturn(CLIENT_VALIDATOR_LIBRARY);
controller.replay();
@@ -103,7 +103,7 @@
expect(converterDescriptor.getMessage()).andReturn(VALIDATOR_MESSAGE);
expect((Map<String, Object>)
converterDescriptor.getAdditionalParameters()).andReturn(
(Map<String, Object>) VALIDATOR_PARAMS);
- expect(scriptService.getScript(NumberConverter.class)).andReturn(script);
+ expect(scriptService.getScript(environment.getFacesContext(),
NumberConverter.class)).andReturn(script);
expect(script.getName()).andReturn(REGEX_VALIDATOR).atLeastOnce();
expect(script.getResource()).andReturn(CLIENT_VALIDATOR_LIBRARY);
controller.replay();