1 19 20 package org.openide.xml; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.CharConversionException ; 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 40 import org.w3c.dom.DocumentType ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.NodeList ; 43 import org.xml.sax.EntityResolver ; 44 import org.xml.sax.ErrorHandler ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 import org.xml.sax.SAXParseException ; 48 import org.xml.sax.XMLReader ; 49 50 public class XMLUtil68942Test extends NbTestCase { 51 static { 52 System.setProperty("org.openide.util.Lookup", "org.openide.xml.XMLUtil68942Test$Lkp"); System.setProperty("jaxp.debug", "true"); 54 } 55 56 public XMLUtil68942Test(String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws Exception { 61 Object lookup = Lkp.getDefault(); 62 assertNotNull(lookup); 63 assertEquals("right class", Lkp.class, lookup.getClass()); 64 65 72 URL u = XMLUtil.class.getProtectionDomain().getCodeSource().getLocation(); 73 URL core = new URL (u, "../core/core.jar"); 74 URLClassLoader loader = new MyLoader(core, XMLUtil.class.getClassLoader()); 75 76 Lkp l = (Lkp)Lkp.getDefault(); 77 l.set(Lookups.singleton(loader)); 78 79 Object c = loader.getResource("org/netbeans/core/startup/SAXFactoryImpl.class"); 81 assertNotNull("Class can be found", c); 82 } 83 84 public void testClassLoaderFoundFor68942() throws Exception { 85 Object orig = Thread.currentThread().getContextClassLoader(); 86 String data = 87 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 88 "<p>\n" + 89 "</p>\n"; 90 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 91 Document doc = XMLUtil.parse(new InputSource (new StringReader (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 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 it = l.loadedClasses.iterator(); 104 while (it.hasNext()) { 105 Class c = (Class )it.next(); 106 if (c.getName().startsWith("org.netbeans.core.startup.DOMFactoryImpl")) { 107 return; 109 } 110 if (c.getName().startsWith("org.netbeans.core.startup.SAXFactoryImpl")) { 111 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 { 130 public Set loadedClasses = new HashSet (); 131 132 public MyLoader(URL u, ClassLoader parent) { 133 super(new URL [] { u }, parent); 134 } 135 136 protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 137 138 Class retValue; 139 140 retValue = super.loadClass(name, resolve); 141 142 loadedClasses.add(retValue); 143 return retValue; 144 } 145 146 147 } 148 } 149 | Popular Tags |