1
2
3
4 package org.mod4j.runtime.validation;
5
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertFalse;
8 import static org.junit.Assert.assertNotNull;
9 import static org.junit.Assert.assertTrue;
10 import static org.junit.Assert.fail;
11
12 import org.junit.Test;
13 import org.mod4j.runtime.validation.BusinessRuleUtils;
14 import org.springframework.validation.BindException;
15 import org.springframework.validation.Errors;
16 import org.springframework.validation.FieldError;
17
18
19
20
21 public class BusinessRuleUtilsTest {
22
23 class ObjectToValidate {
24 private String stringValue;
25
26 private Long longValue;
27
28 private Integer integerValue;
29
30 public Long getLongValue() {
31 return longValue;
32 }
33
34 public void setLongValue(Long longValue) {
35 this.longValue = longValue;
36 }
37
38 public Integer getIntegerValue() {
39 return integerValue;
40 }
41
42 public void setIntegerValue(Integer inteerValue) {
43 this.integerValue = integerValue;
44 }
45
46 public ObjectToValidate(String value) {
47 this.stringValue = value;
48 }
49
50 public ObjectToValidate(Long value) {
51 this.longValue = value;
52 }
53
54 public ObjectToValidate(Integer value) {
55 this.integerValue = value;
56 }
57
58 public String getStringValue() {
59 return stringValue;
60 }
61
62 public void setStringValue(String value) {
63 this.stringValue = value;
64 }
65 }
66
67
68
69
70
71 @Test
72 public void testValidateEmptyOrWhitespace() {
73 Object target = new ObjectToValidate("");
74 Errors errors = new BindException(target, "target");
75 BusinessRuleUtils.validateEmptyOrWhitespace(errors, "stringValue");
76 assertTrue(errors.hasErrors());
77 assertEquals("field.required", errors.getFieldError("stringValue").getCode());
78 }
79
80
81
82
83
84 @Test
85 public void testValidateMinLengthFail() {
86 ObjectToValidate target = new ObjectToValidate("12");
87 Errors errors = new BindException(target, "target");
88 BusinessRuleUtils.validateMinLength(errors, "stringValue", 3);
89 assertTrue(errors.hasErrors());
90 FieldError error = errors.getFieldError("stringValue");
91 assertNotNull(error);
92 assertEquals("field.length.min", error.getCode());
93 assertEquals("stringValue should be at least 3 long, but was 2", error.getDefaultMessage());
94 }
95
96 private void autoBoxer(Object obj) {
97
98 if (obj instanceof String) {
99 System.out.println("Its a String: " + obj);
100 } else if (obj instanceof Integer) {
101 System.out.println("Its a Integer: " + obj);
102 } else if (obj instanceof Long) {
103 System.out.println("Its a Long: " + obj);
104 }
105
106 }
107
108 @Test
109 public void testAutoboing() {
110 autoBoxer(5);
111 autoBoxer(5L);
112 autoBoxer("5");
113 autoBoxer("5 hello");
114 }
115
116
117
118
119
120 @Test
121 public void testValidateMinLengthSucceed() {
122 ObjectToValidate target = new ObjectToValidate("123");
123 Errors errors = new BindException(target, "target");
124 BusinessRuleUtils.validateMinLength(errors, "stringValue", 3);
125 assertFalse(errors.hasErrors());
126 target.setStringValue("1234");
127 BusinessRuleUtils.validateMinLength(errors, "stringValue", 3);
128 assertFalse(errors.hasErrors());
129 }
130
131
132
133
134
135 @Test
136 public void testValidateMaxLengthFail() {
137 ObjectToValidate target = new ObjectToValidate("1234");
138 Errors errors = new BindException(target, "target");
139 BusinessRuleUtils.validateMaxLength(errors, "stringValue", 3);
140 assertTrue(errors.hasErrors());
141 FieldError error = errors.getFieldError("stringValue");
142 assertNotNull(error);
143 assertEquals("field.length.max", error.getCode());
144 assertEquals("stringValue should be at most 3 long, but was 4", error.getDefaultMessage());
145 }
146
147
148
149
150
151 @Test
152 public void testValidateMaxLengthSucceed() {
153 ObjectToValidate target = new ObjectToValidate("123");
154 Errors errors = new BindException(target, "target");
155 BusinessRuleUtils.validateMaxLength(errors, "stringValue", 3);
156 assertFalse(errors.hasErrors());
157 target.setStringValue("12");
158 BusinessRuleUtils.validateMaxLength(errors, "stringValue", 3);
159 assertFalse(errors.hasErrors());
160 }
161
162 @Test
163 public void testValidateMaxValueFail() {
164 ObjectToValidate target = new ObjectToValidate(5L);
165 Errors errors = new BindException(target, "target");
166 BusinessRuleUtils.validateMaxValue(errors, "longValue", 4);
167 assertTrue(errors.hasErrors());
168 FieldError error = errors.getFieldError();
169 assertNotNull(error);
170 assertEquals("field.value.max", error.getCode());
171 assertEquals("longValue should be at most 4, but was 5", error.getDefaultMessage());
172 }
173
174 @Test
175 public void testValidateMaxValueSucceed() {
176 ObjectToValidate target = new ObjectToValidate(5L);
177 Errors errors = new BindException(target, "target");
178 BusinessRuleUtils.validateMaxValue(errors, "longValue", 5);
179 assertFalse(errors.hasErrors());
180 BusinessRuleUtils.validateMaxValue(errors, "longValue", 6);
181 assertFalse(errors.hasErrors());
182 }
183
184 @Test
185 public void testValidateMaxValueSucceedWithInteger() {
186 ObjectToValidate target = new ObjectToValidate(new Integer(7));
187 Errors errors = new BindException(target, "target");
188 BusinessRuleUtils.validateMaxValue(errors, "integerValue", 8);
189 assertFalse(errors.hasErrors());
190 BusinessRuleUtils.validateMaxValue(errors, "integerValue", 9);
191 assertFalse(errors.hasErrors());
192 }
193
194 @Test
195 public void testValidateMinValueFail() {
196 ObjectToValidate target = new ObjectToValidate(5L);
197 Errors errors = new BindException(target, "target");
198 BusinessRuleUtils.validateMinValue(errors, "longValue", 6);
199 assertTrue(errors.hasErrors());
200 FieldError error = errors.getFieldError();
201 assertNotNull(error);
202 assertEquals("field.value.min", error.getCode());
203 assertEquals("longValue should be at least 6, but was 5", error.getDefaultMessage());
204 }
205
206 @Test
207 public void testValidateMinValueSucceed() {
208 ObjectToValidate target = new ObjectToValidate(5L);
209 Errors errors = new BindException(target, "target");
210 BusinessRuleUtils.validateMinValue(errors, "longValue", 5);
211 assertFalse(errors.hasErrors());
212 BusinessRuleUtils.validateMinValue(errors, "longValue", 4);
213 assertFalse(errors.hasErrors());
214 }
215
216 @Test
217 public void testValidateMinValueSucceedWithInteger() {
218 ObjectToValidate target = new ObjectToValidate(new Integer(5));
219 Errors errors = new BindException(target, "target");
220 BusinessRuleUtils.validateMinValue(errors, "integerValue", 5);
221 assertFalse(errors.hasErrors());
222 BusinessRuleUtils.validateMinValue(errors, "integerValue", 2);
223 assertFalse(errors.hasErrors());
224 }
225
226 @Test
227 public void testValidateMinLengthWithLong() {
228 ObjectToValidate target = new ObjectToValidate(5L);
229 Errors errors = new BindException(target, "target");
230 try {
231 BusinessRuleUtils.validateMinLength(errors, "longValue", 5);
232 fail("Expected ClassCastException");
233 } catch (ClassCastException e) {
234
235 }
236 }
237
238 @Test
239 public void testValidateMaxLengthWithLong() {
240 ObjectToValidate target = new ObjectToValidate(5L);
241 Errors errors = new BindException(target, "target");
242 try {
243 BusinessRuleUtils.validateMaxLength(errors, "longValue", 5);
244 fail("Expected ClassCastException");
245 } catch (ClassCastException e) {
246
247 }
248 }
249
250 @Test
251 public void testValidateMinValueWithString() {
252 ObjectToValidate target = new ObjectToValidate("value");
253 Errors errors = new BindException(target, "target");
254 BusinessRuleUtils.validateMinValue(errors, "longValue", 5);
255 }
256
257 @Test
258 public void testValidateMaxValueWithString() {
259 ObjectToValidate target = new ObjectToValidate("value");
260 Errors errors = new BindException(target, "target");
261 try {
262 BusinessRuleUtils.validateMaxValue(errors, "longValue", 5);
263 fail("Expected IllegalArgumentException");
264 } catch (IllegalArgumentException e) {
265
266 }
267 }
268
269 }