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.model;
23
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.nuiton.validator.AbstractValidatorDetectorTest;
27 import org.nuiton.validator.NuitonValidator;
28 import org.nuiton.validator.NuitonValidatorScope;
29 import org.nuiton.validator.xwork2.XWork2NuitonValidatorProvider;
30
31 import java.io.File;
32 import java.util.Iterator;
33 import java.util.SortedSet;
34 import java.util.regex.Pattern;
35
36
37
38
39
40
41
42 public class ModelValidatorDetectorTestImpl extends AbstractValidatorDetectorTest {
43
44 public static final String CONTEXT = "context";
45
46 public ModelValidatorDetectorTestImpl() {
47 super(XWork2NuitonValidatorProvider.PROVIDER_NAME);
48 }
49
50 @Override
51 protected File getRootDirectory(File basedir) {
52 File testResourcesDir = new File(basedir, "src" + File.separator + "test" + File.separator + "resources");
53 return testResourcesDir;
54 }
55
56 @Test
57 public void detectAllValidators() {
58 SortedSet<NuitonValidator<?>> result;
59 NuitonValidator<?> validator;
60 Iterator<NuitonValidator<?>> iterator;
61
62
63
64 result = detectValidators(Person.class, Pet.class);
65
66 Assert.assertNotNull(result);
67 Assert.assertEquals(3, result.size());
68
69 iterator = result.iterator();
70 validator = iterator.next();
71
72 assertValidatorModel(validator, null, Person.class, NuitonValidatorScope.values());
73 assertValidatorEffectiveScopes(validator, NuitonValidatorScope.ERROR, NuitonValidatorScope.WARNING);
74 assertValidatorEffectiveFields(validator, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME, Person.PROPERTY_FIRSTNAME);
75 assertValidatorEffectiveFields(validator, NuitonValidatorScope.WARNING, Person.PROPERTY_PET);
76
77 validator = iterator.next();
78 assertValidatorModel(validator, null, Pet.class, NuitonValidatorScope.values());
79 assertValidatorEffectiveScopes(validator, NuitonValidatorScope.ERROR);
80 assertValidatorEffectiveFields(validator, NuitonValidatorScope.ERROR, Pet.PROPERTY_NAME);
81
82 validator = iterator.next();
83 assertValidatorModel(validator, CONTEXT, Pet.class, NuitonValidatorScope.values());
84 assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
85 assertValidatorEffectiveFields(validator, NuitonValidatorScope.INFO, Pet.PROPERTY_NAME);
86 }
87
88 @Test
89 public void detectValidatorsWithFilteredScopes() {
90
91 SortedSet<NuitonValidator<?>> result;
92 NuitonValidator<?> validator;
93 Iterator<NuitonValidator<?>> iterator;
94
95
96
97 result = detectValidators(null, new NuitonValidatorScope[]{NuitonValidatorScope.WARNING}, Person.class, Pet.class);
98
99 Assert.assertNotNull(result);
100 Assert.assertEquals(1, result.size());
101
102 iterator = result.iterator();
103
104 validator = iterator.next();
105 assertValidatorModel(validator, null, Person.class, NuitonValidatorScope.WARNING);
106 assertValidatorEffectiveScopes(validator, NuitonValidatorScope.WARNING);
107 assertValidatorEffectiveFields(validator, NuitonValidatorScope.WARNING, Person.PROPERTY_PET);
108
109
110
111 result = detectValidators(null,
112 new NuitonValidatorScope[]{NuitonValidatorScope.FATAL}, Person.class, Pet.class);
113
114 Assert.assertNotNull(result);
115 Assert.assertTrue(result.isEmpty());
116 }
117
118 @Test
119 public void detectValidatorsWithFilteredContext() {
120
121 SortedSet<NuitonValidator<?>> result;
122 NuitonValidator<?> validator;
123 Iterator<NuitonValidator<?>> iterator;
124
125
126
127
128 result = detectValidators(Pattern.compile(CONTEXT), Person.class, Pet.class);
129
130 Assert.assertNotNull(result);
131 Assert.assertEquals(1, result.size());
132
133 iterator = result.iterator();
134
135 validator = iterator.next();
136 assertValidatorModel(validator, CONTEXT, Pet.class, NuitonValidatorScope.values());
137 assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
138 assertValidatorEffectiveFields(validator, NuitonValidatorScope.INFO, Pet.PROPERTY_NAME);
139
140
141
142
143 result = detectValidators(Pattern.compile(".*-fake"),
144 Person.class, Pet.class);
145
146 Assert.assertNotNull(result);
147 Assert.assertTrue(result.isEmpty());
148 }
149
150 @Test
151 public void detectValidatorsWithFilteredContextAndFilteredScope() {
152
153 SortedSet<NuitonValidator<?>> result;
154 NuitonValidator<?> validator;
155 Iterator<NuitonValidator<?>> iterator;
156
157
158
159 result = detectValidators(Pattern.compile(CONTEXT),
160 new NuitonValidatorScope[]{NuitonValidatorScope.INFO, NuitonValidatorScope.FATAL},
161 Person.class, Pet.class);
162
163 Assert.assertNotNull(result);
164 Assert.assertEquals(1, result.size());
165
166 iterator = result.iterator();
167
168 validator = iterator.next();
169 assertValidatorModel(validator, CONTEXT, Pet.class, NuitonValidatorScope.FATAL, NuitonValidatorScope.INFO);
170 assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
171 assertValidatorEffectiveFields(validator, NuitonValidatorScope.INFO, Pet.PROPERTY_NAME);
172
173
174
175 result = detectValidators(Pattern.compile(".*-fake"),
176 new NuitonValidatorScope[]{NuitonValidatorScope.FATAL},
177 Person.class, Pet.class);
178
179 Assert.assertNotNull(result);
180 Assert.assertTrue(result.isEmpty());
181 }
182
183
184 }