commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester WDRulesWrapperTestCase.java
Date Mon, 28 Apr 2003 17:51:32 GMT
rdonkin     2003/04/28 10:51:32

  Modified:    digester build.xml
               digester/src/java/org/apache/commons/digester package.html
  Added:       digester/src/java/org/apache/commons/digester
                        WithDefaultsRulesWrapper.java
               digester/src/test/org/apache/commons/digester
                        WDRulesWrapperTestCase.java
  Log:
  Added new Rules implementation. This wraps an existing implementation, adding support for
default rules (to be fired whenever no other rule matches).
  
  Revision  Changes    Path
  1.45      +13 -2     jakarta-commons/digester/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/build.xml,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- build.xml	27 Apr 2003 18:53:15 -0000	1.44
  +++ build.xml	28 Apr 2003 17:51:31 -0000	1.45
  @@ -256,7 +256,8 @@
                                  test.xmlrules,
                                  test.node,
                                  test.factory,
  -                               test.regex
  +                               test.regex,
  +                               test.wdrules
                                 "
      description="Run all unit test cases">
     </target>
  @@ -344,6 +345,16 @@
       </java>
     </target>
   
  +  <target name="test.wdrules" depends="compile.tests"
  +   description="Run tests for WithDefaultsRulesWrapper">
  +    <echo message="Running tests for WithDefaultsRulesWrapper ..."/>
  +    <java classname="${test.runner}" fork="yes"
  +        failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.digester.WDRulesWrapperTestCase"/>
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </target>
  +  
     <target name="test.matching.extended" depends="compile.tests"
      description="Run rule Digester extended test cases">
       <echo message="Running rule Digester tests for ExtendedBaseRules ..."/>
  
  
  
  1.20      +24 -0     jakarta-commons/digester/src/java/org/apache/commons/digester/package.html
  
  Index: package.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/package.html,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- package.html	2 Apr 2003 19:05:12 -0000	1.19
  +++ package.html	28 Apr 2003 17:51:31 -0000	1.20
  @@ -797,6 +797,30 @@
     ...
   </pre>
   
  +<h4>WithDefaultsRulesWrapper</h4>
  +<p>
  +<a href="WithDefaultsRulesWrapper.html"> WithDefaultsRulesWrapper</a> allows
default 
  +<code>Rule</code>'s to be added to any existing <code>Rules</code>
implementation.
  +These default <code>Rule</code>'s will be returned for any match for which
the wrapped 
  +implementation does not return any matches. 
  +</p>
  +<p>
  +For example,
  +<pre>
  +    Rule alpha;
  +    ...
  +    WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
  +    rules.addDefault(alpha);
  +    ...
  +    digester.setRules(rules);
  +    ...
  +</pre>
  +when a pattern does not match any other rule, then rule alpha will be called.
  +</p>
  +<p>
  +<code>WithDefaultsRulesWrapper</code> follows the <em>Decorator</em>
pattern.
  +</p>
  +
   <a name="doc.RuleSets"></a>
   <h3>Encapsulated Rule Sets</h3>
   
  
  
  
  1.1                  jakarta-commons/digester/src/java/org/apache/commons/digester/WithDefaultsRulesWrapper.java
  
  Index: WithDefaultsRulesWrapper.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/WithDefaultsRulesWrapper.java,v
