1 19 20 package org.netbeans.modules.j2ee.sun.validation.constraints; 21 22 import junit.framework.*; 23 import java.util.ArrayList ; 24 25 import org.netbeans.modules.j2ee.sun.validation.Constants; 26 import org.netbeans.modules.j2ee.sun.validation.Failure; 27 28 33 public class ConstraintTest extends TestCase{ 34 35 private ArrayList primaryColours = new ArrayList (); 36 37 38 public ConstraintTest(String name){ 39 super(name); 40 } 41 42 43 public static void main(String 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", "Element Value", "Element Name", "Failure Message", 57 "Generic Failure Message"); 59 assertNotNull(failure); 60 assertTrue("Failure Message".equals(failure.failureMessage())); 62 ConstraintFailure constraintFailure = (ConstraintFailure) failure; 63 assertTrue("Constraint".equals( 64 constraintFailure.getConstraint())); assertTrue("Element Value".equals( 66 constraintFailure.getFailedValue())); assertTrue("Element Name".equals(constraintFailure.getName())); assertTrue("Generic Failure Message".equals( 69 constraintFailure.getGenericfailureMessage())); } 71 72 73 public void testRangeConstraint() { 74 Constraint constraint = new RangeConstraint("100", "250"); assertNotNull(constraint); 76 assertTrue("Value : 152", constraint.match("152", "message").isEmpty()); assertTrue("Value : 300(out of range)", !(constraint.match("300", "message").isEmpty())); assertTrue("Value : xyz(non-numeric)", !(constraint.match("xyz", "message").isEmpty())); } 83 84 85 public void testNonZeroLengthConstraint() { 86 Constraint constraint = new NonZeroLengthConstraint(); 87 String str = new String (); 88 assertNotNull(constraint); 89 assertNotNull(str); 90 assertTrue("Value : xyz", constraint.match("red", "message").isEmpty()); assertTrue("Value : null", constraint.match(null, "message").isEmpty()); assertTrue("Value : Empty String", !(constraint.match(str, "message").isEmpty())); } 97 98 99 public void testNonBlankConstraint() { 100 Constraint constraint = new NonBlankConstraint(); 101 String str = new String (); 102 assertNotNull(constraint); 103 assertNotNull(str); 104 assertTrue("Value : xyz", constraint.match("xyz", "message").isEmpty()); assertTrue("Value : null", constraint.match(null, "message").isEmpty()); assertTrue("Value : Empty String", constraint.match(str, "message").isEmpty()); assertTrue("Value : Blank String", !(constraint.match(" ", "message").isEmpty())); } 113 114 115 public void testZeroToMaxIntegerConstraint() { 116 Constraint constraint = new ZeroToMaxIntegerConstraint(); 117 String str = new String (); 118 assertNotNull(constraint); 119 assertNotNull(str); 120 assertTrue("Value : 0", constraint.match("0", "message").isEmpty()); assertTrue("Value : null", constraint.match(null, "message").isEmpty()); assertTrue("Value : Empty String", constraint.match(str, "message").isEmpty()); assertTrue("Value : Blank String", !(constraint.match(" ", "message").isEmpty())); assertTrue("Value : 1234", constraint.match("1234", "message").isEmpty()); assertTrue("Value : -1", !(constraint.match("-1", "message").isEmpty())); } 133 134 135 public void testIntegerGreaterThanConstraint() { 136 Constraint constraint = new IntegerGreaterThanConstraint("120"); 137 String str = new String (); 138 assertNotNull(constraint); 139 assertNotNull(str); 140 assertTrue("Value : xyz", !(constraint.match("xyz", "message").isEmpty())); assertTrue("Value : null", constraint.match(null, "message").isEmpty()); assertTrue("Value : Empty String", constraint.match(str, "message").isEmpty()); assertTrue("Value : Blank String", !(constraint.match(" ", "message").isEmpty())); assertTrue("Value : 120", !(constraint.match("120", "message").isEmpty())); assertTrue("Value : 121", constraint.match("121", "message").isEmpty()); } 153 154 155 public void testConstraintUtils() { 156 ConstraintUtils constraintUtils = 157 new ConstraintUtils(); 158 assertNotNull(constraintUtils); 159 160 String message = 161 constraintUtils.formatFailureMessage("Constraint", "Element"); int index = message.lastIndexOf("failed_for"); 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 [] array = { "abc", "xyz" }; String [] emptyArray = {}; 180 181 assertNotNull(mandatoryConstraint); 182 assertNotNull(opationalConstraint); 183 assertNotNull(mandatoryArrayConstraint); 184 assertNotNull(opationalArrayConstraint); 185 186 assertTrue("Value : xyz", mandatoryConstraint.match("xyz", "message").isEmpty()); assertTrue("Value : null", !(mandatoryConstraint.match(null, "message").isEmpty())); 191 assertTrue("Value : xyz", opationalConstraint.match("xyz", "message").isEmpty()); assertTrue("Value : null", opationalConstraint.match(null, "message").isEmpty()); 196 assertTrue("Value : String[]", mandatoryArrayConstraint.match(array, "message").isEmpty()); assertTrue("Value : Empty String[]", !(mandatoryArrayConstraint.match(emptyArray, 200 "message").isEmpty())); 202 assertTrue("Value : String[]", opationalArrayConstraint.match(array, "message").isEmpty()); assertTrue("Value : Empty String[]", opationalArrayConstraint.match(emptyArray, 206 "message").isEmpty()); } 208 209 210 public void testInConstraint() { 211 ArrayList primaryColours = new ArrayList (); 212 primaryColours.add("red"); primaryColours.add("green"); primaryColours.add("yellow"); primaryColours.add("blue"); 217 Constraint constraint = new InConstraint(primaryColours); 218 assertNotNull(constraint); 219 assertTrue("Value : red", constraint.match("red", "message").isEmpty()); assertTrue("Value : black(not in enumeration)", !(constraint.match("black", "message").isEmpty())); } 224 225 226 public void testBooleanConstraint() { 227 Constraint constraint = new BooleanConstraint(); 228 assertNotNull(constraint); 229 assertTrue("Value : TRUE", constraint.match("TRUE", "message").isEmpty()); assertTrue("Value : xyz", !(constraint.match("xyz", "message").isEmpty())); } 234 235 236 public void testAndConstraint() { 237 Constraint constraint = new AndConstraint(new MandatoryConstraint(), 238 new NumberConstraint()); 239 assertNotNull(constraint); 240 assertTrue("Value : 123", constraint.match("123", "message").isEmpty()); assertTrue("Value : xyz", !(constraint.match("xyz", "message").isEmpty())); } 245 246 247 public void testNumberConstraint() { 248 Constraint constraint = new NumberConstraint(); 249 assertNotNull(constraint); 250 assertTrue("Value : 1234", constraint.match("1234", "message" ).isEmpty()); assertTrue("Value : abc", !(constraint.match("abc", "message").isEmpty())); } 255 256 257 public void testMandatoryConstraint(){ 258 Constraint constraint = new MandatoryConstraint(); 259 assertNotNull(constraint); 260 assertTrue("Value : abc", constraint.match("abc", "message").isEmpty()); assertTrue("Value : null length string", constraint.match("", "message").isEmpty()); assertTrue("Value : null", !(constraint.match(null, "message").isEmpty())); } 267 268 269 272 public static Test suite(){ 273 TestSuite suite = new TestSuite(ConstraintTest.class); 274 return suite; 275 } 276 277 278 281 protected void setUp() { 282 } 283 284 285 288 protected void tearDown() { 289 } 290 291 292 private void nyi() { 293 } 295 } 296 | Popular Tags |