Author: alexsmirnov
Date: 2010-10-27 16:58:16 -0400 (Wed, 27 Oct 2010)
New Revision: 19715
Added:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptRenderer.java
branches/RF-8742/ui/validator/ui/src/main/resources/META-INF/faces-config.xml
branches/RF-8742/ui/validator/ui/src/test/resources/META-INF/
branches/RF-8742/ui/validator/ui/src/test/resources/META-INF/resources/
branches/RF-8742/ui/validator/ui/src/test/resources/META-INF/resources/foo.bar/
branches/RF-8742/ui/validator/ui/src/test/resources/META-INF/resources/foo.bar/test_script.js
Modified:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/LibraryResource.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/UIValidatorScript.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/TestBean.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/UIViewResourceTest.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java
Log:
RESOLVED - issue RF-9508: UIValidatorScript component test and implementation
https://jira.jboss.org/browse/RF-9508
Modified:
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/LibraryResource.java
===================================================================
---
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/LibraryResource.java 2010-10-27
19:04:55 UTC (rev 19714)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/LibraryResource.java 2010-10-27
20:58:16 UTC (rev 19715)
@@ -87,6 +87,11 @@
}
return true;
}
+
+ @Override
+ public String toString() {
+ return getLibrary()+':'+getResourceName();
+ }
public static Iterable<LibraryResource> of(Iterable<LibraryScriptString>
scripts){
LinkedHashSet<LibraryResource> resources = Sets.newLinkedHashSet();
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/UIValidatorScript.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/UIValidatorScript.java 2010-10-27
19:04:55 UTC (rev 19714)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/component/UIValidatorScript.java 2010-10-27
20:58:16 UTC (rev 19715)
@@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.el.ValueExpression;
import javax.faces.component.NamingContainer;
@@ -43,7 +44,10 @@
import javax.faces.render.Renderer;
import org.richfaces.renderkit.html.ComponentValidatorScript;
+import org.richfaces.renderkit.html.ValidatorScriptRenderer;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
@@ -58,6 +62,75 @@
*/
public class UIValidatorScript extends UIComponent {
+ private final class AttributesMap implements Map<String, Object> {
+ public void clear() {
+ // do nothing
+
+ }
+
+ public boolean containsKey(Object key) {
+
+ return
"target".equals(key)||"id".equals(key)||"clientId".equals(key);
+ }
+
+ public boolean containsValue(Object value) {
+ return false;
+ }
+
+ public Set<java.util.Map.Entry<String, Object>> entrySet() {
+ return Collections.emptySet();
+ }
+
+ public Object get(Object key) {
+ if("target".equals(key)){
+ return getTarget();
+ } else if ("id".equals(key)) {
+ return getId();
+ } else if ("clientId".equals(key)) {
+ return getClientId();
+ }
+ return null;
+ }
+
+ public boolean isEmpty() {
+ return false;
+ }
+
+ public Set<String> keySet() {
+ return
ImmutableSet.of("target","id","clientId");
+ }
+
+ public Object put(String key, Object value) {
+ if("target".equals(key)){
+ String target = getTarget();
+ setTarget((String) value);
+ return target;
+ } else if ("id".equals(key)) {
+ String id = getId();
+ setId((String) value);
+ return id;
+ }
+ return null;
+ }
+
+ public void putAll(Map<? extends String, ? extends Object> m) {
+
+
+ }
+
+ public Object remove(Object key) {
+ return null;
+ }
+
+ public int size() {
+ return 3;
+ }
+
+ public Collection<Object> values() {
+ return ImmutableList.<Object>of(getTarget(),getId(),getClientId());
+ }
+ }
+
public static final String COMPONENT_TYPE =
"org.richfaces.ValidatroScript";
public static final String COMPONENT_FAMILY = "org.richfaces.Script";
@@ -66,6 +139,8 @@
private UIComponent parent;
private String target="form";
private final List<ComponentValidatorScript> scripts = Lists.newArrayList();
+
+ private final Map<String, Object> attributesMap = new AttributesMap();
private String clientId;
@@ -117,7 +192,7 @@
@Override
public Map<String, Object> getAttributes() {
- return Collections.emptyMap();
+ return attributesMap;
}
@Override
@@ -229,8 +304,7 @@
@Override
public String getRendererType() {
- // TODO Auto-generated method stub
- return null;
+ return ValidatorScriptRenderer.RENDERER_TYPE;
}
@Override
Added:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptRenderer.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptRenderer.java
(rev 0)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptRenderer.java 2010-10-27
20:58:16 UTC (rev 19715)
@@ -0,0 +1,73 @@
+/**
+ *
+ */
+package org.richfaces.renderkit.html;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+
+import javax.faces.FacesException;
+import javax.faces.application.Resource;
+import javax.faces.application.ResourceHandler;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.Renderer;
+
+import org.richfaces.component.UIValidatorScript;
+import org.richfaces.validator.LibraryResource;
+
+import com.google.common.collect.Sets;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class ValidatorScriptRenderer extends Renderer {
+
+ public static final String RENDERER_TYPE =
"org.richfaces.renderer.ValidatorScriptRenderer";
+ private static final String TEXT_JAVASCRIPT = "text/javascript";
+ private static final String SRC = "src";
+ private static final String TYPE = "type";
+ private static final String SCRIPT = "script";
+
+ @Override
+ public void encodeEnd(FacesContext context, UIComponent component) throws IOException
{
+ UIValidatorScript validatorScript = (UIValidatorScript) component;
+
+ Collection<ComponentValidatorScript> scripts =
validatorScript.getScripts();
+ // flatten all dependent resources.
+ LinkedHashSet<LibraryResource> resources = Sets.newLinkedHashSet();
+ for (ComponentValidatorScript script : scripts) {
+ resources.addAll(script.getResources());
+ }
+ // render dependencies
+ for (LibraryResource resource : resources) {
+ encodeResource(context, resource);
+ }
+ ResponseWriter responseWriter = context.getResponseWriter();
+ responseWriter.startElement(SCRIPT, null);
+ responseWriter.writeAttribute(TYPE, TEXT_JAVASCRIPT, null);
+ for (ComponentValidatorScript componentValidatorScript : scripts) {
+ responseWriter.writeText(componentValidatorScript.toScript(), null);
+ responseWriter.write('\n');
+ }
+ responseWriter.endElement(SCRIPT);
+ }
+
+ private void encodeResource(FacesContext context, LibraryResource resource) throws
IOException {
+ ResourceHandler resourceHandler = context.getApplication().getResourceHandler();
+ Resource jsfResource = resourceHandler.createResource(resource.getResourceName(),
resource.getLibrary());
+ if (null != jsfResource) {
+ ResponseWriter responseWriter = context.getResponseWriter();
+ responseWriter.startElement(SCRIPT, null);
+ responseWriter.writeAttribute(TYPE, TEXT_JAVASCRIPT, null);
+ responseWriter.writeURIAttribute(SRC, jsfResource.getRequestPath(), null);
+ responseWriter.endElement(SCRIPT);
+ } else {
+ throw new FacesException("Dependent resource "+resource.toString()+
" not found");
+ }
+ }
+
+}
Property changes on:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptRenderer.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: branches/RF-8742/ui/validator/ui/src/main/resources/META-INF/faces-config.xml
===================================================================
--- branches/RF-8742/ui/validator/ui/src/main/resources/META-INF/faces-config.xml
(rev 0)
+++
branches/RF-8742/ui/validator/ui/src/main/resources/META-INF/faces-config.xml 2010-10-27
20:58:16 UTC (rev 19715)
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<faces-config version="2.0" metadata-complete="false"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:cdk="http://jboss.org/schema/richfaces/cdk/extensions"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<component>
+ <component-type>org.richfaces.ValidatroScript</component-type>
+
<component-class>org.richfaces.component.UIValidatorScript</component-class>
+</component>
+<render-kit>
+ <renderer>
+ <component-family>org.richfaces.Script</component-family>
+
<renderer-type>org.richfaces.renderer.ValidatorScriptRenderer</renderer-type>
+
<renderer-class>org.richfaces.renderkit.html.ValidatorScriptRenderer</renderer-class>
+ </renderer>
+</render-kit>
+</faces-config>
\ No newline at end of file
Property changes on:
branches/RF-8742/ui/validator/ui/src/main/resources/META-INF/faces-config.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/TestBean.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/TestBean.java 2010-10-27
19:04:55 UTC (rev 19714)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/TestBean.java 2010-10-27
20:58:16 UTC (rev 19715)
@@ -3,14 +3,56 @@
*/
package org.richfaces.component;
+import java.util.Collection;
+import java.util.Collections;
+
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.renderkit.html.ComponentValidatorScript;
+import org.richfaces.validator.LibraryResource;
+
/**
* @author asmirnov
*
*/
public class TestBean {
+ public static final String TEST_SCRIPT_NAME = "test_script";
+
+ public static final String FOO_BAR = "foo.bar";
+
+ public static final String FOO = "foo";
+
public static final String FOO_VALUE = "fooValue";
+ public static final String TEST_SCRIPT = "function " + FOO +
"(id){alert(id);}";
+
+ private static final LibraryResource TEST_RESOURCE = new LibraryResource(FOO_BAR,
TEST_SCRIPT_NAME + ".js");
+
+ private static final ComponentValidatorScript SCRIPT = new ComponentValidatorScript()
{
+
+ public String toScript() {
+ return TEST_SCRIPT;
+ }
+
+ public void appendScript(StringBuffer functionString) {
+ functionString.append(TEST_SCRIPT);
+ }
+
+ public Collection<LibraryResource> getResources() {
+ return Collections.singleton(TEST_RESOURCE);
+ }
+
+ public String getName() {
+ return FOO;
+ }
+
+ public String createCallScript(String clientId) {
+ return FOO+"("+clientId+")";
+ }
+ };
+
private String value=FOO_VALUE;
/**
@@ -27,6 +69,14 @@
this.value = value;
}
+ public String action() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ UIViewRoot viewRoot = facesContext.getViewRoot();
+ UIValidatorScript resource = (UIValidatorScript)
facesContext.getApplication().createComponent(UIValidatorScript.COMPONENT_TYPE);
+ resource.addOrFindScript(SCRIPT );
+ viewRoot.addComponentResource(facesContext, resource);
+ return null;
+ }
}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/UIViewResourceTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/UIViewResourceTest.java 2010-10-27
19:04:55 UTC (rev 19714)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/component/UIViewResourceTest.java 2010-10-27
20:58:16 UTC (rev 19715)
@@ -1,8 +1,10 @@
package org.richfaces.component;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
-import javax.faces.render.ResponseStateManager;
+import java.net.MalformedURLException;
import org.jboss.test.faces.FacesEnvironment;
import org.jboss.test.faces.FacesEnvironment.FacesRequest;
@@ -29,6 +31,7 @@
"
xmlns:h=\"http://java.sun.com/jsf/html\">"+
"<h:form id=\"helloForm\" >"+
" <h:inputText id=\"input\"
value=\"#{test.value}\" />\n" +
+ " <h:commandButton id=\"command\"
value=\"Ok\" action=\"#{test.action}\"/>\n" +
"</h:form>\n" +
"</html>").
withResource(FacesEnvironment.FACES_CONFIG_XML,
"org/richfaces/component/faces-config.xml").
@@ -46,15 +49,22 @@
FacesRequest request =
environment.createFacesRequest("http://localhost/test.jsf?foo=bar");
assertNotNull(request.execute());
String contentAsString = request.getConnection().getContentAsString();
- assertTrue(contentAsString.contains(ResponseStateManager.VIEW_STATE_PARAM));
- assertTrue(contentAsString.contains(TestBean.FOO_VALUE));
- FacesRequest request2 =
request.submit().withParameter("helloForm:input", "BAZ");
+ assertFalse(contentAsString.contains(TestBean.TEST_SCRIPT));
+ FacesRequest request2 = submit(request).submit();
request2.execute();
String content2 = request2.getConnection().getContentAsString();
- assertTrue(content2.contains("BAZ"));
- assertFalse(content2.contains(TestBean.FOO_VALUE));
-
+ assertFalse(content2.contains(TestBean.TEST_SCRIPT));
}
+ private FacesRequest submit(FacesRequest request) throws MalformedURLException {
+ FacesRequest request2 =
request.submit().withParameter("helloForm:input",
"BAZ").withParameter("helloForm:command", "Ok");
+ request2.execute();
+ String content2 = request2.getConnection().getContentAsString();
+ assertTrue(content2.contains(TestBean.TEST_SCRIPT));
+ assertTrue(content2.contains(TestBean.TEST_SCRIPT_NAME));
+ assertTrue(content2.contains(TestBean.FOO_BAR));
+ return request2;
+ }
+
}
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-27
19:04:55 UTC (rev 19714)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java 2010-10-27
20:58:16 UTC (rev 19715)
@@ -85,7 +85,7 @@
expect((Map<String, Object>)
descriptor.getValidatorParameters()).andReturn(
(Map<String, Object>) VALIDATOR_PARAMS);
expect(scriptService.getScript(RegexValidator.class)).andReturn(script);
- expect(script.getName()).andReturn(REGEX_VALIDATOR);
+ expect(script.getName()).andReturn(REGEX_VALIDATOR).atLeastOnce();
expect(script.getResource()).andReturn(CLIENT_VALIDATOR_LIBRARY);
controller.replay();
Collection<? extends LibraryScriptString> clientSideScripts =
renderer.getClientSideValidatorScript(environment.getFacesContext(), descriptors);
@@ -104,7 +104,7 @@
expect((Map<String, Object>)
converterDescriptor.getValidatorParameters()).andReturn(
(Map<String, Object>) VALIDATOR_PARAMS);
expect(scriptService.getScript(NumberConverter.class)).andReturn(script);
- expect(script.getName()).andReturn(REGEX_VALIDATOR);
+ expect(script.getName()).andReturn(REGEX_VALIDATOR).atLeastOnce();
expect(script.getResource()).andReturn(CLIENT_VALIDATOR_LIBRARY);
controller.replay();
LibraryScriptFunction clientSideScript =
Added:
branches/RF-8742/ui/validator/ui/src/test/resources/META-INF/resources/foo.bar/test_script.js
===================================================================
Property changes on:
branches/RF-8742/ui/validator/ui/src/test/resources/META-INF/resources/foo.bar/test_script.js
___________________________________________________________________
Name: svn:mime-type
+ text/plain