View Javadoc

1   /* $Id: TestLocalRules.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 defining custom rules on the plugin class itself.
33   */
34  
35  public class TestLocalRules
36  {
37  
38      // --------------------------------------------------------------- Test cases
39      @Test
40      public void testLocalRules()
41          throws Exception
42      {
43          // * tests that the plugin class can define an addRules method,
44          // which gets detected and executed.
45          Digester digester = new Digester();
46          PluginRules rc = new PluginRules();
47          digester.setRules( rc );
48  
49          PluginDeclarationRule pdr = new PluginDeclarationRule();
50          digester.addRule( "root/plugin", pdr );
51  
52          PluginCreateRule pcr2 = new PluginCreateRule( Widget.class );
53          digester.addRule( "root/widget", pcr2 );
54          digester.addSetNext( "root/widget", "addChild" );
55  
56          Container root = new Container();
57          digester.push( root );
58  
59          try
60          {
61              digester.parse( Utils.getInputStream( this, "test4a.xml" ) );
62          }
63          catch ( Exception e )
64          {
65              throw e;
66          }
67  
68          Object child;
69          List<Widget> children = root.getChildren();
70          assertNotNull( children );
71          assertEquals( 3, children.size() );
72  
73          // min/max rules should be in effect
74          // setproperties should be in effect
75          child = children.get( 0 );
76          assertNotNull( child );
77          assertEquals( Slider.class, child.getClass() );
78          Slider slider1 = (Slider) child;
79          assertEquals( "slider1", slider1.getLabel() );
80          assertEquals( 1, slider1.getMin() );
81          assertEquals( 2, slider1.getMax() );
82  
83          // range rules should not be in effect
84          // setproperties should be in effect
85          child = children.get( 1 );
86          assertNotNull( child );
87          assertEquals( Slider.class, child.getClass() );
88          Slider slider2 = (Slider) child;
89          assertEquals( "slider2", slider2.getLabel() );
90          assertEquals( 0, slider2.getMin() );
91          assertEquals( 0, slider2.getMax() );
92  
93          // setproperties should be working on text label
94          child = children.get( 2 );
95          assertNotNull( child );
96          assertEquals( TextLabel.class, child.getClass() );
97          assertEquals( "text1", ( (TextLabel) child ).getLabel() );
98      }
99  
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 }