View Javadoc

1   /* $Id: TestRecursion.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 plugins with custom rules which include PluginCreateRule instances, allowing recursive datastructures
33   * to be processed.
34   */
35  
36  public class TestRecursion
37  {
38  
39      // --------------------------------------------------------------- Test cases
40      @Test
41      public void testRecursiveRules()
42          throws Exception
43      {
44          // * tests that a rule can declare custom PluginCreateRules
45          // that allow it to plug in instances of itself below
46          // itself.
47  
48          Digester digester = new Digester();
49          PluginRules rc = new PluginRules();
50          digester.setRules( rc );
51  
52          PluginDeclarationRule pdr = new PluginDeclarationRule();
53          digester.addRule( "*/plugin", pdr );
54  
55          PluginCreateRule pcr = new PluginCreateRule( Widget.class );
56          digester.addRule( "root/widget", pcr );
57          digester.addSetNext( "root/widget", "addChild" );
58  
59          Container root = new Container();
60          digester.push( root );
61  
62          try
63          {
64              digester.parse( Utils.getInputStream( this, "test6.xml" ) );
65          }
66          catch ( Exception e )
67          {
68              throw e;
69          }
70  
71          int nDescendants = countWidgets( root );
72          assertEquals( 10, nDescendants );
73      }
74  
75      private int countWidgets( Container c )
76      {
77          List<Widget> l = c.getChildren();
78          int sum = 0;
79          for ( Widget w : l )
80          {
81              ++sum;
82              if ( w instanceof Container )
83              {
84                  sum += countWidgets( (Container) w );
85              }
86          }
87          return sum;
88      }
89  }