1.1 2003/04/28 17:51:31 rdonkin Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/28 17:51:31 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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 apache@apache.org.
   *
   * 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.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  /**
   * <p><code>Rules</code> <em>Decorator</em> that returns default
rules 
   * when no matches are returned by the wrapped implementation.</p>
   *
   * <p>This allows default <code>Rule</code>'s to be added to any existing

   * <code>Rules</code> implementation. 
   * These default <code>Rule</code>'s will be returned for any match for which

   * the wrapped implementation does not return any matches.</p>
   * <p> For example,
   * <pre>
   *   Rule alpha;
   *   ...
   *   WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
   *   rules.addDefault(alpha);
   *   ...
   *   digester.setRules(rules);
   *   ...
   * </pre>
   * when a pattern does not match any other rule, then rule alpha will be called.
   * </p>
   * <p><code>WithDefaultsRulesWrapper</code> follows the <em>Decorator</em>
pattern.</p>
   *
   * @author Robert Burrell Donkin
   * @version $Revision: 1.1 $ $Date: 2003/04/28 17:51:31 $
   */
  
  public class WithDefaultsRulesWrapper implements Rules {
  
      // --------------------------------------------------------- Fields
      
      /** The Rules implementation that this class wraps. */
      private Rules wrappedRules;
      /** Rules to be fired when the wrapped implementations returns none. */
      private List defaultRules = new ArrayList();
      /** All rules (preserves order in which they were originally added) */
      private List allRules = new ArrayList();
      
      // --------------------------------------------------------- Constructor
      
      /** 
       * Base constructor.
       *
       * @param wrappedRules the wrapped <code>Rules</code> implementation, not
null
       * @throws IllegalArgumentException when <code>wrappedRules</code> is null
       */
      public WithDefaultsRulesWrapper(Rules wrappedRules) {
          if (wrappedRules == null) {
              throw new IllegalArgumentException("Wrapped rules must not be null");
          }
          this.wrappedRules = wrappedRules;
      }
  
      // --------------------------------------------------------- Properties
      
      /** Gets digester using these Rules */
      public Digester getDigester() {
          return wrappedRules.getDigester();
      }
      
      /** Sets digeseter using these Rules */
      public void setDigester(Digester digester) {
          wrappedRules.setDigester(digester);
          Iterator it = defaultRules.iterator();
          while (it.hasNext()) {
              Rule rule = (Rule) it.next();
              rule.setDigester(digester);
          }
      }
      
      /** Gets namespace to apply to Rule's added */
      public String getNamespaceURI() {
          return wrappedRules.getNamespaceURI();
      }
      
      /** Sets namespace to apply to Rule's added subsequently */
      public void setNamespaceURI(String namespaceURI) {
          wrappedRules.setNamespaceURI(namespaceURI);
      }
      
      /** Gets Rule's which will be fired when the wrapped implementation returns no matches
*/
      public List getDefaults() {
          return defaultRules;
      }	
      
      // --------------------------------------------------------- Public Methods
      
      public List match(String pattern) {
          return match("", pattern);
      }	
      
      /**
       * Return list of rules matching given pattern.
       * If wrapped implementation returns any matches return those.
       * Otherwise, return default matches.
       */
      public List match(String namespaceURI, String pattern) {
          List matches = wrappedRules.match(namespaceURI, pattern);
          if (matches ==  null || matches.isEmpty()) {	
              // a little bit of defensive programming
              return new ArrayList(defaultRules);
          }
          // otherwise
          return matches;
      }
      
      /** Adds a rule to be fired when wrapped implementation returns no matches */
      public void addDefault(Rule rule) {
          // set up rule
          if (wrappedRules.getDigester() != null) {
              rule.setDigester(wrappedRules.getDigester());
          }
          
          if (wrappedRules.getNamespaceURI() != null) {
              rule.setNamespaceURI(wrappedRules.getNamespaceURI());
          }
          
          defaultRules.add(rule);
          allRules.add(rule);
      }
      
      /** Gets all rules */
      public List rules() {
          return allRules;
      }
      
      /** Clears all Rule's */
      public void clear() {
          wrappedRules.clear();
          allRules.clear();
          defaultRules.clear();
      }
      
      /** 
       * Adds a Rule to be fired on given pattern.
       * Pattern matching is delegated to wrapped implementation.
       */
      public void add(String pattern, Rule rule) {
          wrappedRules.add(pattern, rule);
          allRules.add(rule);
      }	
  }
  
  
  
  1.1                  jakarta-commons/digester/src/test/org/apache/commons/digester/WDRulesWrapperTestCase.java
  
  Index: WDRulesWrapperTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/WDRulesWrapperTestCase.java,v
