001/* $Id: TestDeclaration.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.PluginDeclarationRule;
028import org.apache.commons.digester3.plugins.PluginRules;
029import org.junit.Test;
030
031/**
032 * Test cases for basic PluginDeclarationRule behaviour.
033 */
034
035public class TestDeclaration
036{
037
038    // --------------------------------------------------------------- Test cases
039    @Test
040    public void testPredeclaration()
041        throws Exception
042    {
043        // * tests that rules can be declared via a PluginDeclarationRule
044
045        Digester digester = new Digester();
046        PluginRules rc = new PluginRules();
047        digester.setRules( rc );
048
049        PluginDeclarationRule pdr = new PluginDeclarationRule();
050        digester.addRule( "root/plugin", pdr );
051
052        PluginCreateRule pcr = new PluginCreateRule( Widget.class );
053        digester.addRule( "root/widget", pcr );
054        digester.addSetNext( "root/widget", "addChild" );
055
056        Container root = new Container();
057        digester.push( root );
058
059        try
060        {
061            digester.parse( Utils.getInputStream( this, "test3.xml" ) );
062        }
063        catch ( Exception e )
064        {
065            throw e;
066        }
067
068        Object child;
069        List<Widget> children = root.getChildren();
070        assertNotNull( children );
071        assertEquals( 2, children.size() );
072
073        child = children.get( 0 );
074        assertNotNull( child );
075        assertEquals( TextLabel.class, child.getClass() );
076        assertEquals( "label1", ( (TextLabel) child ).getLabel() );
077
078        child = children.get( 1 );
079        assertNotNull( child );
080        assertEquals( TextLabel.class, child.getClass() );
081        assertEquals( "label2", ( (TextLabel) child ).getLabel() );
082    }
083}