1 /* 2 * Copyright (c) 2003, 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.xpath.ptests; 25 26 import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL; 27 28 import javax.xml.xpath.XPath; 29 import javax.xml.xpath.XPathFactory; 30 import javax.xml.xpath.XPathFactoryConfigurationException; 31 32 import jaxp.library.JAXPDataProvider; 33 import jaxp.library.JAXPBaseTest; 34 import static org.testng.Assert.assertNotNull; 35 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 39 /** 40 * Class containing the test cases for XPathFactory API. 41 */ 42 public class XPathFactoryTest extends JAXPBaseTest { 43 /** 44 * Valid URL for creating a XPath factory. 45 */ 46 private static final String VALID_URL = "http://java.sun.com/jaxp/xpath/dom"; 47 48 /** 49 * Invalid URL not able to create a XPath factory. 50 */ 51 private static final String INVALID_URL = "http://java.sun.com/jaxp/xpath/dom1"; 52 53 /** 54 * XPathFactory implementation class name. 55 */ 56 private static final String XPATH_FACTORY_CLASSNAME = "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl"; 57 58 59 /** 60 * Provide valid XPathFactory instantiation parameters. 61 * 62 * @return a data provider contains XPathFactory instantiation parameters. 63 */ 64 @DataProvider(name = "parameters") 65 public Object[][] getValidateParameters() { 66 return new Object[][] { { VALID_URL, XPATH_FACTORY_CLASSNAME, null }, { VALID_URL, XPATH_FACTORY_CLASSNAME, this.getClass().getClassLoader() } }; 67 } 68 69 /** 70 * Test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 71 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName 72 * points to correct implementation of javax.xml.xpath.XPathFactory , should 73 * return newInstance of XPathFactory 74 * 75 * @param uri 76 * @param factoryClassName 77 * @param classLoader 78 * @throws XPathFactoryConfigurationException 79 */ 80 @Test(dataProvider = "parameters") 81 public void testNewInstance(String uri, String factoryClassName, ClassLoader classLoader) throws XPathFactoryConfigurationException { 82 XPathFactory xpf = XPathFactory.newInstance(uri, factoryClassName, classLoader); 83 XPath xpath = xpf.newXPath(); 84 assertNotNull(xpath); 85 } 86 87 /** 88 * Test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 89 * factoryClassName, java.lang.ClassLoader classLoader) 90 * 91 * @param factoryClassName 92 * @param classLoader 93 * @throws XPathFactoryConfigurationException 94 * is expected when factoryClassName is null 95 */ 96 @Test(expectedExceptions = XPathFactoryConfigurationException.class, dataProvider = "new-instance-neg", dataProviderClass = JAXPDataProvider.class) 97 public void testNewInstanceWithNullFactoryClassName(String factoryClassName, ClassLoader classLoader) throws XPathFactoryConfigurationException { 98 XPathFactory.newInstance(VALID_URL, factoryClassName, classLoader); 99 } 100 101 /** 102 * Test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 103 * factoryClassName, java.lang.ClassLoader classLoader) uri is null , should 104 * throw NPE 105 * 106 * @throws XPathFactoryConfigurationException 107 */ 108 @Test(expectedExceptions = NullPointerException.class) 109 public void testNewInstanceWithNullUri() throws XPathFactoryConfigurationException { 110 XPathFactory.newInstance(null, XPATH_FACTORY_CLASSNAME, this.getClass().getClassLoader()); 111 } 112 113 /** 114 * Test for XPathFactory.newInstance(java.lang.String uri, java.lang.String 115 * factoryClassName, java.lang.ClassLoader classLoader) 116 * 117 * @throws IllegalArgumentException 118 * is expected when uri is empty 119 */ 120 @Test(expectedExceptions = IllegalArgumentException.class) 121 public void testNewInstanceWithEmptyUri() throws XPathFactoryConfigurationException { 122 XPathFactory.newInstance("", XPATH_FACTORY_CLASSNAME, this.getClass().getClassLoader()); 123 } 124 125 /** 126 * Test for constructor - XPathFactory.newInstance(). 127 */ 128 @Test 129 public void testCheckXPathFactory01() { 130 assertNotNull(XPathFactory.newInstance()); 131 } 132 133 /** 134 * XPathFactory.newInstance(String uri) throws NPE if uri is null. 135 * 136 * @throws XPathFactoryConfigurationException If the specified object model 137 * is unavailable, or if there is a configuration error. 138 */ 139 @Test(expectedExceptions = NullPointerException.class) 140 public void testCheckXPathFactory02() throws XPathFactoryConfigurationException { 141 XPathFactory.newInstance(null); 142 } 143 144 /** 145 * XPathFactory.newInstance(String uri) throws XPFCE if uri is just a blank 146 * string. 147 * 148 * @throws XPathFactoryConfigurationException If the specified object model 149 * is unavailable, or if there is a configuration error. 150 */ 151 @Test(expectedExceptions = XPathFactoryConfigurationException.class) 152 public void testCheckXPathFactory03() throws XPathFactoryConfigurationException { 153 XPathFactory.newInstance(" "); 154 } 155 156 /** 157 * Test for constructor - XPathFactory.newInstance(String uri) with valid 158 * url - "http://java.sun.com/jaxp/xpath/dom". 159 * 160 * @throws XPathFactoryConfigurationException If the specified object model 161 * is unavailable, or if there is a configuration error. 162 */ 163 @Test 164 public void testCheckXPathFactory04() throws XPathFactoryConfigurationException { 165 assertNotNull(XPathFactory.newInstance(VALID_URL)); 166 } 167 168 /** 169 * Test for constructor - XPathFactory.newInstance(String uri) with invalid 170 * url - "http://java.sun.com/jaxp/xpath/dom1". 171 * 172 * @throws XPathFactoryConfigurationException If the specified object model 173 * is unavailable, or if there is a configuration error. 174 */ 175 @Test(expectedExceptions = XPathFactoryConfigurationException.class) 176 public void testCheckXPathFactory05() throws XPathFactoryConfigurationException { 177 XPathFactory.newInstance(INVALID_URL); 178 } 179 180 /** 181 * Test for constructor - XPathFactory.newInstance() and creating XPath with 182 * newXPath(). 183 */ 184 @Test 185 public void testCheckXPathFactory06() { 186 assertNotNull(XPathFactory.newInstance().newXPath()); 187 } 188 189 /** 190 * Test for constructor - XPathFactory.newInstance(String uri) with valid 191 * url - "http://java.sun.com/jaxp/xpath/dom" and creating XPath with 192 * newXPath(). 193 * 194 * @throws XPathFactoryConfigurationException If the specified object model 195 * is unavailable, or if there is a configuration error. 196 */ 197 @Test 198 public void testCheckXPathFactory07() throws XPathFactoryConfigurationException { 199 assertNotNull(XPathFactory.newInstance(VALID_URL).newXPath()); 200 } 201 202 /** 203 * Test for constructor - XPathFactory.newInstance(String uri) with valid 204 * uri - DOM_OBJECT_MODEL.toString(). 205 * 206 * @throws XPathFactoryConfigurationException If the specified object model 207 * is unavailable, or if there is a configuration error. 208 */ 209 @Test 210 public void testCheckXPathFactory08() throws XPathFactoryConfigurationException { 211 assertNotNull(XPathFactory.newInstance(DOM_OBJECT_MODEL)); 212 } 213 }