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.xwork2;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.nuiton.validator.NuitonValidator;
30 import org.nuiton.validator.NuitonValidatorModel;
31 import org.nuiton.validator.NuitonValidatorScope;
32 import org.nuiton.validator.ValidatorTestHelper;
33 import org.nuiton.validator.model.Person;
34 import org.nuiton.validator.model.Pet;
35
36 import java.io.File;
37 import java.util.Arrays;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.Set;
41 import java.util.SortedSet;
42 import java.util.regex.Pattern;
43
44
45
46
47
48
49
50 public class XWork2NuitonValidatorProviderTest {
51
52
53 private static final Log log =
54 LogFactory.getLog(XWork2NuitonValidatorProviderTest.class);
55
56 protected XWork2NuitonValidatorProvider provider;
57
58 @Before
59 public void setUp() {
60 provider = new XWork2NuitonValidatorProvider();
61 }
62
63 @Test
64 public void testGetModel() throws Exception {
65
66 NuitonValidatorModel<Person> model =
67 provider.getModel(Person.class, null);
68
69 Assert.assertNotNull(model);
70 Assert.assertNull(model.getContext());
71 Assert.assertEquals(Person.class, model.getType());
72 Set<NuitonValidatorScope> scopes = new HashSet<NuitonValidatorScope>(
73 Arrays.asList(NuitonValidatorScope.values()));
74 Assert.assertEquals(scopes, model.getScopes());
75 }
76
77 @Test
78 public void testNewValidator() throws Exception {
79
80
81 NuitonValidatorModel<Person> model =
82 provider.getModel(Person.class, null);
83
84 NuitonValidator<Person> validator = provider.newValidator(model);
85
86 Assert.assertNotNull(validator);
87 }
88
89 @Test
90 public void testDetectValidators() {
91
92 String context = "context";
93
94 File basedir = ValidatorTestHelper.getBasedir();
95 File testResourcesDir = new File(basedir, "src" + File.separator + "test" + File.separator + "resources");
96
97 SortedSet<NuitonValidator<?>> result;
98 NuitonValidator<?> validator;
99 Iterator<NuitonValidator<?>> iterator;
100
101
102
103 result = provider.detectValidators(testResourcesDir, null, null, Person.class, Pet.class);
104
105 Assert.assertNotNull(result);
106 Assert.assertEquals(3, result.size());
107
108
109 iterator = result.iterator();
110 validator = iterator.next();
111
112 ValidatorTestHelper.assertValidatorModel(validator, null, Person.class, NuitonValidatorScope.values());
113 ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.ERROR, NuitonValidatorScope.WARNING);
114
115 validator = iterator.next();
116 ValidatorTestHelper.assertValidatorModel(validator, null, Pet.class, NuitonValidatorScope.values());
117 ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.ERROR);
118
119 validator = iterator.next();
120 ValidatorTestHelper.assertValidatorModel(validator, context, Pet.class, NuitonValidatorScope.values());
121 ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
122
123
124
125 result = provider.detectValidators(testResourcesDir, null, new NuitonValidatorScope[]{NuitonValidatorScope.WARNING}, Person.class, Pet.class);
126
127 Assert.assertNotNull(result);
128 Assert.assertEquals(1, result.size());
129
130 iterator = result.iterator();
131
132 validator = iterator.next();
133 ValidatorTestHelper.assertValidatorModel(validator, null, Person.class, NuitonValidatorScope.WARNING);
134 ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.WARNING);
135
136
137
138 result = provider.detectValidators(testResourcesDir, Pattern.compile(context), null, Person.class, Pet.class);
139
140 Assert.assertNotNull(result);
141 Assert.assertEquals(1, result.size());
142
143 iterator = result.iterator();
144
145 validator = iterator.next();
146 ValidatorTestHelper.assertValidatorModel(validator, context, Pet.class, NuitonValidatorScope.values());
147 ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
148
149
150 result = provider.detectValidators(testResourcesDir, null,
151 new NuitonValidatorScope[]{NuitonValidatorScope.FATAL}, Person.class, Pet.class);
152
153 Assert.assertNotNull(result);
154 Assert.assertTrue(result.isEmpty());
155
156
157
158 result = provider.detectValidators(testResourcesDir, Pattern.compile(".*-fake"),
159 null, Person.class, Pet.class);
160
161 Assert.assertNotNull(result);
162 Assert.assertTrue(result.isEmpty());
163 }
164
165 }