Skip to content

Commit f843e40

Browse files
committed
Integration test for method based validation
1 parent 7864f27 commit f843e40

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.validate;
2+
3+
import javax.faces.application.FacesMessage;
4+
import javax.faces.bean.ManagedBean;
5+
import javax.faces.bean.RequestScoped;
6+
import javax.faces.component.UIComponent;
7+
import javax.faces.context.FacesContext;
8+
import javax.faces.validator.ValidatorException;
9+
10+
import org.ocpsoft.prettyfaces.annotation.ForwardTo;
11+
import org.ocpsoft.prettyfaces.annotation.JSFValidator;
12+
import org.ocpsoft.prettyfaces.annotation.ParameterBinding;
13+
import org.ocpsoft.prettyfaces.annotation.URLPattern;
14+
15+
@ManagedBean
16+
@RequestScoped
17+
@URLPattern("/validate/{value}")
18+
@ForwardTo("/faces/method-validation.xhtml")
19+
public class MethodValidationBean
20+
{
21+
22+
@ParameterBinding
23+
@JSFValidator(validateWith = "#{methodValidationBean.validate}")
24+
private String value;
25+
26+
public void validate(FacesContext context, UIComponent comp, Object value)
27+
{
28+
if (value.toString().contains("666")) {
29+
throw new ValidatorException(new FacesMessage());
30+
}
31+
}
32+
33+
public String getValue()
34+
{
35+
return value;
36+
}
37+
38+
public void setValue(String value)
39+
{
40+
this.value = value;
41+
}
42+
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.validate;
2+
3+
import static junit.framework.Assert.assertTrue;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.client.HttpClient;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.impl.client.DefaultHttpClient;
10+
import org.jboss.arquillian.container.test.api.Deployment;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.shrinkwrap.api.spec.WebArchive;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.ocpsoft.prettyfaces.test.PrettyFacesTestBase;
16+
17+
@RunWith(Arquillian.class)
18+
public class MethodValidationTest extends PrettyFacesTestBase
19+
{
20+
21+
@Deployment(testable = false)
22+
public static WebArchive getDeployment()
23+
{
24+
return getBaseDeployment()
25+
.addClass(MethodValidationBean.class)
26+
.addAsWebResource("basic/method-validation.xhtml", "method-validation.xhtml");
27+
}
28+
29+
@Test
30+
public void testValidationMethodSuccess() throws Exception
31+
{
32+
String page = getPageAsString("/validate/test123");
33+
assertTrue(page.contains("Parameter = [test123]"));
34+
}
35+
36+
@Test
37+
public void testValidationMethodFailed() throws Exception
38+
{
39+
HttpClient client = new DefaultHttpClient();
40+
HttpGet get = new HttpGet(getFullUrl("/validate/test666"));
41+
HttpResponse response = client.execute(get);
42+
assertEquals(404, response.getStatusLine().getStatusCode());
43+
}
44+
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets">
3+
<body>
4+
5+
<p>Parameter = [#{methodValidationBean.value}]</p>
6+
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)