View Javadoc

1   /* $Id: TestRuleInfo.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.List;
24  
25  import org.apache.commons.digester3.Digester;
26  import org.apache.commons.digester3.plugins.PluginCreateRule;
27  import org.apache.commons.digester3.plugins.PluginDeclarationRule;
28  import org.apache.commons.digester3.plugins.PluginRules;
29  import org.junit.Test;
30  
31  /**
32   * Test cases for the declaration of custom rules for a plugin using a separate class to define the rules.
33   */
34  
35  public class TestRuleInfo
36  {
37  
38      // --------------------------------------------------------------- Test cases
39      @Test
40      public void testRuleInfoExplicitClass()
41          throws Exception
42      {
43          // * tests that custom rules can be declared on a
44          // separate class by explicitly declaring the rule class.
45  
46          Digester digester = new Digester();
47          PluginRules rc = new PluginRules();
48          digester.setRules( rc );
49  
50          PluginDeclarationRule pdr = new PluginDeclarationRule();
51          digester.addRule( "root/plugin", pdr );
52  
53          PluginCreateRule pcr = new PluginCreateRule( Widget.class );
54          digester.addRule( "root/widget", pcr );
55          digester.addSetNext( "root/widget", "addChild" );
56  
57          Container root = new Container();
58          digester.push( root );
59  
60          try
61          {
62              digester.parse( Utils.getInputStream( this, "test5a.xml" ) );
63          }
64          catch ( Exception e )
65          {
66              throw e;
67          }
68  
69          Object child;
70          List<Widget> children = root.getChildren();
71          assertNotNull( children );
72          assertEquals( 1, children.size() );
73  
74          child = children.get( 0 );
75          assertNotNull( child );
76          assertEquals( TextLabel2.class, child.getClass() );
77          TextLabel2 label = (TextLabel2) child;
78  
79          // id should not be mapped, label should
80          assertEquals( "anonymous", label.getId() );
81          assertEquals( "std label", label.getLabel() );
82      }
83  
84      @Test
85      public void testRuleInfoExplicitMethod()
86          throws Exception
87      {
88          // * tests that custom rules can be declared on a
89          // separate class by explicitly declaring the rule class.
90          // and explicitly declaring the rule method name.
91  
92          Digester digester = new Digester();
93          PluginRules rc = new PluginRules();
94          digester.setRules( rc );
95  
96          PluginDeclarationRule pdr = new PluginDeclarationRule();
97          digester.addRule( "root/plugin", pdr );
98  
99          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 }