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.field;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.junit.After;
27  import org.junit.AfterClass;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.BeforeClass;
31  import org.nuiton.validator.NuitonValidator;
32  import org.nuiton.validator.NuitonValidatorFactory;
33  import org.nuiton.validator.NuitonValidatorResult;
34  
35  import java.io.File;
36  import java.util.List;
37  
38  /**
39   * Abstract class to test a specific validator.
40   *
41   * To implements a test on a new validator, just extends this class
42   * and implements the method {@link #testValidator()}.
43   *
44   * @param <B> the type of bean to validate.
45   * @author Tony Chemit - chemit@codelutin.com
46   */
47  public abstract class AbstractFieldValidatorTest<B> extends Assert {
48  
49      /** Logger */
50      private static final Log log =
51              LogFactory.getLog(AbstractFieldValidatorTest.class);
52  
53      protected static NuitonValidator<?> cacheValidator;
54  
55      protected static File basedir;
56  
57      protected final Class<B> type;
58  
59      protected NuitonValidator<B> validator;
60  
61      protected B bean;
62  
63      public AbstractFieldValidatorTest(Class<B> type) {
64          this.type = type;
65      }
66  
67      /**
68       * the method to test the given validator on the given bean.
69       *
70       * When coming here a validator and bean were instanciated and the bean was
71       * setted into validator via setBean method.
72       *
73       * @throws Exception if any error ?
74       */
75      public abstract void testValidator() throws Exception;
76  
77      @Before
78      @SuppressWarnings("unchecked")
79      public void setUp() throws Exception {
80          log.debug("start test " + getClass().getSimpleName());
81          bean = type.newInstance();
82          if (cacheValidator == null) {
83              validator = NuitonValidatorFactory.newValidator(type);
84              cacheValidator = validator;
85          } else {
86              validator = (NuitonValidator<B>) cacheValidator;
87          }
88      }
89  
90      @After
91      @SuppressWarnings("unchecked")
92      public void tearDown() {
93      }
94  
95      @AfterClass
96      public static void afterclass() throws Exception {
97          cacheValidator = null;
98      }
99  
100     @BeforeClass
101     public static void initValidator() throws Exception {
102 
103         String b = System.getenv("basedir");
104         if (b == null) {
105             b = new File("").getAbsolutePath();
106         }
107         basedir = new File(b);
108     }
109 
110     @SuppressWarnings("unchecked")
111     protected void assertFieldInError(String fieldName, String error, boolean required) {
112 
113         NuitonValidatorResult result = validator.validate(bean);
114 
115         List<String> errorMessages = result.getErrorMessages(fieldName);
116 
117         boolean errorFound = errorMessages.contains(error);
118 
119         if (required) {
120 
121             // must have this error
122 
123             assertTrue("error " + error + " should not exist but was found.", errorFound);
124         } else {
125 
126             // must not have this error
127             assertFalse("error " + error + " should exist but was not found.", errorFound);
128         }
129     }
130 }