View Javadoc

1   /* $Id: NamespaceSnapshotTestCase.java 1125518 2011-05-20 19:21:57Z 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;
20  
21  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.digester3.binder.AbstractRulesModule;
30  import org.apache.commons.digester3.binder.RuleProvider;
31  import org.junit.Test;
32  import org.xml.sax.Attributes;
33  
34  /**
35   * Tests namespace snapshotting.
36   */
37  
38  public class NamespaceSnapshotTestCase
39  {
40  
41      /**
42       * A test case specific helper rule.
43       */
44      static class NamespaceSnapshotRule
45          extends Rule
46      {
47          /**
48           * @see Rule#begin(String, String, Attributes)
49           */
50          @Override
51          public final void begin( final String namespace, final String name, final Attributes attributes )
52          {
53              Digester d = getDigester();
54              Map<String, String> namespaces = d.getCurrentNamespaces();
55              ( (NamespacedBox) d.peek() ).setNamespaces( namespaces );
56          }
57  
58          public static class Provider implements RuleProvider<NamespaceSnapshotRule>
59          {
60  
61              /**
62               * {@inheritDoc}
63               */
64              public NamespaceSnapshotRule get()
65              {
66                  return new NamespaceSnapshotRule();
67              }
68  
69          }
70  
71      }
72  
73      /**
74       * Namespace snapshot test case.
75       */
76      @Test
77      public void testNamespaceSnapshots()
78          throws Exception
79      {
80  
81          Digester digester = newLoader( new AbstractRulesModule()
82          {
83  
84              @Override
85              protected void configure()
86              {
87                  forPattern( "box" ).createObject().ofType( NamespacedBox.class )
88                      .then()
89                      .setProperties()
90                      .then()
91                      .addRuleCreatedBy( new NamespaceSnapshotRule.Provider() );
92                  forPattern( "box/subBox" ).createObject().ofType( NamespacedBox.class )
93                      .then()
94                      .setProperties()
95                      .then()
96                      .addRuleCreatedBy( new NamespaceSnapshotRule.Provider() )
97                      .then()
98                      .setNext( "addChild" );
99              }
100 
101         }).setNamespaceAware( true ).newDigester();
102 
103         NamespacedBox root = digester.parse( getInputStream( "Test11.xml" ) );
104 
105         Map<String, String> nsmap = root.getNamespaces();
106         assertEquals( 3, nsmap.size() );
107         assertEquals( "", nsmap.get( "" ) );
108         assertEquals( "http://commons.apache.org/digester/Foo", nsmap.get( "foo" ) );
109         assertEquals( "http://commons.apache.org/digester/Bar", nsmap.get( "bar" ) );
110 
111         List<Box> children = root.getChildren();
112         assertEquals( 3, children.size() );
113 
114         NamespacedBox child1 = (NamespacedBox) children.get( 0 );
115         nsmap = child1.getNamespaces();
116         assertEquals( 3, nsmap.size() );
117         assertEquals( "", nsmap.get( "" ) );
118         assertEquals( "http://commons.apache.org/digester/Foo1", nsmap.get( "foo" ) );
119         assertEquals( "http://commons.apache.org/digester/Bar1", nsmap.get( "bar" ) );
120 
121         NamespacedBox child2 = (NamespacedBox) children.get( 1 );
122         nsmap = child2.getNamespaces();
123         assertEquals( 5, nsmap.size() );
124         assertEquals( "", nsmap.get( "" ) );
125         assertEquals( "http://commons.apache.org/digester/Foo", nsmap.get( "foo" ) );
126         assertEquals( "http://commons.apache.org/digester/Bar", nsmap.get( "bar" ) );
127         assertEquals( "http://commons.apache.org/digester/Alpha", nsmap.get( "alpha" ) );
128         assertEquals( "http://commons.apache.org/digester/Beta", nsmap.get( "beta" ) );
129 
130         NamespacedBox child3 = (NamespacedBox) children.get( 2 );
131         nsmap = child3.getNamespaces();
132         assertEquals( 4, nsmap.size() );
133         assertEquals( "", nsmap.get( "" ) );
134         assertEquals( "http://commons.apache.org/digester/Foo3", nsmap.get( "foo" ) );
135         assertEquals( "http://commons.apache.org/digester/Alpha", nsmap.get( "alpha" ) );
136         assertEquals( "http://commons.apache.org/digester/Bar", nsmap.get( "bar" ) );
137 
138     }
139 
140     // ------------------------------------------------ Utility Support Methods
141 
142     /**
143      * Return an appropriate InputStream for the specified test file (which must be inside our current package.
144      * 
145      * @param name Name of the test file we want
146      * @exception IOException if an input/output error occurs
147      */
148     protected InputStream getInputStream( String name )
149         throws IOException
150     {
151 
152         return ( this.getClass().getResourceAsStream( "/org/apache/commons/digester3/" + name ) );
153 
154     }
155 
156 }