niallp      2004/09/03 11:12:26

  Modified:    .        build-tests.xml
  Added:       src/test/org/apache/struts/validator PojoBean.java
                        TestValidWhen.java
  Log:
  Add Test Cases for ValidWhen parser
  
  Revision  Changes    Path
  1.26      +4 -0      jakarta-struts/build-tests.xml
  
  Index: build-tests.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/build-tests.xml,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- build-tests.xml   22 Mar 2004 07:16:37 -0000      1.25
  +++ build-tests.xml   3 Sep 2004 18:12:26 -0000       1.26
  @@ -114,6 +114,7 @@
         <pathelement location="${junit.jar}"/>
         <pathelement location="${servlet.jar}"/>
         <pathelement location="${struts.jar}"/>
  +      <pathelement location="${antlr.jar}"/>
       </path>
   
   <!-- ========== Executable Targets ======================================== -->
  @@ -732,6 +733,9 @@
           <test       name="org.apache.struts.action.TestDynaActionFormClass"/>
           <test       name="org.apache.struts.config.TestModuleConfig"/>
           <test       name="org.apache.struts.config.TestActionConfigMatcher"/>
  +
  +        <!-- Tests for org.apache.struts.validator -->
  +        <test       name="org.apache.struts.validator.TestValidWhen"/>
   
           <!-- Tests for org.apache.struts.util -->
           <batchtest fork="yes">
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/validator/PojoBean.java
  
  Index: PojoBean.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/validator/PojoBean.java,v 1.1 
2004/09/03 18:12:26 niallp Exp $
   * $Revision: 1.1 $
   * $Date: 2004/09/03 18:12:26 $
   *
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.struts.validator;
  
  import java.util.Map;
  import java.util.HashMap;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * Test Bean class.
   */
  public class PojoBean {
  
      protected String stringValue1;
  
      protected String stringValue2;    
  
      protected int intValue1;
  
      protected int intValue2;
      
      protected Integer integerValue1;
      
      protected Integer integerValue2;
  
      protected PojoBean[] beans;
  
      protected Map map = new HashMap();
      
      /**
       * Default Constructor
       */
      public PojoBean() {
      }
      
      /**
       * Construct Bean with a pair of String values.
       */
      public PojoBean(String stringValue1, String stringValue2) {
         setStringValue1(stringValue1);
         setStringValue2(stringValue2);
      }
      
      /**
       * Construct Bean with a pair of integer values.
       */
      public PojoBean(int intValue1, int intValue2) {
         setIntValue1(intValue1);
         setIntValue2(intValue2);
         setIntegerValue1(new Integer(intValue1));
         setIntegerValue2(new Integer(intValue2));
      }
      
      /**
       * Set the stringValue1.
       */
      public void setStringValue1(String stringValue1) {
         this.stringValue1 = stringValue1;
      }
      
      /**
       * Return stringValue1.
       */
      public String getStringValue1() {
         return stringValue1;
      }
      
      /**
       * Set the stringValue2.
       */
      public void setStringValue2(String stringValue2) {
         this.stringValue2 = stringValue2;
      }
      
      /**
       * Return stringValue2.
       */
      public String getStringValue2() {
         return stringValue2;
      }
      
      /**
       * Set the intValue1.
       */
      public void setIntValue1(int intValue1) {
         this.intValue1= intValue1;
      }
      
      /**
       * Return intValue1.
       */
      public int getIntValue1() {
         return intValue1;
      }
      
      /**
       * Set the intValue2.
       */
      public void setIntValue2(int intValue2) {
         this.intValue2= intValue2;
      }
      
      /**
       * Return intValue2.
       */
      public int getIntValue2() {
         return intValue2;
      }
      
      /**
       * Set the integerValue1.
       */
      public void setIntegerValue1(Integer integerValue1) {
         this.integerValue1= integerValue1;
      }
      
      /**
       * Return integerValue1.
       */
      public Integer getIntegerValue1() {
         return integerValue1;
      }
      
      /**
       * Set the integerValue2.
       */
      public void setIntegerValue2(Integer integerValue2) {
         this.integerValue2= integerValue2;
      }
      
      /**
       * Return integerValue2.
       */
      public Integer getIntegerValue2() {
         return integerValue2;
      }
      
      /**
       * Set the PojoBean[].
       */
      public void setBeans(PojoBean[] beans) {
         this.beans = beans;
      }
      
      /**
       * Return PojoBean[].
       */
      public PojoBean[] getBeans() {
         return beans;
      }
      
      /**
       * Return and indexed Bean
       */
      public PojoBean getBean(int index) {
         return beans[index];
      }
      
      /**
       * Return the Map
       */
      public Object getMap() {
         return map;
      }
      
      /**
       * Return the Map
       */
      public void setMap(Map map) {
         this.map = map;
      }
  
      /**
       * Set a  Mapped property
       */
      public void setMapped(String key, Object value) {
         map.put(key, value);
      }
  
      /**
       * Set a  Mapped property
       */
      public Object getMapped(String key) {
         return map.get(key);
      }
  
  }
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/validator/TestValidWhen.java
  
  Index: TestValidWhen.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/validator/TestValidWhen.java,v 1.1 
