1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package org.jaxen.test;
51
52
53 import java.io.ByteArrayInputStream;
54 import java.io.ByteArrayOutputStream;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.io.ObjectInputStream;
58 import java.io.ObjectOutputStream;
59 import java.util.HashMap;
60 import java.util.Map;
61
62 import javax.xml.parsers.DocumentBuilder;
63 import javax.xml.parsers.DocumentBuilderFactory;
64 import javax.xml.parsers.ParserConfigurationException;
65
66 import org.jaxen.SimpleNamespaceContext;
67 import org.jaxen.UnsupportedAxisException;
68 import org.w3c.dom.Document;
69 import org.w3c.dom.Element;
70
71 import junit.framework.TestCase;
72
73 /***
74 * <p>
75 * Test for namespace context.
76 * </p>
77 *
78 * @author Elliotte Rusty Harold
79 * @version 1.1b10
80 *
81 */
82 public class SimpleNamespaceContextTest extends TestCase
83 {
84
85 /***
86 * Need to make sure that changing the map after it's used to create the
87 * namespace context does not affect the context. i.e.
88 * data encapsulation is not violated.
89 */
90 public void testMapCopy() {
91 Map map = new HashMap();
92 SimpleNamespaceContext context = new SimpleNamespaceContext(map);
93 map.put("pre", "http://www.example.org/");
94 assertNull(context.translateNamespacePrefixToUri("pre"));
95 }
96
97 public void testCantUseNonStringsAsValues() {
98 Map map = new HashMap();
99 map.put("key", new Object());
100 try {
101 new SimpleNamespaceContext(map);
102 fail("added non String value to namespace context");
103 }
104 catch (Exception ex) {
105 assertNotNull(ex.getMessage());
106 }
107 }
108
109 public void testCantUseNonStringsAsKeys() {
110 Map map = new HashMap();
111 map.put(new Object(), "value");
112 try {
113 new SimpleNamespaceContext(map);
114 fail("added non String key to namespace context");
115 }
116 catch (Exception ex) {
117 assertNotNull(ex.getMessage());
118 }
119 }
120
121 public void testContextFromElement() throws ParserConfigurationException, UnsupportedAxisException {
122 SimpleNamespaceContext context = new SimpleNamespaceContext();
123 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
124 factory.setNamespaceAware(true);
125 DocumentBuilder builder = factory.newDocumentBuilder();
126 Document doc = builder.newDocument();
127 Element root = doc.createElementNS("http://www.example.org/", "pre:root");
128 doc.appendChild(root);
129 context.addElementNamespaces(new org.jaxen.dom.DocumentNavigator(), root);
130
131 assertEquals("http://www.example.org/", context.translateNamespacePrefixToUri("pre"));
132 }
133
134 public void testSerialization() throws IOException, ClassNotFoundException {
135
136
137 SimpleNamespaceContext original = new SimpleNamespaceContext();
138 original.addNamespace("a", "http://www.a.com");
139 original.addNamespace("b", "http://www.b.com");
140
141
142 ByteArrayOutputStream out = new ByteArrayOutputStream();
143 ObjectOutputStream oos = new ObjectOutputStream(out);
144 oos.writeObject(original);
145 oos.close();
146
147
148 byte[] pickled = out.toByteArray();
149 InputStream in = new ByteArrayInputStream(pickled);
150 ObjectInputStream ois = new ObjectInputStream(in);
151 Object o = ois.readObject();
152 SimpleNamespaceContext copy = (SimpleNamespaceContext) o;
153
154
155 assertEquals("http://www.a.com", copy.translateNamespacePrefixToUri("a"));
156 assertEquals("http://www.b.com", copy.translateNamespacePrefixToUri("b"));
157 assertEquals("", "");
158
159 }
160
161 }