View Javadoc

1   /* $Id: IncludeTest.java 1382109 2012-09-07 18:19:16Z 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.xmlrules;
20  
21  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.StringReader;
25  import java.net.URL;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.commons.digester3.Digester;
30  import org.apache.commons.digester3.Rule;
31  import org.apache.commons.digester3.binder.AbstractRulesModule;
32  import org.junit.Test;
33  
34  /**
35   * Test for the include class functionality
36   */
37  public class IncludeTest
38  {
39  
40      public static class TestDigesterRulesModule
41          extends AbstractRulesModule
42      {
43  
44          @Override
45          protected void configure()
46          {
47              forPattern( "bar" ).addRule( new Rule()
48              {
49  
50                  @Override
51                  public void body( String namespace, String name, String text )
52                      throws Exception
53                  {
54                      List<String> stringList = getDigester().peek();
55                      stringList.add( text );
56                  }
57  
58              } );
59          }
60  
61      }
62  
63      @Test
64      public void testBasicInclude()
65          throws Exception
66      {
67          final String rulesXml = "<?xml version='1.0'?>"
68                  + "<!DOCTYPE digester-rules PUBLIC \"-//Apache Commons //DTD digester-rules XML V1.0//EN\" "
69                  + "\"http://commons.apache.org/digester/dtds/digester-rules-3.0.dtd\">"
70                  + "<digester-rules>"
71                  + " <pattern value='root/foo'>"
72                  + "   <include class='org.apache.commons.digester3.xmlrules.IncludeTest$TestDigesterRulesModule' />"
73                  + " </pattern>"
74                  + "</digester-rules>";
75  
76          String xml = "<?xml version='1.0' ?><root><foo><bar>short</bar></foo></root>";
77  
78          List<String> list = new ArrayList<String>();
79          Digester digester = newLoader( new FromXmlRulesModule()
80          {
81  
82              @Override
83              protected void loadRules()
84              {
85                  loadXMLRulesFromText( rulesXml );
86              }
87  
88          } ).newDigester();
89          digester.push( list );
90          digester.parse( new StringReader( xml ) );
91  
92          assertEquals( "Number of entries", 1, list.size() );
93          assertEquals( "Entry value", "short", list.get( 0 ) );
94      }
95  
96      @Test
97      public void testUrlInclude()
98          throws Exception
99      {
100         final String rulesXml = "<?xml version='1.0'?>"
101                 + "<!DOCTYPE digester-rules PUBLIC \"-//Apache Commons //DTD digester-rules XML V1.0//EN\" "
102                 + "\"http://commons.apache.org/digester/dtds/digester-rules-3.0.dtd\">"
103                 + "<digester-rules>"
104                 + " <pattern value='root/foo1'>"
105                 + "   <include url='classpath:org/apache/commons/digester3/xmlrules/testrulesinclude.xml' />"
106                 + " </pattern>"
107                 + " <pattern value='root/foo2'>"
108                 + "   <include url='classpath:org/apache/commons/digester3/xmlrules/testrulesinclude.xml' />"
109                 + " </pattern>"
110                 + "</digester-rules>";
111 
112         String xml = "<?xml version='1.0' ?><root><foo1><bar><foo value='foo1'/></bar></foo1><foo2><bar><foo value='foo2'/></bar></foo2></root>";
113 
114         List<String> list = new ArrayList<String>();
115         Digester digester = newLoader( new FromXmlRulesModule()
116         {
117 
118             @Override
119             protected void loadRules()
120             {
121                 loadXMLRulesFromText( rulesXml );
122             }
123 
124         }).newDigester();
125         digester.push( list );
126         digester.parse( new StringReader( xml ) );
127         assertEquals( "[foo1, foo2]", list.toString() );
128     }
129 
130     /**
131      * Validates that circular includes are detected and result in an exception
132      */
133     @Test( expected = org.apache.commons.digester3.binder.DigesterLoadingException.class )
134     public void testCircularInclude()
135         throws Exception
136     {
137         final URL url = ClassLoader.getSystemResource( "org/apache/commons/digester3/xmlrules/testCircularRules.xml" );
138         newLoader( new FromXmlRulesModule()
139         {
140 
141             @Override
142             protected void loadRules()
143             {
144                 loadXMLRules( url );
145             }
146 
147         }).newDigester();
148     }
149 
150 }