FileDocCategorySizeDatePackage
ConstraintTest.javaAPI DocGlassfish v2 API14347Fri May 04 22:35:18 BST 2007com.sun.enterprise.tools.common.validation.constraints

ConstraintTest

public class ConstraintTest extends TestCase
author
Rajeshwar Patil
version
%I%, %G%

Fields Summary
private ArrayList
primaryColours
Constructors Summary
public ConstraintTest(String name)



      
        super(name);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run(suite());
    
private voidnyi()

        fail("Not yet implemented");                                    //NOI18N
    
protected voidsetUp()
Initialize; allocate any resources needed to perform Tests.

    
public static junit.framework.Testsuite()
Define suite of all the Tests to run.

        TestSuite suite = new TestSuite(ConstraintTest.class);
        return suite;
    
protected voidtearDown()
Free all the resources initilized/allocated to perform Tests.

    
public voidtestAndConstraint()

      Constraint constraint = new AndConstraint(new MandatoryConstraint(),
          new NumberConstraint());
      assertNotNull(constraint);
      assertTrue("Value : 123",                                         //NOI18N
          constraint.match("123", "message").isEmpty());                //NOI18N
      assertTrue("Value : xyz",                                         //NOI18N
          !(constraint.match("xyz", "message").isEmpty()));             //NOI18N
    
public voidtestBooleanConstraint()

      Constraint constraint = new BooleanConstraint();
      assertNotNull(constraint);
      assertTrue("Value : TRUE",                                        //NOI18N
          constraint.match("TRUE", "message").isEmpty());               //NOI18N
      assertTrue("Value : xyz",                                         //NOI18N
          !(constraint.match("xyz", "message").isEmpty()));             //NOI18N
    
public voidtestCardinalConstraint()

      CardinalConstraint mandatoryConstraint = 
            new CardinalConstraint(Constants.MANDATORY_ELEMENT);
      CardinalConstraint opationalConstraint = 
            new CardinalConstraint(Constants.OPTIONAL_ELEMENT);
      CardinalConstraint mandatoryArrayConstraint = 
            new CardinalConstraint(Constants.MANDATORY_ARRAY);
      CardinalConstraint opationalArrayConstraint = 
            new CardinalConstraint(Constants.OPTIONAL_ARRAY);
      String[] array = { "abc", "xyz" };                                //NOI18N    
      String[] emptyArray = {};
      
      assertNotNull(mandatoryConstraint);
      assertNotNull(opationalConstraint);
      assertNotNull(mandatoryArrayConstraint);
      assertNotNull(opationalArrayConstraint);
      
      assertTrue("Value : xyz",                                         //NOI18N
          mandatoryConstraint.match("xyz",  "message").isEmpty());      //NOI18N
      assertTrue("Value : null",                                        //NOI18N
          !(mandatoryConstraint.match(null,  "message").isEmpty()));    //NOI18N

      assertTrue("Value : xyz",                                         //NOI18N
          opationalConstraint.match("xyz",  "message").isEmpty());      //NOI18N
      assertTrue("Value : null", //NOI18N
          opationalConstraint.match(null,  "message").isEmpty());       //NOI18N
      
      assertTrue("Value : String[]", //NOI18N
          mandatoryArrayConstraint.match(array,  "message").isEmpty()); //NOI18N
      assertTrue("Value : Empty String[]",                              //NOI18N
          !(mandatoryArrayConstraint.match(emptyArray,
                "message").isEmpty()));                                 //NOI18N
      
      assertTrue("Value : String[]",                                    //NOI18N
          opationalArrayConstraint.match(array,  "message").isEmpty()); //NOI18N
      assertTrue("Value : Empty String[]",                              //NOI18N
          opationalArrayConstraint.match(emptyArray,
                "message").isEmpty());                                  //NOI18N
    
public voidtestConstraintFailure()

      Failure failure = new ConstraintFailure("Constraint",             //NOI18N
        "Element Value", "Element Name", "Failure Message",
            "Generic Failure Message");                                 //NOI18N

      assertNotNull(failure);
      assertTrue("Failure Message".equals(failure.failureMessage()));   //NOI18N
      
      ConstraintFailure constraintFailure = (ConstraintFailure) failure;
      assertTrue("Constraint".equals(
            constraintFailure.getConstraint()));                        //NOI18N
      assertTrue("Element Value".equals(
            constraintFailure.getFailedValue()));                       //NOI18N
      assertTrue("Element Name".equals(constraintFailure.getName()));   //NOI18N
      assertTrue("Generic Failure Message".equals(
            constraintFailure.getGenericfailureMessage()));             //NOI18N
    
public voidtestConstraintUtils()

      ConstraintUtils constraintUtils = 
            new ConstraintUtils();
      assertNotNull(constraintUtils);
      
      String message = 
        constraintUtils.formatFailureMessage("Constraint",          //NOI18N
                "Element");                                             //NOI18N
      int index = message.lastIndexOf("failed_for");                    //NOI18N 
      //Test to make sure that the strings are picked up from the bundle.
      assertTrue(-1 == index);
    
public voidtestCreate()

      Constraint constraint = new NumberConstraint();
      assertNotNull(constraint);
    
