Skip to content

Commit bc4c514

Browse files
committed
Added integration tests for converting and validating using JSF
1 parent 223fe49 commit bc4c514

9 files changed

Lines changed: 215 additions & 1 deletion

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.convert;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
6+
import org.ocpsoft.prettyfaces.annotation.ForwardTo;
7+
import org.ocpsoft.prettyfaces.annotation.JSFConverter;
8+
import org.ocpsoft.prettyfaces.annotation.ParameterBinding;
9+
import org.ocpsoft.prettyfaces.annotation.URLPattern;
10+
11+
@ManagedBean
12+
@RequestScoped
13+
@URLPattern("/convert/{value}")
14+
@ForwardTo("/faces/convert.xhtml")
15+
public class JsfConverterBean
16+
{
17+
18+
@ParameterBinding
19+
@JSFConverter(converterId = "UppercaseConverter")
20+
private String value;
21+
22+
public String getValue()
23+
{
24+
return value;
25+
}
26+
27+
public void setValue(String value)
28+
{
29+
this.value = value;
30+
}
31+
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.convert;
2+
3+
import static junit.framework.Assert.assertTrue;
4+
5+
import org.jboss.arquillian.container.test.api.Deployment;
6+
import org.jboss.arquillian.junit.Arquillian;
7+
import org.jboss.shrinkwrap.api.spec.WebArchive;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.ocpsoft.prettyfaces.test.PrettyFacesTestBase;
11+
12+
@RunWith(Arquillian.class)
13+
public class JsfConverterTest extends PrettyFacesTestBase
14+
{
15+
16+
@Deployment(testable = false)
17+
public static WebArchive getDeployment()
18+
{
19+
return getBaseDeployment()
20+
.addClass(JsfConverterBean.class)
21+
.addClass(UppercaseConverter.class)
22+
.addAsWebResource("basic/convert.xhtml", "convert.xhtml");
23+
}
24+
25+
@Test
26+
public void testConvert() throws Exception
27+
{
28+
String page = getPageAsString("/convert/hello");
29+
assertTrue(page.contains("Parameter = [HELLO]"));
30+
}
31+
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.convert;
2+
3+
import javax.faces.component.UIComponent;
4+
import javax.faces.context.FacesContext;
5+
import javax.faces.convert.Converter;
6+
import javax.faces.convert.FacesConverter;
7+
8+
@FacesConverter("UppercaseConverter")
9+
public class UppercaseConverter implements Converter
10+
{
11+
12+
@Override
13+
public Object getAsObject(FacesContext context, UIComponent component, String value)
14+
{
15+
if (value != null) {
16+
return value.toUpperCase();
17+
}
18+
return null;
19+
}
20+
21+
@Override
22+
public String getAsString(FacesContext context, UIComponent component, Object value)
23+
{
24+
return value.toString();
25+
}
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.validate;
2+
3+
import java.util.ArrayList;
4+
5+
import javax.faces.application.FacesMessage;
6+
import javax.faces.component.UIComponent;
7+
import javax.faces.context.FacesContext;
8+
import javax.faces.validator.FacesValidator;
9+
import javax.faces.validator.Validator;
10+
import javax.faces.validator.ValidatorException;
11+
12+
@FacesValidator("EvenLengthValidator")
13+
public class EvenLengthValidator implements Validator
14+
{
15+
16+
@Override
17+
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
18+
{
19+
if (value == null || value.toString().length() % 2 != 0) {
20+
throw new ValidatorException(new ArrayList<FacesMessage>());
21+
}
22+
}
23+
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.validate;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
6+
import org.ocpsoft.prettyfaces.annotation.ForwardTo;
7+
import org.ocpsoft.prettyfaces.annotation.JSFValidator;
8+
import org.ocpsoft.prettyfaces.annotation.ParameterBinding;
9+
import org.ocpsoft.prettyfaces.annotation.URLPattern;
10+
11+
@ManagedBean
12+
@RequestScoped
13+
@URLPattern("/validate/{value}")
14+
@ForwardTo("/faces/validate.xhtml")
15+
public class JsfValidatorBean
16+
{
17+
18+
@ParameterBinding
19+
@JSFValidator(validatorId = "EvenLengthValidator")
20+
private String value;
21+
22+
public String getValue()
23+
{
24+
return value;
25+
}
26+
27+
public void setValue(String value)
28+
{
29+
this.value = value;
30+
}
31+
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 JsfValidatorTest extends PrettyFacesTestBase
19+
{
20+
21+
@Deployment(testable = false)
22+
public static WebArchive getDeployment()
23+
{
24+
return getBaseDeployment()
25+
.addClass(JsfValidatorBean.class)
26+
.addClass(EvenLengthValidator.class)
27+
.addAsWebResource("basic/validate.xhtml", "validate.xhtml");
28+
}
29+
30+
@Test
31+
public void testValidationSuccess() throws Exception
32+
{
33+
String page = getPageAsString("/validate/abcd");
34+
assertTrue(page.contains("Parameter = [abcd]"));
35+
}
36+
37+
@Test
38+
public void testValidationFailed() throws Exception
39+
{
40+
HttpClient client = new DefaultHttpClient();
41+
HttpGet get = new HttpGet(getFullUrl("/validate/abc"));
42+
HttpResponse response = client.execute(get);
43+
assertEquals(404, response.getStatusLine().getStatusCode());
44+
}
45+
46+
}
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 = [#{jsfConverterBean.value}]</p>
6+
7+
</body>
8+
</html>
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 = [#{jsfValidatorBean.value}]</p>
6+
7+
</body>
8+
</html>

‎test-harness/src/main/java/org/ocpsoft/prettyfaces/test/PrettyFacesTestBase.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ protected static JavaArchive getPrettyFacesArchive()
7777
protected String getPageAsString(String path) throws IOException
7878
{
7979
HttpClient client = new DefaultHttpClient();
80-
HttpGet get = new HttpGet("http://localhost:9090/prettyfaces-test" + path);
80+
HttpGet get = new HttpGet(getFullUrl(path));
8181
HttpResponse response = client.execute(get);
8282
String page = EntityUtils.toString(response.getEntity());
8383
return page;
8484
}
8585

86+
protected String getFullUrl(String path)
87+
{
88+
return "http://localhost:9090/prettyfaces-test" + path;
89+
}
90+
8691
}

0 commit comments

Comments
 (0)