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 javax.xml.parsers.ptests;
  25 
  26 import javax.xml.parsers.DocumentBuilderFactory;
  27 import javax.xml.parsers.FactoryConfigurationError;
  28 import javax.xml.parsers.SAXParserFactory;
  29 
  30 import org.testng.annotations.AfterTest;
  31 import org.testng.annotations.BeforeTest;
  32 import org.testng.annotations.Test;
  33 
  34 /**
  35  * Class containing the test cases for SAXParserFactory/DocumentBuilderFactory
  36  * newInstance methods.
  37  */
  38 public class FactoryConfErrorTest {
  39     private String DBFproperty = null;
  40     private String SPFproperty = null;
  41 
  42     /**
  43      * Set properties DocumentBuilderFactory and SAXParserFactory to invalid
  44      * value before any test run.
  45      */
  46     @BeforeTest
  47     public void setup() {
  48         DBFproperty = System.getProperty("javax.xml.parsers.DocumentBuilderFactory");
  49         SPFproperty = System.getProperty("javax.xml.parsers.SAXParserFactory");
  50         System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "xx");
  51         System.setProperty("javax.xml.parsers.SAXParserFactory", "xx");
  52     }
  53 
  54     /**
  55      * Restore properties DocumentBuilderFactory and SAXParserFactory to default
  56      * value after all tests run.
  57      */
  58     @AfterTest
  59     public void cleanup() {
  60         System.setProperty("javax.xml.parsers.DocumentBuilderFactory", DBFproperty != null ? DBFproperty : "");
  61         System.setProperty("javax.xml.parsers.SAXParserFactory", SPFproperty != null ? SPFproperty : "");
  62     }
  63 
  64     /**
  65      * To test exception thrown if javax.xml.parsers.SAXParserFactory property
  66      * is invalid.
  67      */
  68     @Test(expectedExceptions = FactoryConfigurationError.class)
  69     public void testNewInstance01() {
  70         SAXParserFactory.newInstance();
  71     }
  72 
  73     /**
  74      * To test exeception thrown if javax.xml.parsers.DocumentBuilderFactory is
  75      * invalid.
  76      */
  77     @Test(expectedExceptions = FactoryConfigurationError.class)
  78     public void testNewInstance02() {
  79         DocumentBuilderFactory.newInstance();
  80     }
  81 }