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.bean.simple;
23  
24  import com.google.common.collect.Lists;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.junit.After;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.nuiton.validator.NuitonValidatorScope;
32  import org.nuiton.validator.bean.SimpleBean;
33  
34  import java.util.Arrays;
35  import java.util.List;
36  
37  /**
38   * To test the {@link SimpleBeanValidator}.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 2.5.2
42   */
43  public class SimpleBeanValidatorTest {
44  
45      /** Logger */
46      private static final Log log =
47              LogFactory.getLog(SimpleBeanValidatorTest.class);
48  
49      protected SimpleBeanValidator<SimpleBean> validator;
50  
51      protected SimpleBean bean;
52  
53      ValidatorListenerImpl fatalListener;
54  
55      ValidatorListenerImpl errorListener;
56  
57      ValidatorListenerImpl warningListener;
58  
59      ValidatorListenerImpl infoListener;
60  
61      @Before
62      public void setUp() {
63  
64          bean = new SimpleBean();
65      }
66  
67      protected void prepareValidator(String context) {
68  
69          validator = SimpleBeanValidator.newValidator(SimpleBean.class, context);
70  
71          validator.addSimpleBeanValidatorListener(fatalListener = new ValidatorListenerImpl(NuitonValidatorScope.FATAL));
72          validator.addSimpleBeanValidatorListener(errorListener = new ValidatorListenerImpl(NuitonValidatorScope.ERROR));
73          validator.addSimpleBeanValidatorListener(warningListener = new ValidatorListenerImpl(NuitonValidatorScope.WARNING));
74          validator.addSimpleBeanValidatorListener(infoListener = new ValidatorListenerImpl(NuitonValidatorScope.INFO));
75      }
76  
77      @After
78      public void tearDown() {
79          bean = null;
80          if (validator != null) {
81              validator.setBean(null);
82              validator = null;
83          }
84      }
85  
86      private static final String STRING_VALUE_FATAL = "stringValue.fatal";
87  
88      private static final String STRING_VALUE_ERROR = "stringValue.error";
89  
90      private static final String STRING_VALUE_WARNING = "stringValue.warning";
91  
92      private static final String INT_VALUE_FATAL = "intValue.fatal";
93  
94      private static final String INT_VALUE_ERROR = "intValue.error";
95  
96      private static final String INT_VALUE_INFO = "intValue.info";
97  
98      @Test(expected = IllegalStateException.class)
99      public void testValidateWithBad() {
100 
101         // with marchepo context, there is a unknown field in scope error
102 
103         prepareValidator("marchepo");
104     }
105 
106     @Test
107     public void validate() {
108 
109         prepareValidator(null);
110 
111         assertMessages(fatalListener);
112         assertMessages(errorListener);
113         assertMessages(warningListener);
114         assertMessages(infoListener);
115 
116         if (log.isDebugEnabled()) {
117             log.debug("-----------------------------------------------");
118         }
119         validator.setBean(bean);
120 
121         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
122         assertMessages(errorListener, STRING_VALUE_ERROR, INT_VALUE_ERROR);
123         assertMessages(warningListener, STRING_VALUE_WARNING);
124         assertMessages(infoListener, INT_VALUE_INFO);
125 
126         if (log.isDebugEnabled()) {
127             log.debug("-----------------------------------------------");
128         }
129         bean.setStringValue("one");
130 
131         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
132         assertMessages(errorListener, INT_VALUE_ERROR);
133         assertMessages(warningListener, STRING_VALUE_WARNING);
134         assertMessages(infoListener, INT_VALUE_INFO);
135 
136         if (log.isDebugEnabled()) {
137             log.debug("-----------------------------------------------");
138         }
139         bean.setStringValue("oneone");
140 
141         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
142         assertMessages(errorListener, INT_VALUE_ERROR);
143         assertMessages(warningListener);
144         assertMessages(infoListener, INT_VALUE_INFO);
145 
146         if (log.isDebugEnabled()) {
147             log.debug("-----------------------------------------------");
148         }
149         bean.setIntValue(1);
150 
151         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
152         assertMessages(errorListener);
153         assertMessages(warningListener);
154         assertMessages(infoListener, INT_VALUE_INFO);
155 
156         if (log.isDebugEnabled()) {
157             log.debug("-----------------------------------------------");
158         }
159         bean.setIntValue(10);
160 
161         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
162         assertMessages(errorListener);
163         assertMessages(warningListener);
164         assertMessages(infoListener);
165 
166         if (log.isDebugEnabled()) {
167             log.debug("-----------------------------------------------");
168         }
169 
170         bean.setStringValue(null);
171         bean.setIntValue(0);
172 
173         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
174         assertMessages(errorListener, STRING_VALUE_ERROR, INT_VALUE_ERROR);
175         assertMessages(warningListener, STRING_VALUE_WARNING);
176         assertMessages(infoListener, INT_VALUE_INFO);
177 
178 
179         if (log.isDebugEnabled()) {
180             log.debug("-----------------------------------------------");
181         }
182 
183         bean.setStringValue("5");
184         bean.setIntValue(5);
185         assertMessages(fatalListener);
186         assertMessages(errorListener);
187         assertMessages(warningListener, STRING_VALUE_WARNING);
188         assertMessages(infoListener, INT_VALUE_INFO);
189     }
190 
191     @Test
192     public void convert() {
193 
194         prepareValidator(null);
195 
196         assertMessages(errorListener);
197         assertMessages(warningListener);
198         assertMessages(infoListener);
199 
200         if (log.isDebugEnabled()) {
201             log.debug("-----------------------------------------------");
202         }
203 
204         validator.setBean(bean);
205 
206         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
207         assertMessages(errorListener, STRING_VALUE_ERROR, INT_VALUE_ERROR);
208         assertMessages(warningListener, STRING_VALUE_WARNING);
209         assertMessages(infoListener, INT_VALUE_INFO);
210 
211 
212         if (log.isDebugEnabled()) {
213             log.debug("-----------------------------------------------");
214         }
215 
216         Object value = validator.convert("intValue", "abc", Class.class);
217 
218         Assert.assertNull(value);
219 
220         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
221         assertMessages(errorListener, STRING_VALUE_ERROR, "error.convertor.class");
222         assertMessages(warningListener, STRING_VALUE_WARNING);
223         assertMessages(infoListener, INT_VALUE_INFO);
224 
225         if (log.isDebugEnabled()) {
226             log.debug("-----------------------------------------------");
227         }
228         bean.setStringValue("one");
229 
230         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
231         assertMessages(errorListener, "error.convertor.class");
232         assertMessages(warningListener, STRING_VALUE_WARNING);
233         assertMessages(infoListener, INT_VALUE_INFO);
234 
235         if (log.isDebugEnabled()) {
236             log.debug("-----------------------------------------------");
237         }
238 
239         value = validator.convert("intValue", "3", Integer.class);
240 
241         bean.setIntValue((Integer) value);
242 
243         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
244         assertMessages(errorListener);
245         assertMessages(warningListener, STRING_VALUE_WARNING);
246         assertMessages(infoListener, INT_VALUE_INFO);
247 
248         if (log.isDebugEnabled()) {
249             log.debug("-----------------------------------------------");
250         }
251 
252         bean.setIntValue(-1);
253         assertMessages(fatalListener, STRING_VALUE_FATAL, INT_VALUE_FATAL);
254         assertMessages(errorListener, INT_VALUE_ERROR);
255         assertMessages(warningListener, STRING_VALUE_WARNING);
256         assertMessages(infoListener, INT_VALUE_INFO);
257 
258         if (log.isDebugEnabled()) {
259             log.debug("-----------------------------------------------");
260         }
261     }
262 
263     void assertMessages(ValidatorListenerImpl listener,
264                         String... expected) {
265         List<String> actual = listener.getMessages();
266         Assert.assertEquals(" shoudl have " +
267                             Arrays.toString(expected) + " but had " + actual,
268                             expected.length, actual.size());
269         for (String m : expected) {
270             Assert.assertEquals("could not find " + m + " in " + actual,
271                                 true, actual.contains(m));
272         }
273     }
274 
275     static class ValidatorListenerImpl implements SimpleBeanValidatorListener {
276 
277         final NuitonValidatorScope scope;
278 
279         public ValidatorListenerImpl(NuitonValidatorScope scope) {
280             this.scope = scope;
281         }
282 
283         List<String> messages = Lists.newArrayList();
284 
285         public List<String> getMessages() {
286             return messages;
287         }
288 
289         @Override
290         public void onFieldChanged(SimpleBeanValidatorEvent event) {
291             if (scope == event.getScope()) {
292                 String[] messagesToDelete = event.getMessagesToDelete();
293                 if (messagesToDelete != null && messagesToDelete.length > 0) {
294                     if (log.isDebugEnabled()) {
295                         log.debug(event.getScope() + " messages to delete : " + Arrays.toString(messagesToDelete));
296                     }
297                     for (String m : messagesToDelete) {
298                         messages.remove(m);
299                     }
300                 }
301                 String[] messagesToAdd = event.getMessagesToAdd();
302                 if (messagesToAdd != null && messagesToAdd.length > 0) {
303                     if (log.isDebugEnabled()) {
304                         log.debug(event.getScope() + " messages to add : " + Arrays.toString(messagesToAdd));
305                     }
306                     messages.addAll(Arrays.asList(messagesToAdd));
307                 }
308             }
309         }
310     }
311 }