Repository: deltaspike Updated Branches: refs/heads/master 0f41aff13 -> 2708723b1
DELTASPIKE-725 - Test for validator with parameters Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/2708723b Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/2708723b Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/2708723b Branch: refs/heads/master Commit: 2708723b10b644da49c47551e173a15f5bf54355 Parents: 0f41aff Author: Rafael Benevides <[email protected]> Authored: Fri Oct 17 15:24:22 2014 -0300 Committer: Rafael Benevides <[email protected]> Committed: Mon Oct 20 08:40:09 2014 -0200 ---------------------------------------------------------------------- deltaspike/modules/jsf/impl/pom.xml | 7 +- .../jsf/impl/injection/uc004/AnotherBean.java | 68 ++++++++++++++ .../injection/uc004/AnotherBeanConverter.java | 63 +++++++++++++ .../injection/uc004/InjectionDroneTest.java | 97 ++++++++++++++++++++ .../test/jsf/impl/injection/uc004/MyBean.java | 63 +++++++++++++ .../impl/injection/uc004/MyBeanValidator.java | 91 ++++++++++++++++++ .../test/jsf/impl/util/ArchiveUtils.java | 1 + .../injection/testValidatorTagParameter.xhtml | 41 +++++++++ 8 files changed, 430 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/pom.xml b/deltaspike/modules/jsf/impl/pom.xml index f9432a6..cd41904 100644 --- a/deltaspike/modules/jsf/impl/pom.xml +++ b/deltaspike/modules/jsf/impl/pom.xml @@ -141,7 +141,12 @@ <type>pom</type> <scope>test</scope> </dependency> - + <dependency> + <groupId>org.javassist</groupId> + <artifactId>javassist</artifactId> + <version>3.18.2-GA</version> + <scope>test</scope> + </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBean.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBean.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBean.java new file mode 100644 index 0000000..9bde6a9 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBean.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.jsf.impl.injection.uc004; + +public class AnotherBean +{ + + private String value; + + public AnotherBean() + { + + } + + public AnotherBean(String value) + { + this.value = value; + } + + public String getValue() + { + return value; + } + + public void setValue(String value) + { + this.value = value; + } + + public Object getAsObject(String value) + { + if (value != null && !value.isEmpty()) + { + return new AnotherBean(value); + } + else + { + return null; + } + } + + public String getAsString(Object object) + { + if (object instanceof AnotherBean) + { + AnotherBean myValue = (AnotherBean) object; + return myValue.getValue(); + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBeanConverter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBeanConverter.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBeanConverter.java new file mode 100644 index 0000000..48fe153 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/AnotherBeanConverter.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.jsf.impl.injection.uc004; + +import javax.faces.application.FacesMessage; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; +import javax.faces.convert.FacesConverter; +import javax.inject.Inject; + +@FacesConverter("myValueConveter") +public class AnotherBeanConverter implements Converter +{ + + @Inject + private AnotherBean myValue; + + @Override + public Object getAsObject(FacesContext context, UIComponent component, + String value) + { + try + { + if (value == null || value.isEmpty()) + { + return null; + } + Integer.parseInt(value); + return myValue.getAsObject(value); + } + catch (NumberFormatException e) + { + throw new ConverterException(new FacesMessage("Value is not an Integer")); + } + + } + + @Override + public String getAsString(FacesContext context, UIComponent component, + Object value) + { + return myValue.getAsString(value); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/InjectionDroneTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/InjectionDroneTest.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/InjectionDroneTest.java new file mode 100644 index 0000000..ff999a4 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/InjectionDroneTest.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.jsf.impl.injection.uc004; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.deltaspike.test.category.WebProfileCategory; +import org.apache.deltaspike.test.jsf.impl.util.ArchiveUtils; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.drone.api.annotation.Drone; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.arquillian.warp.WarpTest; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; + +@WarpTest +@RunWith(Arquillian.class) +@Category(WebProfileCategory.class) +public class InjectionDroneTest +{ + @Drone + private WebDriver driver; + + @ArquillianResource + private URL contextPath; + + @Deployment + public static WebArchive deploy() + { + return ShrinkWrap + .create(WebArchive.class, "injection-uc004.war") + .addPackage(MyBean.class.getPackage()) + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJsfArchive()) + .addAsLibraries(ArchiveUtils.getDeltaSpikeSecurityArchive()) + .addAsWebResource("injection/testValidatorTagParameter.xhtml", "/testValidatorConverter.xhtml") + .addAsWebInfResource("META-INF/test.taglib.xml", "classes/META-INF/test.taglib.xml") + .addAsWebInfResource("default/WEB-INF/web.xml", "web.xml") + .addAsWebInfResource("default/WEB-INF/faces-config.xml", "faces-config.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + @RunAsClient + public void testValidator() throws MalformedURLException + { + driver.get(new URL(contextPath, "testValidatorConverter.xhtml").toString()); + System.out.println("Antes do Click:" + driver.getPageSource()); + WebElement convertedValue = driver.findElement(By.id("validator:stringValue")); + convertedValue.sendKeys("DeltaSpike"); + WebElement testConveterButton = driver.findElement(By.id("validator:testValidatorButton")); + testConveterButton.click(); + System.out.println("*****************************"); + System.out.println("Depois do Click:" + driver.getPageSource()); + Assert.assertTrue(ExpectedConditions.textToBePresentInElement(By.id("messages"), "Worked").apply(driver)); + } + + @Test + @RunAsClient + public void testValidatorWithError() throws MalformedURLException + { + driver.get(new URL(contextPath, "testValidatorConverter.xhtml").toString()); + WebElement convertedValue = driver.findElement(By.id("validator:stringValue")); + convertedValue.sendKeys("Wrong Value"); + WebElement testConveterButton = driver.findElement(By.id("validator:testValidatorButton")); + testConveterButton.click(); + Assert.assertTrue(ExpectedConditions.textToBePresentInElement(By.id("validator:errorMessage"), + "The valid value should be DeltaSpike").apply(driver)); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBean.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBean.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBean.java new file mode 100644 index 0000000..e978bdd --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBean.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.jsf.impl.injection.uc004; + +import javax.enterprise.inject.Model; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; + +@Model +public class MyBean +{ + + private String stringValue; + + private AnotherBean convertedValue; + + public String getStringValue() + { + return stringValue; + } + + public void setStringValue(String stringValue) + { + this.stringValue = stringValue; + } + + public AnotherBean getConvertedValue() + { + return convertedValue; + } + + public void setConvertedValue(AnotherBean convertedValue) + { + this.convertedValue = convertedValue; + } + + public void testAction() + { + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Worked")); + } + + public boolean isValid(String value, String validValue) + { + return validValue.equals(value); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBeanValidator.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBeanValidator.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBeanValidator.java new file mode 100644 index 0000000..c0148af --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/injection/uc004/MyBeanValidator.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.jsf.impl.injection.uc004; + +import javax.faces.application.FacesMessage; +import javax.faces.component.StateHolder; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.validator.FacesValidator; +import javax.faces.validator.Validator; +import javax.faces.validator.ValidatorException; +import javax.inject.Inject; + +@FacesValidator("myBeanValidator") +public class MyBeanValidator implements Validator, StateHolder +{ + + @Inject + private MyBean myBean; + + private String validValue = "Apache"; + + private boolean isTransient; + + public void setValidValue(String validValue) + { + this.validValue = validValue; + } + + public String getValidValue() + { + return validValue; + } + + @Override + public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException + { + if (value instanceof String) + { + String valueAString = (String) value; + if (!myBean.isValid(valueAString, validValue)) + { + throw new ValidatorException(new FacesMessage("The valid value should be " + validValue)); + } + + } + } + + @Override + public Object saveState(FacesContext context) + { + return this.validValue; + } + + @Override + public void restoreState(FacesContext context, Object state) + { + this.validValue = (String) state; + + } + + @Override + public boolean isTransient() + { + return isTransient; + } + + @Override + public void setTransient(boolean newTransientValue) + { + this.isTransient = newTransientValue; + + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/util/ArchiveUtils.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/util/ArchiveUtils.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/util/ArchiveUtils.java index acbdf26..24c057c 100644 --- a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/util/ArchiveUtils.java +++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/util/ArchiveUtils.java @@ -53,6 +53,7 @@ public class ArchiveUtils .addPackages(true, "org.jboss.arquillian.graphene") .addPackages(true, "org.jboss.arquillian.ajocado") .addPackages(true, "org.openqa.selenium") + .addPackages(true, "javassist") .addPackage(WebProfileCategory.class.getPackage()); JavaArchive[] coreArchives = ShrinkWrapArchiveUtil.getArchives(null http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2708723b/deltaspike/modules/jsf/impl/src/test/resources/injection/testValidatorTagParameter.xhtml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/test/resources/injection/testValidatorTagParameter.xhtml b/deltaspike/modules/jsf/impl/src/test/resources/injection/testValidatorTagParameter.xhtml new file mode 100644 index 0000000..dca130d --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/test/resources/injection/testValidatorTagParameter.xhtml @@ -0,0 +1,41 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + xmlns:d="http://deltaspike.apache.org/tags"> + +<h:head></h:head> +<body> + <h:messages id="messages" globalOnly="true" /> + <br /> + <h:form id="validator"> + Value String: + <h:inputText id="stringValue" value="#{myBean.stringValue}"> + <d:myBeanValidator validValue="DeltaSpike"/> + </h:inputText> + <h:message id="errorMessage" for="stringValue" /> + <br /> + <h:commandButton id="testValidatorButton" + action="#{myBean.testAction()}" value="Test Validator" /> + </h:form> +</body> +</html>
