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 com.opensymphony.xwork2.ActionContext;
25  import org.junit.Test;
26  
27  import java.util.Locale;
28  
29  /**
30   * @author Tony Chemit - chemit@codelutin.com
31   */
32  public class FieldExpressionWithParamsValidatorTest extends AbstractFieldValidatorTest<FieldExpressionBean> {
33  
34      public static final String MESSAGE = "expression.too.big##100";
35  
36      public static final String MESSAGE2 = "expression.too.big##100##2000";
37  
38      public FieldExpressionWithParamsValidatorTest() {
39          super(FieldExpressionBean.class);
40      }
41  
42      @Test
43      @Override
44      public void testValidator() throws Exception {
45  
46          ActionContext.getContext().setLocale(Locale.ENGLISH);
47  
48          testBooleanType();
49          testShortType();
50          testIntType();
51          testLongType();
52          testDoubleType();
53          testStringType();
54  
55  
56      }
57  
58      protected void testBooleanType() {
59  
60          assertEquals(false, bean.isBooleanValue());
61          assertFieldInError("booleanValue", "expression.boolean.not.equals##true", true);
62          assertFieldInError("booleanValue", "expression.boolean.not.equals##false", false);
63  
64          bean.setBooleanValue(true);
65          assertFieldInError("booleanValue", "expression.boolean.not.equals##true", false);
66          assertFieldInError("booleanValue", "expression.boolean.not.equals##false", true);
67      }
68  
69      protected void testShortType() {
70          assertEquals(0, bean.getShortValue());
71          assertFieldInError("shortValue", MESSAGE, false);
72          assertFieldInError("shortValue", MESSAGE2, false);
73          bean.setShortValue((short) 10);
74          assertFieldInError("shortValue", MESSAGE, false);
75          assertFieldInError("shortValue", MESSAGE2, false);
76          bean.setShortValue((short) 1000);
77          assertFieldInError("shortValue", MESSAGE, true);
78          assertFieldInError("shortValue", MESSAGE2, false);
79          bean.setShortValue((short) 3000);
80          assertFieldInError("shortValue", MESSAGE, true);
81          assertFieldInError("shortValue", MESSAGE2, true);
82      }
83  
84      protected void testIntType() {
85          assertEquals(0, bean.getIntValue());
86          assertFieldInError("intValue", MESSAGE, false);
87          assertFieldInError("intValue", MESSAGE2, false);
88          bean.setIntValue(10);
89          assertFieldInError("intValue", MESSAGE, false);
90          assertFieldInError("intValue", MESSAGE2, false);
91          bean.setIntValue(1000);
92          assertFieldInError("intValue", MESSAGE, true);
93          assertFieldInError("intValue", MESSAGE2, false);
94          bean.setIntValue(3000);
95          assertFieldInError("intValue", MESSAGE, true);
96          assertFieldInError("intValue", MESSAGE2, true);
97      }
98  
99      protected void testLongType() {
100         assertEquals(0, bean.getLongValue());
101         assertFieldInError("longValue", MESSAGE, false);
102         assertFieldInError("longValue", MESSAGE2, false);
103         bean.setLongValue(10);
104         assertFieldInError("longValue", MESSAGE, false);
105         assertFieldInError("longValue", MESSAGE2, false);
106         bean.setLongValue(1000);
107         assertFieldInError("longValue", MESSAGE, true);
108         assertFieldInError("longValue", MESSAGE2, false);
109         bean.setLongValue(3000);
110         assertFieldInError("longValue", MESSAGE, true);
111         assertFieldInError("longValue", MESSAGE2, true);
112     }
113 
114     protected void testDoubleType() {
115         assertEquals(0.0, bean.getDoubleValue(), 0);
116         assertFieldInError("doubleValue", MESSAGE, false);
117         assertFieldInError("doubleValue", "expression.too.big##100##2000", false);
118         bean.setDoubleValue(10);
119         assertFieldInError("doubleValue", MESSAGE, false);
120         assertFieldInError("doubleValue", "expression.too.big##100##2000", false);
121         bean.setDoubleValue(1000);
122         assertFieldInError("doubleValue", MESSAGE, true);
123         assertFieldInError("doubleValue", "expression.too.big##100##2000", false);
124         bean.setDoubleValue(3000);
125         assertFieldInError("doubleValue", MESSAGE, true);
126         assertFieldInError("doubleValue", "expression.too.big##100##2000", true);
127     }
128 
129     protected void testStringType() {
130         assertEquals(null, bean.getStringValue());
131 
132         assertFieldInError("stringValue", "expression.stringNotValue##1000", true);
133         assertFieldInError("stringValue", "expression.stringNotValue##1000##3000", true);
134         bean.setStringValue("100");
135 
136         assertFieldInError("stringValue", "expression.stringNotValue##1000", true);
137         assertFieldInError("stringValue", "expression.stringNotValue##1000##3000", true);
138 
139         bean.setStringValue("1000");
140         assertFieldInError("stringValue", "expression.stringNotValue##1000", false);
141         assertFieldInError("stringValue", "expression.stringNotValue##1000##3000", false);
142 
143         bean.setStringValue("3000");
144         assertFieldInError("stringValue", "expression.stringNotValue##1000", true);
145         assertFieldInError("stringValue", "expression.stringNotValue##1000##3000", false);
146     }
147 }