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 CollectionUniqueKeyValidatorTest extends AbstractValidatorBeanFieldValidatorTest {
33  
34      static protected ValidatorBeanEntry beanEntry = new ValidatorBeanEntry(0, "stringValue");
35  
36      static protected ValidatorBeanEntry beanEntry2 = new ValidatorBeanEntry(0, "fake");
37  
38      static protected ValidatorBeanEntry beanEntry3 = new ValidatorBeanEntry(0, "stringValue", "stringValue2");
39  
40      @Test
41      @Override
42      public void testValidator() throws Exception {
43          assertNull(bean.getEntries());
44  
45          // no entry
46          assertFieldInError("entries", "collectionUniqueKey.one.failed", false);
47          assertFieldInError("entries", "collectionUniqueKey.two.failed", false);
48          assertFieldInError("entries", "collectionUniqueKey.three.failed", false);
49          assertFieldInError("entries", "collectionUniqueKey.four.failed", false);
50          assertFieldInError("entries", "collectionUniqueKey.five.failed", false);
51  
52          // add a entry
53          bean.setEntries(Arrays.asList(beanEntry));
54  
55          assertFieldInError("entries", "collectionUniqueKey.one.failed", false);
56          assertFieldInError("entries", "collectionUniqueKey.two.failed", false);
57          assertFieldInError("entries", "collectionUniqueKey.three.failed", false);
58          assertFieldInError("entries", "collectionUniqueKey.four.failed", false);
59          assertFieldInError("entries", "collectionUniqueKey.five.failed", false);
60  
61          // add violating property
62          bean.setEntry(beanEntry3);
63          assertFieldInError("entries", "collectionUniqueKey.one.failed", false);
64          assertFieldInError("entries", "collectionUniqueKey.two.failed", false);
65          assertFieldInError("entries", "collectionUniqueKey.three.failed", false);
66          assertFieldInError("entries", "collectionUniqueKey.four.failed", false);
67          assertFieldInError("entries", "collectionUniqueKey.five.failed", true);
68  
69  
70          // two entries with same key
71          bean.setEntries(Arrays.asList(beanEntry, beanEntry));
72  
73          assertFieldInError("entries", "collectionUniqueKey.one.failed", true);
74          assertFieldInError("entries", "collectionUniqueKey.two.failed", true);
75          assertFieldInError("entries", "collectionUniqueKey.three.failed", true);
76          assertFieldInError("entries", "collectionUniqueKey.four.failed", true);
77  
78          // add a entry
79          bean.setEntries(Arrays.asList(beanEntry2));
80  
81          assertFieldInError("entries", "collectionUniqueKey.one.failed", false);
82          assertFieldInError("entries", "collectionUniqueKey.two.failed", false);
83          assertFieldInError("entries", "collectionUniqueKey.three.failed", false);
84          assertFieldInError("entries", "collectionUniqueKey.four.failed", false);
85  
86          // add two entries (will violated unique key on intValue)
87          bean.setEntries(Arrays.asList(beanEntry2, beanEntry));
88  
89          assertFieldInError("entries", "collectionUniqueKey.one.failed", true);
90          assertFieldInError("entries", "collectionUniqueKey.two.failed", false);
91          assertFieldInError("entries", "collectionUniqueKey.three.failed", false);
92          assertFieldInError("entries", "collectionUniqueKey.four.failed", false);
93  
94  
95          // two entries with same key (except validator four)
96          bean.setEntries(Arrays.asList(beanEntry, beanEntry3));
97          assertFieldInError("entries", "collectionUniqueKey.one.failed", true);
98          assertFieldInError("entries", "collectionUniqueKey.two.failed", true);
99          assertFieldInError("entries", "collectionUniqueKey.three.failed", true);
100         assertFieldInError("entries", "collectionUniqueKey.four.failed", false);
101 
102         beanEntry.setStringValue2("stringValue2");
103         // two entries with same key
104         bean.setEntries(Arrays.asList(beanEntry, beanEntry3));
105         assertFieldInError("entries", "collectionUniqueKey.one.failed", true);
106         assertFieldInError("entries", "collectionUniqueKey.two.failed", true);
107         assertFieldInError("entries", "collectionUniqueKey.three.failed", true);
108         assertFieldInError("entries", "collectionUniqueKey.four.failed", true);
109 
110 
111     }
112 }