1 /*
   2  * Copyright (c) 2014, 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.XMLInputFactoryTest;
  25 
  26 import javax.xml.stream.XMLInputFactory;
  27 
  28 import jaxp.library.JAXPTestUtilities;
  29 
  30 import org.testng.Assert;
  31 import org.testng.annotations.Listeners;
  32 import org.testng.annotations.Test;
  33 
  34 /*
  35  * @bug 6756677
  36  * @summary Test XMLInputFactory.newFactory(String factoryId, ClassLoader classLoader).
  37  */
  38 @Listeners({jaxp.library.BasePolicy.class})
  39 public class Bug6756677Test {
  40 
  41     @Test
  42     public void testNewInstance() {
  43         String myFactory = "stream.XMLInputFactoryTest.MyInputFactory";
  44         try {
  45             System.setProperty("MyInputFactory", myFactory);
  46             XMLInputFactory xif = XMLInputFactory.newInstance("MyInputFactory", null);
  47             System.out.println(xif.getClass().getName());
  48             Assert.assertTrue(xif.getClass().getName().equals(myFactory));
  49 
  50         } catch (UnsupportedOperationException oe) {
  51             Assert.fail(oe.getMessage());
  52         }
  53     }
  54 
  55     // newFactory was added in StAX 1.2
  56     @Test
  57     public void testNewFactory() {
  58         String myFactory = "stream.XMLInputFactoryTest.MyInputFactory";
  59         ClassLoader cl = null;
  60         try {
  61             System.setProperty("MyInputFactory", myFactory);
  62             XMLInputFactory xif = XMLInputFactory.newFactory("MyInputFactory", cl);
  63             System.out.println(xif.getClass().getName());
  64             Assert.assertTrue(xif.getClass().getName().equals(myFactory));
  65 
  66         } catch (UnsupportedOperationException oe) {
  67             Assert.fail(oe.getMessage());
  68         }
  69     }
  70 
  71     String Temp_Result = "";
  72     boolean PASSED = true;
  73     boolean FAILED = false;
  74 
  75     String XMLInputFactoryClassName = "com.sun.xml.internal.stream.XMLInputFactoryImpl";
  76     String XMLInputFactoryID = "javax.xml.stream.XMLInputFactory";
  77     ClassLoader CL = null;
  78 
  79     // jaxp-test jaxp-product-tests javax.xml.jaxp14.ptests.FactoryTest
  80     @Test
  81     public void test() {
  82         if (!test29()) {
  83             Assert.fail(Temp_Result);
  84         }
  85         if (!test31()) {
  86             Assert.fail(Temp_Result);
  87         }
  88     }
  89 
  90     /*
  91      * test for XMLInputFactory.newInstance(java.lang.String factoryClassName,
  92      * java.lang.ClassLoader classLoader) classloader is null and
  93      * factoryClassName points to correct implementation of
  94      * javax.xml.stream.XMLInputFactory , should return newInstance of
  95      * XMLInputFactory
  96      */
  97     @Test
  98     public boolean test29() {
  99         try {
 100             System.setProperty(XMLInputFactoryID, XMLInputFactoryClassName);
 101             XMLInputFactory xif = XMLInputFactory.newInstance(XMLInputFactoryID, CL);
 102             if (xif instanceof XMLInputFactory) {
 103                 System.out.println(" test29() passed");
 104                 return PASSED;
 105             } else {
 106                 System.out.println(" test29() failed");
 107                 Temp_Result = "test29() failed: xif not an instance of XMLInputFactory ";
 108                 return FAILED;
 109             }
 110         } catch (javax.xml.stream.FactoryConfigurationError fce) {
 111             System.out.println("Failed : FactoryConfigurationError in test29 " + fce);
 112             Temp_Result = "test29() failed ";
 113             return FAILED;
 114         } catch (Exception e) {
 115             System.out.println("Failed : Exception in test29 " + e);
 116             Temp_Result = "test29() failed ";
 117             return FAILED;
 118         }
 119     }
 120 
 121     /*
 122      * test for XMLInputFactory.newInstance(java.lang.String factoryClassName,
 123      * java.lang.ClassLoader classLoader) classloader is
 124      * default(Class.getClassLoader()) and factoryClassName points to correct
 125      * implementation of javax.xml.stream.XMLInputFactory , should return
 126      * newInstance of XMLInputFactory
 127      */
 128     @Test
 129     public boolean test31() {
 130         try {
 131             Bug6756677Test test3 = new Bug6756677Test();
 132             ClassLoader cl = (test3.getClass()).getClassLoader();
 133             System.setProperty(XMLInputFactoryID, XMLInputFactoryClassName);
 134             XMLInputFactory xif = XMLInputFactory.newInstance(XMLInputFactoryID, cl);
 135             if (xif instanceof XMLInputFactory) {
 136                 System.out.println(" test31() passed");
 137                 return PASSED;
 138             } else {
 139                 System.out.println(" test31() failed");
 140                 Temp_Result = "test31() failed: xif not an instance of XMLInputFactory ";
 141                 return FAILED;
 142             }
 143         } catch (javax.xml.stream.FactoryConfigurationError fce) {
 144             System.out.println("Failed : FactoryConfigurationError in test31 " + fce);
 145             Temp_Result = "test31() failed ";
 146             return FAILED;
 147         } catch (Exception e) {
 148             System.out.println("Failed : Exception in test31 " + e);
 149             Temp_Result = "test31() failed ";
 150             return FAILED;
 151         }
 152     }
 153 }