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;
23  
24  import org.junit.After;
25  import org.junit.Assert;
26  import org.junit.Before;
27  
28  import java.io.File;
29  import java.util.Iterator;
30  import java.util.SortedSet;
31  import java.util.regex.Pattern;
32  
33  /**
34   * Abstract test to detects and test your validators.
35   *
36   * @author Tony Chemit - chemit@codelutin.com
37   * @since 2.0
38   */
39  public abstract class AbstractValidatorDetectorTest {
40  
41      protected final String providerName;
42  
43      protected File rootDirectory;
44  
45      public AbstractValidatorDetectorTest(String providerName) {
46          this.providerName = providerName;
47      }
48  
49      protected NuitonValidatorProvider provider;
50  
51      protected abstract File getRootDirectory(File basedir);
52  
53      @Before
54      public void setUp() throws Exception {
55          provider = NuitonValidatorFactory.getProvider(providerName);
56          rootDirectory = getRootDirectory(ValidatorTestHelper.getBasedir());
57      }
58  
59      @After
60      public void tearDown() throws Exception {
61          provider = null;
62      }
63  
64      protected SortedSet<NuitonValidator<?>> detectValidators(Class<?>... types) {
65          return detectValidators(null, NuitonValidatorScope.values(), types);
66      }
67  
68      protected SortedSet<NuitonValidator<?>> detectValidators(Pattern context, Class<?>... types) {
69          return detectValidators(context, NuitonValidatorScope.values(), types);
70      }
71  
72      protected SortedSet<NuitonValidator<?>> detectValidators(Pattern context, NuitonValidatorScope[] scopes, Class<?>... types) {
73          SortedSet<NuitonValidator<?>> validators =
74                  provider.detectValidators(rootDirectory, context, scopes, types);
75          return validators;
76      }
77  
78      public void assertValidatorModel(NuitonValidator<?> validator,
79                                       String expectedContext,
80                                       Class<?> expectedType,
81                                       NuitonValidatorScope... expectedScopes) {
82          ValidatorTestHelper.assertValidatorModel(
83                  validator,
84                  expectedContext,
85                  expectedType,
86                  expectedScopes
87          );
88      }
89  
90      public void assertValidatorEffectiveScopes(NuitonValidator<?> validator,
91                                                 NuitonValidatorScope... expectedScopes) {
92          ValidatorTestHelper.assertValidatorEffectiveScopes(validator,
93                                                             expectedScopes
94          );
95      }
96  
97      public void assertValidatorEffectiveFields(NuitonValidator<?> validator,
98                                                 String... expectedFields) {
99  
100         ValidatorTestHelper.assertValidatorEffectiveFields(validator,
101                                                            expectedFields);
102     }
103 
104     public void assertValidatorEffectiveFields(NuitonValidator<?> validator,
105                                                NuitonValidatorScope scope,
106                                                String... expectedFields) {
107         ValidatorTestHelper.assertValidatorEffectiveFields(validator,
108                                                            scope,
109                                                            expectedFields);
110     }
111 
112     public void assertValidatorSetWithMultiContextName(SortedSet<NuitonValidator<?>> result,
113                                                        Object... contextThenClass) {
114 
115         Assert.assertNotNull(result);
116         Assert.assertEquals(contextThenClass.length / 2, result.size());
117 
118         Iterator<NuitonValidator<?>> itr = result.iterator();
119         int index = 0;
120 
121         NuitonValidatorScope[] scopes = NuitonValidatorScope.values();
122 
123         while (itr.hasNext()) {
124             NuitonValidator<?> next = itr.next();
125             ValidatorTestHelper.assertValidatorModel(next,
126                                                      (String) contextThenClass[2 * index],
127                                                      (Class<?>) contextThenClass[2 * index + 1],
128                                                      scopes);
129             index++;
130         }
131 
132     }
133 
134     public void assertValidatorSetWithSameContextName(SortedSet<NuitonValidator<?>> result,
135                                                       String context,
136                                                       Class<?>... contextThenClass) {
137 
138         Assert.assertNotNull(result);
139         Assert.assertEquals(contextThenClass.length, result.size());
140 
141         Iterator<NuitonValidator<?>> itr = result.iterator();
142         int index = 0;
143 
144         NuitonValidatorScope[] scopes = NuitonValidatorScope.values();
145 
146         while (itr.hasNext()) {
147             NuitonValidator<?> next = itr.next();
148             ValidatorTestHelper.assertValidatorModel(next,
149                                                      context,
150                                                      contextThenClass[index++],
151                                                      scopes);
152         }
153 
154     }
155 
156 
157 }