1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.extensions.validator.test.beanval;
20
21 import org.apache.myfaces.extensions.validator.test.beanval.view.RestrictGroupValidationTestCase1PageBean;
22 import org.apache.myfaces.extensions.validator.test.beanval.model.SimulatedUserInformation;
23 import org.junit.Test;
24
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27
28 public class RestrictGroupsTestCase extends BaseBeanValPropertyValidationTestCase<RestrictGroupValidationTestCase1PageBean>
29 {
30
31 protected RestrictGroupValidationTestCase1PageBean getBeanToTest()
32 {
33 return new RestrictGroupValidationTestCase1PageBean();
34 }
35
36 @Test
37 public void testRestrictedGroup()
38 {
39 createValueBindingForComponent(this.inputComponent1, "#{testBean.model1.property1}");
40 setValueToValidate(this.inputComponent1, "");
41
42 validateComponents();
43
44 assertComponentValid(this.inputComponent1);
45 assertNavigationBlocked(false);
46
47 checkMessageCount(0);
48 }
49
50 @Test
51 public void testRestrictedGroupWithAdminRole()
52 {
53 createRequestScopedBean("currentUser", new SimulatedUserInformation("admin"));
54
55 createValueBindingForComponent(this.inputComponent1, "#{testBean.model2.property1}");
56 createValueBindingForComponent(this.inputComponent2, "#{testBean.model2.property2}");
57 setValueToValidate(this.inputComponent1, "");
58 setValueToValidate(this.inputComponent2, "");
59
60 validateComponents();
61
62 assertComponentValid(this.inputComponent1);
63 assertComponentValid(this.inputComponent2);
64
65 assertNavigationBlocked(false);
66 checkMessageCount(0);
67 }
68
69 @Test
70 public void testRestrictedGroupWithUserRole()
71 {
72 createRequestScopedBean("currentUser", new SimulatedUserInformation("user"));
73
74 createValueBindingForComponent(this.inputComponent1, "#{testBean.model2.property1}");
75 createValueBindingForComponent(this.inputComponent2, "#{testBean.model2.property2}");
76 setValueToValidate(this.inputComponent1, "");
77 setValueToValidate(this.inputComponent2, "");
78
79 validateComponents();
80
81 assertComponentInvalid(this.inputComponent1);
82 assertComponentValid(this.inputComponent2);
83
84 assertNavigationBlocked(true);
85 checkMessageCount(1);
86 checkMessageSeverities(FacesMessage.SEVERITY_ERROR);
87 }
88
89 @Test
90 public void testRestrictedGroupWithCorrectViewId()
91 {
92 FacesContext.getCurrentInstance().getViewRoot().setViewId("/pages/groupValidationAwarePage.xhtml");
93
94 createValueBindingForComponent(this.inputComponent1, "#{testBean.model3.property1}");
95 createValueBindingForComponent(this.inputComponent2, "#{testBean.model3.property2}");
96 setValueToValidate(this.inputComponent1, "");
97 setValueToValidate(this.inputComponent2, "");
98
99 validateComponents();
100
101 assertComponentValid(this.inputComponent1);
102 assertComponentValid(this.inputComponent2);
103
104 assertNavigationBlocked(false);
105 checkMessageCount(0);
106 }
107
108 @Test
109 public void testRestrictedGroupWithWrongViewId()
110 {
111 createValueBindingForComponent(this.inputComponent1, "#{testBean.model3.property1}");
112 createValueBindingForComponent(this.inputComponent2, "#{testBean.model3.property2}");
113 setValueToValidate(this.inputComponent1, "");
114 setValueToValidate(this.inputComponent2, "");
115
116 validateComponents();
117
118 assertComponentInvalid(this.inputComponent1);
119 assertComponentValid(this.inputComponent2);
120
121 assertNavigationBlocked(true);
122 checkMessageCount(1);
123 checkMessageSeverities(FacesMessage.SEVERITY_ERROR);
124 }
125
126 @Test
127 public void testUsedAndRestrictedGroup()
128 {
129 createValueBindingForComponent(this.inputComponent1, "#{testBean.model4.property1}");
130 createValueBindingForComponent(this.inputComponent2, "#{testBean.model4.property2}");
131 setValueToValidate(this.inputComponent1, "");
132 setValueToValidate(this.inputComponent2, "");
133
134 validateComponents();
135
136 assertComponentValid(this.inputComponent1);
137 assertComponentInvalid(this.inputComponent2);
138
139 assertNavigationBlocked(true);
140 checkMessageCount(1);
141 checkMessageSeverities(FacesMessage.SEVERITY_ERROR);
142 }
143 }