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 
  24 package common;
  25 
  26 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  27 
  28 import javax.xml.parsers.SAXParserFactory;
  29 import javax.xml.transform.TransformerFactory;
  30 import javax.xml.transform.TransformerFactoryConfigurationError;
  31 
  32 import org.testng.Assert;
  33 import org.testng.annotations.Listeners;
  34 import org.testng.annotations.Test;
  35 
  36 /*
  37  * @test
  38  * @bug 6350682
  39  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  40  * @run testng/othervm -DrunSecMngr=true common.Bug6350682
  41  * @run testng/othervm common.Bug6350682
  42  * @summary Test SAXParserFactory and TransformerFactory can newInstance when setContextClassLoader(null).
  43  */
  44 @Listeners({jaxp.library.BasePolicy.class})
  45 public class Bug6350682 {
  46 
  47     @Test
  48     public void testSAXParserFactory() {
  49         try {
  50             runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null));
  51             if (Bug6350682.class.getClassLoader() == null)
  52                 System.out.println("this class loader is NULL");
  53             else
  54                 System.out.println("this class loader is NOT NULL");
  55             SAXParserFactory factory = SAXParserFactory.newInstance();
  56             Assert.assertTrue(factory != null, "Failed to get an instance of a SAXParserFactory");
  57         } catch (Exception e) {
  58             e.printStackTrace();
  59             Assert.fail("Exception occured: " + e.getMessage());
  60         }
  61     }
  62 
  63     @Test
  64     public void testTransformerFactory() {
  65         try {
  66             runWithAllPerm(() -> Thread.currentThread().setContextClassLoader(null));
  67             TransformerFactory factory = TransformerFactory.newInstance();
  68             Assert.assertTrue(factory != null, "Failed to get an instance of a TransformerFactory");
  69         } catch (Exception e) {
  70             e.printStackTrace();
  71             Assert.fail("Exception occured: " + e.getMessage());
  72         } catch (TransformerFactoryConfigurationError error) {
  73             error.printStackTrace();
  74             Assert.fail(error.toString());
  75         }
  76     }
  77 }
  78