You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/02/05 16:27:20 UTC

svn commit: r1067457 - /commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java

Author: simonetripodi
Date: Sat Feb  5 15:27:20 2011
New Revision: 1067457

URL: http://svn.apache.org/viewvc?rev=1067457&view=rev
Log:
started importing testcases, first checkin of BeanPropertySetterRuleTestCase, an adapted version of the proper on /trunk
old testcases continue pass!

Added:
    commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java   (with props)

Added: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java?rev=1067457&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java (added)
+++ commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java Sat Feb  5 15:27:20 2011
@@ -0,0 +1,300 @@
+/* $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;
+
+import static org.apache.commons.digester3.DigesterLoader.newLoader;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.digester3.rules.ExtendedBaseRules;
+import org.junit.Test;
+import org.xml.sax.SAXException;
+
+/**
+ * <p> Test case for <code>BeanPropertySetterRule</code>.
+ * This contains tests for the main applications of the rule
+ * and two more general tests of digester functionality used by this rule.
+ */
+public class BeanPropertySetterRuleTestCase extends AbstractTestCase {
+
+    /**
+     * Simple test xml document used in the tests.
+     */
+    protected final static String TEST_XML =
+        "<?xml version='1.0'?>" +
+        "<root>ROOT BODY" +
+        "<alpha>ALPHA BODY</alpha>" +
+        "<beta>BETA BODY</beta>" +
+        "<gamma>GAMMA BODY</gamma>" +
+        "<delta>DELTA BODY</delta>" +
+        "</root>";
+
+    /**
+     * This is a general digester test but it fits into here pretty well.
+     * This tests that the rule calling order is properly enforced.
+     */
+    @Test
+    public void testDigesterRuleCallOrder() throws SAXException, IOException {
+        List<Rule> callOrder = new ArrayList<Rule>();
+
+        // add first test rule
+        final OrderRule firstRule = new OrderRule("first");
+        firstRule.setOrder(callOrder);
+
+        // add second test rule
+        final OrderRule secondRule = new OrderRule("second");
+        secondRule.setOrder(callOrder);
+
+        // add third test rule
+        final OrderRule thirdRule = new OrderRule("third");
+        thirdRule.setOrder(callOrder);
+
+        newBasicDigester(new AbstractRulesModule() {
+
+            @Override
+            protected void configure() {
+                forPattern("root/alpha").addRule(firstRule)
+                                        .then().addRule(secondRule)
+                                        .then().addRule(thirdRule);
+            }
+
+        }).parse(xmlTestReader());
+
+        // we should have nine entries in our list of calls
+
+        assertEquals(
+                "Nine calls should have been made.",
+                9,
+                callOrder.size());
+
+        // begin should be called in the order added
+        assertEquals(
+                "First rule begin not called first.",
+                "first",
+                ((OrderRule) callOrder.get(0)).getIdentifier());
+
+        assertEquals(
+                "Second rule begin not called second.",
+                "second",
+                ((OrderRule) callOrder.get(1)).getIdentifier());
+
+        assertEquals(
+                "Third rule begin not called third.",
+                "third",
+                ((OrderRule) callOrder.get(2)).getIdentifier());
+
+        // body text should be called in the order added
+        assertEquals(
+                "First rule body text not called first.",
+                "first",
+                ((OrderRule) callOrder.get(3)).getIdentifier());
+
+        assertEquals(
+                "Second rule body text not called second.",
+                "second",
+                ((OrderRule) callOrder.get(4)).getIdentifier());
+
+        assertEquals(
+                "Third rule body text not called third.",
+                "third",
+                ((OrderRule) callOrder.get(5)).getIdentifier());
+
+        // end should be called in reverse order
+        assertEquals(
+                "Third rule end not called first.",
+                "third",
+                ((OrderRule) callOrder.get(6)).getIdentifier());
+
+        assertEquals(
+                "Second rule end not called second.",
+                "second",
+                ((OrderRule) callOrder.get(7)).getIdentifier());
+
+        assertEquals(
+                "First rule end not called third.",
+                "first",
+                ((OrderRule) callOrder.get(8)).getIdentifier());
+    }
+
+    /**
+     * This is a general digester test but it fits into here pretty well.
+     * This tests that the body text stack is functioning correctly.
+     */
+    @Test
+    public void testDigesterBodyTextStack() throws SAXException, IOException {
+        final OrderRule rootRule = new OrderRule("root");
+
+        // add test rule to catch body text
+        final OrderRule alphaRule = new OrderRule("root/alpha");
+
+        // add test rule to catch body text
+        final OrderRule betaRule = new OrderRule("root/beta");
+
+        // add test rule to catch body text
+        final OrderRule gammaRule = new OrderRule("root/gamma");
+
+        newBasicDigester(new AbstractRulesModule() {
+
+            @Override
+            protected void configure() {
+                forPattern("root").addRule(rootRule);
+                forPattern("root/alpha").addRule(alphaRule);
+                forPattern("root/beta").addRule(betaRule);
+                forPattern("root/gamma").addRule(gammaRule);
+            }
+
+        }).parse(xmlTestReader());
+
+        assertEquals(
+                "Root body text not set correct.",
+                "ROOT BODY",
+                rootRule.getBodyText());
+
+        assertEquals(
+                "Alpha body text not set correct.",
+                "ALPHA BODY",
+                alphaRule.getBodyText());
+
+        assertEquals(
+                "Beta body text not set correct.",
+                "BETA BODY",
+                betaRule.getBodyText());
+
+        assertEquals(
+                "Gamma body text not set correct.",
+                "GAMMA BODY",
+                gammaRule.getBodyText());
+    }
+
+    /**
+     * Test that you can successfully set a given property
+     */
+    @Test
+    public void testSetGivenProperty() throws SAXException, IOException {
+        SimpleTestBean bean = (SimpleTestBean) newBasicDigester(new AbstractRulesModule() {
+
+            @Override
+            protected void configure() {
+                forPattern("root").createObject().ofType("org.apache.commons.digester3.SimpleTestBean")
+                                  .then().setBeanProperty().withName("alpha");
+                forPattern("root/alpha").setBeanProperty().withName("beta");
+                forPattern("root/delta").setBeanProperty().withName("delta");
+            }
+
+        }).parse(xmlTestReader());
+
+        // check properties are set correctly
+        assertEquals(
+                "Property alpha not set correctly",
+                "ROOT BODY",
+                bean.getAlpha());
+
+        assertEquals(
+                "Property beta not set correctly",
+                "ALPHA BODY",
+                bean.getBeta());
+
+        assertTrue(
+                "Property gamma not set correctly",
+                bean.getGamma() == null);
+
+        assertEquals("Property delta not set correctly",
+                     "DELTA BODY",
+                     bean.getDeltaValue());
+    }
+
+    /**
+     * Test that trying to set an unknown property throws an exception.
+     */
+    @Test
+    public void testSetUnknownProperty() {
+        // Attempt to parse the input
+        try {
+            SimpleTestBean bean = (SimpleTestBean) newBasicDigester(new AbstractRulesModule() {
+
+                @Override
+                protected void configure() {
+                    forPattern("root").createObject().ofType("org.apache.commons.digester3.SimpleTestBean")
+                                      .then().setBeanProperty().withName("unknown");
+                }
+
+            }).parse(xmlTestReader());
+            fail("Should have thrown NoSuchMethodException");
+            assertNotNull(bean); // just to avoid compiler warning on unused variable
+        } catch (Exception e) {
+            if (e instanceof InvocationTargetException) {
+                Throwable t =
+                    ((InvocationTargetException) e).getTargetException();
+                if (t instanceof NoSuchMethodException) {
+                    // Expected result
+                } else {
+                    fail("Should have thrown NoSuchMethodException, threw " + t);
+                }
+            }
+        }
+    }
+
+    /**
+     * Test that you can successfully automatically set properties.
+     */
+    @Test
+    public void testAutomaticallySetProperties() throws SAXException, IOException {
+        SimpleTestBean bean = (SimpleTestBean) newLoader(new AbstractRulesModule() {
+
+            @Override
+            protected void configure() {
+                forPattern("root").createObject().ofType("org.apache.commons.digester3.SimpleTestBean");
+                forPattern("root/?").setBeanProperty();
+            }
+
+        }).newDigester(new ExtendedBaseRules()).parse(xmlTestReader());
+
+        // check properties are set correctly
+        assertEquals(
+                "Property alpha not set correctly",
+                "ALPHA BODY",
+                bean.getAlpha());
+
+        assertEquals(
+                "Property beta not set correctly",
+                "BETA BODY",
+                bean.getBeta());
+
+        assertEquals(
+                "Property gamma not set correctly",
+                "GAMMA BODY",
+                bean.getGamma());
+    }
+
+    /**
+     * Get input stream from {@link #TEST_XML}.
+     */
+    private Reader xmlTestReader() {
+        return new StringReader(TEST_XML);
+    }
+
+}

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

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

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