001/* $Id: SetPropertyRuleTestCase.java 1125741 2011-05-21 16:22:25Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.digester3;
020
021import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertNull;
025import static org.junit.Assert.fail;
026
027import java.io.Reader;
028import java.io.StringReader;
029import java.lang.reflect.InvocationTargetException;
030
031import org.apache.commons.digester3.binder.AbstractRulesModule;
032import org.apache.commons.digester3.binder.DigesterLoader;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.Test;
036import org.xml.sax.SAXException;
037
038/**
039 * <p>
040 * Test case for <code>SetPropertyRule</code>.
041 * </p>
042 */
043public class SetPropertyRuleTestCase
044{
045
046    // ----------------------------------------------------- Instance Variables
047
048    /**
049     * Simple test xml document used in the tests.
050     */
051    protected final static String TEST_XML_1 = "<?xml version='1.0'?><root>"
052        + "<set name='alpha' value='ALPHA VALUE'/>" + "<set name='beta' value='BETA VALUE'/>"
053        + "<set name='delta' value='DELTA VALUE'/>" + "</root>";
054
055    /**
056     * Simple test xml document used in the tests.
057     */
058    protected final static String TEST_XML_2 = "<?xml version='1.0'?><root>"
059        + "<set name='unknown' value='UNKNOWN VALUE'/>" + "</root>";
060
061    private final DigesterLoader loader = newLoader( new AbstractRulesModule()
062    {
063
064        @Override
065        protected void configure()
066        {
067            forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" );
068            forPattern( "root/set" ).setProperty( "name" ).extractingValueFromAttribute( "value" );
069        }
070
071    });
072
073    /**
074     * The digester instance we will be processing.
075     */
076    protected Digester digester = null;
077
078    // --------------------------------------------------- Overall Test Methods
079
080    /**
081     * Set up instance variables required by this test case.
082     */
083    @Before
084    public void setUp()
085    {
086
087        digester = loader.newDigester();
088
089    }
090
091    /**
092     * Tear down instance variables required by this test case.
093     */
094    @After
095    public void tearDown()
096    {
097
098        digester = null;
099
100    }
101
102    // ------------------------------------------------ Individual Test Methods
103
104    /**
105     * Positive test for SetPropertyRule.
106     */
107    @Test
108    public void testPositive()
109        throws Exception
110    {
111        // Parse the input
112        SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_1 ) );
113
114        // Check that the properties were set correctly
115        assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
116        assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
117        assertNull( "gamma property not set", bean.getGamma() );
118        assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
119
120    }
121
122    /**
123     * Negative test for SetPropertyRule.
124     */
125    @Test
126    public void testNegative()
127    {
128        // Parse the input (should fail)
129        try
130        {
131            SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
132            fail( "Should have thrown NoSuchMethodException" );
133            assertNotNull( bean ); // just to prevent compiler warning on unused var
134        }
135        catch ( Exception e )
136        {
137            if ( e instanceof NoSuchMethodException )
138            {
139                // Expected result
140            }
141            else if ( e instanceof InvocationTargetException )
142            {
143                Throwable t = ( (InvocationTargetException) e ).getTargetException();
144                if ( t instanceof NoSuchMethodException )
145                {
146                    // Expected result
147                }
148                else
149                {
150                    fail( "Should have thrown ITE->NoSuchMethodException, threw " + t );
151                }
152            }
153            else if ( e instanceof SAXException )
154            {
155                Exception ee = ( (SAXException) e ).getException();
156                if ( ee != null )
157                {
158                    if ( ee instanceof NoSuchMethodException )
159                    {
160                        // Expected result
161                    }
162                    else
163                    {
164                        fail( "Should have thrown SE->NoSuchMethodException, threw " + ee );
165                    }
166                }
167                else
168                {
169                    fail( "Should have thrown NoSuchMethodException, threw " + e.getClass().getName() );
170                }
171            }
172            else
173            {
174                fail( "Should have thrown NoSuchMethodException, threw " + e );
175            }
176        }
177
178    }
179
180    /**
181     * Get input stream from specified String containing XML data.
182     */
183    private Reader xmlTestReader( String xml )
184    {
185        return new StringReader( xml );
186    }
187
188    /**
189     * Test SetPropertyRule when matched XML element has no attributes. See: DIGESTER-114
190     */
191    @Test
192    public void testElementWithNoAttributes()
193        throws Exception
194    {
195        String TEST_XML_3 = "<?xml version='1.0'?><root><set/></root>";
196
197        // Parse the input - should not throw an exception
198        @SuppressWarnings( "unused" )
199        SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_3 ) );
200    }
201
202}