Author: alexsmirnov
Date: 2010-12-08 19:56:15 -0500 (Wed, 08 Dec 2010)
New Revision: 20467
Added:
branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/csv.js
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/MockTestBase.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/TestCriteria.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/BooleanConverterTest.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/ConverterTestBase.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/validator/
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/validator/ValidatorTestBase.java
Removed:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/MockConverterTest.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/TestCriteria.java
Modified:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
Log:
CODING IN PROGRESS - issue RF-9217: CSV: development tests for client side(qunit)
https://jira.jboss.org/browse/RF-9217
Modified:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java 2010-12-08
16:16:00 UTC (rev 20466)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ValidatorScriptBase.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -9,6 +9,7 @@
public abstract class ValidatorScriptBase extends JSFunctionDefinition implements
ComponentValidatorScript {
protected static final String ELEMENT = "element";
+ protected static final JSReference ELEMENT_LITERAL = new JSReference(ELEMENT);
protected static final String EVENT = "event";
protected static final JSReference EVENT_LITERAL = new JSReference(EVENT);
protected static final String DISABLE_AJAX = "disableAjax";
@@ -20,7 +21,7 @@
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 GET_VALUE_FUNCTION = new
JSFunction(VALUE_FUNCTION_NAME,CLIENT_ID_LITERAL,ELEMENT_LITERAL);
protected static final JSFunction SEND_ERROR_FUNCTION = new
JSFunction(CSV_NAMESPACE+"sendMessage",CLIENT_ID_LITERAL,new
JSReference("e"));
protected static final JSFunction CLEAR_ERROR_FUNCTION = new
JSFunction(CSV_NAMESPACE+"clearMessage",CLIENT_ID_LITERAL);
Added:
branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/csv.js
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/csv.js
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/csv.js 2010-12-09
00:56:15 UTC (rev 20467)
@@ -0,0 +1,152 @@
+(function($, rf) {
+
+ rf.csv = rf.csv || {};
+
+ var _messages = {};
+
+ var RE_MESSAGE_PATTERN = /\{(\d+)\}/g;
+
+ $.extend(rf.csv, {
+ RE_DIGITS: /^-?\d+$/,
+ RE_FLOAT: /^(-?\d+)?(\.(\d+)?(e[+-]?\d+)?)?$/,
+ MESSAGE_EVENT_NAME: "onmessage."+rf.Event.RICH_NAMESPACE,
+ // Messages API
+ addMessage: function (messagesObject) {
+ $.extend(_messages, messagesObject);
+ },
+ getMessage: function(customMessage, messageId, values) {
+ var message = customMessage || _messages[messageId] || "";
+ if (message) {
+ var msgObject =
message.replace(RE_MESSAGE_PATTERN,"\n$1\n").split("\n");
+ var value;
+ for (var i=1; i<msgObject.length; i+=2) {
+ value = values[msgObject[i]];
+ msgObject[i] = typeof value == "undefined" ? "" : value;
+ }
+ message = msgObject.join('');
+ }
+ return {message:message};
+ },
+ sendMessage: function (componentId, message) {
+ rf.Event.fireById(document, MESSAGE_EVENT_NAME, message);
+ rf.Event.fireById(componentId, MESSAGE_EVENT_NAME, message);
+ },
+ clearMessage: function (componentId) {
+ rf.Event.fireById(document, MESSAGE_EVENT_NAME, message);
+ rf.Event.fireById(componentId, MESSAGE_EVENT_NAME, message);
+ },
+ getValue: function (clientId, element){
+ var value;
+ var element = element || rf.getDomElement(id);
+ if (element.value) {
+ value = element.value;
+ } else {
+ var component = rf.$(element);
+ // TODO: add getValue to baseComponent and change jsdocs
+ value = component && typeof component["getValue"] ==
"function" ? component.getValue() : "";
+ }
+ return value;
+ },
+ validate: function (event, id, converter, validators) {
+ var value = getValue(id);
+ if (converter) {
+ try {
+ converter.options.componentId = id;
+ value = getConverter([converter.name])(value, converter.options);
+ } catch (e){
+ sendMessage(id, e.message);
+ return false;
+ }
+ }
+ if (validators) {
+ var validatorFunction;
+ try {
+ for (i=0;i<validators.length;i++) {
+ validatorFunction = getValidator(validators[i].type);
+ if (validatorFunction) {
+ validatorFunction(id, value, validators[i]);
+ }
+ }
+ } catch (e) {
+ sendMessage(id, result);
+ return false;
+ }
+ }
+ return true;
+ },
+ // Converters
+ convertBoolean: function(value,message,options){
+ options = options || {};
+ var result;
+ value = $.trim(value).toLowerCase();
+ result = value=='true' ? true : value.length<1 ? null : false;
+ return result;
+ },
+ addFormValidators: function (formId, callValidatorFunctions) {
+
+ }
+ });
+
+ /*
+ // component ids hash that can send messages
+ // each hash item contains array of message component id that receive messages from the
component
+ _componentIds = {};
+ // array of message component id that will receive messages from all components
+ _messageComponentIds = {};
+
+ var messageDispatchers = {};
+ var addDispatcher = function (dispatcherId) {
+ };
+ var removeDispatcher: function (dispatcherId) {
+ };
+
+ rf.MessageDispatcher = function(id) {
+ this.id = id;
+ };
+ rf.BaseComponent.extend(rf.MessageDispatcher);
+
+ $.extend(rf.MessageDispatcher.prototype, {
+ register: function (messageComponentId, componentIds) {
+ if (!componentIds || componentIds.length==0) {
+ // global message listener
+ _messageComponents.push(messageComponentId);
+ }
+ var messageComponents;
+ for (var i=0;i<componentIds.length;i++) {
+ messageComponents = _components[componentIds[i]];
+ if (!messageComponents) {
+ messageComponents = _components[componentIds[i]] = [];
+ }
+ messageComponents.push(messageComponentId);
+ }
+ },
+ unregister: function (messageComponentId) {
+ var messageComponents;
+ for (var i=0;i<_components.length;i++) {
+ messageComponents = _components[i];
+ if (!messageComponents) {
+ messageComponents = _components[componentIds[i]] = [];
+ }
+ messageComponents.push(messageComponentId);
+ }
+ },
+ send: function (componentId, message) {
+ var messageComponents = _components[componentId];
+ if (messageComponents) {
+ for (var i=0;i<messageComponents.length;i++) {
+ rf.$(messageComponents[id]).update(message);
+ }
+ }
+ }
+ });
+ */
+
+ /*
+ * message.constructor () {
+ * rf.Event.bindById(componentId, "onMessage.RichFaces", onMessage );
+ * rf.Event.bindById(document, "onMessage.RichFaces", onMessage );
+ * }
+ *
+ */
+
+})(jQuery, window.RichFaces || (window.RichFaces={}));
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/csv.js
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/MockConverterTest.java
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/MockConverterTest.java 2010-12-08
16:16:00 UTC (rev 20466)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/MockConverterTest.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -1,61 +0,0 @@
-package org.richfaces.convert;
-
-import static org.junit.Assert.*;
-
-import java.util.List;
-
-import org.jboss.test.faces.mock.MockFacesEnvironment;
-import org.jboss.test.qunit.Qunit;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-import com.google.common.collect.ImmutableList;
-
-(a)RunWith(Parameterized.class)
-public class MockConverterTest {
-
- @Rule
- public final Qunit qunit =
Qunit.builder().loadJsfResource("jquery.js").loadJsfResource("richfaces.js")
-
.loadJsfResource("richfaces-event.js").loadJsfResource("richfaces-csv.js",
"org.richfaces").loadJsfResource("resource.js").build();
-
- private final TestCriteria criteria;
-
- private MockFacesEnvironment facesEnvironment;
-
- public MockConverterTest(TestCriteria criteria) {
- this.criteria = criteria;
- }
-
- @Before
- public void setUp(){
- this.facesEnvironment = MockFacesEnvironment.createEnvironment();
- }
-
- @After
- public void tearDown(){
- this.facesEnvironment.release();
- this.facesEnvironment = null;
- }
- @Test
- public void testConvert() throws Exception {
-
- }
-
- @Parameters
- public static List<TestCriteria[]> getTestCriterias() {
- return ImmutableList.of(pass("true"), pass("ok"),
fail("123"));
- }
-
- private static TestCriteria[] pass(String string) {
- return new TestCriteria[] { new TestCriteria(string) };
- }
-
- private static TestCriteria[] fail(String string) {
- return new TestCriteria[] { new TestCriteria(string) };
- }
-}
Deleted:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/TestCriteria.java
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/TestCriteria.java 2010-12-08
16:16:00 UTC (rev 20466)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/TestCriteria.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -1,20 +0,0 @@
-package org.richfaces.convert;
-
-public class TestCriteria {
-
- private final String string;
-
- public TestCriteria(String string) {
- this.string = string;
-
- }
-
- /**
- * <p class="changed_added_4_0"></p>
- * @return the string
- */
- public String getString() {
- return string;
- }
-
-}
Added:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/MockTestBase.java
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/MockTestBase.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/MockTestBase.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -0,0 +1,90 @@
+package org.richfaces.javascript.client;
+
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+
+import org.jboss.test.faces.mock.MockFacesEnvironment;
+import org.jboss.test.qunit.Qunit;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+
+(a)RunWith(Parameterized.class)
+public abstract class MockTestBase {
+
+ @Rule
+ public final Qunit qunit =
Qunit.builder().loadJsfResource("jquery.js").loadJsfResource("richfaces.js")
+
.loadJsfResource("richfaces-event.js").loadJsfResource("csv.js",
"org.richfaces").build();
+ protected final TestCriteria criteria;
+ protected MockFacesEnvironment facesEnvironment;
+ protected UIComponent component;
+
+ public MockTestBase(TestCriteria criteria) {
+ this.criteria = criteria;
+ }
+
+ @Before
+ public void setUp() {
+ this.facesEnvironment = MockFacesEnvironment.createEnvironment();
+ component = facesEnvironment.createMock(UIComponent.class);
+ facesEnvironment.replay();
+ }
+
+ @After
+ public void tearDown() {
+ this.facesEnvironment.verify();
+ this.facesEnvironment.release();
+ this.facesEnvironment = null;
+ }
+
+ protected Object getErrorMessage() {
+ return ImmutableMap.of("detail","error");
+ }
+
+ protected Object getJavaScriptOptions() {
+ return criteria.getOptions();
+ }
+
+ protected abstract String getJavaScriptFunctionName();
+
+ protected static List<TestCriteria[]> options(TestCriteria ...criterias){
+ Builder<TestCriteria[]> builder = ImmutableList.builder();
+ for (TestCriteria testCriteria : criterias) {
+ builder.add(optionsArray(testCriteria));
+ }
+ return builder.build();
+ }
+
+ protected static TestCriteria pass(Object value) {
+ return new TestCriteria(value);
+ }
+
+ protected static TestCriteria pass(Object value, String option1, Object value1) {
+ TestCriteria testCriteria = pass(value);
+ Map<String, Object> options = testCriteria.getOptions();
+ options.put(option1, value1);
+ return testCriteria;
+ }
+
+ protected static TestCriteria pass(Object value, String option1, Object value1,
String option2, Object value2) {
+ TestCriteria testCriteria = pass(value);
+ Map<String, Object> options = testCriteria.getOptions();
+ options.put(option1, value1);
+ options.put(option2, value2);
+ return testCriteria;
+ }
+
+ private static TestCriteria[] optionsArray(TestCriteria testCriteria) {
+ return new TestCriteria[] { testCriteria };
+ }
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/MockTestBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/TestCriteria.java
(from rev 20466,
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/TestCriteria.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/TestCriteria.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/TestCriteria.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -0,0 +1,33 @@
+package org.richfaces.javascript.client;
+
+import java.util.Map;
+
+import com.google.common.collect.Maps;
+
+public class TestCriteria {
+
+ private final Object value;
+
+ private final Map<String, Object> options = Maps.newHashMap();
+
+ public TestCriteria(Object value) {
+ this.value = value;
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @return the string
+ */
+ public Object getValue() {
+ return value;
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @return the options
+ */
+ public Map<String, Object> getOptions() {
+ return options;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/TestCriteria.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/BooleanConverterTest.java
(from rev 20466,
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/convert/MockConverterTest.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/BooleanConverterTest.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/BooleanConverterTest.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -0,0 +1,31 @@
+package org.richfaces.javascript.client.converter;
+
+import java.util.List;
+
+import javax.faces.convert.BooleanConverter;
+import javax.faces.convert.Converter;
+
+import org.junit.runners.Parameterized.Parameters;
+import org.richfaces.javascript.client.TestCriteria;
+
+public class BooleanConverterTest extends ConverterTestBase {
+
+ public BooleanConverterTest(TestCriteria criteria) {
+ super(criteria);
+ }
+
+ @Override
+ protected Converter createConverter() {
+ return new BooleanConverter();
+ }
+
+ @Override
+ protected String getJavaScriptFunctionName() {
+ return "convertBoolean";
+ }
+
+ @Parameters
+ public static List<TestCriteria[]> getTestCriterias() {
+ return options(pass("true"), pass("ok"),
pass("123"),pass("0"),pass("1"));
+ }
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/BooleanConverterTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/ConverterTestBase.java
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/ConverterTestBase.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/ConverterTestBase.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -0,0 +1,50 @@
+package org.richfaces.javascript.client.converter;
+
+import static org.junit.Assert.*;
+
+
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+import org.ajax4jsf.javascript.JSFunction;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.richfaces.javascript.client.MockTestBase;
+import org.richfaces.javascript.client.TestCriteria;
+
+
+public abstract class ConverterTestBase extends MockTestBase {
+
+ public ConverterTestBase(TestCriteria criteria) {
+ super(criteria);
+ }
+
+ @Test
+ public void testConvert() throws Exception {
+ Converter converter = createConverter();
+ try {
+ Object convertedValue =
+ converter.getAsObject(facesEnvironment.getFacesContext(), component,
criteria.getValue().toString());
+ Object jsConvertedValue =
+ convertOnClient();
+ assertEquals(convertedValue, jsConvertedValue);
+ } catch (ConverterException e) {
+ // JSF conversion error - JavaScript should throw exception too.
+ try {
+ convertOnClient();
+ assertFalse("Client-side converted didn't throw exception for
value:"+criteria.getValue(), true);
+ } catch (Exception jsException){
+ // Test passed
+ }
+ }
+ }
+
+ protected Object convertOnClient() throws ConverterException {
+ JSFunction clientSideFunction = new JSFunction("RichFaces.csv." +
getJavaScriptFunctionName(),criteria.getValue(),getErrorMessage(),getJavaScriptOptions());
+ return qunit.runScript(clientSideFunction.toScript());
+ }
+
+ protected abstract Converter createConverter();
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/converter/ConverterTestBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/validator/ValidatorTestBase.java
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/validator/ValidatorTestBase.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/validator/ValidatorTestBase.java 2010-12-09
00:56:15 UTC (rev 20467)
@@ -0,0 +1,42 @@
+package org.richfaces.javascript.client.validator;
+
+import static org.junit.Assert.*;
+
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+import javax.validation.ValidationException;
+
+import org.junit.Test;
+import org.richfaces.javascript.client.MockTestBase;
+import org.richfaces.javascript.client.TestCriteria;
+
+public abstract class ValidatorTestBase extends MockTestBase {
+
+ public ValidatorTestBase(TestCriteria criteria) {
+ super(criteria);
+ }
+
+
+ @Test
+ public void testValidator() throws Exception {
+ Validator validator = createValidator();
+ try {
+ validator.validate(facesEnvironment.getFacesContext(), component,
criteria.getValue());
+ validateOnClient();
+ } catch (ValidatorException e) {
+ // client-side script has to throw exception too.
+ try {
+ validateOnClient();
+ assertFalse("JSF validator throws exception for value:
"+criteria.getValue(),true);
+ } catch (Exception e2) {
+ // both methods throws exceptions - it's ok.
+ }
+ }
+ }
+
+ protected void validateOnClient() throws ValidationException {
+
+ }
+
+ protected abstract Validator createValidator();
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/javascript/client/validator/ValidatorTestBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain