Author: alexsmirnov
Date: 2010-10-26 18:19:31 -0400 (Tue, 26 Oct 2010)
New Revision: 19675
Added:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
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/renderkit/html/AjaxOnlyScript.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientAndAjaxScript.java
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
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/ComponentValidatorScript.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetComponentScriptTest.java
Log:
RESOLVED - issue RF-9507: ClientValidatorRendererunit tests and implementation.
https://jira.jboss.org/browse/RF-9507
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-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/api/src/main/java/org/richfaces/validator/LibraryResource.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -1,5 +1,11 @@
package org.richfaces.validator;
+import java.util.LinkedHashSet;
+
+import org.richfaces.renderkit.html.LibraryScriptString;
+
+import com.google.common.collect.Sets;
+
/**
* This class represent information about external JavaScript library as JSF resource
*
@@ -83,4 +89,11 @@
return true;
}
+ public static Iterable<LibraryResource> of(Iterable<LibraryScriptString>
scripts){
+ LinkedHashSet<LibraryResource> resources = Sets.newLinkedHashSet();
+ for (LibraryScriptString scriptString : scripts) {
+ resources.add(scriptString.getResource());
+ }
+ return resources;
+ }
}
\ No newline at end of file
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/AjaxOnlyScript.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/AjaxOnlyScript.java 2010-10-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/AjaxOnlyScript.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -1,38 +1,27 @@
package org.richfaces.renderkit.html;
import java.util.Collection;
+import java.util.Collections;
import org.richfaces.validator.LibraryResource;
-public class AjaxOnlyScript implements ComponentValidatorScript {
+public class AjaxOnlyScript extends ValidatorScriptBase {
- public AjaxOnlyScript(String ajaxScript) {
- // TODO Auto-generated constructor stub
- }
- public String toScript() {
- // TODO Auto-generated method stub
- return null;
- }
+ private final String ajaxScript;
- public void appendScript(StringBuffer functionString) {
- // TODO Auto-generated method stub
-
+ public AjaxOnlyScript(String ajaxScript) {
+ super();
+ this.ajaxScript = ajaxScript;
+
}
- public String getName() {
- // TODO Auto-generated method stub
- return null;
- }
-
public Collection<LibraryResource> getResources() {
- // TODO Auto-generated method stub
- return null;
+ return Collections.emptySet();
}
- public String createCallScript(String clientId) {
- // TODO Auto-generated method stub
- return null;
+ @Override
+ protected Object buildBody() {
+ return ajaxScript;
}
-
}
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientAndAjaxScript.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientAndAjaxScript.java 2010-10-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientAndAjaxScript.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -2,59 +2,26 @@
import java.util.Collection;
-import org.ajax4jsf.javascript.JSFunctionDefinition;
-import org.ajax4jsf.javascript.ScriptStringBase;
-import org.richfaces.validator.LibraryResource;
-public class ClientAndAjaxScript extends JSFunctionDefinition implements
ComponentValidatorScript {
+public class ClientAndAjaxScript extends ClientOnlyScript{
- private static final NullConverterScript NULL_CONVERTER_SCRIPT = new
NullConverterScript();
-
+ final String ajaxScript;
- private final LibraryScriptString converter;
-
- private final Collection<? extends LibraryScriptString> validators;
-
- private final String ajaxScript;
-
- private boolean bodyProcessed = false;
-
public ClientAndAjaxScript(LibraryScriptString clientSideConverterScript,
Collection<? extends LibraryScriptString> validatorScripts, String
ajaxScript) {
- this.converter = null==clientSideConverterScript ?
NULL_CONVERTER_SCRIPT:clientSideConverterScript;
- this.validators = validatorScripts;
+ super(clientSideConverterScript,validatorScripts);
this.ajaxScript = ajaxScript;
}
- public void appendScript(StringBuffer functionString) {
- if(!bodyProcessed){
- addToBody(buildBody());
- bodyProcessed = true;
- }
- super.appendScript(functionString);
+ protected void finishValidation(StringBuffer body) {
+ // AJAX callback
+ body.append("if(!").append(DISABLE_AJAX).append("){\n");
+ body.append(ajaxScript).append(EOL).append("}\n");
}
-
- protected Object buildBody() {
- // TODO Auto-generated method stub
- return null;
- }
-
-
- public Collection<LibraryResource> getResources() {
- // TODO Auto-generated method stub
- return null;
- }
-
- public String createCallScript(String clientId) {
- // TODO Auto-generated method stub
- return null;
- }
-
-
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2010-10-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -1,40 +1,67 @@
package org.richfaces.renderkit.html;
import java.util.Collection;
+import java.util.LinkedHashSet;
import org.richfaces.validator.LibraryResource;
-import org.richfaces.validator.LibraryScript;
-public class ClientOnlyScript implements ComponentValidatorScript {
+import com.google.common.collect.Sets;
- public ClientOnlyScript(LibraryScript clientSideConverterScript,
- Collection<LibraryScriptFunction> validatorScripts) {
- // TODO Auto-generated constructor stub
- }
+public class ClientOnlyScript extends ValidatorScriptBase{
- public String toScript() {
- // TODO Auto-generated method stub
- return null;
- }
+ protected final LibraryScriptString converter;
+ protected final Collection<? extends LibraryScriptString> validators;
- public void appendScript(StringBuffer functionString) {
- // TODO Auto-generated method stub
-
+ public ClientOnlyScript(LibraryScriptString clientSideConverterScript,
+ Collection<? extends LibraryScriptString> validatorScripts) {
+ super();
+ this.converter = null==clientSideConverterScript ?
NULL_CONVERTER_SCRIPT:clientSideConverterScript;
+ this.validators = validatorScripts;
+
}
- public String getName() {
- // TODO Auto-generated method stub
- return null;
+ public Collection<LibraryResource> getResources() {
+ LinkedHashSet<LibraryResource> resources = Sets.newLinkedHashSet();
+ resources.add(converter.getResource());
+ for (LibraryScriptString scriptString : validators) {
+ resources.add(scriptString.getResource());
+ }
+ return resources;
}
- public Collection<LibraryResource> getResources() {
- // TODO Auto-generated method stub
- return null;
+ @Override
+ protected Object buildBody() {
+ StringBuffer body = new StringBuffer();
+ // Get component value by clientId.
+ body.append("var
").append(ClientValidatorRenderer.VALUE_VAR).append("=");
+ GET_VALUE_FUNCTION.appendScript(body);
+ body.append(EOL);
+ // Try client-side validation
+ body.append("try {\n");
+ // convert value
+ body.append("var
").append(ClientValidatorRenderer.CONVERTED_VALUE_VAR).append("=");
+ converter.appendScript(body);
+ body.append(EOL);
+ // call validators
+ for (LibraryScriptString validatorScript : validators) {
+ validatorScript.appendScript(body);
+ body.append(EOL);
+ }
+ finishValidation(body);
+ body.append("return true;\n");
+ // Catch errors
+ body.append("} catch(e) {\n");
+ SEND_ERROR_FUNCTION.appendScript(body);body.append(EOL);
+ body.append("return false;\n}");
+ return body;
}
- public String createCallScript(String clientId) {
- // TODO Auto-generated method stub
- return null;
+ protected void finishValidation(StringBuffer body) {
+ // clear messages after successful validation
+ body.append("if(!").append(DISABLE_AJAX).append("){\n");
+ CLEAR_ERROR_FUNCTION.appendScript(body);
+ body.append(EOL).append("}\n");
}
+
}
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-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientValidatorRenderer.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -15,6 +15,7 @@
import javax.faces.render.ClientBehaviorRenderer;
import org.ajax4jsf.javascript.JSLiteral;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.richfaces.application.ServiceTracker;
import org.richfaces.component.UIValidatorScript;
import org.richfaces.component.behavior.ClientValidatorBehavior;
@@ -127,37 +128,42 @@
ComponentValidatorScript createValidatorScript(ClientBehaviorContext
behaviorContext,
ClientValidatorBehavior behavior) {
+ ValidatorScriptBase validatorScript;
Collection<ValidatorDescriptor> validators =
behavior.getValidators(behaviorContext);
if (!validators.isEmpty()) {
try {
ConverterDescriptor converter = behavior.getConverter(behaviorContext);
if (null != converter) {
try {
- LibraryScriptFunction clientSideConverterScript =
+ LibraryScriptString clientSideConverterScript =
getClientSideConverterScript(behaviorContext.getFacesContext(), converter);
- return createValidatorScript(behaviorContext, behavior,
validators, clientSideConverterScript);
+ validatorScript = createValidatorScript(behaviorContext,
behavior, validators, clientSideConverterScript);
} catch (ScriptNotFoundException e) {
// ajax-only validation
- return new AjaxOnlyScript();
+ validatorScript = new
AjaxOnlyScript(behavior.getAjaxScript(behaviorContext));
}
} else {
- return createValidatorScript(behaviorContext, behavior, validators,
null);
+ validatorScript = createValidatorScript(behaviorContext, behavior,
validators, null);
}
} catch (ConverterNotFoundException e) {
throw new FacesException(e);
}
+ String clientId =
behaviorContext.getComponent().getClientId(behaviorContext.getFacesContext());
+ String name = ScriptUtils.getValidJavascriptName(clientId+":v");
+ validatorScript.setName(name);
+ return validatorScript;
} else {
// No validation required.
return null;
}
}
- private ComponentValidatorScript createValidatorScript(ClientBehaviorContext
behaviorContext,
+ private ValidatorScriptBase createValidatorScript(ClientBehaviorContext
behaviorContext,
ClientValidatorBehavior behavior, Collection<ValidatorDescriptor>
validators,
- LibraryScriptFunction clientSideConverterScript) {
+ LibraryScriptString clientSideConverterScript) {
Collection<? extends LibraryScriptString> validatorScripts =
getClientSideValidatorScript(behaviorContext.getFacesContext(), validators);
if (validatorScripts.isEmpty()) {
- return new AjaxOnlyScript();
+ return new AjaxOnlyScript(behavior.getAjaxScript(behaviorContext));
} else if (validatorScripts.size() < validators.size()) {
return new ClientAndAjaxScript(clientSideConverterScript, validatorScripts,
behavior.getAjaxScript(behaviorContext));
Modified:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ComponentValidatorScript.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ComponentValidatorScript.java 2010-10-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ComponentValidatorScript.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -16,6 +16,7 @@
* @param clientId
* @return
*/
- String createCallScript(String clientId);
+ String createCallScript(String clientId);
+
}
Added:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
(rev 0)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -0,0 +1,46 @@
+package org.richfaces.renderkit.html;
+
+import org.ajax4jsf.javascript.JSFunction;
+import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.JSLiteral;
+
+public abstract class ValidatorScriptBase extends JSFunctionDefinition implements
ComponentValidatorScript {
+
+ protected static final String EVENT = "event";
+ protected static final JSLiteral EVENT_LITERAL = new JSLiteral(EVENT);
+ protected static final JSLiteral THIS = new JSLiteral("this");
+ protected static final String DISABLE_AJAX = "disableAjax";
+ protected static final String EOL = "\n;";
+ protected static final String CLIENT_ID = "clientId";
+ protected static final JSLiteral CLIENT_ID_LITERAL = new JSLiteral(CLIENT_ID);
+ protected static final NullConverterScript NULL_CONVERTER_SCRIPT = new
NullConverterScript();
+ protected static final String CSV_NAMESPACE = "RichFaces.csv.";
+ protected static final String VALUE_FUNCTION_NAME =
CSV_NAMESPACE+"getValue";
+ protected static final JSFunction GET_VALUE_FUNCTION = new
JSFunction(VALUE_FUNCTION_NAME,CLIENT_ID_LITERAL);
+ protected static final JSFunction SEND_ERROR_FUNCTION = new
JSFunction(CSV_NAMESPACE+"sendMessage",CLIENT_ID_LITERAL,new
JSLiteral("e"));
+ protected static final JSFunction CLEAR_ERROR_FUNCTION = new
JSFunction(CSV_NAMESPACE+"clearMessage",CLIENT_ID_LITERAL);
+
+ private boolean bodyProcessed = false;
+
+ public ValidatorScriptBase() {
+ super(CLIENT_ID,"element",EVENT,DISABLE_AJAX);
+ }
+
+ public void appendScript(StringBuffer functionString) {
+ if(!bodyProcessed){
+ // pending RF-9565
+ addToBody(buildBody());
+ bodyProcessed = true;
+ }
+ super.appendScript(functionString);
+ }
+
+ protected abstract Object buildBody();
+
+ public String createCallScript(String clientId) {
+ JSFunction callFunction = new JSFunction(getName(),clientId,THIS,EVENT_LITERAL);
+ return callFunction.toScript();
+ }
+
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetComponentScriptTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetComponentScriptTest.java 2010-10-26
17:10:27 UTC (rev 19674)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetComponentScriptTest.java 2010-10-26
22:19:31 UTC (rev 19675)
@@ -151,7 +151,7 @@
@Test
public void testCreateValidatorScriptEmptyValidator() throws Exception {
ClientValidatorRenderer renderer =
createStubRenderer(createConverterFunction());
- setupBehavior(NumberConverter.class);
+ setupBehavior(null);
ComponentValidatorScript validatorScript = callGetScript(renderer);
assertNull(validatorScript);
}
@@ -259,6 +259,7 @@
validatorDescriptors.add(validatorDescriptor);
}
expect(mockBehavior.getValidators(behaviorContext)).andReturn(validatorDescriptors);
+
expect(input.getClientId(environment.getFacesContext())).andStubReturn("foo:bar");
}
private void setupDescription(Class<?> converter, ValidatorDescriptor
descriptor) {