View Javadoc

1   /* $Id: TestConfigurablePluginAttributes.java 1102402 2011-05-12 18:03:26Z simonetripodi $
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.digester3.plugins;
20  
21  import static org.junit.Assert.*;
22  
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  import org.apache.commons.digester3.Digester;
27  import org.apache.commons.digester3.plugins.PluginCreateRule;
28  import org.apache.commons.digester3.plugins.PluginDeclarationRule;
29  import org.apache.commons.digester3.plugins.PluginRules;
30  import org.junit.Test;
31  
32  /**
33   * Test cases for functionality which sets what xml attributes specify the plugin class or plugin declaration id.
34   */
35  
36  public class TestConfigurablePluginAttributes
37  {
38  
39      // --------------------------------------------------------------- Test cases
40      @Test
41      public void testDefaultBehaviour()
42          throws Exception
43      {
44          // tests that by default the attributes used are
45          // named "plugin-class" and "plugin-id"
46  
47          Digester digester = new Digester();
48          digester.setNamespaceAware( true );
49          PluginRules rc = new PluginRules();
50          digester.setRules( rc );
51  
52          PluginDeclarationRule pdr = new PluginDeclarationRule();
53          digester.addRule( "root/plugin", pdr );
54  
55          PluginCreateRule widgetPluginRule = new PluginCreateRule( Widget.class );
56          digester.addRule( "root/widget", widgetPluginRule );
57          digester.addSetNext( "root/widget", "addWidget" );
58  
59          PluginCreateRule gadgetPluginRule = new PluginCreateRule( Widget.class );
60          digester.addRule( "root/gadget", gadgetPluginRule );
61          digester.addSetNext( "root/gadget", "addGadget" );
62  
63          MultiContainer root = new MultiContainer();
64          digester.push( root );
65  
66          try
67          {
68              digester.parse( Utils.getInputStream( this, "test7.xml" ) );
69  
70          }
71          catch ( Exception e )
72          {
73              throw e;
74          }
75  
76          List<Widget> widgets = root.getWidgets();
77          assertNotNull( widgets );
78          assertEquals( 4, widgets.size() );
79  
80          assertEquals( TextLabel.class, widgets.get( 0 ).getClass() );
81          assertEquals( TextLabel.class, widgets.get( 1 ).getClass() );
82          assertEquals( TextLabel.class, widgets.get( 2 ).getClass() );
83          assertEquals( TextLabel.class, widgets.get( 3 ).getClass() );
84  
85          List<Widget> gadgets = root.getGadgets();
86          assertNotNull( gadgets );
87          assertEquals( 4, gadgets.size() );
88  
89          assertEquals( TextLabel.class, gadgets.get( 0 ).getClass() );
90          assertEquals( TextLabel.class, gadgets.get( 1 ).getClass() );
91          assertEquals( TextLabel.class, gadgets.get( 2 ).getClass() );
92          assertEquals( TextLabel.class, gadgets.get( 3 ).getClass() );
93      }
94  
95      @Test
96      public void testGlobalOverride()
97          throws Exception
98      {
99          // Tests that using setDefaultPluginXXXX overrides behaviour for all
100         // PluginCreateRule instances. Also tests specifying attributes
101         // with "null" for namespace (ie attributes not in any namespace).
102         //
103         // note that in order not to screw up all other tests, we need
104         // to reset the global names after we finish here!
105 
106         Digester digester = new Digester();
107         digester.setNamespaceAware( true );
108         PluginRules rc = new PluginRules();
109         digester.setRules( rc );
110 
111         rc.setPluginIdAttribute( null, "id" );
112         rc.setPluginClassAttribute( null, "class" );
113 
114         PluginDeclarationRule pdr = new PluginDeclarationRule();
115         digester.addRule( "root/plugin", pdr );
116 
117         PluginCreateRule widgetPluginRule = new PluginCreateRule( Widget.class );
118         digester.addRule( "root/widget", widgetPluginRule );
119         digester.addSetNext( "root/widget", "addWidget" );
120 
121         PluginCreateRule gadgetPluginRule = new PluginCreateRule( Widget.class );
122         digester.addRule( "root/gadget", gadgetPluginRule );
123         digester.addSetNext( "root/gadget", "addGadget" );
124 
125         MultiContainer root = new MultiContainer();
126         digester.push( root );
127 
128         try
129         {
130             digester.parse( Utils.getInputStream( this, "test7.xml" ) );
131 
132         }
133         catch ( Exception e )
134         {
135             throw e;
136         }
137 
138         List<Widget> widgets = root.getWidgets();
139         assertNotNull( widgets );
140         assertEquals( 4, widgets.size() );
141 
142         assertEquals( Slider.class, widgets.get( 0 ).getClass() );
143         assertEquals( Slider.class, widgets.get( 1 ).getClass() );
144         assertEquals( Slider.class, widgets.get( 2 ).getClass() );
145         assertEquals( Slider.class, widgets.get( 3 ).getClass() );
146 
147         List<Widget> gadgets = root.getGadgets();
148         assertNotNull( gadgets );
149         assertEquals( 4, gadgets.size() );
150 
151         assertEquals( Slider.class, gadgets.get( 0 ).getClass() );
152         assertEquals( Slider.class, gadgets.get( 1 ).getClass() );
153         assertEquals( Slider.class, gadgets.get( 2 ).getClass() );
154         assertEquals( Slider.class, gadgets.get( 3 ).getClass() );
155     }
156 
157     @Test
158     public void testInstanceOverride()
159         throws Exception
160     {
161         // Tests that using setPluginXXXX overrides behaviour for only
162         // that particular PluginCreateRule instance. Also tests that
163         // attributes can be in namespaces.
164 
165         Digester digester = new Digester();
166         digester.setNamespaceAware( true );
167         PluginRules rc = new PluginRules();
168         digester.setRules( rc );
169 
170         PluginDeclarationRule pdr = new PluginDeclarationRule();
171         digester.addRule( "root/plugin", pdr );
172 
173         // for plugins at pattern "root/widget", use xml attributes "id" and
174         // "class" in the custom namespace as the values for plugin id and
175         // class, not the default (and non-namespaced) values of
176         // "plugin-id" and "plugin-class".
177         PluginCreateRule widgetPluginRule = new PluginCreateRule( Widget.class );
178         widgetPluginRule.setPluginIdAttribute( "http://commons.apache.org/digester/plugins", "id" );
179         widgetPluginRule.setPluginClassAttribute( "http://commons.apache.org/digester/plugins", "class" );
180         digester.addRule( "root/widget", widgetPluginRule );
181         digester.addSetNext( "root/widget", "addWidget" );
182 
183         PluginCreateRule gadgetPluginRule = new PluginCreateRule( Widget.class );
184         digester.addRule( "root/gadget", gadgetPluginRule );
185         digester.addSetNext( "root/gadget", "addGadget" );
186 
187         MultiContainer root = new MultiContainer();
188         digester.push( root );
189 
190         try
191         {
192             digester.parse( Utils.getInputStream( this, "test7.xml" ) );
193         }
194         catch ( Exception e )
195         {
196             throw e;
197         }
198 
199         List<Widget> widgets = root.getWidgets();
200         assertNotNull( widgets );
201         assertEquals( 4, widgets.size() );
202 
203         assertEquals( TextLabel2.class, widgets.get( 0 ).getClass() );
204         assertEquals( TextLabel2.class, widgets.get( 1 ).getClass() );
205         assertEquals( TextLabel2.class, widgets.get( 2 ).getClass() );
206         assertEquals( TextLabel2.class, widgets.get( 3 ).getClass() );
207 
208         List<Widget> gadgets = root.getGadgets();
209         assertNotNull( gadgets );
210         assertEquals( 4, gadgets.size() );
211 
212         assertEquals( TextLabel.class, gadgets.get( 0 ).getClass() );
213         assertEquals( TextLabel.class, gadgets.get( 1 ).getClass() );
214         assertEquals( TextLabel.class, gadgets.get( 2 ).getClass() );
215         assertEquals( TextLabel.class, gadgets.get( 3 ).getClass() );
216     }
217 
218     // inner classes used for testing
219 
220     public static class MultiContainer
221     {
222         private LinkedList<Widget> widgets = new LinkedList<Widget>();
223 
224         private LinkedList<Widget> gadgets = new LinkedList<Widget>();
225 
226         public MultiContainer()
227         {
228         }
229 
230         public void addWidget( Widget child )
231         {
232             widgets.add( child );
233         }
234 
235         public List<Widget> getWidgets()
236         {
237             return widgets;
238         }
239 
240         public void addGadget( Widget child )
241         {
242             gadgets.add( child );
243         }
244 
245         public List<Widget> getGadgets()
246         {
247             return gadgets;
248         }
249     }
250 }