1 /* 2 * Copyright (c) 1999, 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 javax.xml.jaxp14.ptests; 25 26 import static org.testng.Assert.assertNotNull; 27 28 import javax.xml.xpath.XPath; 29 import javax.xml.xpath.XPathFactory; 30 import javax.xml.xpath.XPathFactoryConfigurationException; 31 32 import jaxp.library.JAXPBaseTest; 33 34 import org.testng.annotations.DataProvider; 35 import org.testng.annotations.Test; 36 37 /* 38 * @summary Tests for XPathFactory.newInstance(uri, factoryClassName , classLoader) 39 */ 40 public class XPathFactoryTest extends JAXPBaseTest { 41 42 private static final String URI = "http://java.sun.com/jaxp/xpath/dom"; 43 private static final String XPATH_FACTORY_CLASSNAME = "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl"; 44 45 @DataProvider(name = "parameters") 46 public Object[][] getValidateParameters() { 47 return new Object[][] { { URI, XPATH_FACTORY_CLASSNAME, null }, { URI, XPATH_FACTORY_CLASSNAME, this.getClass().getClassLoader() } }; 48 } 49 50 /* 51 * test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 52 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName 53 * points to correct implementation of javax.xml.xpath.XPathFactory , should 54 * return newInstance of XPathFactory 55 */ 56 @Test(dataProvider = "parameters") 57 public void testNewInstance(String uri, String factoryClassName, ClassLoader classLoader) throws XPathFactoryConfigurationException { 58 XPathFactory xpf = XPathFactory.newInstance(uri, factoryClassName, classLoader); 59 XPath xpath = xpf.newXPath(); 60 assertNotNull(xpath); 61 } 62 63 @DataProvider(name = "invalid-parameters") 64 public Object[][] getInvalidateParameters() { 65 return new Object[][] { { URI, null, null }, { URI, null, this.getClass().getClassLoader() } }; 66 } 67 68 /* 69 * test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 70 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName is 71 * null , should throw XPathFactoryConfigurationException 72 */ 73 @Test(expectedExceptions = XPathFactoryConfigurationException.class, dataProvider = "invalid-parameters") 74 public void testNewInstanceWithNullFactoryClassName(String uri, String factoryClassName, ClassLoader classLoader) throws XPathFactoryConfigurationException { 75 XPathFactory.newInstance(uri, factoryClassName, classLoader); 76 } 77 78 /* 79 * test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 80 * factoryClassName, java.lang.ClassLoader classLoader) uri is null , should 81 * throw NPE 82 */ 83 @Test(expectedExceptions = NullPointerException.class) 84 public void testNewInstanceWithNullUri() throws XPathFactoryConfigurationException { 85 XPathFactory.newInstance(null, XPATH_FACTORY_CLASSNAME, this.getClass().getClassLoader()); 86 } 87 88 /* 89 * test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 90 * factoryClassName, java.lang.ClassLoader classLoader) uri is empty, should 91 * throw IllegalArgumentException 92 */ 93 @Test(expectedExceptions = IllegalArgumentException.class) 94 public void testNewInstanceWithEmptyUri() throws XPathFactoryConfigurationException { 95 XPathFactory.newInstance("", XPATH_FACTORY_CLASSNAME, this.getClass().getClassLoader()); 96 } 97 98 }