<commit_before>/* $Id$
 *
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed 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 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 junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.xml.sax.SAXException;


/**
 * <p> Test case for <code>SetNestedPropertiesRule</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 SetNestedPropertiesRuleTestCase extends TestCase {


    // ----------------------------------------------------- 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</alpha>" +
        "<beta>BETA BODY</beta>" +
        "<gamma>GAMMA BODY</gamma>" +
        "<delta>DELTA BODY</delta>" +
        "</root>";


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


    // ----------------------------------------------------------- Constructors


    /**
     * Construct a new instance of this test case.
     *
     * @param name Name of the test case
     */
    public SetNestedPropertiesRuleTestCase(String name) {

        super(name);

    }


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


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

        digester = new Digester();

    }


    /**
     * Return the tests included in this test suite.
     */
    public static Test suite() {

        return (new TestSuite(SetNestedPropertiesRuleTestCase.class));

    }


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

        digester = null;

    }



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


    /**
     * Test that you can successfully automatically set properties.
     */
    public void testAutomaticallySetProperties()
        throws SAXException, IOException {

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

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

        assertEquals(
                "Property delta not set correctly",
                "DELTA BODY",
                bean.getDeltaValue());
    }

    /**
     * Test that it is an error when a child element exists but no corresponding
     * java property exists.
     */
    public void testMandatoryProperties()
        throws SAXException, IOException {

        String TEST_XML =
            "<?xml version='1.0'?>" +
            "<root>ROOT BODY" +
            "<badprop>ALPHA BODY</badprop>" +
            "</root>";

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

        try {
            SimpleTestBean bean = (SimpleTestBean) digester.parse(
                new StringReader(TEST_XML));

            // we should never get here...
            fail("No exception thrown by parse when unknown child element found.");
        } catch(org.xml.sax.SAXParseException e) {
            String msg = e.getMessage();
            if (msg.contains("badprop")) {
                // ok, this is expected; there is no "setBadprop" method on the
                // SimpleTestBean class...
            } else {
                fail("Unexpected parse exception:" + e.getMessage());
            }
        }
    }

    /**
     * Test that you can customise the property mappings using the
     * constructor which takes arrays-of-strings.
     */
    public void testCustomisedProperties1()
        throws SAXException, IOException {

        String TEST_XML =
            "<?xml version='1.0'?>" +
            "<root>ROOT BODY" +
            "<alpha>ALPHA BODY</alpha>" +
            "<beta>BETA BODY</beta>" +
            "<gamma-alt>GAMMA BODY</gamma-alt>" +
            "<delta>DELTA BODY</delta>" +
            "</root>";

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

        // ignore the "alpha" element (target=null)
        // don't remap the "beta" element
        // map the gamma-alt element into the gamma property<commit_msg>Fix accidental use of java1.5 method.<commit_after>/* $Id$
 *
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed 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 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 junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.xml.sax.SAXException;


/**
 * <p> Test case for <code>SetNestedPropertiesRule</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 SetNestedPropertiesRuleTestCase extends TestCase {


    // ----------------------------------------------------- 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</alpha>" +
        "<beta>BETA BODY</beta>" +
        "<gamma>GAMMA BODY</gamma>" +
        "<delta>DELTA BODY</delta>" +
        "</root>";


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


    // ----------------------------------------------------------- Constructors


    /**
     * Construct a new instance of this test case.
     *
     * @param name Name of the test case
     */
    public SetNestedPropertiesRuleTestCase(String name) {

        super(name);

    }


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


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

        digester = new Digester();

    }


    /**
     * Return the tests included in this test suite.
     */
    public static Test suite() {

        return (new TestSuite(SetNestedPropertiesRuleTestCase.class));

    }


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

        digester = null;

    }



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


    /**
     * Test that you can successfully automatically set properties.
     */
    public void testAutomaticallySetProperties()
        throws SAXException, IOException {

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

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

        assertEquals(
                "Property delta not set correctly",
                "DELTA BODY",
                bean.getDeltaValue());
    }

    /**
     * Test that it is an error when a child element exists but no corresponding
     * java property exists.
     */
    public void testMandatoryProperties()
        throws SAXException, IOException {

        String TEST_XML =
            "<?xml version='1.0'?>" +
            "<root>ROOT BODY" +
            "<badprop>ALPHA BODY</badprop>" +
            "</root>";

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

        try {
            SimpleTestBean bean = (SimpleTestBean) digester.parse(
                new StringReader(TEST_XML));

            // we should never get here...
            fail("No exception thrown by parse when unknown child element found.");
        } catch(org.xml.sax.SAXParseException e) {
            String msg = e.getMessage();
            if (msg.indexOf("badprop") >= 0) {
                // ok, this is expected; there is no "setBadprop" method on the
                // SimpleTestBean class...
            } else {
                fail("Unexpected parse exception:" + e.getMessage());
            }
        }
    }

    /**
     * Test that you can customise the property mappings using the
     * constructor which takes arrays-of-strings.
     */
    public void testCustomisedProperties1()
        throws SAXException, IOException {

        String TEST_XML =
            "<?xml version='1.0'?>" +
            "<root>ROOT BODY" +
            "<alpha>ALPHA BODY</alpha>" +
            "<beta>BETA BODY</beta>" +
            "<gamma-alt>GAMMA BODY</gamma-alt>" +
            "<delta>DELTA BODY</delta>" +
            "</root>";

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

        // ignore the "alpha" element (target=null)
        // don't remap the "beta" element
        // map the gamma-alt element into the gamma property
{'fraction_non_alphanumeric': 0.1296933923594032, 'fraction_numerical': 0.0033612067552057714, 'mean_word_length': 1.8147208121827412, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 6, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251789', 'n_tokens_mistral': 3180, 'n_tokens_neox': 2983, 'n_words': 989}