View Javadoc

1   /* $Id: TestInline.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.PluginRules;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for declaration of plugin classes "inline" (ie by specifying plugin-class).
32   */
33  
34  public class TestInline
35  {
36  
37      // --------------------------------------------------------------- Test cases
38      @Test
39      public void testInlineDeclaration()
40          throws Exception
41      {
42          // * tests that plugins can be specified by class, and that the
43          // correct class gets loaded.
44          // * tests that autosetproperties works
45          // * tests that multiple different classes can be loaded via the
46          // same plugin rule (ie at the same pattern).
47          Digester digester = new Digester();
48          PluginRules rc = new PluginRules();
49          digester.setRules( rc );
50  
51          PluginCreateRule pcr = new PluginCreateRule( Widget.class );
52          digester.addRule( "root/widget", pcr );
53          digester.addSetNext( "root/widget", "addChild" );
54  
55          Container root = new Container();
56          digester.push( root );
57  
58          try
59          {
60              digester.parse( Utils.getInputStream( this, "test1.xml" ) );
61          }
62          catch ( Exception e )
63          {
64              throw e;
65          }
66  
67          Object child;
68          List<Widget> children = root.getChildren();
69          assertNotNull( children );
70          assertEquals( 2, children.size() );
71  
72          child = children.get( 0 );
73          assertNotNull( child );
74          assertEquals( TextLabel.class, child.getClass() );
75          TextLabel label1 = (TextLabel) child;
76          assertEquals( "anonymous", label1.getId() );
77          assertEquals( "1", label1.getLabel() );
78  
79          child = children.get( 1 );
80          assertNotNull( child );
81          assertEquals( TextLabel.class, child.getClass() );
82          TextLabel label2 = (TextLabel) child;
83          assertEquals( "L1", label2.getId() );
84          assertEquals( "2", label2.getLabel() );
85      }
86  
87      @Test
88      public void testLeadingSlash()
89          throws Exception
90      {
91          // Tests that PluginRules handles patterns with a leading slash.
92          //
93          // This test doesn't really belong in this class. If a separate test
94          // case class is created for PluginRules, then this method should be
95          // moved there.
96  
97          Digester digester = new Digester();
98          PluginRules rc = new PluginRules();
99          digester.setRules( rc );
100 
101         PluginCreateRule pcr = new PluginCreateRule( Widget.class );
102         digester.addRule( "/root/widget", pcr );
103         digester.addSetNext( "/root/widget", "addChild" );
104 
105         Container root = new Container();
106         digester.push( root );
107 
108         try
109         {
110             digester.parse( Utils.getInputStream( this, "test1.xml" ) );
111         }
112         catch ( Exception e )
113         {
114             throw e;
115         }
116 
117         Object child;
118         List<Widget> children = root.getChildren();
119         assertNotNull( children );
120         assertEquals( 2, children.size() );
121 
122         child = children.get( 0 );
123         assertNotNull( child );
124         assertEquals( TextLabel.class, child.getClass() );
125         TextLabel label1 = (TextLabel) child;
126         assertEquals( "anonymous", label1.getId() );
127         assertEquals( "1", label1.getLabel() );
128 
129         child = children.get( 1 );
130         assertNotNull( child );
131         assertEquals( TextLabel.class, child.getClass() );
132         TextLabel label2 = (TextLabel) child;
133         assertEquals( "L1", label2.getId() );
134         assertEquals( "2", label2.getLabel() );
135     }
136 
137 }