001/* $Id: TestInline.java 1102402 2011-05-12 18:03: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.plugins;
020
021import static org.junit.Assert.*;
022
023import java.util.List;
024
025import org.apache.commons.digester3.Digester;
026import org.apache.commons.digester3.plugins.PluginCreateRule;
027import org.apache.commons.digester3.plugins.PluginRules;
028import org.junit.Test;
029
030/**
031 * Test cases for declaration of plugin classes "inline" (ie by specifying plugin-class).
032 */
033
034public class TestInline
035{
036
037    // --------------------------------------------------------------- Test cases
038    @Test
039    public void testInlineDeclaration()
040        throws Exception
041    {
042        // * tests that plugins can be specified by class, and that the
043        // correct class gets loaded.
044        // * tests that autosetproperties works
045        // * tests that multiple different classes can be loaded via the
046        // same plugin rule (ie at the same pattern).
047        Digester digester = new Digester();
048        PluginRules rc = new PluginRules();
049        digester.setRules( rc );
050
051        PluginCreateRule pcr = new PluginCreateRule( Widget.class );
052        digester.addRule( "root/widget", pcr );
053        digester.addSetNext( "root/widget", "addChild" );
054
055        Container root = new Container();
056        digester.push( root );
057
058        try
059        {
060            digester.parse( Utils.getInputStream( this, "test1.xml" ) );
061        }
062        catch ( Exception e )
063        {
064            throw e;
065        }
066
067        Object child;
068        List<Widget> children = root.getChildren();
069        assertNotNull( children );
070        assertEquals( 2, children.size() );
071
072        child = children.get( 0 );
073        assertNotNull( child );
074        assertEquals( TextLabel.class, child.getClass() );
075        TextLabel label1 = (TextLabel) child;
076        assertEquals( "anonymous", label1.getId() );
077        assertEquals( "1", label1.getLabel() );
078
079        child = children.get( 1 );
080        assertNotNull( child );
081        assertEquals( TextLabel.class, child.getClass() );
082        TextLabel label2 = (TextLabel) child;
083        assertEquals( "L1", label2.getId() );
084        assertEquals( "2", label2.getLabel() );
085    }
086
087    @Test
088    public void testLeadingSlash()
089        throws Exception
090    {
091        // Tests that PluginRules handles patterns with a leading slash.
092        //
093        // This test doesn't really belong in this class. If a separate test
094        // case class is created for PluginRules, then this method should be
095        // moved there.
096
097        Digester digester = new Digester();
098        PluginRules rc = new PluginRules();
099        digester.setRules( rc );
100
101        PluginCreateRule pcr = new PluginCreateRule( Widget.class );
102        digester.addRule( "/root/widget", pcr );
103        digester.addSetNext( "/root/widget", "addChild" );
104
105        Container root = new Container();
106        digester.push( root );
107
108        try
109        {
110            digester.parse( Utils.getInputStream( this, "test1.xml" ) );
111        }
112        catch ( Exception e )
113        {
114            throw e;
115        }
116
117        Object child;
118        List<Widget> children = root.getChildren();
119        assertNotNull( children );
120        assertEquals( 2, children.size() );
121
122        child = children.get( 0 );
123        assertNotNull( child );
124        assertEquals( TextLabel.class, child.getClass() );
125        TextLabel label1 = (TextLabel) child;
126        assertEquals( "anonymous", label1.getId() );
127        assertEquals( "1", label1.getLabel() );
128
129        child = children.get( 1 );
130        assertNotNull( child );
131        assertEquals( TextLabel.class, child.getClass() );
132        TextLabel label2 = (TextLabel) child;
133        assertEquals( "L1", label2.getId() );
134        assertEquals( "2", label2.getLabel() );
135    }
136
137}