commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1067656 - in /commons/sandbox/digester3/trunk/src: main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
Date Sun, 06 Feb 2011 13:03:56 GMT
Author: simonetripodi
Date: Sun Feb  6 13:03:56 2011
New Revision: 1067656

URL: http://svn.apache.org/viewvc?rev=1067656&view=rev
Log:
first checkin of WithDefaultsRulesWrapper + related TestCase class, adapted version of the
propers on /trunk
old testcases continue to pass!

Added:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
  (with props)
    commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
  (with props)

Added: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java?rev=1067656&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
(added)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
Sun Feb  6 13:03:56 2011
@@ -0,0 +1,130 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.digester3.rules;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.digester3.Digester;
+import org.apache.commons.digester3.Rule;
+import org.apache.commons.digester3.spi.Rules;
+
+/**
+ * <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> instances to be added to any

+ * existing <code>Rules</code> implementation. These default <code>Rule</code>

+ * instances 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>
+ */
+public class WithDefaultsRulesWrapper implements Rules {
+
+    /** The Rules implementation that this class wraps. */
+    private Rules wrappedRules;
+
+    /** Rules to be fired when the wrapped implementations returns none. */
+    private List<Rule> defaultRules = new ArrayList<Rule>();
+
+    /** All rules (preserves order in which they were originally added) */
+    private List<Rule> allRules = new ArrayList<Rule>();
+
+    // --------------------------------------------------------- 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;
+    }
+
+    /** Sets digeseter using these Rules */
+    public void setDigester(Digester digester) {
+        wrappedRules.setDigester(digester);
+        for (Rule rule : defaultRules) {
+            rule.setDigester(digester);
+        }
+    }
+
+    /** Gets Rule's which will be fired when the wrapped implementation returns no matches
*/
+    public List<Rule> getDefaults() {
+        return defaultRules;
+    }
+
+    /**
+     * Return list of rules matching given pattern.
+     * If wrapped implementation returns any matches return those.
+     * Otherwise, return default matches.
+     */
+    public List<Rule> match(String namespaceURI, String pattern) {
+        List<Rule> matches = wrappedRules.match(namespaceURI, pattern);
+        if (matches ==  null || matches.isEmpty()) {
+            // a little bit of defensive programming
+            return new ArrayList<Rule>(defaultRules);
+        }
+        // otherwise
+        return matches;
+    }
+
+    /** Adds a rule to be fired when wrapped implementation returns no matches */
+    public void addDefault(Rule rule) {
+        defaultRules.add(rule);
+        allRules.add(rule);
+    }
+
+    /** Gets all rules */
+    public List<Rule> 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);
+    }
+
+}

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java?rev=1067656&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
(added)
+++ commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
Sun Feb  6 13:03:56 2011
@@ -0,0 +1,104 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.digester3.rules;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+
+import org.apache.commons.digester3.OrderRule;
+import org.apache.commons.digester3.Rule;
+import org.junit.Test;
+
+/**
+ * Test case for WithDefaultsRulesWrapper
+ *
+ * @author Robert Burrell Donkin
+ * @version $Revision$ $Date$
+ */
+
+public class WithDefaultsRulesWrapperTestCase {
+
+    @Test
+    public void testClear() {
+        // test clear wrapped
+        WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
+        rules.add("alpha", new OrderRule("Tom"));
+        rules.add("alpha", new OrderRule("Dick"));
+        rules.add("alpha", new OrderRule("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 OrderRule("Tom"));
+        rules.add("alpha", new OrderRule("Dick"));
+        rules.add("alpha", new OrderRule("Harry"));
+        rules.addDefault(new OrderRule("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 OrderRule("Roger"));
+        assertEquals("Wrong number of rules registered (3)", 1 , rules.rules().size());
+        rules.clear();
+        assertEquals("Clear Failed (3)", 0 , rules.rules().size());
+    }
+
+    @Test
+    public void testRules() {
+        // test rules
+        WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
+        rules.add("alpha", new OrderRule("Tom"));
+        rules.add("alpha", new OrderRule("Dick"));
+        rules.addDefault(new OrderRule("Roger"));
+        rules.add("alpha", new OrderRule("Harry"));
+
+        assertNotNull("Rules should not be null",  rules.rules());
+        assertEquals("Wrong order (1)", "Tom" , ((OrderRule) rules.rules().get(0)).getIdentifier());
+        assertEquals("Wrong order (2)", "Dick" , ((OrderRule) rules.rules().get(1)).getIdentifier());
+        assertEquals("Wrong order (3)", "Roger" , ((OrderRule) rules.rules().get(2)).getIdentifier());
+        assertEquals("Wrong order (4)", "Harry" , ((OrderRule) rules.rules().get(3)).getIdentifier());
+    }
+
+    @Test
+    public void testMatch() {
+        // test no defaults
+        WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
+        rules.add("alpha", new OrderRule("Tom"));
+        rules.add("alpha", new OrderRule("Dick"));
+        rules.add("alpha", new OrderRule("Harry"));
+        rules.addDefault(new OrderRule("Roger"));
+        rules.addDefault(new OrderRule("Rabbit"));
+
+        List<Rule> matches = rules.match("", "alpha");
+        assertEquals("Wrong size (1)", 3 , matches.size());
+        assertEquals("Wrong order (1)", "Tom" , ((OrderRule) matches.get(0)).getIdentifier());
+        assertEquals("Wrong order (2)", "Dick" , ((OrderRule) matches.get(1)).getIdentifier());
+        assertEquals("Wrong order (3)", "Harry" , ((OrderRule) matches.get(2)).getIdentifier());
+
+        matches = rules.match("", "not-alpha");
+        assertEquals("Wrong size (2)", 2 , matches.size());
+        assertEquals("Wrong order (4)", "Roger" , ((OrderRule) matches.get(0)).getIdentifier());
+        assertEquals("Wrong order (5)", "Rabbit" , ((OrderRule) matches.get(1)).getIdentifier());
+    }
+
+}

Propchange: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/rules/WithDefaultsRulesWrapperTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message