XPathFactoryTest.java revision 654:480aa80c1cb7
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
24package javax.xml.xpath.ptests;
25
26import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL;
27import javax.xml.xpath.XPathFactory;
28import javax.xml.xpath.XPathFactoryConfigurationException;
29import static jaxp.library.JAXPTestUtilities.failUnexpected;
30import static org.testng.AssertJUnit.assertNotNull;
31import org.testng.annotations.Test;
32
33/**
34 * Class containing the test cases for XPathFactory API.
35 */
36public class XPathFactoryTest {
37    /**
38     * Valid URL for creating a XPath factory.
39     */
40    private static final String VALID_URL = "http://java.sun.com/jaxp/xpath/dom";
41
42    /**
43     * Invalid URL not able to create a XPath factory.
44     */
45    private static final String INVALID_URL = "http://java.sun.com/jaxp/xpath/dom1";
46
47    /**
48     * Test for constructor - XPathFactory.newInstance().
49     */
50    @Test
51    public void testCheckXPathFactory01() {
52        assertNotNull(XPathFactory.newInstance());
53    }
54
55    /**
56     * XPathFactory.newInstance(String uri) throws NPE if uri is null.
57     */
58    @Test(expectedExceptions = NullPointerException.class)
59    private void testCheckXPathFactory02() {
60        try {
61            XPathFactory.newInstance(null);
62        } catch (XPathFactoryConfigurationException ex) {
63            failUnexpected(ex);
64        }
65    }
66
67    /**
68     * XPathFactory.newInstance(String uri) throws XPFCE if uri is just a blank
69     * string.
70     *
71     * @throws XPathFactoryConfigurationException
72     */
73    @Test(expectedExceptions = XPathFactoryConfigurationException.class)
74    public void testCheckXPathFactory03() throws XPathFactoryConfigurationException {
75        XPathFactory.newInstance(" ");
76    }
77
78    /**
79     * Test for constructor - XPathFactory.newInstance(String uri) with valid
80     * url - "http://java.sun.com/jaxp/xpath/dom".
81     */
82    @Test
83    public void testCheckXPathFactory04() {
84        try {
85            assertNotNull(XPathFactory.newInstance(VALID_URL));
86        } catch (XPathFactoryConfigurationException ex) {
87            failUnexpected(ex);
88        }
89    }
90
91    /**
92     * Test for constructor - XPathFactory.newInstance(String uri) with invalid
93     * url - "http://java.sun.com/jaxp/xpath/dom1".
94     *
95     * @throws XPathFactoryConfigurationException
96     */
97    @Test(expectedExceptions = XPathFactoryConfigurationException.class)
98    public void testCheckXPathFactory05() throws XPathFactoryConfigurationException {
99        XPathFactory.newInstance(INVALID_URL);
100    }
101
102    /**
103     * Test for constructor - XPathFactory.newInstance() and creating XPath with
104     * newXPath().
105     */
106    @Test
107    public void testCheckXPathFactory06() {
108        assertNotNull(XPathFactory.newInstance().newXPath());
109    }
110
111    /**
112     * Test for constructor - XPathFactory.newInstance(String uri) with valid
113     * url - "http://java.sun.com/jaxp/xpath/dom" and creating XPath with
114     * newXPath().
115     */
116    @Test
117    public void testCheckXPathFactory07() {
118        try {
119            assertNotNull(XPathFactory.newInstance(VALID_URL).newXPath());
120        } catch (XPathFactoryConfigurationException ex) {
121            failUnexpected(ex);
122        }
123    }
124
125    /**
126     * Test for constructor - XPathFactory.newInstance(String uri) with valid
127     * uri - DOM_OBJECT_MODEL.toString().
128     */
129    @Test
130    public void testCheckXPathFactory08() {
131        try {
132            assertNotNull(XPathFactory.newInstance(DOM_OBJECT_MODEL));
133        } catch (XPathFactoryConfigurationException ex) {
134            failUnexpected(ex);
135        }
136    }
137}
138