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 LengthValidatorTest extends ValidatorTestCase
35 {
36 public LengthValidatorTest(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(LengthValidatorTest.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 LengthValidator validator = new LengthValidator();
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 LengthValidator validator = new LengthValidator();
82
83 doTestNullContext(wrapper, validator);
84 }
85
86 public void testNullComponent()
87 {
88 LengthValidator validator = new LengthValidator();
89
90 doTestNullComponent(facesContext, validator);
91 }
92
93 public void testTooLargeLength()
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 LengthValidator validator = new LengthValidator();
105 validator.setMaximum(2);
106 validator.validate(facesContext, component, "someValue");
107
108
109 fail("Expected Null pointer exception");
110 }
111 catch (ValidatorException ve)
112 {
113
114 }
115 mock.verify();
116 }
117
118 public void testExactFailure()
119 {
120
121 Mock mock = buildMockUIComponent();
122 UIComponent component = (UIComponent) mock.proxy();
123 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
124 setMockLabelForComponent(wrapper);
125
126 try
127 {
128 LengthValidator validator = new LengthValidator();
129 String value = "999999";
130 validator.setMinimum(2);
131 validator.setMaximum(2);
132 validator.validate(facesContext, component, value);
133 fail("Expected ValidatorException for exact");
134 }
135 catch (ValidatorException ve)
136 {
137
138 }
139
140 mock.verify();
141 }
142
143 public void testSanitySuccess()
144 {
145
146
147 LengthValidator validator = new LengthValidator();
148 Mock mock = buildMockUIComponent();
149 UIComponent component = (UIComponent) mock.proxy();
150 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
151
152 String values[] = {"Hallo","world"};
153 validator.setMinimum(2);
154 for (int i = 0; i < values.length ; i++)
155 {
156 doTestValidate(validator, facesContext, wrapper, values[i]);
157 }
158 }
159
160 public void testStateHolderSaveRestore()
161 {
162 LengthValidator validator = new LengthValidator();
163 Mock mock = buildMockUIComponent();
164 UIComponent component = (UIComponent) mock.proxy();
165 MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
166
167 validator.setMaximum(5);
168 validator.setMessageDetailMaximum("Validation failed");
169 LengthValidator restoreValidator = new LengthValidator();
170
171 doTestStateHolderSaveRestore(validator, restoreValidator,
172 facesContext, wrapper);
173 }
174 }
175
176
177
178
179
180
181
182