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.junit.Test;
25  import org.nuiton.validator.xwork2.field.ValidatorBean.ValidatorBeanEntry;
26  
27  import java.util.Arrays;
28  
29  /**
30   * @author Tony Chemit - chemit@codelutin.com
31   */
32  public class CollectionFieldExpressionValidatorTest extends AbstractValidatorBeanFieldValidatorTest {
33  
34      protected static final String PROPERTY = "entries";
35  
36      static protected ValidatorBeanEntry beanEntry0 = new ValidatorBeanEntry(0, "stringValue");
37  
38      static protected ValidatorBeanEntry beanEntry0Bis = new ValidatorBeanEntry(0, "fake");
39  
40      static protected ValidatorBeanEntry beanEntry1 = new ValidatorBeanEntry(1, "fake");
41  
42      static protected ValidatorBeanEntry beanEntry3 = new ValidatorBeanEntry(3, "fake");
43  
44      static protected ValidatorBeanEntry beanEntry5 = new ValidatorBeanEntry(5, "fake");
45  
46      @Test
47      @Override
48      public void testValidator() throws Exception {
49          assertNull(bean.getEntries());
50  
51          // no entry
52          assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne", true);
53          assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne", true);
54          assertFieldInError(PROPERTY, "collectionFieldExpression.all", false);
55          assertFieldInError(PROPERTY, "collectionFieldExpression.none", false);
56  
57  
58          // add a matching etry
59          bean.setEntries(Arrays.asList(beanEntry0));
60  
61          assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne", false);
62          assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne", false);
63          assertFieldInError(PROPERTY, "collectionFieldExpression.all", false);
64          assertFieldInError(PROPERTY, "collectionFieldExpression.none", true);
65  
66          // two matching etries
67          bean.setEntries(Arrays.asList(beanEntry0, beanEntry0));
68  
69          assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne", false);
70          assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne", true);
71          assertFieldInError(PROPERTY, "collectionFieldExpression.all", false);
72          assertFieldInError(PROPERTY, "collectionFieldExpression.none", true);
73  
74          // add a none matching etry
75          bean.setEntries(Arrays.asList(beanEntry0Bis));
76  
77          assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne", true);
78          assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne", true);
79          assertFieldInError(PROPERTY, "collectionFieldExpression.all", true);
80          assertFieldInError(PROPERTY, "collectionFieldExpression.none", false);
81  
82          // add a none matching etry and a matching entry
83          bean.setEntries(Arrays.asList(beanEntry0Bis, beanEntry0));
84  
85          assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne", false);
86          assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne", false);
87          assertFieldInError(PROPERTY, "collectionFieldExpression.all", true);
88          assertFieldInError(PROPERTY, "collectionFieldExpression.none", true);
89      }
90  
91      @Test
92      public void testValidatorWithContext() throws Exception {
93          assertNull(bean.getEntries());
94  
95          // no entry
96          assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", true);
97          assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
98          assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", false);
99          assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
100 
101         // add a matching etry
102         bean.setEntries(Arrays.asList(beanEntry0));
103 
104         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", true);
105         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
106         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", true);
107         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
108 
109         // add a none matching etry
110         bean.setEntries(Arrays.asList(beanEntry0Bis));
111 
112         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", true);
113         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
114         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", true);
115         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
116 
117         // add a none matching etry and a matching entry
118         bean.setEntries(Arrays.asList(beanEntry0Bis, beanEntry0));
119 
120         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", true);
121         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
122         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", true);
123         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
124 
125         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1));
126         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", false);
127         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", false);
128         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
129         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
130 
131         bean.setEntries(Arrays.asList(beanEntry1, beanEntry0));
132         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", true);
133         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", true);
134         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
135         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
136 
137         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3));
138         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", false);
139         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", false);
140         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", false);
141         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", true);
142 
143         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3, beanEntry5));
144         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", false);
145         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", false);
146         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", true);
147         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", true);
148 
149         bean.setEntries(Arrays.asList(beanEntry0, beanEntry3, beanEntry1));
150         assertFieldInError(PROPERTY, "collectionFieldExpression.atLeastOne.useSensitiveContext", false);
151         assertFieldInError(PROPERTY, "collectionFieldExpression.all.useSensitiveContext", true);
152         assertFieldInError(PROPERTY, "collectionFieldExpression.exactlyOne.useSensitiveContext", false);
153         assertFieldInError(PROPERTY, "collectionFieldExpression.none.useSensitiveContext", false);
154     }
155 
156     @Test
157     public void testValidatorWithContextAndFirst() throws Exception {
158         assertNull(bean.getEntries());
159         String message = "collectionFieldExpression.all.useFirst";
160 
161         // no entry
162         assertFieldInError(PROPERTY, message, false);
163 
164         bean.setEntries(Arrays.asList(beanEntry0));
165         assertFieldInError(PROPERTY, message, false);
166 
167 
168         bean.setEntries(Arrays.asList(beanEntry0Bis));
169         assertFieldInError(PROPERTY, message, false);
170 
171         bean.setEntries(Arrays.asList(beanEntry1));
172         assertFieldInError(PROPERTY, message, true);
173 
174         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1));
175         assertFieldInError(PROPERTY, message, false);
176 
177         bean.setEntries(Arrays.asList(beanEntry1, beanEntry0));
178         assertFieldInError(PROPERTY, message, true);
179 
180         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3));
181         assertFieldInError(PROPERTY, message, false);
182 
183         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3, beanEntry5));
184         assertFieldInError(PROPERTY, message, false);
185 
186         bean.setEntries(Arrays.asList(beanEntry0, beanEntry3, beanEntry1));
187         assertFieldInError(PROPERTY, message, true);
188     }
189 
190     @Test
191     public void testValidatorWithContextAndLast() throws Exception {
192         assertNull(bean.getEntries());
193         String message = "collectionFieldExpression.all.useLast";
194 
195         // no entry
196         assertFieldInError(PROPERTY, message, false);
197 
198         bean.setEntries(Arrays.asList(beanEntry0));
199         assertFieldInError(PROPERTY, message, true);
200 
201 
202         bean.setEntries(Arrays.asList(beanEntry0Bis));
203         assertFieldInError(PROPERTY, message, true);
204 
205         bean.setEntries(Arrays.asList(beanEntry1));
206         assertFieldInError(PROPERTY, message, false);
207 
208         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1));
209         assertFieldInError(PROPERTY, message, false);
210 
211         bean.setEntries(Arrays.asList(beanEntry1, beanEntry0));
212         assertFieldInError(PROPERTY, message, true);
213 
214         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3));
215         assertFieldInError(PROPERTY, message, false);
216 
217         bean.setEntries(Arrays.asList(beanEntry1, beanEntry3));
218         assertFieldInError(PROPERTY, message, false);
219 
220         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3, beanEntry5));
221         assertFieldInError(PROPERTY, message, false);
222 
223         bean.setEntries(Arrays.asList(beanEntry0, beanEntry3, beanEntry1));
224         assertFieldInError(PROPERTY, message, true);
225     }
226 
227     @Test
228     public void testValidatorWithContextAndFirstAndLast() throws Exception {
229         assertNull(bean.getEntries());
230 
231         String message = "collectionFieldExpression.all.useFirstAndLast";
232         // no entry
233         assertFieldInError(PROPERTY, message, false);
234 
235         bean.setEntries(Arrays.asList(beanEntry0));
236         assertFieldInError(PROPERTY, message, true);
237 
238 
239         bean.setEntries(Arrays.asList(beanEntry0Bis));
240         assertFieldInError(PROPERTY, message, true);
241 
242         bean.setEntries(Arrays.asList(beanEntry1));
243         assertFieldInError(PROPERTY, message, true);
244 
245         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1));
246         assertFieldInError(PROPERTY, message, false);
247 
248         bean.setEntries(Arrays.asList(beanEntry1, beanEntry0));
249         assertFieldInError(PROPERTY, message, true);
250 
251         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3));
252         assertFieldInError(PROPERTY, message, false);
253 
254         bean.setEntries(Arrays.asList(beanEntry1, beanEntry3));
255         assertFieldInError(PROPERTY, message, true);
256 
257         bean.setEntries(Arrays.asList(beanEntry0, beanEntry1, beanEntry3, beanEntry5));
258         assertFieldInError(PROPERTY, message, false);
259 
260         bean.setEntries(Arrays.asList(beanEntry0, beanEntry3, beanEntry1));
261         assertFieldInError(PROPERTY, message, true);
262     }
263 }