| 1 38 39 40 package encoding; 41 42 import com.sun.xml.fastinfoset.EncodingConstants; 43 import com.sun.xml.fastinfoset.util.PrefixArray; 44 import java.util.Iterator ; 45 import junit.framework.Test; 46 import junit.framework.TestCase; 47 import junit.framework.TestSuite; 48 49 public class NamespacesScopeTest extends TestCase { 50 51 PrefixArray _prefixArray = new PrefixArray(); 52 53 public NamespacesScopeTest() { 54 } 55 56 public void testDefaultPrefixes() throws Exception { 57 String n = _prefixArray.getNamespaceFromPrefix(""); 58 assertEquals("", n); 59 60 n = _prefixArray.getNamespaceFromPrefix(EncodingConstants.XML_NAMESPACE_PREFIX); 61 assertEquals(EncodingConstants.XML_NAMESPACE_NAME, n); 62 } 63 64 public void testOnePrefix() throws Exception { 65 _prefixArray.add("p"); 66 67 _prefixArray.pushScopeWithPrefixEntry("p", "n", 1, 1); 68 String n = _prefixArray.getNamespaceFromPrefix("p"); 69 assertEquals("n", n); 70 _prefixArray.popScopeWithPrefixEntry(1); 71 72 n = _prefixArray.getNamespaceFromPrefix("p"); 73 assertEquals(null, n); 74 } 75 76 public void testPrefixWithTwoNamespaces() throws Exception { 77 _prefixArray.add("p"); 78 79 _prefixArray.pushScopeWithPrefixEntry("p", "n1", 1, 1); 80 _prefixArray.pushScopeWithPrefixEntry("p", "n2", 2, 2); 81 82 String n = _prefixArray.getNamespaceFromPrefix("p"); 83 assertEquals("n2", n); 84 85 _prefixArray.popScopeWithPrefixEntry(2); 86 87 n = _prefixArray.getNamespaceFromPrefix("p"); 88 assertEquals("n1", n); 89 90 _prefixArray.popScopeWithPrefixEntry(1); 91 92 n = _prefixArray.getNamespaceFromPrefix("p"); 93 assertEquals(null, n); 94 } 95 96 public void testTenDistinctPrefixes() throws Exception { 97 _testNDistinctPrefixes(10); 98 _testNDistinctPrefixes(10); 99 } 100 101 public void testOneThousandDistinctPrefixes() throws Exception { 102 _testNDistinctPrefixes(1000); 103 } 104 105 public void testTenDistinctPrefixesWithClear() throws Exception { 106 _testNDistinctPrefixesWithClear(10); 107 _testNDistinctPrefixes(10); 108 } 109 110 public void _testNDistinctPrefixes(int prefixes) throws Exception { 111 for (int i = 1; i <= prefixes; i++) { 112 _prefixArray.add("p" + i); 113 _prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i); 114 115 String n = _prefixArray.getNamespaceFromPrefix("p" + i); 116 assertEquals("n" + i, n); 117 118 } 119 120 for (int i = 1; i <= prefixes; i++) { 121 String n = _prefixArray.getNamespaceFromPrefix("p" + i); 122 assertEquals("n" + i, n); 123 } 124 125 for (int i = prefixes; i > 0; i--) { 126 _prefixArray.popScopeWithPrefixEntry(i); 127 } 128 129 for (int i = 1; i <= prefixes; i++) { 130 String n = _prefixArray.getNamespaceFromPrefix("p" + i); 131 assertEquals(null, n); 132 } 133 } 134 135 public void _testNDistinctPrefixesWithClear(int prefixes) throws Exception { 136 for (int i = 1; i <= prefixes; i++) { 137 _prefixArray.add("p" + i); 138 _prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i); 139 } 140 141 _prefixArray.clearCompletely(); 142 } 143 144 public void testGetPrefixes() throws Exception { 145 for (int i = 1; i <= 10; i++) { 146 _prefixArray.add("p" + i); 147 _prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i); 148 } 149 150 int i = 0; 151 Iterator p = _prefixArray.getPrefixes(); 152 while (p.hasNext()) { 153 String prefix = (String )p.next(); 154 if (i == 0) { 155 assertEquals("xml", prefix); 156 } else { 157 assertEquals("p" + i, prefix); 158 } 159 i++; 160 } 161 } 162 163 public void testGetPrefixesFromNamespace() throws Exception { 164 for (int i = 1; i <= 10; i++) { 165 _prefixArray.add("p" + i); 166 _prefixArray.pushScopeWithPrefixEntry("p" + i, "n", i, 1); 167 } 168 169 int i = 1; 170 Iterator p = _prefixArray.getPrefixesFromNamespace("n"); 171 while (p.hasNext()) { 172 String prefix = (String )p.next(); 173 assertEquals("p" + i, prefix); 174 i++; 175 } 176 } 177 178 private void add(String prefix, String namespaceName, int prefixIndex, int namespaceNameIndex) throws Exception { 179 _prefixArray.add(prefix); 180 _prefixArray.pushScopeWithPrefixEntry(prefix, namespaceName, prefixIndex, namespaceNameIndex); 181 } 182 183 public static Test suite() { 184 TestSuite suite = new TestSuite(NamespacesScopeTest.class); 185 return suite; 186 } 187 188 } | Popular Tags |