1 16 package org.apache.cocoon.xml; 17 18 import junit.framework.TestCase; 19 20 import org.xml.sax.ContentHandler ; 21 import org.xml.sax.helpers.DefaultHandler ; 22 23 28 public class NamespacesTableTestCase extends TestCase { 29 public NamespacesTableTestCase(String name) { 30 super(name); 31 } 32 33 public void testSimple() { 34 NamespacesTable ns = new NamespacesTable(); 35 36 ns.addDeclaration("ns1", "http://ns1"); 37 ns.addDeclaration("ns2", "http://ns2"); 38 39 ns.enterScope(); 40 41 assertEquals("http://ns1", ns.getUri("ns1")); 42 assertEquals("ns1", ns.getPrefix("http://ns1")); 43 44 assertEquals("http://ns2", ns.getUri("ns2")); 45 assertEquals("ns2", ns.getPrefix("http://ns2")); 46 47 ns.enterScope(); 48 49 ns.addDeclaration("ns3", "http://ns3"); 50 ns.enterScope(); 51 52 assertEquals("ns1", ns.getPrefix("http://ns1")); 53 assertEquals("ns3", ns.getPrefix("http://ns3")); 54 assertEquals(0, ns.getCurrentScopeDeclarations().length); 55 ns.leaveScope(); 56 57 assertNull(ns.getUri("ns3")); 59 assertEquals(1, ns.getCurrentScopeDeclarations().length); 61 assertEquals("ns3", ns.getCurrentScopeDeclarations()[0].getPrefix()); 62 63 ns.leaveScope(); 64 65 assertNull(ns.getPrefix(ns.getPrefix("http://ns3"))); 66 assertNull(ns.getUri("ns3")); 67 68 ns.leaveScope(); 69 assertNull(ns.getUri("ns1")); 71 assertNull(ns.getPrefix("http://ns1")); 72 73 assertNull(ns.getUri("ns2")); 74 assertNull(ns.getPrefix("http://ns2")); 75 76 NamespacesTable.Declaration[] prefixes = ns.getCurrentScopeDeclarations(); 78 assertEquals(2, prefixes.length); 79 80 assertEquals("ns2", prefixes[0].getPrefix()); 81 assertEquals("http://ns2", prefixes[0].getUri()); 82 assertEquals("ns1", prefixes[1].getPrefix()); 83 assertEquals("http://ns1", prefixes[1].getUri()); 84 85 } 86 87 public void testOverride() { 88 NamespacesTable ns = new NamespacesTable(); 89 90 ns.addDeclaration("ns1", "http://ns1"); 91 ns.enterScope(); 92 ns.addDeclaration("ns1", "http://otherns1"); 93 ns.enterScope(); 94 ns.addDeclaration("ns1", "http://yetanotherns1"); 95 ns.enterScope(); 96 97 assertEquals("http://yetanotherns1", ns.getUri("ns1")); 98 assertEquals(0, ns.getPrefixes("http://ns1").length); 99 100 ns.leaveScope(); 101 ns.leaveScope(); 102 103 assertEquals("http://ns1", ns.getUri("ns1")); 104 assertEquals(1, ns.getPrefixes("http://ns1").length); 105 106 ns.leaveScope(); 107 assertNull(ns.getUri("ns1")); 108 } 109 110 public void testMultiDeclaration() { 111 NamespacesTable ns = new NamespacesTable(); 112 ns.addDeclaration("ns1", "http://ns1"); 113 ns.enterScope(); 114 ns.addDeclaration("ns2", "http://ns1"); 116 ns.addDeclaration("ns3", "http://ns1"); 117 ns.enterScope(); 118 119 String [] prefixes = ns.getPrefixes("http://ns1"); 120 assertEquals(3, prefixes.length); 121 assertEquals("ns3", prefixes[0]); 122 assertEquals("ns2", prefixes[1]); 123 assertEquals("ns1", prefixes[2]); 124 } 125 126 public void testStreamDeclarations() throws Exception { 127 NamespacesTable ns = new NamespacesTable(); 128 ns.addDeclaration("ns1", "http://ns1"); 129 ns.enterScope(); 130 ns.addDeclaration("ns2", "http://ns2"); 131 ns.enterScope(new DefaultHandler () { 132 public void startPrefixMapping(String prefix, String uri) throws org.xml.sax.SAXException { 133 assertEquals("ns2", prefix); 134 assertEquals("http://ns2", uri); 135 }; 136 }); 137 138 ns.addDeclaration("ns3", "http://ns3"); 140 ns.enterScope(); 141 ns.leaveScope(); 142 143 ns.leaveScope(new DefaultHandler () { 144 public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException { 145 assertEquals("ns2", prefix); 146 }; 147 }); 148 } 149 150 154 public void testJXImport() throws Exception { 155 NamespacesTable ns = new NamespacesTable(); 156 ContentHandler handler = new DefaultHandler (); 157 158 ns.addDeclaration("ft", "http://apache.org/cocoon/forms/1.0#template"); 159 ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance"); 160 ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0"); 161 ns.enterScope(handler); 162 assertEquals("ft", ns.getPrefix("http://apache.org/cocoon/forms/1.0#template")); 163 assertEquals("fi", ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance")); 164 assertEquals("jx", ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0")); 165 166 ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0"); 168 ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance"); 169 ns.addDeclaration("bu", "http://apache.org/cocoon/browser-update/1.0"); 170 171 ns.leaveScope(handler); 172 assertNull(ns.getPrefix("http://apache.org/cocoon/forms/1.0#template")); 173 assertNull(ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance")); 174 assertNull(ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0")); 175 assertEquals(3, ns.getCurrentScopeDeclarations().length); 176 177 } 178 179 public void testDuplicate() throws Exception { 180 NamespacesTable ns = new NamespacesTable(); 181 182 ns.addDeclaration("ns1", "http://ns1"); 183 ns.enterScope(); 184 ns.addDeclaration("ns1", "http://ns1"); 185 ns.enterScope(); 186 ns.leaveScope(); 187 ns.removeDeclaration("ns1"); 188 assertEquals("http://ns1", ns.getUri("ns1")); 189 ns.leaveScope(); 190 ns.removeDeclaration("ns1"); 191 assertNull(ns.getUri("ns1")); 192 } 193 194 } 195 | Popular Tags |