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.component.UIComponent;
22 import javax.faces.validator.ValidatorException;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
28 import org.jmock.Mock;
29
30
31
32
33
34 public class DoubleRangeValidatorTest extends ValidatorTestCase
35 {
36 public DoubleRangeValidatorTest(String testName)
37 {
38 super(testName);
39 }
40
41 @Override
42 protected void setUp() throws Exception
43 {
44 super.setUp();
45 }
46
47 @Override
48 protected void tearDown() throws Exception
49 {
50 super.tearDown();
51 }
52
53 public static Test suite()
54 {
55 return new TestSuite(DoubleRangeValidatorTest.class);
56 }
57
58
59
60
61
62
63 public void testNull() throws ValidatorException
64 {
65 Mock mock = buildMockUIComponent();
66 UIComponent component = (UIComponent) mock.proxy();
67 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
68 DoubleRangeValidator validator = new DoubleRangeValidator();
69
70 doTestNull(facesContext, wrapper, validator);
71 }
72
73
74
75
76 public void testNullContext()
77 {
78 Mock mock = buildMockUIComponent();
79 UIComponent component = (UIComponent) mock.proxy();
80 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
81 DoubleRangeValidator validator = new DoubleRangeValidator();
82
83 doTestNullContext(wrapper, validator);
84 }
85
86 public void testNullComponent()
87 {
88 DoubleRangeValidator validator = new DoubleRangeValidator();
89
90 doTestNullComponent(facesContext, validator);
91 }
92
93 public void testTooLarge()
94 {
95
96
97 Mock mock = mock(UIComponent.class);
98 UIComponent component = (UIComponent) mock.proxy();
99 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
100 setMockLabelForComponent(wrapper);
101
102 try
103 {
104 DoubleRangeValidator validator = new DoubleRangeValidator();
105 validator.setMaximum(100);
106 validator.validate(facesContext, component, 1000);
107
108
109 fail("Expected Null pointer exception");
110 }
111 catch (ValidatorException ve)
112 {
113
114 }
115 mock.verify();
116 }
117
118 public void testWrongType()
119 {
120
121
122 Mock mock = mock(UIComponent.class);
123 UIComponent component = (UIComponent) mock.proxy();
124 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
125 setMockLabelForComponent(wrapper);
126
127 try
128 {
129 DoubleRangeValidator validator = new DoubleRangeValidator();
130 validator.setMaximum(2);
131 validator.validate(facesContext, component, "thisShouldFail");
132
133
134 fail("Expected Null pointer exception");
135 }
136 catch (ValidatorException ve)
137 {
138
139 }
140 mock.verify();
141 }
142
143 public void testExactFailure()
144 {
145
146 Mock mock = buildMockUIComponent();
147 UIComponent component = (UIComponent) mock.proxy();
148 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
149 setMockLabelForComponent(wrapper);
150
151 try
152 {
153 DoubleRangeValidator validator = new DoubleRangeValidator();
154 double value = 20d;
155 validator.setMinimum(2);
156 validator.setMaximum(2);
157 validator.validate(facesContext, component, value);
158 fail("Expected ValidatorException for exact");
159 }
160 catch (ValidatorException ve)
161 {
162
163 }
164
165 mock.verify();
166 }
167
168 public void testSanitySuccess()
169 {
170
171
172 DoubleRangeValidator validator = new DoubleRangeValidator();
173 Mock mock = buildMockUIComponent();
174 UIComponent component = (UIComponent) mock.proxy();
175 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
176
177 Double values[] = {200d,500d};
178 validator.setMinimum(2);
179 for (int i = 0; i < values.length ; i++)
180 {
181 doTestValidate(validator, facesContext, wrapper, values[i]);
182 }
183 }
184
185 public void testStringBasedValues()
186 {
187 DoubleRangeValidator validator = new DoubleRangeValidator();
188 Mock mock = buildMockUIComponent();
189 UIComponent component = (UIComponent) mock.proxy();
190 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
191
192 String values[] = {"200.05","500.00"};
193 validator.setMinimum(200);
194 for (int i = 0; i < values.length ; i++)
195 {
196 doTestValidate(validator, facesContext, wrapper, values[i]);
197 }
198 }
199
200 public void testStateHolderSaveRestore()
201 {
202 DoubleRangeValidator validator = new DoubleRangeValidator();
203 Mock mock = buildMockUIComponent();
204 UIComponent component = (UIComponent) mock.proxy();
205 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
206
207 validator.setMaximum(5);
208 validator.setMessageDetailMaximum("Validation failed");
209 DoubleRangeValidator restoreValidator = new DoubleRangeValidator();
210
211 doTestStateHolderSaveRestore(validator, restoreValidator,
212 facesContext, wrapper);
213 }
214 }
215
216
217
218
219
220
221
222