KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > constraints > ConstraintTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * ConstraintTest.java March 24, 2003, 11:04 AM
26  */

27
28 package com.sun.enterprise.tools.common.validation.constraints;
29
30 import junit.framework.*;
31 import java.util.ArrayList JavaDoc;
32
33 import com.sun.enterprise.tools.common.validation.Constants;
34 import com.sun.enterprise.tools.common.validation.Failure;
35
36 /**
37  *
38  * @author Rajeshwar Patil
39  * @version %I%, %G%
40  */

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

67       assertNotNull(failure);
68       assertTrue("Failure Message".equals(failure.failureMessage())); //NOI18N
69

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

199       assertTrue("Value : xyz", //NOI18N
200
opationalConstraint.match("xyz", "message").isEmpty()); //NOI18N
201
assertTrue("Value : null", //NOI18N
202
opationalConstraint.match(null, "message").isEmpty()); //NOI18N
203

204       assertTrue("Value : String[]", //NOI18N
205
mandatoryArrayConstraint.match(array, "message").isEmpty()); //NOI18N
206
assertTrue("Value : Empty String[]", //NOI18N
207
!(mandatoryArrayConstraint.match(emptyArray,
208                 "message").isEmpty())); //NOI18N
209

210       assertTrue("Value : String[]", //NOI18N
211
opationalArrayConstraint.match(array, "message").isEmpty()); //NOI18N
212
assertTrue("Value : Empty String[]", //NOI18N
213
opationalArrayConstraint.match(emptyArray,
214                 "message").isEmpty()); //NOI18N
215
}
216
217
218     public void testInConstraint() {
219       ArrayList JavaDoc primaryColours = new ArrayList JavaDoc();
220       primaryColours.add("red"); //NOI18N
221
primaryColours.add("green"); //NOI18N
222
primaryColours.add("yellow"); //NOI18N
223
primaryColours.add("blue"); //NOI18N
224

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

280     public static Test suite(){
281         TestSuite suite = new TestSuite(ConstraintTest.class);
282         return suite;
283     }
284
285
286     /**
287     * Initialize; allocate any resources needed to perform Tests.
288     */

289     protected void setUp() {
290     }
291
292
293     /**
294     * Free all the resources initilized/allocated to perform Tests.
295     */

296     protected void tearDown() {
297     }
298
299
300     private void nyi() {
301         fail("Not yet implemented"); //NOI18N
302
}
303 }
304
Popular Tags