KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > constraints > ConstraintTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.validation.constraints;
21
22 import junit.framework.*;
23 import java.util.ArrayList JavaDoc;
24
25 import org.netbeans.modules.j2ee.sun.validation.Constants;
26 import org.netbeans.modules.j2ee.sun.validation.Failure;
27
28 /**
29  *
30  * @author Rajeshwar Patil
31  * @version %I%, %G%
32  */

33 public class ConstraintTest extends TestCase{
34
35     private ArrayList JavaDoc primaryColours = new ArrayList JavaDoc();
36
37
38     public ConstraintTest(String JavaDoc name){
39         super(name);
40     }
41
42
43     public static void main(String JavaDoc args[]){
44         junit.textui.TestRunner.run(suite());
45     }
46
47
48     public void testCreate() {
49       Constraint constraint = new NumberConstraint();
50       assertNotNull(constraint);
51     }
52
53
54     public void testConstraintFailure(){
55       Failure failure = new ConstraintFailure("Constraint", //NOI18N
56
"Element Value", "Element Name", "Failure Message",
57             "Generic Failure Message"); //NOI18N
58

59       assertNotNull(failure);
60       assertTrue("Failure Message".equals(failure.failureMessage())); //NOI18N
61

62       ConstraintFailure constraintFailure = (ConstraintFailure) failure;
63       assertTrue("Constraint".equals(
64             constraintFailure.getConstraint())); //NOI18N
65
assertTrue("Element Value".equals(
66             constraintFailure.getFailedValue())); //NOI18N
67
assertTrue("Element Name".equals(constraintFailure.getName())); //NOI18N
68
assertTrue("Generic Failure Message".equals(
69             constraintFailure.getGenericfailureMessage())); //NOI18N
70
}
71
72
73     public void testRangeConstraint() {
74       Constraint constraint = new RangeConstraint("100", "250"); //NOI18N
75
assertNotNull(constraint);
76       assertTrue("Value : 152", //NOI18N
77
constraint.match("152", "message").isEmpty()); //NOI18N
78
assertTrue("Value : 300(out of range)", //NOI18N
79
!(constraint.match("300", "message").isEmpty())); //NOI18N
80
assertTrue("Value : xyz(non-numeric)", //NOI18N
81
!(constraint.match("xyz", "message").isEmpty())); //NOI18N
82
}
83
84
85     public void testNonZeroLengthConstraint() {
86       Constraint constraint = new NonZeroLengthConstraint();
87       String JavaDoc str = new String JavaDoc();
88       assertNotNull(constraint);
89       assertNotNull(str);
90       assertTrue("Value : xyz", //NOI18N
91
constraint.match("red", "message").isEmpty()); //NOI18N
92
assertTrue("Value : null", //NOI18N
93
constraint.match(null, "message").isEmpty()); //NOI18N
94
assertTrue("Value : Empty String", //NOI18N
95
!(constraint.match(str, "message").isEmpty())); //NOI18N
96
}
97
98
99     public void testNonBlankConstraint() {
100       Constraint constraint = new NonBlankConstraint();
101       String JavaDoc str = new String JavaDoc();
102       assertNotNull(constraint);
103       assertNotNull(str);
104       assertTrue("Value : xyz", //NOI18N
105
constraint.match("xyz", "message").isEmpty()); //NOI18N
106
assertTrue("Value : null", //NOI18N
107
constraint.match(null, "message").isEmpty()); //NOI18N
108
assertTrue("Value : Empty String", //NOI18N
109
constraint.match(str, "message").isEmpty()); //NOI18N
110
assertTrue("Value : Blank String", //NOI18N
111
!(constraint.match(" ", "message").isEmpty())); //NOI18N
112
}
113
114
115     public void testZeroToMaxIntegerConstraint() {
116       Constraint constraint = new ZeroToMaxIntegerConstraint();
117       String JavaDoc str = new String JavaDoc();
118       assertNotNull(constraint);
119       assertNotNull(str);
120       assertTrue("Value : 0", //NOI18N
121
constraint.match("0", "message").isEmpty()); //NOI18N
122
assertTrue("Value : null", //NOI18N
123
constraint.match(null, "message").isEmpty()); //NOI18N
124
assertTrue("Value : Empty String", //NOI18N
125
constraint.match(str, "message").isEmpty()); //NOI18N
126
assertTrue("Value : Blank String", //NOI18N
127
!(constraint.match(" ", "message").isEmpty())); //NOI18N
128
assertTrue("Value : 1234", //NOI18N
129
constraint.match("1234", "message").isEmpty()); //NOI18N
130
assertTrue("Value : -1", //NOI18N
131
!(constraint.match("-1", "message").isEmpty())); //NOI18N
132
}
133
134
135     public void testIntegerGreaterThanConstraint() {
136       Constraint constraint = new IntegerGreaterThanConstraint("120");
137       String JavaDoc str = new String JavaDoc();
138       assertNotNull(constraint);
139       assertNotNull(str);
140       assertTrue("Value : xyz", //NOI18N
141
!(constraint.match("xyz", "message").isEmpty())); //NOI18N
142
assertTrue("Value : null", //NOI18N
143
constraint.match(null, "message").isEmpty()); //NOI18N
144
assertTrue("Value : Empty String", //NOI18N
145
constraint.match(str, "message").isEmpty()); //NOI18N
146
assertTrue("Value : Blank String", //NOI18N
147
!(constraint.match(" ", "message").isEmpty())); //NOI18N
148
assertTrue("Value : 120", //NOI18N
149
!(constraint.match("120", "message").isEmpty())); //NOI18N
150
assertTrue("Value : 121", //NOI18N
151
constraint.match("121", "message").isEmpty()); //NOI18N
152
}
153
154
155     public void testConstraintUtils() {
156       ConstraintUtils constraintUtils =
157             new ConstraintUtils();
158       assertNotNull(constraintUtils);
159       
160       String JavaDoc message =
161         constraintUtils.formatFailureMessage("Constraint", //NOI18N
162
"Element"); //NOI18N
163
int index = message.lastIndexOf("failed_for"); //NOI18N
164
//Test to make sure that the strings are picked up from the bundle.
165
assertTrue(-1 == index);
166     }
167
168
169     public void testCardinalConstraint() {
170       CardinalConstraint mandatoryConstraint =
171             new CardinalConstraint(Constants.MANDATORY_ELEMENT);
172       CardinalConstraint opationalConstraint =
173             new CardinalConstraint(Constants.OPTIONAL_ELEMENT);
174       CardinalConstraint mandatoryArrayConstraint =
175             new CardinalConstraint(Constants.MANDATORY_ARRAY);
176       CardinalConstraint opationalArrayConstraint =
177             new CardinalConstraint(Constants.OPTIONAL_ARRAY);
178       String JavaDoc[] array = { "abc", "xyz" }; //NOI18N
179
String JavaDoc[] emptyArray = {};
180       
181       assertNotNull(mandatoryConstraint);
182       assertNotNull(opationalConstraint);
183       assertNotNull(mandatoryArrayConstraint);
184       assertNotNull(opationalArrayConstraint);
185       
186       assertTrue("Value : xyz", //NOI18N
187
mandatoryConstraint.match("xyz", "message").isEmpty()); //NOI18N
188
assertTrue("Value : null", //NOI18N
189
!(mandatoryConstraint.match(null, "message").isEmpty())); //NOI18N
190

191       assertTrue("Value : xyz", //NOI18N
192
opationalConstraint.match("xyz", "message").isEmpty()); //NOI18N
193
assertTrue("Value : null", //NOI18N
194
opationalConstraint.match(null, "message").isEmpty()); //NOI18N
195

196       assertTrue("Value : String[]", //NOI18N
197
mandatoryArrayConstraint.match(array, "message").isEmpty()); //NOI18N
198
assertTrue("Value : Empty String[]", //NOI18N
199
!(mandatoryArrayConstraint.match(emptyArray,
200                 "message").isEmpty())); //NOI18N
201

202       assertTrue("Value : String[]", //NOI18N
203
opationalArrayConstraint.match(array, "message").isEmpty()); //NOI18N
204
assertTrue("Value : Empty String[]", //NOI18N
205
opationalArrayConstraint.match(emptyArray,
206                 "message").isEmpty()); //NOI18N
207
}
208
209
210     public void testInConstraint() {
211       ArrayList JavaDoc primaryColours = new ArrayList JavaDoc();
212       primaryColours.add("red"); //NOI18N
213
primaryColours.add("green"); //NOI18N
214
primaryColours.add("yellow"); //NOI18N
215
primaryColours.add("blue"); //NOI18N
216

217       Constraint constraint = new InConstraint(primaryColours);
218       assertNotNull(constraint);
219       assertTrue("Value : red", //NOI18N
220
constraint.match("red", "message").isEmpty()); //NOI18N
221
assertTrue("Value : black(not in enumeration)", //NOI18N
222
!(constraint.match("black", "message").isEmpty())); //NOI18N
223
}
224
225
226     public void testBooleanConstraint() {
227       Constraint constraint = new BooleanConstraint();
228       assertNotNull(constraint);
229       assertTrue("Value : TRUE", //NOI18N
230
constraint.match("TRUE", "message").isEmpty()); //NOI18N
231
assertTrue("Value : xyz", //NOI18N
232
!(constraint.match("xyz", "message").isEmpty())); //NOI18N
233
}
234
235
236     public void testAndConstraint() {
237       Constraint constraint = new AndConstraint(new MandatoryConstraint(),
238           new NumberConstraint());
239       assertNotNull(constraint);
240       assertTrue("Value : 123", //NOI18N
241
constraint.match("123", "message").isEmpty()); //NOI18N
242
assertTrue("Value : xyz", //NOI18N
243
!(constraint.match("xyz", "message").isEmpty())); //NOI18N
244
}
245
246
247     public void testNumberConstraint() {
248       Constraint constraint = new NumberConstraint();
249       assertNotNull(constraint);
250       assertTrue("Value : 1234", //NOI18N
251
constraint.match("1234", "message" ).isEmpty()); //NOI18N
252
assertTrue("Value : abc", //NOI18N
253
!(constraint.match("abc", "message").isEmpty())); //NOI18N
254
}
255
256
257     public void testMandatoryConstraint(){
258           Constraint constraint = new MandatoryConstraint();
259           assertNotNull(constraint);
260           assertTrue("Value : abc", //NOI18N
261
constraint.match("abc", "message").isEmpty()); //NOI18N
262
assertTrue("Value : null length string", //NOI18N
263
constraint.match("", "message").isEmpty()); //NOI18N
264
assertTrue("Value : null", //NOI18N
265
!(constraint.match(null, "message").isEmpty())); //NOI18N
266
}
267
268
269     /**
270     * Define suite of all the Tests to run.
271     */

272     public static Test suite(){
273         TestSuite suite = new TestSuite(ConstraintTest.class);
274         return suite;
275     }
276
277
278     /**
279     * Initialize; allocate any resources needed to perform Tests.
280     */

281     protected void setUp() {
282     }
283
284
285     /**
286     * Free all the resources initilized/allocated to perform Tests.
287     */

288     protected void tearDown() {
289     }
290
291
292     private void nyi() {
293         ///fail("Not yet implemented"); //NOI18N
294
}
295 }
296
Popular Tags