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