001/* $Id: ObjectParamRuleTestCase.java 1125720 2011-05-21 14:51:26Z 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.assertTrue;
025
026import java.io.IOException;
027import java.io.StringReader;
028import java.util.ArrayList;
029
030import org.apache.commons.digester3.binder.AbstractRulesModule;
031import org.junit.Test;
032import org.xml.sax.SAXException;
033
034/**
035 * <p>
036 * Tests for the <code>ObjectParamRuleTestCase</code>
037 * 
038 * @author Mark Huisman
039 */
040public class ObjectParamRuleTestCase
041{
042
043    // ------------------------------------------------ Individual Test Methods
044
045    private StringBuilder sb =
046        new StringBuilder().append( "<arraylist><A/><B/><C/><D desc=\"the fourth\"/><E/></arraylist>" );
047
048    /**
049     * Test method calls with the ObjectParamRule rule. It should be possible to pass any subclass of Object as a
050     * parameter, provided that either the element or the element + attribute has been matched.
051     */
052    @Test
053    public void testBasic()
054        throws SAXException, IOException
055    {
056        Digester digester = newLoader( new AbstractRulesModule()
057        {
058
059            @Override
060            protected void configure()
061            {
062                forPattern( "arraylist" ).createObject().ofType( ArrayList.class );
063                forPattern( "arraylist/A" ).callMethod( "add" ).withParamCount( 1 )
064                    .then()
065                    .objectParam( new Integer( -9 ) );
066                forPattern( "arraylist/B" ).callMethod( "add" ).withParamCount( 1 )
067                    .then()
068                    .objectParam( new Float( 3.14159 ) );
069                forPattern( "arraylist/C" ).callMethod( "add" ).withParamCount( 1 )
070                    .then()
071                    .objectParam( new Long( 999999999 ) );
072                forPattern( "arraylist/D" ).callMethod( "add" ).withParamCount( 1 )
073                    .then()
074                    .objectParam( new String( "foobarbazbing" ) ).matchingAttribute( "desc" );
075                forPattern( "arraylist/E" ).callMethod( "add" ).withParamCount( 1 )
076                    .then()
077                    .objectParam( new String( "ignore" ) ).matchingAttribute( "nonexistentattribute" );
078            }
079
080        }).newDigester();
081
082        // Parse it and obtain the ArrayList
083        ArrayList<?> al = digester.parse( new StringReader( sb.toString() ) );
084        assertNotNull( al );
085        assertEquals( al.size(), 4 );
086        assertTrue( al.contains( new Integer( -9 ) ) );
087        assertTrue( al.contains( new Float( 3.14159 ) ) );
088        assertTrue( al.contains( new Long( 999999999 ) ) );
089        assertTrue( al.contains( new String( "foobarbazbing" ) ) );
090        assertTrue( !( al.contains( new String( "ignore" ) ) ) );
091    }
092
093}