001/* $Id: TestRuleInfo.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 the declaration of custom rules for a plugin using a separate class to define the rules.
033 */
034
035public class TestRuleInfo
036{
037
038    // --------------------------------------------------------------- Test cases
039    @Test
040    public void testRuleInfoExplicitClass()
041        throws Exception
042    {
043        // * tests that custom rules can be declared on a
044        // separate class by explicitly declaring the rule class.
045
046        Digester digester = new Digester();
047        PluginRules rc = new PluginRules();
048        digester.setRules( rc );
049
050        PluginDeclarationRule pdr = new PluginDeclarationRule();
051        digester.addRule( "root/plugin", pdr );
052
053        PluginCreateRule pcr = new PluginCreateRule( Widget.class );
054        digester.addRule( "root/widget", pcr );
055        digester.addSetNext( "root/widget", "addChild" );
056
057        Container root = new Container();
058        digester.push( root );
059
060        try
061        {
062            digester.parse( Utils.getInputStream( this, "test5a.xml" ) );
063        }
064        catch ( Exception e )
065        {
066            throw e;
067        }
068
069        Object child;
070        List<Widget> children = root.getChildren();
071        assertNotNull( children );
072        assertEquals( 1, children.size() );
073
074        child = children.get( 0 );
075        assertNotNull( child );
076        assertEquals( TextLabel2.class, child.getClass() );
077        TextLabel2 label = (TextLabel2) child;
078
079        // id should not be mapped, label should
080        assertEquals( "anonymous", label.getId() );
081        assertEquals( "std label", label.getLabel() );
082    }
083
084    @Test
085    public void testRuleInfoExplicitMethod()
086        throws Exception
087    {
088        // * tests that custom rules can be declared on a
089        // separate class by explicitly declaring the rule class.
090        // and explicitly declaring the rule method name.
091
092        Digester digester = new Digester();
093        PluginRules rc = new PluginRules();
094        digester.setRules( rc );
095
096        PluginDeclarationRule pdr = new PluginDeclarationRule();
097        digester.addRule( "root/plugin", pdr );
098
099        PluginCreateRule pcr = new PluginCreateRule( Widget.class );
100        digester.addRule( "root/widget", pcr );
101        digester.addSetNext( "root/widget", "addChild" );
102
103        Container root = new Container();
104        digester.push( root );
105
106        try
107        {
108            digester.parse( Utils.getInputStream( this, "test5b.xml" ) );
109        }
110        catch ( Exception e )
111        {
112            throw e;
113        }
114
115        Object child;
116        List<Widget> children = root.getChildren();
117        assertNotNull( children );
118        assertEquals( 1, children.size() );
119
120        child = children.get( 0 );
121        assertNotNull( child );
122        assertEquals( TextLabel2.class, child.getClass() );
123        TextLabel2 label = (TextLabel2) child;
124
125        // id should not be mapped, altlabel should
126        assertEquals( "anonymous", label.getId() );
127        assertEquals( "alt label", label.getLabel() );
128    }
129
130    @Test
131    public void testRuleInfoAutoDetect()
132        throws Exception
133    {
134        // * tests that custom rules can be declared on a
135        // separate class with name {plugin-class}RuleInfo,
136        // and they are automatically detected and loaded.
137
138        Digester digester = new Digester();
139        PluginRules rc = new PluginRules();
140        digester.setRules( rc );
141
142        PluginDeclarationRule pdr = new PluginDeclarationRule();
143        digester.addRule( "root/plugin", pdr );
144
145        PluginCreateRule pcr = new PluginCreateRule( Widget.class );
146        digester.addRule( "root/widget", pcr );
147        digester.addSetNext( "root/widget", "addChild" );
148
149        Container root = new Container();
150        digester.push( root );
151
152        try
153        {
154            digester.parse( Utils.getInputStream( this, "test5c.xml" ) );
155        }
156        catch ( Exception e )
157        {
158            throw e;
159        }
160
161        Object child;
162        List<Widget> children = root.getChildren();
163        assertNotNull( children );
164        assertEquals( 1, children.size() );
165
166        child = children.get( 0 );
167        assertNotNull( child );
168        assertEquals( TextLabel2.class, child.getClass() );
169        TextLabel2 label = (TextLabel2) child;
170
171        // id should not be mapped, label should
172        assertEquals( "anonymous", label.getId() );
173        assertEquals( "std label", label.getLabel() );
174    }
175}