View Javadoc

1   /* $Id: TestXmlRuleInfo.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.io.StringReader;
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 xmlrules format files.
33   */
34  
35  public class TestXmlRuleInfo
36  {
37  
38      // --------------------------------------------------------------- Test cases
39      @Test
40      public void testXmlRuleInfoExplicitFile()
41          throws Exception
42      {
43          // * tests that custom rules can be declared on a
44          // separate class by explicitly declaring a file containing
45          // the rules, using a relative or absolute path name.
46  
47          StringBuilder input = new StringBuilder();
48          input.append( "<root>" );
49          input.append( " <plugin" );
50          input.append( "  id='testobject'" );
51          input.append( "  class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
52          input.append( "  file='src/test/resources/org/apache/commons/digester3/plugins/xmlrules1.xml'" );
53          input.append( "  />" );
54          input.append( "  <object plugin-id='testobject'/>" );
55          input.append( "</root>" );
56  
57          Digester digester = new Digester();
58          PluginRules rc = new PluginRules();
59          digester.setRules( rc );
60  
61          PluginDeclarationRule pdr = new PluginDeclarationRule();
62          digester.addRule( "root/plugin", pdr );
63  
64          PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
65          digester.addRule( "root/object", pcr );
66  
67          try
68          {
69              digester.parse( new StringReader( input.toString() ) );
70          }
71          catch ( Exception e )
72          {
73              throw e;
74          }
75  
76          Object root = digester.getRoot();
77          assertEquals( ObjectTestImpl.class, root.getClass() );
78          ObjectTestImpl testObject = (ObjectTestImpl) root;
79          assertEquals( "xmlrules1", testObject.getValue() );
80      }
81  
82      @Test
83      public void testXmlRuleInfoExplicitResource()
84          throws Exception
85      {
86          // * tests that custom rules can be declared on a
87          // separate class by explicitly declaring the rule class.
88          // and explicitly declaring a file which is somewhere in the
89          // classpath.
90  
91          StringBuilder input = new StringBuilder();
92          input.append( "<root>" );
93          input.append( " <plugin" );
94          input.append( "  id='testobject'" );
95          input.append( "  class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
96          input.append( "  resource='org/apache/commons/digester3/plugins/xmlrules2.xml'" );
97          input.append( "  />" );
98          input.append( "  <object plugin-id='testobject'/>" );
99          input.append( "</root>" );
100 
101         Digester digester = new Digester();
102         PluginRules rc = new PluginRules();
103         digester.setRules( rc );
104 
105         PluginDeclarationRule pdr = new PluginDeclarationRule();
106         digester.addRule( "root/plugin", pdr );
107 
108         PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
109         digester.addRule( "root/object", pcr );
110 
111         try
112         {
113             digester.parse( new StringReader( input.toString() ) );
114         }
115         catch ( Exception e )
116         {
117             throw e;
118         }
119 
120         Object root = digester.getRoot();
121         assertEquals( ObjectTestImpl.class, root.getClass() );
122         ObjectTestImpl testObject = (ObjectTestImpl) root;
123         assertEquals( "xmlrules2", testObject.getValue() );
124     }
125 
126     @Test
127     public void testXmlRuleImplicitResource()
128         throws Exception
129     {
130         // * tests that custom rules can be declared on a
131         // separate class by explicitly declaring the rule class.
132         // and explicitly declaring a file which is somewhere in the
133         // classpath.
134 
135         StringBuilder input = new StringBuilder();
136         input.append( "<root>" );
137         input.append( " <plugin" );
138         input.append( "  id='testobject'" );
139         input.append( "  class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
140         input.append( "  />" );
141         input.append( "  <object plugin-id='testobject'/>" );
142         input.append( "</root>" );
143 
144         Digester digester = new Digester();
145         PluginRules rc = new PluginRules();
146         digester.setRules( rc );
147 
148         PluginDeclarationRule pdr = new PluginDeclarationRule();
149         digester.addRule( "root/plugin", pdr );
150 
151         PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
152         digester.addRule( "root/object", pcr );
153 
154         try
155         {
156             digester.parse( new StringReader( input.toString() ) );
157         }
158         catch ( Exception e )
159         {
160             throw e;
161         }
162 
163         Object root = digester.getRoot();
164         assertEquals( ObjectTestImpl.class, root.getClass() );
165         ObjectTestImpl testObject = (ObjectTestImpl) root;
166         assertEquals( "xmlrules-ruleinfo", testObject.getValue() );
167     }
168 }