KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > xml > XMLUtil68942Test


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.xml;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.CharConversionException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33 import junit.framework.Test;
34 import org.netbeans.junit.NbTestCase;
35 import org.netbeans.junit.NbTestSuite;
36 import org.openide.util.Lookup;
37 import org.openide.util.lookup.Lookups;
38 import org.openide.util.lookup.ProxyLookup;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.DocumentType JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.xml.sax.EntityResolver JavaDoc;
44 import org.xml.sax.ErrorHandler JavaDoc;
45 import org.xml.sax.InputSource JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47 import org.xml.sax.SAXParseException JavaDoc;
48 import org.xml.sax.XMLReader JavaDoc;
49
50 public class XMLUtil68942Test extends NbTestCase {
51     static {
52         System.setProperty("org.openide.util.Lookup", "org.openide.xml.XMLUtil68942Test$Lkp"); // NOI18N
53
System.setProperty("jaxp.debug", "true");
54     }
55     
56     public XMLUtil68942Test(String JavaDoc testName) {
57         super(testName);
58     }
59     
60     protected void setUp() throws Exception JavaDoc {
61         Object JavaDoc lookup = Lkp.getDefault();
62         assertNotNull(lookup);
63         assertEquals("right class", Lkp.class, lookup.getClass());
64         
65 // System.setProperty("javax.xml.parsers.SAXParserFactory",
66
// "org.netbeans.core.startup.SAXFactoryImpl");
67
//
68
// System.setProperty(
69
// "javax.xml.parsers.DocumentBuilderFactory",
70
// "org.netbeans.core.startup.DOMFactoryImpl");
71

72         URL JavaDoc u = XMLUtil.class.getProtectionDomain().getCodeSource().getLocation();
73         URL JavaDoc core = new URL JavaDoc(u, "../core/core.jar");
74         URLClassLoader JavaDoc loader = new MyLoader(core, XMLUtil.class.getClassLoader());
75
76         Lkp l = (Lkp)Lkp.getDefault();
77         l.set(Lookups.singleton(loader));
78
79         // verify the class can be loaded
80
Object JavaDoc c = loader.getResource("org/netbeans/core/startup/SAXFactoryImpl.class");
81         assertNotNull("Class can be found", c);
82     }
83
84     public void testClassLoaderFoundFor68942() throws Exception JavaDoc {
85         Object JavaDoc orig = Thread.currentThread().getContextClassLoader();
86         String JavaDoc data =
87                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
88                 "<p>\n" +
89                 "</p>\n";
90         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
91         Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(new StringReader JavaDoc(data)), false, false, null, null);
92
93         XMLUtil.write(doc, baos, "utf-8");
94         
95         assertEquals("Orig context classloader restored", orig, Thread.currentThread().getContextClassLoader());
96         
97         Object JavaDoc inst = javax.xml.parsers.DocumentBuilderFactory.newInstance();
98         assertNotNull("Instance is there", inst);
99         
100         MyLoader l = (MyLoader)Lookup.getDefault().lookup(MyLoader.class);
101         assertNotNull(l);
102         
103         Iterator JavaDoc it = l.loadedClasses.iterator();
104         while (it.hasNext()) {
105             Class JavaDoc c = (Class JavaDoc)it.next();
106             if (c.getName().startsWith("org.netbeans.core.startup.DOMFactoryImpl")) {
107                 // ok
108
return;
109             }
110             if (c.getName().startsWith("org.netbeans.core.startup.SAXFactoryImpl")) {
111                 // ok
112
return;
113             }
114         }
115
116         fail("Expecting DOMFactoryImpl: " + l.loadedClasses);
117     }
118     
119     public static final class Lkp extends ProxyLookup {
120         public Lkp() {
121             super(new Lookup[0]);
122         }
123         
124         public void set(Lookup l) {
125             setLookups(new Lookup[] { l });
126         }
127     }
128     
129     class MyLoader extends URLClassLoader JavaDoc {
130         public Set JavaDoc loadedClasses = new HashSet JavaDoc();
131         
132         public MyLoader(URL JavaDoc u, ClassLoader JavaDoc parent) {
133             super(new URL JavaDoc[] { u }, parent);
134         }
135
136         protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
137
138             Class JavaDoc retValue;
139             
140             retValue = super.loadClass(name, resolve);
141             
142             loadedClasses.add(retValue);
143             return retValue;
144         }
145         
146         
147     }
148 }
149
Popular Tags