1.1 2003/04/28 17:51:32 rdonkin Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/28 17:51:32 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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 apache@apache.org.
   *
   * 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.util.List;
  
  import junit.framework.TestCase;
  
  /**
   * Test case for WithDefaultsRulesWrapper
   *
   * @author Robert Burrell Donkin
   * @version $Revision: 1.1 $ $Date: 2003/04/28 17:51:32 $
   */
  
  public class WDRulesWrapperTestCase extends TestCase {
      
      /** Base constructor */
      public WDRulesWrapperTestCase(String name) {
          super(name);
      }
      
      public void testClear() {
          // test clear wrapped
          WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
          rules.add("alpha", new TestRule("Tom"));
          rules.add("alpha", new TestRule("Dick"));
          rules.add("alpha", new TestRule("Harry"));
          
          assertNotNull("Rules should not be null",  rules.rules());
          assertEquals("Wrong number of rules registered (1)", 3 , rules.rules().size());
          rules.clear();
          assertEquals("Clear Failed (1)", 0 , rules.rules().size());
          
          // mixed
          rules.add("alpha", new TestRule("Tom"));
          rules.add("alpha", new TestRule("Dick"));
          rules.add("alpha", new TestRule("Harry"));
          rules.addDefault(new TestRule("Roger"));
          assertEquals("Wrong number of rules registered (2)", 4 , rules.rules().size());
          rules.clear();
          assertEquals("Clear Failed (2)", 0 , rules.rules().size());
          
          rules.addDefault(new TestRule("Roger"));
          assertEquals("Wrong number of rules registered (3)", 1 , rules.rules().size());
          rules.clear();
          assertEquals("Clear Failed (3)", 0 , rules.rules().size());
      }
      
      public void testRules() {
          // test rules
          WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
          rules.add("alpha", new TestRule("Tom"));
          rules.add("alpha", new TestRule("Dick"));
          rules.addDefault(new TestRule("Roger"));
          rules.add("alpha", new TestRule("Harry"));
          
          assertNotNull("Rules should not be null",  rules.rules());
          assertEquals("Wrong order (1)", "Tom" , ((TestRule) rules.rules().get(0)).getIdentifier());
          assertEquals("Wrong order (2)", "Dick" , ((TestRule) rules.rules().get(1)).getIdentifier());
          assertEquals("Wrong order (3)", "Roger" , ((TestRule) rules.rules().get(2)).getIdentifier());
          assertEquals("Wrong order (4)", "Harry" , ((TestRule) rules.rules().get(3)).getIdentifier());
      }
      
      public void testMatch() {
          // test no defaults
          WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
          rules.add("alpha", new TestRule("Tom"));
          rules.add("alpha", new TestRule("Dick"));
          rules.add("alpha", new TestRule("Harry"));
          rules.addDefault(new TestRule("Roger"));
          rules.addDefault(new TestRule("Rabbit"));
          
          List matches = rules.match("", "alpha");
          assertEquals("Wrong size (1)", 3 , matches.size());
          assertEquals("Wrong order (1)", "Tom" , ((TestRule) matches.get(0)).getIdentifier());
          assertEquals("Wrong order (2)", "Dick" , ((TestRule) matches.get(1)).getIdentifier());
          assertEquals("Wrong order (3)", "Harry" , ((TestRule) matches.get(2)).getIdentifier());
          
          matches = rules.match("", "not-alpha");
          assertEquals("Wrong size (2)", 2 , matches.size());
          assertEquals("Wrong order (4)", "Roger" , ((TestRule) matches.get(0)).getIdentifier());
          assertEquals("Wrong order (5)", "Rabbit" , ((TestRule) matches.get(1)).getIdentifier());
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message