View Javadoc

1   /* $Id: TestDefaultPlugin.java 1212519 2011-12-09 17:01:07Z sebb $
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.List;
24  
25  import org.apache.commons.digester3.Digester;
26  import org.apache.commons.digester3.plugins.PluginConfigurationException;
27  import org.apache.commons.digester3.plugins.PluginCreateRule;
28  import org.apache.commons.digester3.plugins.PluginInvalidInputException;
29  import org.apache.commons.digester3.plugins.PluginRules;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.impl.NoOpLog;
32  import org.junit.Test;
33  import org.xml.sax.SAXParseException;
34  
35  public class TestDefaultPlugin
36  {
37  
38      // --------------------------------------------------------------- Test cases
39      @Test
40      public void testDefaultPlugins1()
41          throws Exception
42      {
43          // * tests that when a PluginCreateRule is defined with a default
44          // class, that the default class is instantiated when no class
45          // or id is specified in the xml file.
46          Digester digester = new Digester();
47          PluginRules rc = new PluginRules();
48          digester.setRules( rc );
49  
50          PluginCreateRule pcr = new PluginCreateRule( Widget.class, TextLabel.class );
51          digester.addRule( "root/widget", pcr );
52          digester.addSetNext( "root/widget", "addChild" );
53  
54          Container root = new Container();
55          digester.push( root );
56  
57          digester.parse( Utils.getInputStream( this, "test2.xml" ) );
58  
59          Object child;
60          List<Widget> children = root.getChildren();
61          assertNotNull( children );
62          assertEquals( 3, children.size() );
63  
64          child = children.get( 0 );
65          assertNotNull( child );
66          assertEquals( TextLabel.class, child.getClass() );
67          TextLabel label1 = (TextLabel) child;
68          assertEquals( "label1", label1.getLabel() );
69  
70          child = children.get( 1 );
71          assertNotNull( child );
72          assertEquals( TextLabel.class, child.getClass() );
73          TextLabel label2 = (TextLabel) child;
74          assertEquals( "label2", label2.getLabel() );
75  
76          child = children.get( 2 );
77          assertNotNull( child );
78          assertEquals( Slider.class, child.getClass() );
79          Slider slider1 = (Slider) child;
80          assertEquals( "slider1", slider1.getLabel() );
81      }
82  
83      public void testDefaultPlugins2()
84          throws Exception
85      {
86          // * tests that when there is no default plugin, it is an error
87          // not to have one of plugin-class or plugin-id specified
88          Digester digester = new Digester();
89          PluginRules rc = new PluginRules();
90          digester.setRules( rc );
91  
92          PluginCreateRule pcr = new PluginCreateRule( Widget.class );
93          digester.addRule( "root/widget", pcr );
94          digester.addSetNext( "root/widget", "addChild" );
95  
96          Container root = new Container();
97          digester.push( root );
98  
99          Log oldLog = digester.getLogger();
100         try
101         {
102             digester.setLogger( new NoOpLog() );
103             digester.parse( Utils.getInputStream( this, "test2.xml" ) );
104             fail("Expected SAXParseException");
105         }
106         catch ( SAXParseException e )
107         {
108             assertEquals( PluginInvalidInputException.class, e.getException().getClass() );
109         }
110         finally
111         {
112             digester.setLogger( oldLog );
113         }
114 
115     }
116 
117     public void testDefaultPlugins3()
118         throws Exception
119     {
120         // * tests that the default plugin must implement or extend the
121         // plugin base class.
122         Digester digester = new Digester();
123         PluginRules rc = new PluginRules();
124         digester.setRules( rc );
125 
126         PluginCreateRule pcr = new PluginCreateRule( Widget.class, Object.class );
127         digester.addRule( "root/widget", pcr );
128         digester.addSetNext( "root/widget", "addChild" );
129 
130         Container root = new Container();
131         digester.push( root );
132 
133         Log oldLog = digester.getLogger();
134         try
135         {
136             digester.setLogger( new NoOpLog() );
137             digester.parse( Utils.getInputStream( this, "test2.xml" ) );
138             fail("Expected SAXParseException");
139         }
140         catch ( SAXParseException e )
141         {
142             assertEquals( PluginConfigurationException.class, e.getException().getClass() );
143         }
144         finally
145         {
146             digester.setLogger( oldLog );
147         }
148     }
149 }