001/* $Id: TestLocalRules.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 defining custom rules on the plugin class itself.
033 */
034
035public class TestLocalRules
036{
037
038    // --------------------------------------------------------------- Test cases
039    @Test
040    public void testLocalRules()
041        throws Exception
042    {
043        // * tests that the plugin class can define an addRules method,
044        // which gets detected and executed.
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 pcr2 = new PluginCreateRule( Widget.class );
053        digester.addRule( "root/widget", pcr2 );
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, "test4a.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( 3, children.size() );
072
073        // min/max rules should be in effect
074        // setproperties should be in effect
075        child = children.get( 0 );
076        assertNotNull( child );
077        assertEquals( Slider.class, child.getClass() );
078        Slider slider1 = (Slider) child;
079        assertEquals( "slider1", slider1.getLabel() );
080        assertEquals( 1, slider1.getMin() );
081        assertEquals( 2, slider1.getMax() );
082
083        // range rules should not be in effect
084        // setproperties should be in effect
085        child = children.get( 1 );
086        assertNotNull( child );
087        assertEquals( Slider.class, child.getClass() );
088        Slider slider2 = (Slider) child;
089        assertEquals( "slider2", slider2.getLabel() );
090        assertEquals( 0, slider2.getMin() );
091        assertEquals( 0, slider2.getMax() );
092
093        // setproperties should be working on text label
094        child = children.get( 2 );
095        assertNotNull( child );
096        assertEquals( TextLabel.class, child.getClass() );
097        assertEquals( "text1", ( (TextLabel) child ).getLabel() );
098    }
099
100    @Test
101    public void testNonStandardLocalRules()
102        throws Exception
103    {
104        // * tests that using PluginDeclarationRule to declare an alternate
105        // rule method name invokes that alternate method instead (the
106        // input xml specifies that a method other than addRules is to
107        // be used)
108        // * tests that if a rule method is defined, then a SetProperties
109        // rule is not automatically added.
110        // * tests that a SetProperties rule applying to one class doesn't
111        // apply to different plugin classes mounted at the same rule.
112        Digester digester = new Digester();
113        PluginRules rc = new PluginRules();
114        digester.setRules( rc );
115
116        PluginDeclarationRule pdr = new PluginDeclarationRule();
117        digester.addRule( "root/plugin", pdr );
118
119        PluginCreateRule pcr2 = new PluginCreateRule( Widget.class );
120        digester.addRule( "root/widget", pcr2 );
121        digester.addSetNext( "root/widget", "addChild" );
122
123        Container root = new Container();
124        digester.push( root );
125
126        try
127        {
128            digester.parse( Utils.getInputStream( this, "test4b.xml" ) );
129        }
130        catch ( Exception e )
131        {
132            throw e;
133        }
134
135        Object child;
136        List<Widget> children = root.getChildren();
137        assertNotNull( children );
138        assertEquals( 3, children.size() );
139
140        // min/max rules should not be in effect
141        // setproperties should not be in effect
142        child = children.get( 0 );
143        assertNotNull( child );
144        assertEquals( Slider.class, child.getClass() );
145        Slider slider1 = (Slider) child;
146        assertEquals( "nolabel", slider1.getLabel() );
147        assertEquals( 0, slider1.getMin() );
148        assertEquals( 0, slider1.getMax() );
149
150        // range rules should be in effect
151        // setproperties should not be in effect
152        child = children.get( 1 );
153        assertNotNull( child );
154        assertEquals( Slider.class, child.getClass() );
155        Slider slider2 = (Slider) child;
156        assertEquals( "nolabel", slider2.getLabel() );
157        assertEquals( 10, slider2.getMin() );
158        assertEquals( 20, slider2.getMax() );
159
160        // setproperties should be working on text label
161        child = children.get( 2 );
162        assertNotNull( child );
163        assertEquals( TextLabel.class, child.getClass() );
164        assertEquals( "text1", ( (TextLabel) child ).getLabel() );
165    }
166}