public voidtestInConstraint()

      ArrayList primaryColours = new ArrayList();
      primaryColours.add("red");                                        //NOI18N
      primaryColours.add("green");                                      //NOI18N
      primaryColours.add("yellow");                                     //NOI18N
      primaryColours.add("blue");                                       //NOI18N

      Constraint constraint = new InConstraint(primaryColours);
      assertNotNull(constraint);
      assertTrue("Value : red",                                         //NOI18N
          constraint.match("red", "message").isEmpty());                //NOI18N
      assertTrue("Value : black(not in enumeration)",                   //NOI18N
          !(constraint.match("black",  "message").isEmpty()));          //NOI18N
    
public voidtestIntegerGreaterThanConstraint()

      Constraint constraint = new IntegerGreaterThanConstraint("120");
      String str = new String();
      assertNotNull(constraint);
      assertNotNull(str);
      assertTrue("Value : xyz",                                         //NOI18N
          !(constraint.match("xyz", "message").isEmpty()));             //NOI18N
      assertTrue("Value : null",                                        //NOI18N
          constraint.match(null,  "message").isEmpty());                //NOI18N
      assertTrue("Value : Empty String",                                //NOI18N
          constraint.match(str,  "message").isEmpty());                 //NOI18N
      assertTrue("Value : Blank String",                                //NOI18N
          !(constraint.match("      ",  "message").isEmpty()));         //NOI18N
      assertTrue("Value : 120",                                         //NOI18N
          !(constraint.match("120",  "message").isEmpty()));            //NOI18N
      assertTrue("Value : 121",                                         //NOI18N
          constraint.match("121",  "message").isEmpty());               //NOI18N
    
public voidtestMandatoryConstraint()

          Constraint constraint = new MandatoryConstraint();
          assertNotNull(constraint);
          assertTrue("Value : abc",                                     //NOI18N
              constraint.match("abc", "message").isEmpty());            //NOI18N
          assertTrue("Value : null length string",                      //NOI18N
              constraint.match("", "message").isEmpty());               //NOI18N
          assertTrue("Value :  null",                                   //NOI18N
              !(constraint.match(null, "message").isEmpty()));          //NOI18N
    
public voidtestNonBlankConstraint()

      Constraint constraint = new NonBlankConstraint();
      String str = new String();
      assertNotNull(constraint);
      assertNotNull(str);
      assertTrue("Value : xyz",                                         //NOI18N
          constraint.match("xyz", "message").isEmpty());                //NOI18N
      assertTrue("Value : null",                                        //NOI18N
          constraint.match(null,  "message").isEmpty());                //NOI18N
      assertTrue("Value : Empty String",                                //NOI18N
          constraint.match(str,  "message").isEmpty());                 //NOI18N
      assertTrue("Value : Blank String",                                //NOI18N
          !(constraint.match("      ",  "message").isEmpty()));         //NOI18N
    
public voidtestNonZeroLengthConstraint()

      Constraint constraint = new NonZeroLengthConstraint();
      String str = new String();
      assertNotNull(constraint);
      assertNotNull(str);
      assertTrue("Value : xyz",                                         //NOI18N
          constraint.match("red", "message").isEmpty());                //NOI18N
      assertTrue("Value : null",                                        //NOI18N
          constraint.match(null,  "message").isEmpty());                //NOI18N
      assertTrue("Value : Empty String",                                //NOI18N
          !(constraint.match(str,  "message").isEmpty()));              //NOI18N
    
public voidtestNumberConstraint()

      Constraint constraint = new NumberConstraint();
      assertNotNull(constraint);
      assertTrue("Value : 1234",                                        //NOI18N
      constraint.match("1234", "message" ).isEmpty());                  //NOI18N
      assertTrue("Value : abc",                                         //NOI18N
        !(constraint.match("abc", "message").isEmpty()));               //NOI18N
    
public voidtestRangeConstraint()

      Constraint constraint = new RangeConstraint("100", "250");        //NOI18N
      assertNotNull(constraint);
      assertTrue("Value : 152",                                         //NOI18N
          constraint.match("152", "message").isEmpty());                //NOI18N
      assertTrue("Value : 300(out of range)",                           //NOI18N
          !(constraint.match("300",  "message").isEmpty()));            //NOI18N
      assertTrue("Value : xyz(non-numeric)",                            //NOI18N
          !(constraint.match("xyz",  "message").isEmpty()));            //NOI18N
    
public voidtestZeroToMaxIntegerConstraint()

      Constraint constraint = new ZeroToMaxIntegerConstraint();
      String str = new String();
      assertNotNull(constraint);
      assertNotNull(str);
      assertTrue("Value : 0",                                           //NOI18N
          constraint.match("0", "message").isEmpty());                  //NOI18N
      assertTrue("Value : null",                                        //NOI18N
          constraint.match(null,  "message").isEmpty());                //NOI18N
      assertTrue("Value : Empty String",                                //NOI18N
          constraint.match(str,  "message").isEmpty());                 //NOI18N
      assertTrue("Value : Blank String",                                //NOI18N
          !(constraint.match("      ",  "message").isEmpty()));         //NOI18N
      assertTrue("Value : 1234",                                        //NOI18N
          constraint.match("1234",  "message").isEmpty());              //NOI18N
      assertTrue("Value : -1",                                          //NOI18N
          !(constraint.match("-1",  "message").isEmpty()));             //NOI18N