Author: alexsmirnov
Date: 2010-10-25 02:24:28 -0400 (Mon, 25 Oct 2010)
New Revision: 19661
Modified:
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
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/RendererGetOrCreateResourceTest.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/ValidatorRendererGetScriptTest.java
Log:
CODING IN PROGRESS - issue RF-9507: ClientValidatorRendererunit tests and implementation.
https://jira.jboss.org/browse/RF-9507
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-25
01:02:43 UTC (rev 19660)
+++
branches/RF-8742/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientValidatorRenderer.java 2010-10-25
06:24:28 UTC (rev 19661)
@@ -134,7 +134,7 @@
if (null != converter) {
try {
LibraryScriptFunction clientSideConverterScript =
- getClientSideConverterScript(behaviorContext, converter);
+
getClientSideConverterScript(behaviorContext.getFacesContext(), converter);
return createValidatorScript(behaviorContext, behavior,
validators, clientSideConverterScript);
} catch (ScriptNotFoundException e) {
// ajax-only validation
@@ -155,7 +155,7 @@
private ComponentValidatorScript createValidatorScript(ClientBehaviorContext
behaviorContext,
ClientValidatorBehavior behavior, Collection<ValidatorDescriptor>
validators,
LibraryScriptFunction clientSideConverterScript) {
- Collection<LibraryScriptFunction> validatorScripts =
getClientSideValidatorScript(behaviorContext, validators);
+ Collection<LibraryScriptFunction> validatorScripts =
getClientSideValidatorScript(behaviorContext.getFacesContext(), validators);
if (validatorScripts.isEmpty()) {
return new AjaxOnlyScript();
} else if (validatorScripts.size() < validators.size()) {
@@ -185,10 +185,10 @@
* @return
* @throws ScriptNotFoundException
*/
- LibraryScriptFunction getClientSideConverterScript(ClientBehaviorContext
behaviorContext,
+ LibraryScriptFunction getClientSideConverterScript(FacesContext facesContext,
ConverterDescriptor converter) throws ScriptNotFoundException {
ClientScriptService clientScriptService =
- ServiceTracker.getService(behaviorContext.getFacesContext(),
ClientScriptService.class);
+ ServiceTracker.getService(facesContext, ClientScriptService.class);
return createClientFunction(converter, VALUE_LITERAL, clientScriptService);
}
@@ -203,15 +203,15 @@
* Build client-side function call for Server-side component descriptor.
* </p>
*
- * @param behaviorContext
+ * @param facesContext
* @param validators
* @return
* @throws ScriptNotFoundException
*/
- Collection<LibraryScriptFunction>
getClientSideValidatorScript(ClientBehaviorContext behaviorContext,
+ Collection<LibraryScriptFunction> getClientSideValidatorScript(FacesContext
facesContext,
Collection<ValidatorDescriptor> validators) {
ClientScriptService clientScriptService =
- ServiceTracker.getService(behaviorContext.getFacesContext(),
ClientScriptService.class);
+ ServiceTracker.getService(facesContext, ClientScriptService.class);
List<LibraryScriptFunction> scripts = Lists.newArrayList();
for (ValidatorDescriptor validator : validators) {
try {
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-25
01:02:43 UTC (rev 19660)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetClientSideScriptTest.java 2010-10-25
06:24:28 UTC (rev 19661)
@@ -1,7 +1,8 @@
package org.richfaces.renderkit.html;
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.*;
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Collections;
@@ -41,10 +42,13 @@
@Mock
private LibraryScript script;
+ private Collection<ValidatorDescriptor> descriptors;
+
@Before
public void setupService() {
-
expect(factory.getInstance(ClientScriptService.class)).andStubReturn(scriptService);
+ expect(factory.getInstance(ClientScriptService.class)).andReturn(scriptService);
ServiceTracker.setFactory(factory);
+ descriptors = Collections.singleton(descriptor);
}
@After
@@ -52,12 +56,15 @@
ServiceTracker.release();
}
- @Test(expected = ScriptNotFoundException.class)
+ @Test()
public void testGetClientSideScriptNotExists() throws Throwable {
expect((Class) descriptor.getValidatorClass()).andReturn(RegexValidator.class);
expect(scriptService.getScript(RegexValidator.class)).andThrow(new
ScriptNotFoundException());
+
controller.replay();
- renderer.getClientSideValidatorScript(behaviorContext,
Collections.singletonList(descriptor));
+ Collection<LibraryScriptFunction> clientSideValidatorScript =
renderer.getClientSideValidatorScript(
+ environment.getFacesContext(), descriptors);
+ assertTrue(clientSideValidatorScript.isEmpty());
controller.verify();
}
@@ -66,7 +73,7 @@
expect((Class)
converterDescriptor.getValidatorClass()).andReturn(NumberConverter.class);
expect(scriptService.getScript(NumberConverter.class)).andThrow(new
ScriptNotFoundException());
controller.replay();
- renderer.getClientSideConverterScript(behaviorContext, converterDescriptor);
+ renderer.getClientSideConverterScript(environment.getFacesContext(),
converterDescriptor);
controller.verify();
}
@@ -76,21 +83,19 @@
expect(descriptor.getMessage()).andReturn(VALIDATOR_MESSAGE);
expect((Map<String, Object>)
descriptor.getValidatorParameters()).andReturn(
(Map<String, Object>) VALIDATOR_PARAMS);
- controller.replay();
expect(scriptService.getScript(RegexValidator.class)).andReturn(script);
expect(script.getName()).andReturn(REGEX_VALIDATOR);
expect(script.getLibrary()).andReturn(ORG_RICHFACES);
expect(script.getResourceName()).andReturn(CLIENT_VALIDATORS_JS);
- Collection<LibraryScriptFunction> clientSideScriptCollection =
renderer.getClientSideValidatorScript(behaviorContext,
Collections.singletonList(descriptor));
-
- assertNotNull(clientSideScriptCollection);
- LibraryScriptFunction clientSideScript =
Iterables.getOnlyElement(clientSideScriptCollection);
-
- assertEquals(ClientValidatorRenderer.CONVERTED_VALUE_VAR,
clientSideScript.getParameters().get(0));
+ controller.replay();
+ Collection<LibraryScriptFunction> clientSideScripts =
renderer.getClientSideValidatorScript(environment.getFacesContext(), descriptors);
+ LibraryScriptFunction clientSideScript =
Iterables.getOnlyElement(clientSideScripts);
+ assertEquals(ClientValidatorRenderer.CONVERTED_VALUE_LITERAL,
clientSideScript.getParameters().get(0));
assertEquals(VALIDATOR_MESSAGE, clientSideScript.getParameters().get(1));
assertEquals(VALIDATOR_PARAMS, clientSideScript.getParameters().get(2));
assertEquals(ORG_RICHFACES, clientSideScript.getLibrary());
assertEquals(CLIENT_VALIDATORS_JS, clientSideScript.getResourceName());
+ controller.verify();
}
@Test
@@ -99,18 +104,19 @@
expect(converterDescriptor.getMessage()).andReturn(VALIDATOR_MESSAGE);
expect((Map<String, Object>)
converterDescriptor.getValidatorParameters()).andReturn(
(Map<String, Object>) VALIDATOR_PARAMS);
- controller.replay();
expect(scriptService.getScript(NumberConverter.class)).andReturn(script);
expect(script.getName()).andReturn(REGEX_VALIDATOR);
expect(script.getLibrary()).andReturn(ORG_RICHFACES);
expect(script.getResourceName()).andReturn(CLIENT_VALIDATORS_JS);
+ controller.replay();
LibraryScriptFunction clientSideScript =
- renderer.getClientSideConverterScript(behaviorContext, converterDescriptor);
- assertEquals(ClientValidatorRenderer.VALUE_VAR,
clientSideScript.getParameters().get(0));
+ renderer.getClientSideConverterScript(environment.getFacesContext(),
converterDescriptor);
+ assertEquals(ClientValidatorRenderer.VALUE_LITERAL,
clientSideScript.getParameters().get(0));
assertEquals(VALIDATOR_MESSAGE, clientSideScript.getParameters().get(1));
assertEquals(VALIDATOR_PARAMS, clientSideScript.getParameters().get(2));
assertEquals(ORG_RICHFACES, clientSideScript.getLibrary());
assertEquals(CLIENT_VALIDATORS_JS, clientSideScript.getResourceName());
+ controller.verify();
}
}
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-25
01:02:43 UTC (rev 19660)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetComponentScriptTest.java 2010-10-25
06:24:28 UTC (rev 19661)
@@ -1,16 +1,18 @@
package org.richfaces.renderkit.html;
-import static org.easymock.EasyMock.*;
-import static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assert.*;
-import static org.junit.matchers.JUnitMatchers.*;
+import static org.easymock.EasyMock.expect;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.containsString;
+import static org.junit.matchers.JUnitMatchers.hasItem;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
-import javax.faces.component.behavior.ClientBehaviorContext;
+import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.NumberConverter;
import javax.validation.constraints.Max;
@@ -30,6 +32,8 @@
import org.richfaces.validator.ScriptNotFoundException;
import org.richfaces.validator.ValidatorDescriptor;
+import com.google.common.collect.Lists;
+
@RunWith(MockTestRunner.class)
public class RendererGetComponentScriptTest extends RendererTestBase {
@@ -224,10 +228,9 @@
final LibraryScriptFunction... validatorFunctions) {
return new ClientValidatorRenderer() {
- private int validatorsCounter = 0;
@Override
- LibraryScriptFunction getClientSideConverterScript(ClientBehaviorContext
behaviorContext,
+ LibraryScriptFunction getClientSideConverterScript(FacesContext
facesContext,
ConverterDescriptor converter) throws ScriptNotFoundException {
if (null == converterFunction) {
throw new ScriptNotFoundException();
@@ -236,12 +239,9 @@
}
@Override
- Collection<LibraryScriptFunction>
getClientSideValidatorScript(ClientBehaviorContext behaviorContext,
- Collection<ValidatorDescriptor> validators) {
-// if (validatorsCounter >= validatorFunctions.length) {
-// throw new ScriptNotFoundException();
-// }
- return Arrays.asList(validatorFunctions);
+ Collection<LibraryScriptFunction>
getClientSideValidatorScript(FacesContext facesContext,
+ Collection<ValidatorDescriptor> validators) {
+ return Lists.newArrayList(validatorFunctions);
}
};
}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetOrCreateResourceTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetOrCreateResourceTest.java 2010-10-25
01:02:43 UTC (rev 19660)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/RendererGetOrCreateResourceTest.java 2010-10-25
06:24:28 UTC (rev 19661)
@@ -12,6 +12,8 @@
import org.jboss.test.faces.mock.Mock;
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 org.richfaces.component.UIValidatorScript;
@@ -26,61 +28,81 @@
@Mock
private UIViewRoot viewRoot;
-
+
@Mock
private ComponentValidatorScript validatorScript;
-
+
private UIValidatorScript scriptResource;
+
+ @Before
+ public void setUpResource(){
+ scriptResource = new UIValidatorScript();
+ }
+ @After
+ public void cleanUpResource(){
+ scriptResource = null;
+ }
/**
- * <p class="changed_added_4_0">No resource exist in view, create a
new one and store in "form" target.</p>
+ * <p class="changed_added_4_0">
+ * No resource exist in view, create a new one and store in "form" target.
+ * </p>
*/
@Test
public void testCreateValidatorScriptResource() {
- FacesContext facesContext = recordResources(null,null);
-
expect(environment.getApplication().createComponent(UIValidatorScript.COMPONENT_TYPE)).andReturn(scriptResource);
- viewRoot.addComponentResource(facesContext, scriptResource);expectLastCall();
+ FacesContext facesContext = recordResources(null, null);
+
expect(environment.getApplication().createComponent(UIValidatorScript.COMPONENT_TYPE))
+ .andReturn(scriptResource);
+ viewRoot.addComponentResource(facesContext, scriptResource);
+ expectLastCall();
verifyResult(facesContext);
}
- private FacesContext recordResources(UIComponent formResource,UIComponent
bodyResource) {
+ private FacesContext recordResources(UIComponent formResource, UIComponent
bodyResource) {
FacesContext facesContext = recordViewRoot();
recordViewResources("form", formResource);
- recordViewResources("body", bodyResource);
+ if(null == formResource || null != bodyResource){
+ recordViewResources("body", bodyResource);
+ }
return facesContext;
}
private void verifyResult(FacesContext facesContext) {
controller.replay();
-
assertSame(scriptResource,renderer.getOrCreateValidatorScriptResource(facesContext));
+ assertSame(scriptResource,
renderer.getOrCreateValidatorScriptResource(facesContext));
controller.verify();
}
/**
- * <p class="changed_added_4_0">Resource already exists in
"form" target</p>
+ * <p class="changed_added_4_0">
+ * Resource already exists in "form" target
+ * </p>
*/
@Test
public void testGetValidatorScriptResourceForm() {
- FacesContext facesContext = recordResources(scriptResource,null);
+ FacesContext facesContext = recordResources(scriptResource, null);
verifyResult(facesContext);
}
/**
- * <p class="changed_added_4_0">Resource already exists in
"body" target</p>
+ * <p class="changed_added_4_0">
+ * Resource already exists in "body" target
+ * </p>
*/
@Test
public void testGetValidatorScriptResourceBody() {
- FacesContext facesContext = recordResources(null,scriptResource);
+ FacesContext facesContext = recordResources(null, scriptResource);
verifyResult(facesContext);
}
-
+
@Test
public void buildAndStoreScript() throws Exception {
- FacesContext facesContext = recordResources(scriptResource,null);
- ClientValidatorRenderer renderer = new ClientValidatorRenderer(){
- ComponentValidatorScript createValidatorScript(ClientBehaviorContext
behaviorContext, ClientValidatorBehavior behavior) {
- return validatorScript;
- };
+ FacesContext facesContext = recordResources(scriptResource, null);
+ ClientValidatorRenderer renderer = new ClientValidatorRenderer() {
+ ComponentValidatorScript createValidatorScript(ClientBehaviorContext
behaviorContext,
+ ClientValidatorBehavior behavior) {
+ return validatorScript;
+ };
};
setupBehaviorContext(input);
expect(input.getClientId(facesContext)).andReturn(FUNCTION_NAME);
@@ -89,15 +111,14 @@
assertEquals(1, scriptResource.getScripts().size());
controller.verify();
}
-
-
- private void recordViewResources(String target, UIComponent resource){
+
+ private void recordViewResources(String target, UIComponent resource) {
FacesContext facesContext = environment.getFacesContext();
List<UIComponent> resources = Lists.newArrayList();
- if(null != resource){
+ if (null != resource) {
resources.add(resource);
}
- expect(viewRoot.getComponentResources(facesContext,
target)).andStubReturn(resources);
+ expect(viewRoot.getComponentResources(facesContext,
target)).andReturn(resources);
}
private FacesContext recordViewRoot() {
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/ValidatorRendererGetScriptTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/ValidatorRendererGetScriptTest.java 2010-10-25
01:02:43 UTC (rev 19660)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/renderkit/html/ValidatorRendererGetScriptTest.java 2010-10-25
06:24:28 UTC (rev 19661)
@@ -26,6 +26,6 @@
@Test(expected=FacesException.class)
public void testGetScriptWrongBehavior() {
- renderer.getScript(null, controller.createNiceMock(ClientBehavior.class));
+ renderer.getScript(behaviorContext,
controller.createNiceMock(ClientBehavior.class));
}
}