View Javadoc

1   /* $Id: TestDeclaration.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 basic PluginDeclarationRule behaviour.
33   */
34  
35  public class TestDeclaration
36  {
37  
38      // --------------------------------------------------------------- Test cases
39      @Test
40      public void testPredeclaration()
41          throws Exception
42      {
43          // * tests that rules can be declared via a PluginDeclarationRule
44  
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 pcr = new PluginCreateRule( Widget.class );
53          digester.addRule( "root/widget", pcr );
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, "test3.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( 2, children.size() );
72  
73          child = children.get( 0 );
74          assertNotNull( child );
75          assertEquals( TextLabel.class, child.getClass() );
76          assertEquals( "label1", ( (TextLabel) child ).getLabel() );
77  
78          child = children.get( 1 );
79          assertNotNull( child );
80          assertEquals( TextLabel.class, child.getClass() );
81          assertEquals( "label2", ( (TextLabel) child ).getLabel() );
82      }
83  }