/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.xml.xpath; import java.io.ByteArrayInputStream; import java.math.BigDecimal; import java.math.BigInteger; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import javax.xml.parsers.DocumentBuilderFactory; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import org.testng.annotations.DataProvider; import org.w3c.dom.Document; import org.w3c.dom.Node; /* * Base class for XPath test */ class XPathTestBase { static final String rawXML = "" + "" + " " + " name1" + " 1111111111" + " 123@xyz.com" + "
" + " 1111 111st ave" + " The City" + " The State" + "
" + "
" + " " + " name1" + " 2222222222" + " 123@xyz.com" + "
" + " 2222 222nd ave" + " The City" + " The State" + "
" + "
" + " " + " name1" + " 3333333333" + " 123@xyz.com" + "
" + " 3333 333rd ave" + " The City" + " The State" + "
" + "
" + "
"; void verifyResult(XPathEvaluationResult result, Object expected) { switch (result.type()) { case BOOLEAN: assertTrue(((Boolean) result.value()).equals(expected)); return; case NUMBER: assertTrue(((Double) result.value()).equals(expected)); return; case STRING: assertTrue(((String) result.value()).equals(expected)); return; case NODESET: XPathNodes nodes = (XPathNodes) result.value(); for (Node n : nodes) { assertEquals(n.getLocalName(), expected); } return; case NODE: assertTrue(((Node) result.value()).getLocalName().equals(expected)); return; } assertFalse(true, "Unsupported type"); } /* * DataProvider: XPath object */ @DataProvider(name = "xpath") Object[][] getXPath() { return new Object[][]{{XPathFactory.newInstance().newXPath()}}; } /* * DataProvider: Numeric types not supported */ @DataProvider(name = "invalidNumericTypes") Object[][] getInvalidNumericTypes() { XPath xpath = XPathFactory.newInstance().newXPath(); return new Object[][]{{xpath, AtomicInteger.class}, {xpath, AtomicInteger.class}, {xpath, AtomicLong.class}, {xpath, BigDecimal.class}, {xpath, BigInteger.class}, {xpath, Byte.class}, {xpath, Float.class}, {xpath, Short.class} }; } /* * DataProvider: XPath and Document objects */ @DataProvider(name = "document") Object[][] getDocument() throws Exception { DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance(); dBF.setValidating(false); dBF.setNamespaceAware(true); Document doc = dBF.newDocumentBuilder().parse( new ByteArrayInputStream(rawXML.getBytes("UTF-8"))); return new Object[][]{{XPathFactory.newInstance().newXPath(), doc}}; } }