001/* $Id: SetPropertiesRuleTestCase.java 1197969 2011-11-05 15:15:10Z 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;
029
030import org.apache.commons.digester3.binder.AbstractRulesModule;
031import org.junit.Test;
032import org.xml.sax.SAXException;
033
034/**
035 * <p>
036 * Test case for <code>SetPropertiesRule</code>.
037 * </p>
038 */
039public class SetPropertiesRuleTestCase
040{
041
042    // ----------------------------------------------------- Instance Variables
043
044    /**
045     * Simple test xml document used in the tests.
046     */
047    protected final static String TEST_XML_1 =
048        "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
049
050    /**
051     * Simple test xml document used in the tests.
052     */
053    protected final static String TEST_XML_2 =
054        "<?xml version='1.0'?><root alpa='ALPA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
055
056    /**
057     * Simple test xml document used in the tests.
058     */
059    protected final static String TEST_XML_3 =
060        "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE' ignore='ignore value'/>";
061
062    // ------------------------------------------------ Individual Test Methods
063
064    /**
065     * Positive test for SetPropertiesRule.
066     */
067    @Test
068    public void testPositive()
069        throws Exception
070    {
071        Digester digester = newLoader( new AbstractRulesModule()
072        {
073
074            @Override
075            protected void configure()
076            {
077                forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
078                    .then()
079                    .setProperties();
080            }
081
082        }).newDigester();
083
084        // Parse the input
085        SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_1 ) );
086
087        // Check that the properties were set correctly
088        assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
089        assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
090        assertNull( "gamma property not set", bean.getGamma() );
091        assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
092
093    }
094
095    /**
096     * Positive test for SetPropertyRule ignoring missing properties.
097     */
098    @Test
099    public void testIgnoreMissing()
100        throws Exception
101    {
102        Digester digester = newLoader( new AbstractRulesModule()
103        {
104
105            @Override
106            protected void configure()
107            {
108                forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
109                    .then()
110                    .setProperties();
111            }
112
113        }).newDigester();
114
115        // Parse the input
116        SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
117
118        // Check that the properties were set correctly
119        assertNull( "alpha property not set", bean.getAlpha() );
120        assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
121        assertNull( "gamma property not set", bean.getGamma() );
122        assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
123
124    }
125
126    /**
127     * Negative test for SetPropertyRule ignoring missing properties.
128     */
129    @Test
130    public void testNegativeNotIgnoreMissing()
131        throws Exception
132    {
133        Digester digester = newLoader( new AbstractRulesModule()
134        {
135
136            @Override
137            protected void configure()
138            {
139                forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
140                    .then()
141                    .setProperties().ignoreMissingProperty( false );
142            }
143
144        }).newDigester();
145
146        try
147        {
148            // Parse the input
149            SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
150            fail( "Should have thrown NoSuchMethodException" );
151            assertNotNull( bean ); // just to prevent compiler warning on unused var
152        }
153        catch ( Exception e )
154        {
155            if ( e instanceof NoSuchMethodException )
156            {
157                // Expected;
158            }
159            else if ( e instanceof SAXException )
160            {
161                Exception ee = ( (SAXException) e ).getException();
162                if ( ee != null )
163                {
164                    if ( ee instanceof NoSuchMethodException )
165                    {
166                        // Expected result
167                    }
168                    else
169                    {
170                        fail( "Should have thrown SE->NoSuchMethodException, threw " + ee );
171                    }
172                }
173                else
174                {
175                    fail( "Should have thrown NoSuchMethodException, threw " + e.getClass().getName() );
176                }
177            }
178            else
179            {
180                fail( "Should have thrown NoSuchMethodException, threw " + e );
181            }
182        }
183    }
184
185    /**
186     * Negative test for SetPropertyRule ignoring missing properties.
187     */
188    @Test
189    public void testPositiveNotIgnoreMissingWithIgnoreAttributes()
190        throws Exception
191    {
192        Digester digester = newLoader( new AbstractRulesModule()
193        {
194
195            @Override
196            protected void configure()
197            {
198                forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
199                    .then()
200                    .setProperties()
201                        .addAlias( "ignore" ).forProperty( null )
202                        .ignoreMissingProperty( false );
203            }
204
205        }).newDigester();
206
207        // Parse the input
208        SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_3 ) );
209
210        // Check that the properties were set correctly
211        assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
212        assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
213        assertNull( "gamma property not set", bean.getGamma() );
214        assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
215    }
216
217    /**
218     * Get input stream from specified String containing XML data.
219     */
220    private Reader xmlTestReader( String xml )
221    {
222        return new StringReader( xml );
223    }
224
225}