jakarta-commons  

cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester RulesBaseTestCase.java TestRule.java

craigmcc
Tue, 04 Sep 2001 20:08:28 -0700

craigmcc    01/09/04 20:33:26

  Modified:    digester build.xml
               digester/src/test/org/apache/commons/digester TestRule.java
  Added:       digester/src/test/org/apache/commons/digester
                        RulesBaseTestCase.java
  Log:
  Add separate unit test suite for Rules (to facilitate subclassing for
  custom Rules implementations).
  
  Submitted by:  Robert Burrell Donkin <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.8       +15 -2     jakarta-commons/digester/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/build.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- build.xml 2001/08/20 21:59:43     1.7
  +++ build.xml 2001/09/05 03:33:26     1.8
  @@ -3,7 +3,7 @@
   
   <!--
           "Digester" component of the Jakarta Commons Subproject
  -        $Id: build.xml,v 1.7 2001/08/20 21:59:43 craigmcc Exp $
  +        $Id: build.xml,v 1.8 2001/09/05 03:33:26 craigmcc Exp $
   -->
   
   
  @@ -224,6 +224,7 @@
   
     <target name="test" depends="compile.tests,
                                  test.digester,
  +                               test.matching,
                                  test.rule
                                 "
      description="Run all unit test cases">
  @@ -232,7 +233,7 @@
   
     <target name="test.digester" depends="compile.tests"
      description="Run basic Digester unit tests ...">
  -    <echo message="Running Digester tests ..."/>
  +    <echo message="Running basic Digester tests ..."/>
       <java classname="${test.runner}" fork="yes"
           failonerror="${test.failonerror}">
         <arg value="org.apache.commons.digester.DigesterTestCase"/>
  @@ -241,6 +242,17 @@
     </target>
   
   
  +  <target name="test.matching" depends="compile.tests"
  +   description="Run rule Digester test cases">
  +    <echo message="Running rule Digester tests ..."/>
  +    <java classname="${test.runner}" fork="yes"
  +        failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.digester.RulesBaseTestCase"/>
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </target>
  +
  +
     <target name="test.rss" depends="compile.tests"
      description="Run RSSDigester test cases">
       <echo message="Running RSSDigester tests ..."/>
  @@ -249,6 +261,7 @@
         <classpath refid="rss.classpath"/>
       </java>
     </target>
  +
   
     <target name="test.rule" depends="compile.tests"
      description="Run rule Digester unit tests ...">
  
  
  
  1.2       +18 -3     
