1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.validator;
20
21 import javax.faces.validator.ValidatorException;
22 import javax.faces.component.UIComponent;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.myfaces.trinidad.validator.RegExpValidator;
28 import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
29 import org.jmock.Mock;
30
31
32
33
34
35 public class RegExpValidatorTest extends ValidatorTestCase
36 {
37 public RegExpValidatorTest(String testName)
38 {
39 super(testName);
40 }
41
42 @Override
43 protected void setUp() throws Exception
44 {
45 super.setUp();
46 }
47
48 @Override
49 protected void tearDown() throws Exception
50 {
51 super.tearDown();
52 }
53
54 public static Test suite()
55 {
56 return new TestSuite(RegExpValidatorTest.class);
57 }
58
59
60
61
62
63
64 public void testNull() throws ValidatorException
65 {
66 Mock mock = buildMockUIComponent();
67 UIComponent component = (UIComponent) mock.proxy();
68 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
69 RegExpValidator validator = new RegExpValidator();
70
71 doTestNull(facesContext, wrapper, validator);
72 }
73
74
75
76
77 public void testNullContext()
78 {
79 Mock mock = buildMockUIComponent();
80 UIComponent component = (UIComponent) mock.proxy();
81 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
82 RegExpValidator validator = new RegExpValidator();
83
84 doTestNullContext(wrapper, validator);
85 }
86
87 public void testNullComponent()
88 {
89 RegExpValidator validator = new RegExpValidator();
90
91 doTestNullComponent(facesContext, validator);
92 }
93
94
95
96
97 public void testNotString()
98 {
99 doTestIsNotString(new RegExpValidator());
100 }
101
102
103
104
105 public void testPatternNotSet()
106 {
107
108
109 Mock mock = mock(UIComponent.class);
110 UIComponent component = (UIComponent) mock.proxy();
111
112 try
113 {
114 RegExpValidator validator = new RegExpValidator();
115 validator.validate(facesContext, component, "someValue");
116
117
118 fail("Expected Null pointer exception");
119 }
120 catch (NullPointerException npe)
121 {
122
123 }
124 mock.verify();
125 }
126
127
128
129
130 public void testBlankValueOnPattern()
131 {
132
133 Mock mock = buildMockUIComponent();
134 UIComponent component = (UIComponent) mock.proxy();
135 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
136 setMockLabelForComponent(wrapper);
137
138 try
139 {
140 RegExpValidator validator = new RegExpValidator();
141 String value = "999999";
142 validator.setPattern("");
143 validator.validate(facesContext, component, value);
144 fail("Expected ValidatorException");
145 }
146 catch (ValidatorException ve)
147 {
148
149 }
150
151 mock.verify();
152 }
153
154
155
156
157
158
159
160 public void testSanitySuccess()
161 {
162
163
164 RegExpValidator validator = new RegExpValidator();
165 Mock mock = buildMockUIComponent();
166 UIComponent component = (UIComponent) mock.proxy();
167 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
168
169 String values[] = {"9123456","9x"};
170 String patterns[] = {"[0-9]*","[9][x]"};
171 for (int i = 0; i < values.length ; i++)
172 {
173 validator.setPattern(patterns[i]);
174 doTestValidate(validator, facesContext, wrapper, values[i]);
175 }
176 }
177
178
179
180
181 public void testStateHolderSaveRestore()
182 {
183 RegExpValidator validator = new RegExpValidator();
184 Mock mock = buildMockUIComponent();
185 UIComponent component = (UIComponent) mock.proxy();
186 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
187
188 validator.setPattern("TestSaveRestore");
189 validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
190 RegExpValidator restoreValidator = new RegExpValidator();
191
192 doTestStateHolderSaveRestore(validator, restoreValidator,
193 facesContext, wrapper);
194 }
195
196
197
198
199 public void testIsEqual()
200 {
201 RegExpValidator validator = new RegExpValidator();
202 RegExpValidator otherValidator = new RegExpValidator();
203 doTestEquals(validator, otherValidator, true);
204 assertEquals(validator.hashCode(), otherValidator.hashCode());
205
206 validator.setPattern("[0-9]");
207 validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
208 otherValidator.setPattern("[0-9]");
209 otherValidator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
210 doTestEquals(validator, otherValidator, true);
211 assertEquals(validator.hashCode(), otherValidator.hashCode());
212
213 otherValidator.setPattern(null);
214 doTestEquals(validator, otherValidator, false);
215 assertEquals(false, (validator.hashCode() == otherValidator.hashCode()));
216 }
217
218 public void testCustomMessageIsSet()
219 {
220 Mock mock = buildMockUIComponent();
221 UIComponent component = (UIComponent) mock.proxy();
222 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
223 setMockLabelForComponent(wrapper);
224 RegExpValidator validator = new RegExpValidator();
225
226 validator.setPattern("[0-9]*");
227 validator.setMessageDetailNoMatch("\"{0}\" in \"{1}\" failed!! {4}");
228
229
230 try
231 {
232 validator.validate(facesContext, component, "9123456");
233 }
234 catch (ValidatorException ve)
235 {
236 String msg = ve.getFacesMessage().getDetail();
237 assertEquals(msg, "\"four\" in \"label\" failed!! [0-9]*");
238 }
239 }
240
241 }
242
243
244
245
246
247
248
249