1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.validator;
23
24 import org.junit.Assert;
25 import org.nuiton.validator.model.Person;
26 import org.nuiton.validator.model.Pet;
27
28 import java.io.File;
29 import java.util.List;
30 import java.util.Set;
31
32
33
34
35
36
37
38 public class ValidatorTestHelper {
39
40 public static File getBasedir() {
41
42
43 String basedirPath = System.getenv("basedir");
44 if (basedirPath == null) {
45
46
47 basedirPath = new File("").getAbsolutePath();
48 }
49 return new File(basedirPath);
50 }
51
52 public static void assertValidatorModel(NuitonValidator<?> validator,
53 String expectedContext,
54 Class<?> expectedType,
55 NuitonValidatorScope... expectedScopes) {
56 Assert.assertNotNull(validator);
57 NuitonValidatorModel<?> model = validator.getModel();
58 Assert.assertNotNull(model);
59 Assert.assertEquals(expectedContext, model.getContext());
60 Assert.assertEquals(expectedType, model.getType());
61 Set<NuitonValidatorScope> scopes = model.getScopes();
62 for (NuitonValidatorScope expectedScope : expectedScopes) {
63 Assert.assertTrue(scopes.contains(expectedScope));
64 }
65 }
66
67 public static void assertValidatorEffectiveScopes(NuitonValidator<?> validator,
68 NuitonValidatorScope... expectedScopes) {
69 Assert.assertNotNull(validator);
70 Set<NuitonValidatorScope> effectiveScopes = validator.getEffectiveScopes();
71 Assert.assertEquals(expectedScopes.length, effectiveScopes.size());
72 for (NuitonValidatorScope expectedScope : expectedScopes) {
73 Assert.assertTrue(effectiveScopes.contains(expectedScope));
74 }
75 }
76
77 public static void assertValidatorEffectiveFields(NuitonValidator<?> validator,
78 String... expectedFields) {
79 Assert.assertNotNull(validator);
80 Set<String> effectiveFields = validator.getEffectiveFields();
81 Assert.assertEquals(expectedFields.length, effectiveFields.size());
82 for (String expectedField : expectedFields) {
83 Assert.assertTrue(effectiveFields.contains(expectedField));
84 }
85 }
86
87 public static void assertValidatorEffectiveFields(NuitonValidator<?> validator,
88 NuitonValidatorScope scope,
89 String... expectedFields) {
90 Assert.assertNotNull(validator);
91 Set<String> effectiveFields = validator.getEffectiveFields(scope);
92 Assert.assertEquals(expectedFields.length, effectiveFields.size());
93 for (String expectedField : expectedFields) {
94 Assert.assertTrue(effectiveFields.contains(expectedField));
95 }
96 }
97
98 public static void testPerson(NuitonValidator<Person> validator) {
99 Assert.assertNotNull(validator);
100
101 Person person = new Person();
102
103 NuitonValidatorResult result;
104
105 result = validator.validate(person);
106
107
108
109 Assert.assertFalse(result.isValid());
110 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME, "person.firstname.required");
111 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME, "person.name.required");
112 assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET, "person.with.no.pet");
113
114 person.setFirstname("Joe");
115 result = validator.validate(person);
116
117
118
119 Assert.assertFalse(result.isValid());
120 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME);
121 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME, "person.name.required");
122 assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET, "person.with.no.pet");
123
124 person.setName("Black");
125 result = validator.validate(person);
126
127
128
129 Assert.assertTrue(result.isValid());
130 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME);
131 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME);
132 assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET, "person.with.no.pet");
133
134 person.addPet(new Pet());
135 result = validator.validate(person);
136
137
138
139 Assert.assertTrue(result.isValid());
140 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME);
141 assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME);
142 assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET);
143
144 }
145
146 public static void assertFieldMessages(NuitonValidatorResult result,
147 NuitonValidatorScope scope,
148 String field,
149 String... expectedMessages) {
150
151 if (expectedMessages.length == 0) {
152
153
154 boolean hasMessages = result.hasMessagesForScope(field, scope);
155 Assert.assertFalse(hasMessages);
156
157 } else {
158
159
160 boolean hasMessages = result.hasMessagesForScope(field, scope);
161 Assert.assertTrue(hasMessages);
162
163 List<String> messages = result.getMessagesForScope(field, scope);
164 Assert.assertNotNull(hasMessages);
165 Assert.assertEquals(expectedMessages.length, messages.size());
166 for (String expectedMessage : expectedMessages) {
167 Assert.assertTrue(messages.contains(expectedMessage));
168 }
169 }
170 }
171 }