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.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   * To test {@link XWork2NuitonValidatorProvider}.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 2.0
49   */
50  public class XWork2NuitonValidatorProviderTest {
51  
52      /** Logger. */
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         // test with all context and all scopes : two validators (Person + Pet + Pet (context))
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         // test with no context and only scope warning : one validator (Person)
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         // test with context 'context' and all scopes  : one validator (Pet)
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         // test with no context and only scope fatal : no validator
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         // test with specific context fake and all scopes : no validator
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 }