Skip to content

Commit 1992fde

Browse files
committed
Some more tests for dynaview
1 parent d4b8e06 commit 1992fde

7 files changed

Lines changed: 208 additions & 56 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ocpsoft.pretty.faces.test.dynaview;
2+
3+
import javax.faces.application.FacesMessage;
4+
import javax.faces.component.UIComponent;
5+
import javax.faces.context.FacesContext;
6+
import javax.faces.validator.FacesValidator;
7+
import javax.faces.validator.Validator;
8+
import javax.faces.validator.ValidatorException;
9+
10+
@FacesValidator(value = "DynaViewParameterValidator")
11+
public class DynaViewParameterValidator implements Validator
12+
{
13+
14+
@Override
15+
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
16+
{
17+
if (value != null && value.toString().equals("invalid")) {
18+
throw new ValidatorException(new FacesMessage("Invalid"));
19+
}
20+
}
21+
22+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.ocpsoft.pretty.faces.test.dynaview;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import org.jboss.arquillian.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.jsfunit.jsfsession.JSFClientSession;
9+
import org.jboss.jsfunit.jsfsession.JSFSession;
10+
import org.jboss.shrinkwrap.api.spec.WebArchive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import com.ocpsoft.pretty.faces.test.PrettyFacesTestBase;
15+
16+
@RunWith(Arquillian.class)
17+
public class ParameterizedDynaViewTest extends PrettyFacesTestBase
18+
{
19+
20+
@Deployment
21+
public static WebArchive createDeployment()
22+
{
23+
return PrettyFacesTestBase.createDeployment()
24+
.addClass(PathParamDynaViewBean.class)
25+
.addClass(QueryParamDynaViewBean.class)
26+
.addClass(DynaViewParameterValidator.class)
27+
.addResource("dynaview/index.xhtml", "index.xhtml")
28+
.addResource("dynaview/correct.xhtml", "correct.xhtml");
29+
}
30+
31+
@Test
32+
public void testPathParamInjectionHappensBeforeViewDetermination() throws Exception
33+
{
34+
35+
// First visit the start page
36+
JSFSession jsfSession = new JSFSession("/index.jsf");
37+
38+
// click on the link talking the user to /pathparam/correct
39+
JSFClientSession client = jsfSession.getJSFClientSession();
40+
client.click("pathParamLink");
41+
42+
// the dynaview code should send the user to the correct page.
43+
assertTrue(client.getPageAsText().contains("The parameter was correctly injected"));
44+
45+
}
46+
47+
@Test
48+
public void testQueryParamInjectionHappensBeforeViewDetermination() throws Exception
49+
{
50+
51+
// First visit the start page
52+
JSFSession jsfSession = new JSFSession("/index.jsf");
53+
54+
// click on the link talking the user to /queryparam?param=correct
55+
JSFClientSession client = jsfSession.getJSFClientSession();
56+
client.click("queryParamLink");
57+
58+
// the dynaview code should send the user to the correct page.
59+
assertTrue(client.getPageAsText().contains("The parameter was correctly injected"));
60+
61+
}
62+
63+
@Test
64+
public void testInvalidPathParamWithDynaView() throws Exception
65+
{
66+
67+
// First visit the start page
68+
JSFSession jsfSession = new JSFSession("/index.jsf");
69+
jsfSession.getWebClient().setThrowExceptionOnFailingStatusCode(false);
70+
71+
// click on the link talking the user to /pathparam/invalid
72+
JSFClientSession client = jsfSession.getJSFClientSession();
73+
client.click("invalidPathParamLink");
74+
75+
// the dynaview code should send the user to the correct page.
76+
assertEquals(404, client.getContentPage().getWebResponse().getStatusCode());
77+
78+
}
79+
80+
@Test
81+
public void testInvalidQueryParamWithDynaView() throws Exception
82+
{
83+
84+
// First visit the start page
85+
JSFSession jsfSession = new JSFSession("/index.jsf");
86+
jsfSession.getWebClient().setThrowExceptionOnFailingStatusCode(false);
87+
88+
// click on the link talking the user to /queryparam?param=invalid
89+
JSFClientSession client = jsfSession.getJSFClientSession();
90+
client.click("invalidQueryParamLink");
91+
92+
// the dynaview code should send the user to the correct page.
93+
assertEquals(404, client.getContentPage().getWebResponse().getStatusCode());
94+
95+
}
96+
97+
}

‎tests-jsf2/src/test/java/com/ocpsoft/pretty/faces/test/dynaview/PathParamDynaViewBean.java‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@
44
import javax.faces.bean.RequestScoped;
55

66
import com.ocpsoft.pretty.faces.annotation.URLMapping;
7+
import com.ocpsoft.pretty.faces.annotation.URLValidator;
78

89
@ManagedBean
910
@RequestScoped
10-
@URLMapping(id = "dynaview", pattern = "/dynaview/#{ pathParamDynaViewBean.value }",
11-
viewId = "#{pathParamDynaViewBean.computeViewId}")
11+
@URLMapping(id = "pathParamMapping", pattern = "/pathparam/#{pathParamDynaViewBean.pathParam}",
12+
viewId = "#{pathParamDynaViewBean.computePathParamViewId}",
13+
validation=@URLValidator(index=0, validatorIds="DynaViewParameterValidator"))
1214
public class PathParamDynaViewBean
1315
{
1416

1517
/**
1618
* Injected value of the path parameter
1719
*/
18-
private String value;
20+
private String pathParam;
1921

2022
/**
2123
* This method is used to compute the view id! It will return
2224
* <code>/correct.jsf</code> if the path parameter property contains the
2325
* string <code>correct</code>. In all other cases it will return
2426
* <code>/wrong.jsf</code>.
2527
*/
26-
public String computeViewId()
28+
public String computePathParamViewId()
2729
{
2830

29-
if (value != null && value.equals("correct"))
31+
if (pathParam != null && pathParam.equals("correct"))
3032
{
3133
return "/correct.jsf";
3234
}
@@ -37,14 +39,14 @@ public String computeViewId()
3739

3840
}
3941

40-
public String getValue()
42+
public String getPathParam()
4143
{
42-
return value;
44+
return pathParam;
4345
}
4446

45-
public void setValue(String value)
47+
public void setPathParam(String value)
4648
{
47-
this.value = value;
49+
this.pathParam = value;
4850
}
4951

5052
}

‎tests-jsf2/src/test/java/com/ocpsoft/pretty/faces/test/dynaview/PathParamDynaViewTest.java‎

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.ocpsoft.pretty.faces.test.dynaview;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
6+
import com.ocpsoft.pretty.faces.annotation.URLMapping;
7+
import com.ocpsoft.pretty.faces.annotation.URLQueryParameter;
8+
import com.ocpsoft.pretty.faces.annotation.URLValidator;
9+
10+
@ManagedBean
11+
@RequestScoped
12+
@URLMapping(id = "queryParamMapping", pattern = "/queryparam",
13+
viewId = "#{queryParamDynaViewBean.computeQueryParamViewId}")
14+
public class QueryParamDynaViewBean
15+
{
16+
17+
/**
18+
* Injected value of the path parameter
19+
*/
20+
@URLQueryParameter(value="param", mappingId="queryParamMapping")
21+
@URLValidator(validatorIds="DynaViewParameterValidator")
22+
private String queryParamParam;
23+
24+
/**
25+
* This method is used to compute the view id! It will return
26+
* <code>/correct.jsf</code> if the query parameter property contains the
27+
* string <code>correct</code>. In all other cases it will return
28+
* <code>/wrong.jsf</code>.
29+
*/
30+
public String computeQueryParamViewId()
31+
{
32+
33+
if (queryParamParam != null && queryParamParam.equals("correct"))
34+
{
35+
return "/correct.jsf";
36+
}
37+
else
38+
{
39+
return "/wrong.jsf";
40+
}
41+
42+
}
43+
44+
public String getQueryParamParam()
45+
{
46+
return queryParamParam;
47+
}
48+
49+
public void setQueryParamParam(String queryParamParam)
50+
{
51+
this.queryParamParam = queryParamParam;
52+
}
53+
54+
}

‎tests-jsf2/src/test/resources/dynaview/correct.xhtml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<body>
1414

1515
<p>
16-
The path parameter was correctly injected!
16+
The parameter was correctly injected!
1717
</p>
1818

1919
</body>

‎tests-jsf2/src/test/resources/dynaview/index.xhtml‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,30 @@
1313
<body>
1414

1515
<p>
16-
<pretty:link id="link" mappingId="dynaview">
16+
<pretty:link id="pathParamLink" mappingId="pathParamMapping">
1717
<f:param value="correct"/>
18-
<h:outputText value="Click me!" />
18+
<h:outputText value="Path Parameter!" />
19+
</pretty:link>
20+
</p>
21+
22+
<p>
23+
<pretty:link id="queryParamLink" mappingId="queryParamMapping">
24+
<f:param name="param" value="correct"/>
25+
<h:outputText value="Query Parameter!" />
26+
</pretty:link>
27+
</p>
28+
29+
<p>
30+
<pretty:link id="invalidPathParamLink" mappingId="pathParamMapping">
31+
<f:param value="invalid"/>
32+
<h:outputText value="Invalid Path Parameter!" />
33+
</pretty:link>
34+
</p>
35+
36+
<p>
37+
<pretty:link id="invalidQueryParamLink" mappingId="queryParamMapping">
38+
<f:param name="param" value="invalid"/>
39+
<h:outputText value="Invalid Query Parameter!" />
1940
</pretty:link>
2041
</p>
2142

0 commit comments

Comments
 (0)