alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Digester example source code file (BeanPropertySetterRuleTestCase.java)

This example Commons Digester source code file (BeanPropertySetterRuleTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Digester tags/keywords

alpha, beanpropertysetterrule, body, body, io, ioexception, property, property, reflection, sax, saxexception, simpletestbean, simpletestbean, test, testrule, testrule, third, util

The Commons Digester BeanPropertySetterRuleTestCase.java source code

/* $Id: BeanPropertySetterRuleTestCase.java 992060 2010-09-02 19:09:47Z simonetripodi $
 *
 * 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.digester;


import static org.junit.Assert.*;

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.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;


/**
 * <p> Test case for BeanPropertySetterRule.
 * 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 {


    // ----------------------------------------------------- Instance Variables

    /**
     * Simple test xml document used in the tests.
     */
    protected final static String TEST_XML =
        "<?xml version='1.0'?>" +
        "<root>ROOT BODY" +
        "<alpha>ALPHA BODY" +
        "<beta>BETA BODY" +
        "<gamma>GAMMA BODY" +
        "<delta>DELTA BODY" +
        "</root>";


    /**
     * The digester instance we will be processing.
     */
    protected Digester digester = null;


    // --------------------------------------------------- Overall Test Methods


    /**
     * Set up instance variables required by this test case.
     */
    @Before
    public void setUp() {

        digester = new Digester();

    }


    /**
     * Tear down instance variables required by this test case.
     */
    @After
    public void tearDown() {

        digester = null;

    }



    // ------------------------------------------------ Individual Test Methods


    /**
     * 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();

        // use the standard rules
        digester.setRules(new RulesBase());

        // add first test rule
        TestRule firstRule = new TestRule("first");
        firstRule.setOrder(callOrder);
        digester.addRule("root/alpha", firstRule);

        // add second test rule
        TestRule secondRule = new TestRule("second");
        secondRule.setOrder(callOrder);
        digester.addRule("root/alpha", secondRule);

        // add third test rule
        TestRule thirdRule = new TestRule("third");
        thirdRule.setOrder(callOrder);
        digester.addRule("root/alpha", thirdRule);


        digester.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",
                ((TestRule) callOrder.get(0)).getIdentifier());

        assertEquals(
                "Second rule begin not called second.",
                "second",
                ((TestRule) callOrder.get(1)).getIdentifier());

        assertEquals(
                "Third rule begin not called third.",
                "third",
                ((TestRule) callOrder.get(2)).getIdentifier());

        // body text should be called in the order added
        assertEquals(
                "First rule body text not called first.",
                "first",
                ((TestRule) callOrder.get(3)).getIdentifier());

        assertEquals(
                "Second rule body text not called second.",
                "second",
                ((TestRule) callOrder.get(4)).getIdentifier());

        assertEquals(
                "Third rule body text not called third.",
                "third",
                ((TestRule) callOrder.get(5)).getIdentifier());

        // end should be called in reverse order
        assertEquals(
                "Third rule end not called first.",
                "third",
                ((TestRule) callOrder.get(6)).getIdentifier());

        assertEquals(
                "Second rule end not called second.",
                "second",
                ((TestRule) callOrder.get(7)).getIdentifier());

        assertEquals(
                "First rule end not called third.",
                "first",
                ((TestRule) 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 {

        // use the standard rules
        digester.setRules(new RulesBase());

        // add test rule to catch body text
        TestRule rootRule = new TestRule("root");
        digester.addRule("root", rootRule);

        // add test rule to catch body text
        TestRule alphaRule = new TestRule("root/alpha");
        digester.addRule("root/alpha", alphaRule);

        // add test rule to catch body text
        TestRule betaRule = new TestRule("root/beta");
        digester.addRule("root/beta", betaRule);

        // add test rule to catch body text
        TestRule gammaRule = new TestRule("root/gamma");
        digester.addRule("root/gamma", gammaRule);

        digester.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 {

        // use the standard rules
        digester.setRules(new RulesBase());

        // going to be setting properties on a SimpleTestBean
        digester.addObjectCreate("root",
                                 "org.apache.commons.digester.SimpleTestBean");

        // we'll set property alpha with the body text of root
        digester.addRule("root", new BeanPropertySetterRule("alpha"));

        // we'll set property beta with the body text of child element alpha
        digester.addRule("root/alpha", new BeanPropertySetterRule("beta"));

        // we'll leave property gamma alone

        // we'll set property delta (a write-only property) also
        digester.addRule("root/delta", new BeanPropertySetterRule("delta"));

        SimpleTestBean bean = (SimpleTestBean) digester.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() {

        // going to be setting properties on a SimpleTestBean
        digester.addObjectCreate("root",
                                 "org.apache.commons.digester.SimpleTestBean");

        // attempt to set an unknown property name
        digester.addRule("root/alpha",
                         new BeanPropertySetterRule("unknown"));

        // Attempt to parse the input
        try {
            SimpleTestBean bean = (SimpleTestBean)
                digester.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 {

        // need the extended rules
        digester.setRules(new ExtendedBaseRules());

        // going to be setting properties on a SimpleTestBean
        digester.addObjectCreate("root",
                                 "org.apache.commons.digester.SimpleTestBean");

        // match all children of root with this rule
        digester.addRule("root/?", new BeanPropertySetterRule());

        SimpleTestBean bean = (SimpleTestBean) digester.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);
    }

}


Other Commons Digester examples (source code examples)

Here is a short list of links related to this Commons Digester BeanPropertySetterRuleTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.