commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1068274 - /commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/SetPropertiesRuleTestCase.java
Date Tue, 08 Feb 2011 06:52:51 GMT
Author: simonetripodi
Date: Tue Feb  8 06:52:51 2011
New Revision: 1068274

URL: http://svn.apache.org/viewvc?rev=1068274&view=rev
Log:
first checkin of SetPropertiesRuleTestCase, adapted versions of the proper on /trunk
old testcases continue pass!

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

Added: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/SetPropertiesRuleTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/SetPropertiesRuleTestCase.java?rev=1068274&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/SetPropertiesRuleTestCase.java
(added)
+++ commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/SetPropertiesRuleTestCase.java
Tue Feb  8 06:52:51 2011
@@ -0,0 +1,197 @@
+/* $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.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.junit.Test;
+import org.xml.sax.SAXException;
+
+/**
+ * Test case for {@code SetPropertiesRule}.
+ */
+public class SetPropertiesRuleTestCase extends AbstractTestCase {
+
+    /**
+     * Simple test xml document used in the tests.
+     */
+    protected final static String TEST_XML_1 =
+        "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA
VALUE'/>";
+
+    /**
+     * Simple test xml document used in the tests.
+     */
+    protected final static String TEST_XML_2 =
+        "<?xml version='1.0'?><root alpa='ALPA VALUE' beta='BETA VALUE' delta='DELTA
VALUE'/>";
+
+    /**
+     * Simple test xml document used in the tests.
+     */
+    protected final static String TEST_XML_3 =
+        "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA
VALUE' ignore='ignore value'/>";
+
+    /**
+     * Positive test for SetPropertiesRule.
+     */
+    @Test
+    public void testPositive() throws Exception {
+        // Parse the input
+        SimpleTestBean bean = (SimpleTestBean) newBasicDigester(new AbstractRulesModule()
{
+
+            @Override
+            protected void configure() {
+                forPattern("root")
+                    .createObject().ofType("org.apache.commons.digester3.SimpleTestBean")
+                    .then()
+                    .setProperties();
+            }
+
+        }).parse(xmlTestReader(TEST_XML_1));
+
+        // Check that the properties were set correctly
+        assertEquals("alpha property set",
+                     "ALPHA VALUE",
+                     bean.getAlpha());
+        assertEquals("beta property set",
+                     "BETA VALUE",
+                     bean.getBeta());
+        assertNull("gamma property not set",
+                   bean.getGamma());
+        assertEquals("delta property set",
+                     "DELTA VALUE",
+                     bean.getDeltaValue());
+    }
+
+    /**
+     * Positive test for SetPropertyRule ignoring missing properties.
+     */
+    @Test
+    public void testIgnoreMissing() throws Exception {
+        // Parse the input
+        SimpleTestBean bean = (SimpleTestBean) newBasicDigester(new AbstractRulesModule()
{
+
+            @Override
+            protected void configure() {
+                forPattern("root")
+                    .createObject().ofType("org.apache.commons.digester3.SimpleTestBean")
+                    .then()
+                    .setProperties();
+            }
+
+        }).parse(xmlTestReader(TEST_XML_2));
+
+        // Check that the properties were set correctly
+        assertNull("alpha property not set",
+                     bean.getAlpha());
+        assertEquals("beta property set",
+                     "BETA VALUE",
+                     bean.getBeta());
+        assertNull("gamma property not set",
+                   bean.getGamma());
+        assertEquals("delta property set",
+                     "DELTA VALUE",
+                     bean.getDeltaValue());
+    }
+
+    /**
+     * Negative test for SetPropertyRule ignoring missing properties.
+     */
+    @Test
+    public void testNegativeNotIgnoreMissing() throws Exception {
+        try {
+            // Parse the input
+            SimpleTestBean bean =  (SimpleTestBean) newBasicDigester(new AbstractRulesModule()
{
+
+                @Override
+                protected void configure() {
+                    forPattern("root")
+                        .createObject().ofType("org.apache.commons.digester3.SimpleTestBean")
+                        .then()
+                        .setProperties().ignoreMissingProperty(false);
+                }
+
+            }).parse(xmlTestReader(TEST_XML_2));
+            fail("Should have thrown NoSuchMethodException");
+            assertNotNull(bean); // just to prevent compiler warning on unused var
+        } catch (Exception e) {
+            if (e instanceof NoSuchMethodException) {
+                // Expected;
+            } else if (e instanceof SAXException) {
+                Exception ee = ((SAXException) e).getException();
+                if (ee != null) {
+                    if (ee instanceof NoSuchMethodException) {
+                        // Expected result
+                    } else {
+                        fail("Should have thrown SE->NoSuchMethodException, threw " +
ee);
+                    }
+                } else {
+                    fail("Should have thrown NoSuchMethodException, threw " +
+                         e.getClass().getName());
+                }
+            } else {
+                fail("Should have thrown NoSuchMethodException, threw " + e);
+            }
+        }
+    }
+
+    /**
+     * Negative test for SetPropertyRule ignoring missing properties.
+     */
+    @Test
+    public void testPositiveNotIgnoreMissingWithIgnoreAttributes() throws Exception {
+        // Parse the input
+        SimpleTestBean bean =  (SimpleTestBean) newBasicDigester(new AbstractRulesModule()
{
+
+            @Override
+            protected void configure() {
+                forPattern("root")
+                    .createObject().ofType("org.apache.commons.digester3.SimpleTestBean")
+                    .then()
+                    .setProperties().ignoreMissingProperty(false).addAlias("ignore", null);
+            }
+
+        }).parse(xmlTestReader(TEST_XML_3));
+
+        // Check that the properties were set correctly
+        assertEquals("alpha property set",
+                     "ALPHA VALUE",
+                     bean.getAlpha());
+        assertEquals("beta property set",
+                     "BETA VALUE",
+                     bean.getBeta());
+        assertNull("gamma property not set",
+                   bean.getGamma());
+        assertEquals("delta property set",
+                     "DELTA VALUE",
+                     bean.getDeltaValue());
+    }
+
+    /**
+     * Get input stream from specified String containing XML data.
+     */
+    private Reader xmlTestReader(String xml) {
+        return new StringReader(xml);
+    }
+
+}

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

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

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



Mime
View raw message