1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2005, 2006 Elliotte Rusty Harold
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id$
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         // construct test object
137         SimpleNamespaceContext original = new SimpleNamespaceContext();
138         original.addNamespace("a", "http://www.a.com");
139         original.addNamespace("b", "http://www.b.com");
140         
141         // serialize
142         ByteArrayOutputStream out = new ByteArrayOutputStream();
143         ObjectOutputStream oos = new ObjectOutputStream(out);
144         oos.writeObject(original);
145         oos.close();
146         
147         //deserialize
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         // test the result
155         assertEquals("http://www.a.com", copy.translateNamespacePrefixToUri("a"));
156         assertEquals("http://www.b.com", copy.translateNamespacePrefixToUri("b"));
157         assertEquals("", "");
158         
159     }
160     
161 }