2004/09/03 18:12:26 niallp Exp $
   * $Revision: 1.1 $
   * $Date: 2004/09/03 18:12:26 $
   *
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.struts.validator;
  
  import java.io.StringReader;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.struts.validator.validwhen.ValidWhenLexer;
  import org.apache.struts.validator.validwhen.ValidWhenParser;
  import org.apache.commons.validator.util.ValidatorUtils;
  import org.apache.commons.logging.LogFactory;
  import org.apache.commons.logging.Log;
  
  
  /**
   * Unit tests for the ValidWhen Parser/Lexer.
   */
  public class TestValidWhen extends TestCase {
      
      /** All logging goes through this logger */
      private static Log log = LogFactory.getLog(TestValidWhen.class);
  
      protected PojoBean testBean;
  
      /**
       * Defines the testcase name for JUnit.
       *
       * @param theName the testcase's name.
       */
      public TestValidWhen(String theName) {
          super(theName);
      }
  
      /**
       * Start the tests.
       *
       * @param theArgs the arguments. Not used
       */
      public static void main(String[] theArgs) {
          junit.awtui.TestRunner.main(
              new String[] {TestValidWhen.class.getName()});
      }
  
      /**
       * @return a test suite (<code>TestSuite</code>) that includes all methods
       *         starting with "test"
       */
      public static Test suite() {
          // All methods starting with "test" will be executed in the test suite.
          return new TestSuite(TestValidWhen.class);
      }
  
      public void setUp() {
          testBean = new PojoBean(123, 789);
          testBean.setStringValue1("ABC"); 
          testBean.setStringValue2(null);
          testBean.setBeans(new PojoBean[] {new PojoBean(11, 12),
                                            new PojoBean(21, 22),
                                            new PojoBean(31, 42),
                                            new PojoBean(41, 52),
                                            new PojoBean(51, 62)});
          testBean.setMapped("testKey", "mappedValue");
      }
  
      public void tearDown() {
          testBean = null;
      }
      
      /**
       * Test Operators.
       */
      public void testProperty() {
  
          // *this*
          doParse("(*this* == 123)", testBean , 0, "intValue1", true);
  
          // Named property
          doParse("(intValue2 == 789)", testBean , 0, "intValue1", true);
  
          // Indexed Property
          doParse("(beans[].intValue1 == 21)", testBean , 1, "intValue1", true);
          doParse("(beans[1].intValue1 == 21)", testBean , 4, "intValue1", true);
  
          // Mapped Property - *** NOT SUPPORTED ***
          // doParse("(mapped(mappedValue) == 'testKey')", testBean , 0, 
"mappedValue", true);
  
  
      }
      
      /**
       * Test Operators.
       */
      public void testOperators() {
  
          // Equal
          doParse("(*this* == 123)", testBean , 0, "intValue1", true);
  
          // Not Equal
          doParse("(*this* != 456)", testBean , 0, "intValue1", true);
  
          // Less Than
          doParse("(*this* < 456)", testBean , 0, "intValue1", true);
  
          // Greater Than
          doParse("(*this* > 100)", testBean , 0, "intValue1", true);
  
          // Less Than Equal
          doParse("(*this* <= 123)", testBean , 0, "intValue1", true);
  
          // Greater Than Equal
          doParse("(*this* >= 123)", testBean , 0, "intValue1", true);
  
      }
  
      /**
       * Test String values.
       */
      public void testString() {
  
           doParse("(*this* != '--')", "XX", 0, "stringValue1", true);
           doParse("(*this* == '--')", "--", 0, "stringValue1", true);
  
  
           String testValue = "dsgUOLMdLsdL";
  
           // single quote
           doParse("(*this* == '" + testValue + "')", testValue, 0, "stringValue1", 
true);
  
           // double quote
           doParse("(*this* == \"" + testValue + "\")", testValue, 0, "stringValue1", 
true);
      }
      
      /**
       * Test Numeric values.
       */
      public void testNumeric() {
  
          // Test Zero
          PojoBean numberBean = new PojoBean(0, -50);
          doParse("(intValue1 == 0)", numberBean, 0, "intValue1", true);
          doParse("(intValue2 != 0)", numberBean, 0, "intValue2", true);
          doParse("(integerValue1 == 0)", numberBean, 0, "integerValue1", true);
          doParse("(integerValue2 != 0)", numberBean, 0, "integerValue2", true);
  
          // int
          doParse("(intValue1 == 123)", testBean , 0, "intValue1", true);
  
          // Integer
          doParse("(integerValue1 == 123)", testBean , 0, "integerValue1", true);
  
          // Negative Numbers
          doParse("((intValue2 < -10)     and (intValue2 > -60))", numberBean, 0, 
"intValue2", true);
          doParse("((integerValue2 < -10) and (integerValue2 > -60))", numberBean, 0, 
"integerValue2", true);
  
          // Hex
          doParse("(integerValue1 == 0x7B)", testBean , 0, "integerValue1", true);
  
          // Octal
          doParse("(integerValue1 == 0173)", testBean , 0, "integerValue1", true);
  
      }
      
      /**
       * Test Null.
       */
      public void testNull() {
  
          // Not Null
          doParse("(*this* != null)", testBean , 0, "stringValue1", true);
  
          // Null
          doParse("(*this* == null)", testBean , 0, "stringValue2", true);
  
  
      }
      
      /**
       * Test Joined expressions ('and' or 'or')
       */
      public void testJoined() {
  
          // Join with 'or'
          doParse("((*this* == 'ABC') or (stringValue2 == null))", testBean , 0, 
"stringValue1", true);
          doParse("((*this* != 'ABC') or (stringValue2 == null))", testBean , 0, 
"stringValue1", true);
          doParse("((*this* == 'ABC') or (stringValue2 != null))", testBean , 0, 
"stringValue1", true);
          doParse("((*this* != 'ABC') or (stringValue2 != null))", testBean , 0, 
"stringValue1", false);
  
          // Join with 'and'
          doParse("((*this* == 'ABC') and (stringValue2 == null))", testBean , 0, 
"stringValue1", true);
          doParse("((*this* != 'ABC') and (stringValue2 == null))", testBean , 0, 
"stringValue1", false);
          doParse("((*this* == 'ABC') and (stringValue2 != null))", testBean , 0, 
"stringValue1", false);
          doParse("((*this* != 'ABC') and (stringValue2 != null))", testBean , 0, 
"stringValue1", false);
  
      }
      
  
      /**
       * Parse the expression and check that the expected result
       * (either true or false) occurs - fail if an exception is thrown
       * opr the wrong result occurs.
       *
       * @param test Test expression
       * @param bean Test Bean
       * @param index index value
       * @param property Bean property
       * @param expected Expected Result
       */
      private void doParse(String test, Object bean, int index, String property, 
boolean expected) {
          boolean result = false;
          try {
              result = doParse(test, bean, index, property);
          } catch (Exception ex) {
              log.error("Parsing " + test + " for property '"+property+"'", ex);
              fail("Parsing " + test + " threw " + ex);
          }
          if (expected) {
              assertTrue(test + " didn't return TRUE for " + property, result);
          } else {
              assertFalse(test + " didn't return FALSE for " + property, result);
          }
      }
  
      /**
       * Parse the expression and check that an Exception is throw.
       * Failes if no expection is thrown.
       *
       * @param test Test expression
       * @param bean Test Bean
       * @param index index value
       * @param property Bean property
       */
      private void doParseFail(String test, Object bean, int index, String property) {
  
          try {
              boolean result = doParse(test, bean, index, property);
              fail("Parsing " + test + " didn't throw exception as expected " + 
result);
          } catch (Exception expected) {
              // ignore exception - expected result
          }
      }
  
      /**
       * Parse the expression returning the result
       *
       * @param test Test expression
       * @param bean Test Bean
       * @param index index value
       * @param property Bean property
       */
      private boolean doParse(String test, Object bean, int index, String property) 
throws Exception {
  
          if (bean == null) {
              throw new NullPointerException("Bean is null for property 
'"+property+"'"); 
          }
  
          String value = String.class.isInstance(bean) 
                              ? (String)bean 
                              : ValidatorUtils.getValueAsString(bean, property);
  
          ValidWhenLexer lexer = new ValidWhenLexer(new StringReader(test));
  
          ValidWhenParser parser = new ValidWhenParser(lexer);
          parser.setForm(bean);
          parser.setIndex(index);
          parser.setValue(value);
  
          parser.expression();
          return parser.getResult();
  
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to