001/* $Id: NamespaceSnapshotTestCase.java 1125518 2011-05-20 19:21:57Z simonetripodi $ 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.commons.digester3; 020 021import static org.apache.commons.digester3.binder.DigesterLoader.newLoader; 022import static org.junit.Assert.assertEquals; 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.commons.digester3.binder.AbstractRulesModule; 030import org.apache.commons.digester3.binder.RuleProvider; 031import org.junit.Test; 032import org.xml.sax.Attributes; 033 034/** 035 * Tests namespace snapshotting. 036 */ 037 038public class NamespaceSnapshotTestCase 039{ 040 041 /** 042 * A test case specific helper rule. 043 */ 044 static class NamespaceSnapshotRule 045 extends Rule 046 { 047 /** 048 * @see Rule#begin(String, String, Attributes) 049 */ 050 @Override 051 public final void begin( final String namespace, final String name, final Attributes attributes ) 052 { 053 Digester d = getDigester(); 054 Map<String, String> namespaces = d.getCurrentNamespaces(); 055 ( (NamespacedBox) d.peek() ).setNamespaces( namespaces ); 056 } 057 058 public static class Provider implements RuleProvider<NamespaceSnapshotRule> 059 { 060 061 /** 062 * {@inheritDoc} 063 */ 064 public NamespaceSnapshotRule get() 065 { 066 return new NamespaceSnapshotRule(); 067 } 068 069 } 070 071 } 072 073 /** 074 * Namespace snapshot test case. 075 */ 076 @Test 077 public void testNamespaceSnapshots() 078 throws Exception 079 { 080 081 Digester digester = newLoader( new AbstractRulesModule() 082 { 083 084 @Override 085 protected void configure() 086 { 087 forPattern( "box" ).createObject().ofType( NamespacedBox.class ) 088 .then() 089 .setProperties() 090 .then() 091 .addRuleCreatedBy( new NamespaceSnapshotRule.Provider() ); 092 forPattern( "box/subBox" ).createObject().ofType( NamespacedBox.class ) 093 .then() 094 .setProperties() 095 .then() 096 .addRuleCreatedBy( new NamespaceSnapshotRule.Provider() ) 097 .then() 098 .setNext( "addChild" ); 099 } 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}