1 23 24 27 28 package com.sun.enterprise.tools.common.validation.constraints; 29 30 import junit.framework.*; 31 import java.util.ArrayList ; 32 33 import com.sun.enterprise.tools.common.validation.Constants; 34 import com.sun.enterprise.tools.common.validation.Failure; 35 36 41 public class ConstraintTest extends TestCase{ 42 43 private ArrayList primaryColours = new ArrayList (); 44 45 46 public ConstraintTest(String name){ 47 super(name); 48 } 49 50 51 public static void main(String 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", "Element Value", "Element Name", "Failure Message", 65 "Generic Failure Message"); 67 assertNotNull(failure); 68 assertTrue("Failure Message".equals(failure.failureMessage())); 70 ConstraintFailure constraintFailure = (ConstraintFailure) failure; 71 assertTrue("Constraint".equals( 72 constraintFailure.getConstraint())); assertTrue("Element Value".equals( 74 constraintFailure.getFailedValue())); assertTrue("Element Name".equals(constraintFailure.getName())); assertTrue("Generic Failure Message".equals( 77 constraintFailure.getGenericfailureMessage())); } 79 80 81 public void testRangeConstraint() { 82 Constraint constraint = new RangeConstraint("100", "250"); assertNotNull(constraint); 84 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())); } 91 92 93 public void testNonZeroLengthConstraint() { 94 Constraint constraint = new NonZeroLengthConstraint(); 95 String str = new String (); 96 assertNotNull(constraint); 97 assertNotNull(str); 98 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())); } 105 106 107 public void testNonBlankConstraint() { 108 Constraint constraint = new NonBlankConstraint(); 109 String str = new String (); 110 assertNotNull(constraint); 111 assertNotNull(str); 112 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())); } 121 122 123 public void testZeroToMaxIntegerConstraint() { 124 Constraint constraint = new ZeroToMaxIntegerConstraint(); 125 String str = new String (); 126 assertNotNull(constraint); 127 assertNotNull(str); 128 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())); } 141 142 143 public void testIntegerGreaterThanConstraint() { 144 Constraint constraint = new IntegerGreaterThanConstraint("120"); 145 String str = new String (); 146 assertNotNull(constraint); 147 assertNotNull(str); 148 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()); } 161 162 163 public void testConstraintUtils() { 164 ConstraintUtils constraintUtils = 165 new ConstraintUtils(); 166 assertNotNull(constraintUtils); 167 168 String message = 169 constraintUtils.formatFailureMessage("Constraint", "Element"); int index = message.lastIndexOf("failed_for"); 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 [] array = { "abc", "xyz" }; String [] emptyArray = {}; 188 189 assertNotNull(mandatoryConstraint); 190 assertNotNull(opationalConstraint); 191 assertNotNull(mandatoryArrayConstraint); 192 assertNotNull(opationalArrayConstraint); 193 194 assertTrue("Value : xyz", mandatoryConstraint.match("xyz", "message").isEmpty()); assertTrue("Value : null", !(mandatoryConstraint.match(null, "message").isEmpty())); 199 assertTrue("Value : xyz", opationalConstraint.match("xyz", "message").isEmpty()); assertTrue("Value : null", opationalConstraint.match(null, "message").isEmpty()); 204 assertTrue("Value : String[]", mandatoryArrayConstraint.match(array, "message").isEmpty()); assertTrue("Value : Empty String[]", !(mandatoryArrayConstraint.match(emptyArray, 208 "message").isEmpty())); 210 assertTrue("Value : String[]", opationalArrayConstraint.match(array, "message").isEmpty()); assertTrue("Value : Empty String[]", opationalArrayConstraint.match(emptyArray, 214 "message").isEmpty()); } 216 217 218 public void testInConstraint() { 219 ArrayList primaryColours = new ArrayList (); 220 primaryColours.add("red"); primaryColours.add("green"); primaryColours.add("yellow"); primaryColours.add("blue"); 225 Constraint constraint = new InConstraint(primaryColours); 226 assertNotNull(constraint); 227 assertTrue("Value : red", constraint.match("red", "message").isEmpty()); assertTrue("Value : black(not in enumeration)", !(constraint.match("black", "message").isEmpty())); } 232 233 234 public void testBooleanConstraint() { 235 Constraint constraint = new BooleanConstraint(); 236 assertNotNull(constraint); 237 assertTrue("Value : TRUE", constraint.match("TRUE", "message").isEmpty()); assertTrue("Value : xyz", !(constraint.match("xyz", "message").isEmpty())); } 242 243 244 public void testAndConstraint() { 245 Constraint constraint = new AndConstraint(new MandatoryConstraint(), 246 new NumberConstraint()); 247 assertNotNull(constraint); 248 assertTrue("Value : 123", constraint.match("123", "message").isEmpty()); assertTrue("Value : xyz", !(constraint.match("xyz", "message").isEmpty())); } 253 254 255 public void testNumberConstraint() { 256 Constraint constraint = new NumberConstraint(); 257 assertNotNull(constraint); 258 assertTrue("Value : 1234", constraint.match("1234", "message" ).isEmpty()); assertTrue("Value : abc", !(constraint.match("abc", "message").isEmpty())); } 263 264 265 public void testMandatoryConstraint(){ 266 Constraint constraint = new MandatoryConstraint(); 267 assertNotNull(constraint); 268 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())); } 275 276 277 280 public static Test suite(){ 281 TestSuite suite = new TestSuite(ConstraintTest.class); 282 return suite; 283 } 284 285 286 289 protected void setUp() { 290 } 291 292 293 296 protected void tearDown() { 297 } 298 299 300 private void nyi() { 301 fail("Not yet implemented"); } 303 } 304 | Popular Tags |