FactoryFindTest.java revision 968:874082a9b565
1/* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24package stream; 25 26import static jaxp.library.JAXPTestUtilities.getSystemProperty; 27import static jaxp.library.JAXPTestUtilities.runWithAllPerm; 28 29import java.io.File; 30import java.io.FileInputStream; 31import java.io.FileNotFoundException; 32import java.io.FileOutputStream; 33import java.io.IOException; 34import java.net.URL; 35import java.net.URLClassLoader; 36import java.util.Properties; 37 38import javax.xml.stream.XMLInputFactory; 39import javax.xml.stream.XMLOutputFactory; 40 41import org.testng.Assert; 42import org.testng.annotations.Listeners; 43import org.testng.annotations.Test; 44 45/* 46 * @test 47 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 48 * @run testng/othervm -DrunSecMngr=true stream.FactoryFindTest 49 * @run testng/othervm stream.FactoryFindTest 50 * @summary Test SaTX factory using factory property and using ContextClassLoader. 51 */ 52@Listeners({jaxp.library.FilePolicy.class}) 53public class FactoryFindTest { 54 55 boolean myClassLoaderUsed = false; 56 57 final static String FACTORY_KEY = "javax.xml.stream.XMLInputFactory"; 58 59// @BeforeClass 60// public void setup(){ 61// policy.PolicyUtil.changePolicy(getClass().getResource("FactoryFindTest.policy").getFile()); 62// } 63 64 @Test(enabled=false) // due to 8156508 65 public void testFactoryFindUsingStaxProperties() { 66 // If property is defined, will take precendence so this test 67 // is ignored :( 68 if (getSystemProperty(FACTORY_KEY) != null) { 69 return; 70 } 71 72 Properties props = new Properties(); 73 String configFile = getSystemProperty("java.home") + File.separator + "lib" + File.separator + "stax.properties"; 74 75 File f = new File(configFile); 76 if (f.exists()) { 77 try { 78 FileInputStream fis = new FileInputStream(f); 79 props.load(fis); 80 fis.close(); 81 } catch (FileNotFoundException e) { 82 return; 83 } catch (IOException e) { 84 return; 85 } 86 } else { 87 props.setProperty(FACTORY_KEY, "com.sun.xml.internal.stream.XMLInputFactoryImpl"); 88 try { 89 FileOutputStream fos = new FileOutputStream(f); 90 props.store(fos, null); 91 fos.close(); 92 f.deleteOnExit(); 93 } catch (FileNotFoundException e) { 94 return; 95 } catch (IOException e) { 96 return; 97 } 98 } 99 100 XMLInputFactory factory = XMLInputFactory.newInstance(); 101 Assert.assertTrue(factory.getClass().getName().equals(props.getProperty(FACTORY_KEY))); 102 } 103 104 @Test 105 public void testFactoryFind() { 106 try { 107 // setSystemProperty("jaxp.debug", "true"); 108 109 XMLInputFactory factory = XMLInputFactory.newInstance(); 110 Assert.assertTrue(factory.getClass().getClassLoader() == null); 111 112 runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null)); 113 factory = XMLInputFactory.newInstance(); 114 Assert.assertTrue(factory.getClass().getClassLoader() == null); 115 116 runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(new MyClassLoader())); 117 factory = XMLInputFactory.newInstance(); 118 // because it's decided by having sm or not in FactoryFind code 119 if (System.getSecurityManager() == null) 120 Assert.assertTrue(myClassLoaderUsed); 121 else 122 Assert.assertFalse(myClassLoaderUsed); 123 124 XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); 125 Assert.assertTrue(ofactory.getClass().getClassLoader() == null); 126 127 runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null)); 128 ofactory = XMLOutputFactory.newInstance(); 129 Assert.assertTrue(ofactory.getClass().getClassLoader() == null); 130 131 runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(new MyClassLoader())); 132 ofactory = XMLOutputFactory.newInstance(); 133 if (System.getSecurityManager() == null) 134 Assert.assertTrue(myClassLoaderUsed); 135 else 136 Assert.assertFalse(myClassLoaderUsed); 137 } catch (Exception ex) { 138 throw new RuntimeException(ex); 139 } 140 } 141 142 class MyClassLoader extends URLClassLoader { 143 144 public MyClassLoader() { 145 super(new URL[0]); 146 } 147 148 public Class loadClass(String name) throws ClassNotFoundException { 149 myClassLoaderUsed = true; 150 return super.loadClass(name); 151 } 152 } 153} 154 155