1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
36
37
38 public class NamespaceSnapshotTestCase
39 {
40
41
42
43
44 static class NamespaceSnapshotRule
45 extends Rule
46 {
47
48
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
63
64 public NamespaceSnapshotRule get()
65 {
66 return new NamespaceSnapshotRule();
67 }
68
69 }
70
71 }
72
73
74
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
141
142
143
144
145
146
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 }