jakarta-commons/digester/src/test/org/apache/commons/digester/TestRule.java
  
  Index: TestRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/TestRule.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestRule.java     2001/08/08 03:21:17     1.1
  +++ TestRule.java     2001/09/05 03:33:26     1.2
  @@ -65,7 +65,7 @@
    * at the identifier.</p>
    *
    * @author Robert Burrell Donkin
  - * @revision $Revision: 1.1 $ $Date: 2001/08/08 03:21:17 $
  + * @revision $Revision: 1.2 $ $Date: 2001/09/05 03:33:26 $
    */
   
   public class TestRule extends Rule {
  @@ -82,7 +82,7 @@
        * Base constructor.
        *
        * @param digester The digester with which this rule is associated
  -     * @param identifier Used to tell which TestRule is which.
  +     * @param identifier Used to tell which TestRule is which
        */
       public TestRule(Digester digester, String identifier) {
   
  @@ -90,7 +90,22 @@
           this.identifier=identifier;
       }
       
  -    
  +
  +    /**
  +     * Constructor sets namespace URI.
  +     *
  +     * @param digester The digester with which this rule is associated
  +     * @param identifier Used to tell which TestRule is which
  +     * @param namespaceURI Set rule namespace
  +     */
  +    public TestRule(Digester digester, String identifier,
  +                    String namespaceURI) {
  +
  +        super(digester);
  +        this.identifier = identifier;
  +        setNamespaceURI(namespaceURI);
  +
  +    }
       
   
       // ------------------------------------------------ Methods
  
  
  
  1.1                  
jakarta-commons/digester/src/test/org/apache/commons/digester/RulesBaseTestCase.java
  
  Index: RulesBaseTestCase.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/RulesBaseTestCase.java,v
 1.1 2001/09/05 03:33:26 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/09/05 03:33:26 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.digester;
  
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.xml.sax.ErrorHandler;
  
  
  
  /**
   * <p>Test Case for the RulesBase matching rules.
   * Most of this material was original contained in the digester test case
   * but was moved into this class so that extensions of the basic matching rules
   * behaviour can extend this test case.
   * </p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/09/05 03:33:26 $
   */
  
  public class RulesBaseTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The digester instance we will be processing.
       */
      protected Digester digester = null;
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public RulesBaseTestCase(String name) {
  
          super(name);
          
      }
  
  
      // -------------------------------------------------- Overall Test Methods
  
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() {
  
          digester = new Digester();
          digester.setRules(createMatchingRulesForTest());
  
      }
  
      /** 
       * <p> This should be overriden by subclasses.
       *
       * @return the matching rules to be tested.
       */
      protected Rules createMatchingRulesForTest()
      {
          return new RulesBase();
      }
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite() {
  
          return (new TestSuite(RulesBaseTestCase.class));
  
      }
  
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
  
          digester = null;
  
      }
  
  
  
      // ------------------------------------------------ Individual Test Methods
  
      /**
       * Basic test for rule creation and matching.
       */
      public void testRules() {
  
          // clear any existing rules
          digester.getRules().clear();
          
          // perform tests
          List list = null;
  
          assertNull("Initial rules list is empty",
                     digester.getRules().match(null,"a"));
          digester.addSetProperties("a");
          assertEquals("Add a matching rule",
                       1, digester.getRules().match(null,"a").size());
          digester.addSetProperties("b");
          assertEquals("Add a non-matching rule",
                       1, digester.getRules().match(null,"a").size());
          digester.addSetProperties("a/b");
          assertEquals("Add a non-matching nested rule",
                       1, digester.getRules().match(null,"a").size());
          digester.addSetProperties("a/b");
          assertEquals("Add a second matching rule",
                       2, digester.getRules().match(null,"a/b").size());
  
          // clean up
          digester.getRules().clear();
          
      }
  
      /**
       * <p>Test matching rules in {@link RulesBase}.</p>
       *
       * <p>Tests:</p>
       * <ul>
       * <li>exact match</li>
       * <li>tail match</li>
       * <li>longest pattern rule</li>
       * </ul>
       */
      public void testRulesBase() {
  
          // clear any existing rules
          digester.getRules().clear();
  
          assertEquals("Initial rules list is empty",
                       0, digester.getRules().rules().size());
  
          // We're going to set up
          digester.addRule("a/b/c/d", new TestRule(digester, "a/b/c/d"));
          digester.addRule("*/d", new TestRule(digester, "*/d"));
          digester.addRule("*/c/d", new TestRule(digester, "*/c/d"));
  
          // Test exact match
          assertEquals("Exact match takes precedence 1",
                       1, digester.getRules().match(null,"a/b/c/d").size());
          assertEquals("Exact match takes precedence 2",
                       "a/b/c/d",
                       ((TestRule) 
digester.getRules().match(null,"a/b/c/d").iterator().next()).getIdentifier());
  
          // Test wildcard tail matching
          assertEquals("Wildcard tail matching rule 1",
                       1, digester.getRules().match(null,"a/b/d").size());
          assertEquals("Wildcard tail matching rule 2",
                       "*/d",
                       ((TestRule) 
digester.getRules().match(null,"a/b/d").iterator().next()).getIdentifier());
  
          // Test the longest matching pattern rule
          assertEquals("Longest tail rule 1",
                       1, digester.getRules().match(null,"x/c/d").size());
          assertEquals("Longest tail rule 2",
                       "*/c/d",
                       ((TestRule) 
digester.getRules().match(null,"x/c/d").iterator().next()).getIdentifier());
  
          // clean up
          digester.getRules().clear();
          
      }
      
      /**
       * Test basic matchings involving namespaces. 
       */
      public void testBasicNamespaceMatching() {
  
          List list=null;
          Iterator it=null;
  
          // clear any existing rules
          digester.getRules().clear();
  
          assertEquals("Initial rules list is empty",
                       0, digester.getRules().rules().size());
  
          // Set up rules
          digester.addRule("alpha/beta/gamma", new TestRule(digester, "No-Namespace"));
          digester.addRule("alpha/beta/gamma", new TestRule(digester, 
"Euclidean-Namespace","euclidean"));
  
          
          list=digester.getRules().rules();        
  
          // test that matching null namespace brings back namespace and non-namespace 
rules
          list=digester.getRules().match(null,"alpha/beta/gamma");
          
          assertEquals("Null namespace match (A)",2,list.size());
          
          it=list.iterator();
          assertEquals("Null namespace match 
(B)","No-Namespace",((TestRule)it.next()).getIdentifier());
          assertEquals("Null namespace match 
(C)","Euclidean-Namespace",((TestRule)it.next()).getIdentifier());
  
          
  
          // test that matching euclid namespace brings back namespace and 
non-namespace rules
          list=digester.getRules().match("euclidean","alpha/beta/gamma");
          
          assertEquals("Matching namespace match (A)",2,list.size());
          
          it=list.iterator();
          assertEquals("Matching namespace match 
(B)","No-Namespace",((TestRule)it.next()).getIdentifier());
          assertEquals("Matching namespace match 
(C)","Euclidean-Namespace",((TestRule)it.next()).getIdentifier());
          
          
          
          // test that matching another namespace brings back only non-namespace rule
          list=digester.getRules().match("hyperbolic","alpha/beta/gamma");
          
          assertEquals("Non matching namespace match (A)",1,list.size());
          
          it=list.iterator();
          assertEquals("Non matching namespace match 
(B)","No-Namespace",((TestRule)it.next()).getIdentifier());
  
          // clean up
          digester.getRules().clear();
          
      }
      
      /**
       * Rules must always be returned in the correct order.
       */
      public void testOrdering() {
  
          // clear any existing rules
          digester.getRules().clear();
  
          assertEquals("Initial rules list is empty",
                       0, digester.getRules().rules().size());
  
          // Set up rules
          digester.addRule("alpha/beta/gamma", new TestRule(digester, "one"));
          digester.addRule("alpha/beta/gamma", new TestRule(digester, "two"));
          digester.addRule("alpha/beta/gamma", new TestRule(digester, "three"));
  
          // test that rules are returned in set order
          List list=digester.getRules().match(null,"alpha/beta/gamma");
          
          assertEquals("Testing ordering mismatch (A)",3,list.size());
          
          Iterator it=list.iterator();
          assertEquals("Testing ordering mismatch 
(B)","one",((TestRule)it.next()).getIdentifier());
          assertEquals("Testing ordering mismatch 
(C)","two",((TestRule)it.next()).getIdentifier());
          assertEquals("Testing ordering mismatch 
(D)","three",((TestRule)it.next()).getIdentifier());
  
          // clean up
          digester.getRules().clear();
          
      }
  }
  
  
  
  • cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester RulesBaseTestCase.java TestRule.java craigmcc