1 /*
   2  * Copyright (c) 2014, 2015, 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 
  24 package stream;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.net.URL;
  32 import java.net.URLClassLoader;
  33 import java.util.Properties;
  34 
  35 import javax.xml.stream.XMLInputFactory;
  36 import javax.xml.stream.XMLOutputFactory;
  37 
  38 import org.testng.Assert;
  39 import org.testng.annotations.BeforeClass;
  40 import org.testng.annotations.Test;
  41 
  42 /*
  43  * @summary Test SaTX factory using factory property and using ContextClassLoader.
  44  */
  45 public class FactoryFindTest {
  46 
  47     boolean myClassLoaderUsed = false;
  48 
  49     final static String FACTORY_KEY = "javax.xml.stream.XMLInputFactory";
  50 
  51     @BeforeClass
  52     public void setup(){
  53         policy.PolicyUtil.changePolicy(getClass().getResource("FactoryFindTest.policy").getFile());
  54     }
  55 
  56     @Test
  57     public void testFactoryFindUsingStaxProperties() {
  58         // If property is defined, will take precendence so this test
  59         // is ignored :(
  60         if (System.getProperty(FACTORY_KEY) != null) {
  61             return;
  62         }
  63 
  64         Properties props = new Properties();
  65         String configFile = System.getProperty("java.home") + File.separator + "lib" + File.separator + "stax.properties";
  66 
  67         File f = new File(configFile);
  68         if (f.exists()) {
  69             try {
  70                 FileInputStream fis = new FileInputStream(f);
  71                 props.load(fis);
  72                 fis.close();
  73             } catch (FileNotFoundException e) {
  74                 return;
  75             } catch (IOException e) {
  76                 return;
  77             }
  78         } else {
  79             props.setProperty(FACTORY_KEY, "com.sun.xml.internal.stream.XMLInputFactoryImpl");
  80             try {
  81                 FileOutputStream fos = new FileOutputStream(f);
  82                 props.store(fos, null);
  83                 fos.close();
  84                 f.deleteOnExit();
  85             } catch (FileNotFoundException e) {
  86                 return;
  87             } catch (IOException e) {
  88                 return;
  89             }
  90         }
  91 
  92         XMLInputFactory factory = XMLInputFactory.newInstance();
  93         Assert.assertTrue(factory.getClass().getName().equals(props.getProperty(FACTORY_KEY)));
  94     }
  95 
  96     @Test
  97     public void testFactoryFind() {
  98         try {
  99             // System.setProperty("jaxp.debug", "true");
 100 
 101             XMLInputFactory factory = XMLInputFactory.newInstance();
 102             Assert.assertTrue(factory.getClass().getClassLoader() == null);
 103 
 104             Thread.currentThread().setContextClassLoader(null);
 105             factory = XMLInputFactory.newInstance();
 106             Assert.assertTrue(factory.getClass().getClassLoader() == null);
 107 
 108             Thread.currentThread().setContextClassLoader(new MyClassLoader());
 109             factory = XMLInputFactory.newInstance();
 110             if (System.getSecurityManager() == null)
 111                 Assert.assertTrue(myClassLoaderUsed);
 112             else
 113                 Assert.assertFalse(myClassLoaderUsed);
 114 
 115             XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
 116             Assert.assertTrue(ofactory.getClass().getClassLoader() == null);
 117 
 118             Thread.currentThread().setContextClassLoader(null);
 119             ofactory = XMLOutputFactory.newInstance();
 120             Assert.assertTrue(ofactory.getClass().getClassLoader() == null);
 121 
 122             Thread.currentThread().setContextClassLoader(new MyClassLoader());
 123             ofactory = XMLOutputFactory.newInstance();
 124             if (System.getSecurityManager() == null)
 125                 Assert.assertTrue(myClassLoaderUsed);
 126             else
 127                 Assert.assertFalse(myClassLoaderUsed);
 128         } catch (Exception ex) {
 129             throw new RuntimeException(ex);
 130         }
 131     }
 132 
 133     class MyClassLoader extends URLClassLoader {
 134 
 135         public MyClassLoader() {
 136             super(new URL[0]);
 137         }
 138 
 139         public Class loadClass(String name) throws ClassNotFoundException {
 140             myClassLoaderUsed = true;
 141             return super.loadClass(name);
 142         }
 143     }
 144 }