View Javadoc
1   /*
2    * #%L
3    * Nuiton Validator
4    * %%
5    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
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   * Tests the validators defined for the test model.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 2.0
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          // test with all context and all scopes : 3 validators (Person + Pet + Pet (context))
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          // test with no context and only scope warning : one validator (Person)
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         // test with no context and only fatal scope : no validator
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         // test with context 'context' and all scopes  : one validator (Pet)
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         // test with specific context fake and all scopes : no validator
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         // test with context 'context' and info-fatal scopes : one validator (Pet)
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         // test with specific context fake and fatal scope : no validator
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 }