001/* $Id: TestFactoryCreate.java 1125767 2011-05-21 18:55:44Z 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.junit.Assert.assertEquals;
022import static org.junit.Assert.fail;
023
024import java.io.StringReader;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028
029import org.apache.commons.digester3.AbstractObjectCreationFactory;
030import org.apache.commons.digester3.Digester;
031import org.junit.Test;
032import org.junit.runner.RunWith;
033import org.junit.runners.Parameterized;
034import org.junit.runners.Parameterized.Parameters;
035import org.xml.sax.Attributes;
036
037/**
038 * Test case for factory create rules.
039 * 
040 * @author Robert Burrell Donkin
041 */
042@RunWith(value = Parameterized.class)
043public class TestFactoryCreate
044{
045
046    private final boolean ignoreCreateExceptions;
047
048    public TestFactoryCreate( boolean ignoreCreateExceptions )
049    {
050        this.ignoreCreateExceptions = ignoreCreateExceptions;
051    }
052
053    @Parameters
054    public static Collection<Object[]> data()
055    {
056        Collection<Object[]> data = new ArrayList<Object[]>(2);
057
058        data.add( new Object[] { true } );
059        data.add( new Object[] { false } );
060
061        return data;
062    }
063
064    // --------------------------------------------------------------- Test cases
065
066    @Test
067    public void testPropagateException()
068        throws Exception
069    {
070
071        // only used with this method
072        class ThrowExceptionCreateRule
073            extends AbstractObjectCreationFactory<Object>
074        {
075            @Override
076            public Object createObject( Attributes attributes )
077                throws Exception
078            {
079                throw new RuntimeException();
080            }
081        }
082
083        // now for the tests
084        String xml = "<?xml version='1.0' ?><root><element/></root>";
085
086        // test default - which is to propagate the exception
087        Digester digester = new Digester();
088        digester.addFactoryCreate( "root", new ThrowExceptionCreateRule(), ignoreCreateExceptions );
089        try
090        {
091
092            digester.parse( new StringReader( xml ) );
093            if ( !ignoreCreateExceptions )
094            {
095                fail( "Exception should be propagated from create rule" );
096            }
097
098        }
099        catch ( Exception e )
100        {
101            if ( ignoreCreateExceptions )
102            {
103                fail( "Exception should not be propagated" );
104            }
105        }
106    }
107
108    @Test
109    public void testFactoryCreateRule()
110        throws Exception
111    {
112
113        // test passing object create
114        Digester digester = new Digester();
115        ObjectCreationFactoryTestImpl factory = new ObjectCreationFactoryTestImpl();
116        digester.addFactoryCreate( "root", factory, ignoreCreateExceptions );
117        String xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
118        digester.parse( new StringReader( xml ) );
119
120        assertEquals( "Object create not called(1)[" + ignoreCreateExceptions + "]", factory.called, true );
121        assertEquals( "Attribute not passed (1)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
122                      "good" );
123        assertEquals( "Attribute not passed (2)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
124                      "bad" );
125        assertEquals( "Attribute not passed (3)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
126                      "ugly" );
127
128        digester = new Digester();
129        digester.addFactoryCreate( "root", "org.apache.commons.digester3.ObjectCreationFactoryTestImpl",
130                                   ignoreCreateExceptions );
131        digester.addSetNext( "root", "add" );
132        xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
133        List<ObjectCreationFactoryTestImpl> list = new ArrayList<ObjectCreationFactoryTestImpl>();
134        digester.push( list );
135        digester.parse( new StringReader( xml ) );
136
137        assertEquals( "List should contain only the factory object", list.size(), 1 );
138        factory = list.get( 0 );
139        assertEquals( "Object create not called(2)[" + ignoreCreateExceptions + "]", factory.called, true );
140        assertEquals( "Attribute not passed (4)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
141                      "good" );
142        assertEquals( "Attribute not passed (5)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
143                      "bad" );
144        assertEquals( "Attribute not passed (6)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
145                      "ugly" );
146
147        digester = new Digester();
148        digester.addFactoryCreate( "root", "org.apache.commons.digester3.ObjectCreationFactoryTestImpl", "override",
149                                   ignoreCreateExceptions );
150        digester.addSetNext( "root", "add" );
151        xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
152        list = new ArrayList<ObjectCreationFactoryTestImpl>();
153        digester.push( list );
154        digester.parse( new StringReader( xml ) );
155
156        assertEquals( "List should contain only the factory object", list.size(), 1 );
157        factory = list.get( 0 );
158        assertEquals( "Object create not called(3)[" + ignoreCreateExceptions + "]", factory.called, true );
159        assertEquals( "Attribute not passed (7)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
160                      "good" );
161        assertEquals( "Attribute not passed (8)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
162                      "bad" );
163        assertEquals( "Attribute not passed (8)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
164                      "ugly" );
165
166        digester = new Digester();
167        digester.addFactoryCreate( "root", "org.apache.commons.digester3.ObjectCreationFactoryTestImpl", "override",
168                                   ignoreCreateExceptions );
169        digester.addSetNext( "root", "add" );
170        xml =
171            new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' "
172                + " override='org.apache.commons.digester3.OtherTestObjectCreationFactory' >" + "<element/></root>" );
173        list = new ArrayList<ObjectCreationFactoryTestImpl>();
174        digester.push( list );
175        digester.parse( new StringReader( xml ) );
176
177        assertEquals( "List should contain only the factory object", list.size(), 1 );
178        factory = list.get( 0 );
179        assertEquals( "Attribute Override Failed (1)", factory.getClass().getName(),
180                      "org.apache.commons.digester3.OtherTestObjectCreationFactory" );
181        assertEquals( "Object create not called(4)[" + ignoreCreateExceptions + "]", factory.called, true );
182        assertEquals( "Attribute not passed (10)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
183                      "good" );
184        assertEquals( "Attribute not passed (11)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
185                      "bad" );
186        assertEquals( "Attribute not passed (12)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
187                      "ugly" );
188
189        digester = new Digester();
190        digester.addFactoryCreate( "root", ObjectCreationFactoryTestImpl.class, "override", ignoreCreateExceptions );
191        digester.addSetNext( "root", "add" );
192        xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
193        list = new ArrayList<ObjectCreationFactoryTestImpl>();
194        digester.push( list );
195        digester.parse( new StringReader( xml ) );
196
197        assertEquals( "List should contain only the factory object", list.size(), 1 );
198        factory = list.get( 0 );
199        assertEquals( "Object create not called(5)[" + ignoreCreateExceptions + "]", factory.called, true );
200        assertEquals( "Attribute not passed (13)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
201                      "good" );
202        assertEquals( "Attribute not passed (14)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
203                      "bad" );
204        assertEquals( "Attribute not passed (15)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
205                      "ugly" );
206
207        digester = new Digester();
208        digester.addFactoryCreate( "root", ObjectCreationFactoryTestImpl.class, "override", ignoreCreateExceptions );
209        digester.addSetNext( "root", "add" );
210        xml =
211            new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' "
212                + " override='org.apache.commons.digester3.OtherTestObjectCreationFactory' >" + "<element/></root>" );
213        list = new ArrayList<ObjectCreationFactoryTestImpl>();
214        digester.push( list );
215        digester.parse( new StringReader( xml ) );
216
217        assertEquals( "List should contain only the factory object", list.size(), 1 );
218        factory = list.get( 0 );
219        assertEquals( "Attribute Override Failed (2)", factory.getClass().getName(),
220                      "org.apache.commons.digester3.OtherTestObjectCreationFactory" );
221        assertEquals( "Object create not called(6)[" + ignoreCreateExceptions + "]", factory.called, true );
222        assertEquals( "Attribute not passed (16)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
223                      "good" );
224        assertEquals( "Attribute not passed (17)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
225                      "bad" );
226        assertEquals( "Attribute not passed (18)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
227                      "ugly" );
228    }
229}