Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathExpressionTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathExpressionTest.java?rev=1897795&r1=1897794&r2=1897795&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathExpressionTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathExpressionTest.java Sun Feb 6 01:51:55 2022 @@ -14,60 +14,73 @@ */ package xmlcursor.xpath.common; +import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlException; -import org.apache.xmlbeans.XmlObject; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import xmlcursor.common.Common; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static xmlcursor.common.BasicCursorTestCase.cur; +import static xmlcursor.xpath.common.XPathTestBase.getQuery; /** * Verifies XPath with Expressions * http://www.w3schools.com/xpath/xpath_expressions.asp */ -@Ignore("abstract class") -public abstract class XPathExpressionTest extends BaseXPathTest { +public class XPathExpressionTest { + + private static final String XML = + "<foo>" + + "<bar><price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></bar><bar1>3.00</bar1>" + + "</foo>"; + //("/catalog/cd[price>10.80]/price //Numerical Expressions - /** * + Addition 6 + 4 10 */ @Test - public void testAddition() { - String sXpath=getQuery("testAddition",0); - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals("<price at=\"val0\">3.00</price>",m_xc.xmlText()); + void testAddition() throws XmlException { + String sXpath = getQuery("testAddition", 0); + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals("<price at=\"val0\">3.00</price>", m_xc.xmlText()); + } } + /** * - Subtraction 6 - 4 2 */ @Test - public void testSubtraction() { - String sXpath=getQuery("testSubtraction",0); - String sExpected="<price at=\"val1\">2</price>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testSubtraction() throws XmlException { + String sXpath = getQuery("testSubtraction", 0); + String sExpected = "<price at=\"val1\">2</price>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** * * Multiplication 6 * 4 24 */ @Test - public void testMultiplication() { - String sXpath=getQuery("testMultiplication",0); - String sExpected="<price at=\"val1\">2</price>"; - m_xc.selectPath(sXpath); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testMultiplication() throws XmlException { + String sXpath = getQuery("testMultiplication", 0); + String sExpected = "<price at=\"val1\">2</price>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** @@ -75,204 +88,353 @@ public abstract class XPathExpressionTes * NOTE: do a case where res is infinite (eg 10 div 3 or 22/7) */ @Test - public void testDiv() { - String sXpath=getQuery("testDiv",0); //get the second(last) price child - String sExpected="<price at=\"val0\">3.00</price>"; - m_xc.selectPath(sXpath); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); - - m_xc.clearSelections(); - m_xc.toStartDoc(); - - sXpath=getQuery("testDiv",1); //get the second(last) price child - sExpected="<price at=\"val1\">2</price>"; - m_xc.selectPath(sXpath); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); - - m_xc.clearSelections(); - m_xc.toStartDoc(); - - String sXpathZero=getQuery("testDiv",2); - int i = 0; - try{ + void testDiv() throws XmlException { + String sXpath = getQuery("testDiv", 0); //get the second(last) price child + String sExpected = "<price at=\"val0\">3.00</price>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + + m_xc.clearSelections(); + m_xc.toStartDoc(); + + sXpath = getQuery("testDiv", 1); //get the second(last) price child + sExpected = "<price at=\"val1\">2</price>"; + m_xc.selectPath(sXpath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + + m_xc.clearSelections(); + m_xc.toStartDoc(); + + String sXpathZero = getQuery("testDiv", 2); m_xc.selectPath(sXpathZero); - i = m_xc.getSelectionCount(); - fail("Division by 0"); - } catch (Exception ignored){} - assertEquals(0,i); - - m_xc.clearSelections(); - m_xc.toStartDoc(); - - String sXpathInf=getQuery("testDiv",3); - m_xc.selectPath(sXpathInf); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + assertThrows(Exception.class, m_xc::getSelectionCount, "Division by 0"); + assertEquals(0, m_xc.getSelectionCount()); + + m_xc.clearSelections(); + m_xc.toStartDoc(); + + String sXpathInf = getQuery("testDiv", 3); + m_xc.selectPath(sXpathInf); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** * mod Modulus (division remainder) 5 mod 2 1 */ - @Test(expected = Exception.class) - public void testMod() { - String sXpath=getQuery("testMod",0); //get the second(last) price child - String sExpected="<price at=\"val1\">2</price>"; - - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); - - m_xc.clearSelections(); - m_xc.toStartDoc(); - - - sXpath=getQuery("testMod",1); //get the second(last) price child - - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); - - String sXpathZero="10 mod 0"; - m_xc.clearSelections(); - m_xc.toStartDoc(); - m_xc.selectPath(sXpathZero); - m_xc.getSelectionCount(); + @Test + void testMod() throws XmlException { + String sXpath = getQuery("testMod", 0); //get the second(last) price child + String sExpected = "<price at=\"val1\">2</price>"; + + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + + m_xc.clearSelections(); + m_xc.toStartDoc(); + + sXpath = getQuery("testMod", 1); //get the second(last) price child + + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + + String sXpathZero = "10 mod 0"; + m_xc.clearSelections(); + m_xc.toStartDoc(); + m_xc.selectPath(sXpathZero); + assertThrows(Exception.class, m_xc::getSelectionCount); + } } //Equality Expressions + /** * = Like (equal) price=9.80 true (if price is 9.80) */ @Test - public void testEqual() throws XmlException { - String sXml="<foo><bar>" + - "<price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></bar><bar>" + - "<price>5.00</price></bar></foo>"; - m_xc=XmlObject.Factory.parse(sXml).newCursor(); - String sXpath=getQuery("testEqual",0); - String sExpected="<bar><price>5.00</price></bar>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testEqual() throws XmlException { + String sXml = "<foo><bar>" + + "<price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></bar><bar>" + + "<price>5.00</price></bar></foo>"; + + try (XmlCursor m_xc = cur(sXml)) { + String sXpath = getQuery("testEqual", 0); + String sExpected = "<bar><price>5.00</price></bar>"; + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } //Existential semantics of equality in a node set //check this--not sure how to create this test @Test - public void testEqualityNodeset() { - String sXpath=getQuery("testEqualityNodeset",0); - String sExpected="<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testEqualityNodeset() throws XmlException { + String sXpath = getQuery("testEqualityNodeset", 0); + String sExpected = "<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** * != Not like (not equal) price!=9.80 false */ @Test - public void testNotEqual() { - assertEquals(0,m_xc.getSelectionCount()); - String sXpath=getQuery("testNotEqual",0); //has to be double-comparison - String sExpected="<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - System.out.println(m_xc.xmlText()); - assertEquals(sExpected,m_xc.xmlText()); + void testNotEqual() throws XmlException { + String sXpath = getQuery("testNotEqual", 0); //has to be double-comparison + String sExpected = "<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; + try (XmlCursor m_xc = cur(XML)) { + assertEquals(0, m_xc.getSelectionCount()); + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + System.out.println(m_xc.xmlText()); + assertEquals(sExpected, m_xc.xmlText()); + } } //Relational Expressions + /** * < Less than price<9.80 false (if price is 9.80) */ @Test - public void testLessThan() { - String sXpath=getQuery("testLessThan",0); - m_xc.selectPath(sXpath); - assertEquals(0,m_xc.getSelectionCount()); + void testLessThan() throws XmlException { + String sXpath = getQuery("testLessThan", 0); + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(0, m_xc.getSelectionCount()); + } } /** * <= Less or equal price<=9.80 true */ @Test - public void testLessOrEqual() { - String sXpath=getQuery("testLessOrEqual",0); - String sExpected="<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testLessOrEqual() throws XmlException { + String sXpath = getQuery("testLessOrEqual", 0); + String sExpected = "<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** * > Greater than price>9.80 false */ @Test - public void testGreaterThan() { - String sXpath=getQuery("testGreaterThan",0); - String sExpected="<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testGreaterThan() throws XmlException { + String sXpath = getQuery("testGreaterThan", 0); + String sExpected = "<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** * >= Greater or equal price>=9.80 true */ @Test - public void testGreaterOrEqual() { - String sXpath=getQuery("testGreaterOrEqual",0); - String sExpected="<bar>" + - "<price at=\"val0\">3.00</price><price at=\"val1\">2</price>" + - "</bar>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testGreaterOrEqual() throws XmlException { + String sXpath = getQuery("testGreaterOrEqual", 0); + String sExpected = "<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } //Boolean Expressions + /** * or or price=9.80 or price=9.70 true (if price is 9.80) */ @Test - public void testOr() { - String sXpath=getQuery("testOr",0); - String sExpected="<price at=\"val1\">2</price>"; - m_xc.selectPath(sXpath); - assertEquals(1,m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals(sExpected,m_xc.xmlText()); + void testOr() throws XmlException { + String sXpath = getQuery("testOr", 0); + String sExpected = "<price at=\"val1\">2</price>"; + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } /** * and and price<=9.80 and price=9.70 false */ @Test - public void testAnd() { - String sXpath=getQuery("testAnd",0); - m_xc.selectPath(sXpath); - assertEquals(0,m_xc.getSelectionCount()); - } - - @Before - public void setUp()throws Exception { - super.setUp(); - String sXml = "<foo>" + - "<bar><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></bar><bar1>3.00</bar1>" + - "</foo>"; - m_xc=XmlObject.Factory.parse(sXml).newCursor(); + void testAnd() throws XmlException { + String sXpath = getQuery("testAnd", 0); + try (XmlCursor m_xc = cur(XML)) { + m_xc.selectPath(sXpath); + assertEquals(0, m_xc.getSelectionCount()); + } + } + + private void verifySelection(XmlCursor c, String[] expected) { + int count = c.getSelectionCount(); + assertEquals(expected.length, count); + for (int i = 0; i < count; i++) { + c.toNextSelection(); + assertEquals(expected[i], c.xmlText()); + } + } + + @Test + void testForExpression() throws Exception { + String sXml = + "<bib>\n" + + " <book>\n" + + " <title>TCP/IP Illustrated</title>\n" + + " <author>Stevens</author>\n" + + " <publisher>Addison-Wesley</publisher>\n" + + " </book>\n" + + " <book>\n" + + " <title>Advanced Programming in the Unix environment</title>\n" + + " <author>Stevens</author>\n" + + " <publisher>Addison-Wesley</publisher>\n" + + " </book>\n" + + " <book>\n" + + " <title>Data on the Web</title>\n" + + " <author>Abiteboul</author>\n" + + " <author>Buneman</author>\n" + + " <author>Suciu</author>\n" + + " </book>\n" + + "</bib>"; + + String query = + "for $a in distinct-values(//author) " + + "return ($a," + + " for $b in //book[author = $a]" + + " return $b/title)"; + + String[] exp = { + "<xml-fragment>Stevens</xml-fragment>", + "<title>TCP/IP Illustrated</title>", + "<title>Advanced Programming in the Unix environment</title>", + "<xml-fragment>Abiteboul</xml-fragment>", + "<title>Data on the Web</title>", + "<xml-fragment>Buneman</xml-fragment>", + "<title>Data on the Web</title>", + "<xml-fragment>Suciu</xml-fragment>", + "<title>Data on the Web</title>" + }; + + try (XmlCursor c = cur(sXml)) { + c.selectPath(query); + verifySelection(c, exp); + } + } + + @Test + void testFor_1() throws Exception { + String query = + "for $i in (10, 20),\n" + + " $j in (1, 2)\n" + + "return ($i + $j)"; + + try (XmlCursor c = cur("<a/>")) { + c.selectPath(query); + String[] expected = new String[]{ + Common.wrapInXmlFrag("11"), + Common.wrapInXmlFrag("12"), + Common.wrapInXmlFrag("21"), + Common.wrapInXmlFrag("22") + }; + verifySelection(c, expected); + } + } + + @Test + void testFor_2() throws Exception { + try (XmlCursor c = cur("<a/>")) { + String query = "sum (for $i in (10, 20)" + + "return $i)"; + c.selectPath(query); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("30"), c.xmlText()); + } + } + + @Test + void testIf() throws Exception { + String sXML = + "<root>" + + "<book price='20'>Pooh</book>" + + "<cd price='25'>Pooh</cd>" + + "<book price='50'>Maid</book>" + + "<cd price='25'>Maid</cd>" + + "</root>"; + + String query1 = + "if (//book[1]/@price) " + + " then //book[1] " + + " else 0"; + + String query2 = + "for $b1 in //book, $b2 in //cd " + + "return " + + "if ( $b1/@price < $b2/@price )" + + " then $b1" + + " else $b2"; + + try (XmlCursor c = cur(sXML)) { + c.selectPath(query1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals("<book price=\"20\">Pooh</book>", c.xmlText()); + + c.selectPath(query2); + assertEquals(4, c.getSelectionCount()); + c.toNextSelection(); + assertEquals("<book price=\"20\">Pooh</book>", c.xmlText()); + c.toNextSelection(); + assertEquals("<book price=\"20\">Pooh</book>", c.xmlText()); + c.toNextSelection(); + assertEquals("<cd price=\"25\">Pooh</cd>", c.xmlText()); + c.toNextSelection(); + assertEquals("<cd price=\"25\">Maid</cd>", c.xmlText()); + } + } + + @Test + void testQuantifiedExpression() throws Exception { + String query = + "some $x in (1, 2, 3), $y in (2, 3, 4) " + + "satisfies $x + $y = 4"; + + try (XmlCursor c = cur("<root></root>")) { + c.selectPath(query); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals("<xml-fragment>true</xml-fragment>", c.xmlText()); + } } }
Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionAuxTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionAuxTest.java?rev=1897795&r1=1897794&r2=1897795&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionAuxTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionAuxTest.java Sun Feb 6 01:51:55 2022 @@ -17,36 +17,26 @@ package xmlcursor.xpath.common; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; -import org.junit.Test; -import tools.util.JarUtil; -import xmlcursor.common.BasicCursorTestCase; +import org.junit.jupiter.api.Test; import xmlcursor.common.Common; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; +import static xmlcursor.common.BasicCursorTestCase.*; /** * Verifies XPath using functions * http://www.w3schools.com/xpath/xpath_functions.asp */ -public class XPathFunctionAuxTest extends BasicCursorTestCase { - - private static String fixPath(String path) { - return path; - } +public class XPathFunctionAuxTest { @Test - public void testFunctionCount_caseB() throws Exception { - XmlObject xDoc = XmlObject.Factory.parse( - JarUtil.getResourceFromJar("xbean/xmlcursor/xpath/cdcatalog.xml")); - + void testFunctionCount_caseB() throws Exception { String ex1Simple = "count(//cd)"; String ex1R1 = Common.XMLFRAG_BEGINTAG + "26" + Common.XMLFRAG_ENDTAG; - XmlObject[] exXml1 = new XmlObject[]{XmlObject.Factory.parse(ex1R1)}; + XmlObject[] exXml1 = {obj(ex1R1)}; - System.out.println("Test 1: " + ex1Simple); - try (XmlCursor x1 = xDoc.newCursor()) { + try (XmlCursor x1 = jcur("xbean/xmlcursor/xpath/cdcatalog.xml")) { x1.selectPath(ex1Simple); XPathCommon.display(x1); XPathCommon.compare(x1, exXml1); @@ -54,156 +44,160 @@ public class XPathFunctionAuxTest extend } @Test - public void testFunctionConcat_caseB() throws Exception { - String sXml = "<foo><bar><price at=\"val0\">3.00</price>" + + void testFunctionConcat_caseB() throws Exception { + String sXml = + "<foo><bar><price at=\"val0\">3.00</price>" + "<price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath = "concat(name(//bar[position()=1]/*[position()=last()])," + + String sXPath = + "concat(name(//bar[position()=1]/*[position()=last()])," + "//price[position()=1]/text())"; String sExpected = Common.wrapInXmlFrag("price3.00"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionStringLength_caseB() throws Exception { + void testFunctionStringLength_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "string-length(name(//bar/*[last()]))"; String sExpected = Common.wrapInXmlFrag("price".length() + ""); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionSubString_caseB() throws Exception { + void testFunctionSubString_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "substring(name(//bar[position()=1]/*[position()=1]),3,3)"; String sExpected = Common.wrapInXmlFrag("ice"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(XmlCursor.TokenType.TEXT, m_xc.currentTokenType()); - assertEquals(sExpected, m_xc.xmlText()); + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(XmlCursor.TokenType.TEXT, m_xc.currentTokenType()); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionSubStringAfter_caseB() throws Exception { + void testFunctionSubStringAfter_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "substring-after(name(//bar[position()=1]/*[position()=1]),'pr')"; String sExpected = Common.wrapInXmlFrag("ice"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionSubStringBefore_caseB() throws Exception { + void testFunctionSubStringBefore_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "substring-before(" + - "name(//bar[position()=1]/*[position()=1]),'ice')"; + "name(//bar[position()=1]/*[position()=1]),'ice')"; String sExpected = Common.wrapInXmlFrag("pr"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionTranslate_caseB() throws Exception { + void testFunctionTranslate_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "translate(//bar[position()=1]/price[position()=1]/text()," + - "'200'," + - "'654')";//0 is now 5 &&4? + "'200'," + + "'654')";//0 is now 5 &&4? String sExpected = Common.wrapInXmlFrag("3.55"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionNumber_caseB() throws Exception { + void testFunctionNumber_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); String sXPath = "number(//price/text())+10"; String sExpected = Common.wrapInXmlFrag("13.0"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionRound_caseB() throws Exception { + void testFunctionRound_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.15</price><price at=\"val1\">2.87</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "round(//bar/price[position()=1]/text())"; String sExpected = Common.wrapInXmlFrag("3.0"); - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionSum_caseB() throws Exception { + void testFunctionSum_caseB() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "sum(//bar/price)"; String sExpected = Common.wrapInXmlFrag("5.0"); - m_xc.selectPath(sXPath); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } @Test - public void testFunctionBoolean_caseB_delete() throws Exception { - String sXml = "<foo><bar>" + + void testFunctionBoolean_caseB_delete() throws Exception { + String sXml = + "<foo><bar>" + "<price at=\"val0\">3.00</price>" + "<price at=\"val1\">2</price>" + "</bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sXPath = "boolean(//foo/text())"; - m_xc.selectPath(sXPath); - System.out.println(m_xc.getSelectionCount()); - assertTrue(m_xc.toNextSelection()); - assertEquals(Common.wrapInXmlFrag("false"), - m_xc.xmlText()); - assertTrue(!m_xc.toNextSelection()); -// System.out.println("DOC " + m_xc.xmlText()); - m_xc.clearSelections(); - m_xc.toStartDoc(); - m_xc.selectPath("boolean(//price/text())"); -// m_xc.selectPath("$this//bar"); - m_xc.toNextSelection(); - // System.out.println("HERE " + m_xc.xmlText()); + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + System.out.println(m_xc.getSelectionCount()); + assertTrue(m_xc.toNextSelection()); + assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); + assertFalse(m_xc.toNextSelection()); + m_xc.clearSelections(); + m_xc.toStartDoc(); + m_xc.selectPath("boolean(//price/text())"); + m_xc.toNextSelection(); + } } @Test - public void testFunctionBoolean_caseB() throws Exception { - String sXml = "<foo><bar>" + + void testFunctionBoolean_caseB() throws Exception { + String sXml = + "<foo><bar>" + "<price at=\"val0\">3.00</price>" + "<price at=\"val1\">2</price>" + "</bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - try (XmlCursor _startPos = m_xc.newCursor()) { - String sXPath = "boolean(//foo/text())";//"boolean(//foo/text())"; + try (XmlCursor m_xc = cur(sXml); + XmlCursor _startPos = m_xc.newCursor()) { m_xc.push(); - m_xc.selectPath(sXPath); + m_xc.selectPath("boolean(//foo/text())"); m_xc.toNextSelection(); assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); m_xc.clearSelections(); @@ -216,7 +210,6 @@ public class XPathFunctionAuxTest extend assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); m_xc.clearSelections(); - //number assertTrue(m_xc.toCursor(_startPos)); //boolean of Nan is false @@ -241,14 +234,16 @@ public class XPathFunctionAuxTest extend } @Test - public void testFunctionFalse_caseB() throws Exception { - m_xc = - XmlObject.Factory.parse( - "<foo><price at=\"val0\">3.00</price></foo>") - .newCursor(); - m_xc.selectPath("name(//*[boolean(text())=false()])"); - String sExpected = Common.wrapInXmlFrag("foo"); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + void testFunctionFalse_caseB() throws Exception { + String sXml = "<foo><price at=\"val0\">3.00</price></foo>"; + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath("name(//*[boolean(text())=false()])"); + String sExpected = Common.wrapInXmlFrag("foo"); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } + + + } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionTest.java?rev=1897795&r1=1897794&r2=1897795&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionTest.java Sun Feb 6 01:51:55 2022 @@ -16,44 +16,88 @@ package xmlcursor.xpath.common; import org.apache.xmlbeans.XmlCursor; +import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; -import org.junit.Ignore; -import org.junit.Test; -import tools.util.JarUtil; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.*; +import static xmlcursor.common.BasicCursorTestCase.*; /** * Verifies XPath using functions * http://www.w3schools.com/xpath/xpath_functions.asp */ -@Ignore("abstract class") -public abstract class XPathFunctionTest extends BaseXPathTest { +public class XPathFunctionTest implements XPathTestBase { // Node Set Functions // http://www.w3.org/TR/xpath#section-Node-Set-Functions + + private static String getQuery(String testName, int testCase) throws IllegalArgumentException { + switch (testName) { + case "testFunctionCount": + return new String[]{ + "count(//cd)", + "//cd[position()=2]"}[testCase]; + + case "testFunctionLocalName": + return "//*[local-name(.)='bar']"; + + case "testFunctionConcat": + return "//bar/*[name(.)=concat(\"pr\",\"ice\")]"; + + case "testFunctionString": + return new String[]{ + "/foo/*[name(.)=" + + "concat(\"bar\",string(./foo/bar/price[last()]))]"}[testCase]; + + case "testFunctionStringLength": + return "//bar/*[string-length(name(.))=5]"; + + case "testFunctionSubString": + return "//bar/*[substring(name(.),3,3)=\"ice\"]"; + + case "testFunctionSubStringAfter": + return "//bar/*[substring-after(name(.),'pr')=\"ice\"]"; + + case "testFunctionSubStringBefore": + return "//bar/*[substring-before(name(.),'ice')=\"pr\"]"; + + case "testFunctionTranslate": + return "//bar/*[translate(name(.),'ice','pr')=\"prpr\"]"; + + case "testFunctionLang": + return new String[]{ + "//price[lang(\"en\")=true()]", + "//foo[lang(\"en\")=true()]"}[testCase]; + + case "testFunctionTrue": + return "//*[boolean(@at)=true()]"; + + default: + return XPathTestBase.getQuery(testName, testCase); + } + } + + /** * count() - * Returns the number of nodes in a node-set - * number=count(node-set) - * + * Returns the number of nodes in a node-set + * number=count(node-set) */ @Test - public void testFunctionCount() throws Exception { - String ex0Simple =getQuery("testFunctionCount",0) ; - String ex0Simple1 =getQuery("testFunctionCount",1) ; + void testFunctionCount() throws Exception { + String ex0Simple = getQuery("testFunctionCount", 0); + String ex0Simple1 = getQuery("testFunctionCount", 1); System.out.println("Test 0: " + ex0Simple); - try (XmlCursor x0 = XmlObject.Factory.parse( - "<foo><cd>1</cd><cd>2</cd></foo>") - .newCursor(); - XmlCursor x01 = x0.newCursor()) { + try (XmlCursor x0 = cur("<foo><cd>1</cd><cd>2</cd></foo>"); + XmlCursor x01 = x0.newCursor()) { /* XmlCursor countCheck = x0.newCursor(); countCheck.selectPath("count(.//cd)"); countCheck.toNextSelection(); System.out.println(" Global count "+countCheck.xmlText()); */ - String sExpectedResult = "<cd>2</cd>" ; + String sExpectedResult = "<cd>2</cd>"; x01.selectPath(ex0Simple1); assertEquals(1, x01.getSelectionCount()); @@ -73,34 +117,33 @@ public abstract class XPathFunctionTest /** * id() - * Selects elements by their unique ID - * node-set=id(value) + * Selects elements by their unique ID + * node-set=id(value) */ @Test - public void testFunctionId() throws Exception { - XmlObject xDoc = XmlObject.Factory.parse( - JarUtil.getResourceFromJar("xbean/xmlcursor/xpath/cdcatalog.xml")); - String ex1Simple = getQuery("testFunctionId",0); //"id(\"bobdylan\")" + void testFunctionId() throws Exception { + XmlObject xDoc = jobj("xbean/xmlcursor/xpath/cdcatalog.xml"); + String ex1Simple = getQuery("testFunctionId", 0); //"id(\"bobdylan\")" + + String ex1R1 = + "<cd id=\"bobdylan\">" + + "<title>Empire Burlesque</title>" + + "<artist>Bob Dylan</artist><country>USA</country>" + + "<company>Columbia</company><price>10.90</price>" + + "<year>1985</year></cd>"; + XmlObject[] exXml1 = {obj(ex1R1)}; - String ex1R1 = "<cd id=\"bobdylan\">" + - "<title>Empire Burlesque</title>" + - "<artist>Bob Dylan</artist><country>USA</country>" + - "<company>Columbia</company><price>10.90</price>" + - "<year>1985</year></cd>"; - XmlObject[] exXml1 = new XmlObject[]{XmlObject.Factory.parse(ex1R1)}; + //"id(\"foobar\")" + String ex2Simple = getQuery("testFunctionId", 1); - String ex2Simple = getQuery("testFunctionId",1); //"id(\"foobar\")" - - String ex3Simple = getQuery("testFunctionId",2); //"//child::cd[position()=3]" + //"//child::cd[position()=3]" + String ex3Simple = getQuery("testFunctionId", 2); String ex3R1 = "<cd id=\"id3\"><title>Greatest Hits</title><artist>Dolly Parton</artist><country>USA</country><company>RCA</company><price>9.90</price><year>1982</year></cd>"; XmlObject[] exXml3 = new XmlObject[]{XmlObject.Factory.parse(ex3R1)}; - System.out.println("Test 1: " + ex1Simple); try (XmlCursor x1 = xDoc.newCursor()) { x1.toChild("catalog"); - System.out.println(x1.currentTokenType()); - System.out.println(x1.getName()); x1.selectPath(ex1Simple); //XPathCommon.display(x1); //assertEquals(1,x1.getSelectionCount()); @@ -108,14 +151,12 @@ public abstract class XPathFunctionTest //assertEquals(ex1R1, x1.xmlText()); } - System.out.println("Test 2: " + ex2Simple); try (XmlCursor x2 = xDoc.newCursor()) { x2.selectPath(ex2Simple); XPathCommon.display(x2); assertFalse(x2.toNextSelection()); } - System.out.println("Test 3: " + ex3Simple); try (XmlCursor x3 = xDoc.newCursor()) { x3.selectPath(ex3Simple); XPathCommon.display(x3); @@ -125,23 +166,22 @@ public abstract class XPathFunctionTest /** * last() - * Returns the position number of the last node in the processed node list - * number=last() + * Returns the position number of the last node in the processed node list + * number=last() */ @Test - public void testFunctionLast() throws Exception { - XmlObject xDoc = XmlObject.Factory.parse( - JarUtil.getResourceFromJar("xbean/xmlcursor/xpath/cdcatalog.xml")); - - String ex1Simple = getQuery("testFunctionLast",0); - String ex1R1 = "<cd>" + - "<title>Unchain my heart</title>" + - "<artist>Joe Cocker</artist><country>USA</country>" + - "<company>EMI</company><price>8.20</price>" + - "<year>1987</year></cd>"; - XmlObject[] exXml1 = new XmlObject[]{XmlObject.Factory.parse(ex1R1)}; + void testFunctionLast() throws Exception { + String ex1Simple = getQuery("testFunctionLast", 0); + String ex1R1 = + "<cd>" + + "<title>Unchain my heart</title>" + + "<artist>Joe Cocker</artist><country>USA</country>" + + "<company>EMI</company><price>8.20</price>" + + "<year>1987</year></cd>"; + + XmlObject xDoc = jobj("xbean/xmlcursor/xpath/cdcatalog.xml"); + XmlObject[] exXml1 = {obj(ex1R1)}; - System.out.println("Test 1: " + ex1Simple); try (XmlCursor x1 = xDoc.newCursor()) { x1.selectPath(ex1Simple); XPathCommon.display(x1); @@ -151,23 +191,25 @@ public abstract class XPathFunctionTest /** * local-name() - * Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name - * string=local-name(node) + * Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name + * string=local-name(node) */ @Test - public void testFunctionLocalName() throws Exception { - String sXPath = getQuery("testFunctionLocalName",0); - String sXml = "<foo xmlns:pre=\"uri.org\">" + - "<pre:bar><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></pre:bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); + void testFunctionLocalName() throws Exception { + String sXPath = getQuery("testFunctionLocalName", 0); + String sXml = + "<foo xmlns:pre=\"uri.org\">" + + "<pre:bar><price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></pre:bar><bar1>3.00</bar1></foo>"; String sExpected = - "<pre:bar xmlns:pre=\"uri.org\"><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></pre:bar>"; + "<pre:bar xmlns:pre=\"uri.org\"><price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></pre:bar>"; - m_xc.selectPath(sXPath); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } // /** @@ -181,23 +223,27 @@ public abstract class XPathFunctionTest /** * namespace-uri() - * Returns the namespace URI of a specified node - * uri=namespace-uri(node) + * Returns the namespace URI of a specified node + * uri=namespace-uri(node) */ @Test - public void testFunctionNamespaceURI() throws Exception { - String sXPath = getQuery("testFunctionNamespaceURI",0); + void testFunctionNamespaceURI() throws Exception { + String sXPath = getQuery("testFunctionNamespaceURI", 0); - String sXml = "<foo xmlns:pre=\"uri.org\">" + - "<pre:bar><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></pre:bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sExpected = "<pre:bar xmlns:pre=\"uri.org\">" + - "<price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></pre:bar>"; - m_xc.selectPath(sXPath); - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + String sXml = + "<foo xmlns:pre=\"uri.org\">" + + "<pre:bar><price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></pre:bar><bar1>3.00</bar1></foo>"; + String sExpected = + "<pre:bar xmlns:pre=\"uri.org\">" + + "<price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></pre:bar>"; + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } } // /** @@ -214,37 +260,40 @@ public abstract class XPathFunctionTest /** * concat() - * Returns the concatenation of all its arguments string=concat(val1, val2, ..) - * Example: concat('The',' ','XML') Result: 'The XML' + * Returns the concatenation of all its arguments string=concat(val1, val2, ..) + * Example: concat('The',' ','XML') Result: 'The XML' */ @Test - public void testFunctionConcat() throws Exception { - String sXPath=getQuery("testFunctionConcat",0); - String sXml = "<foo><bar><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - m_xc.selectPath(sXPath); - assertEquals(2, m_xc.getSelectionCount()); + void testFunctionConcat() throws Exception { + String sXPath = getQuery("testFunctionConcat", 0); + String sXml = + "<foo><bar><price at=\"val0\">3.00</price>" + + "<price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(2, m_xc.getSelectionCount()); + } } /** * string() - * Converts the value argument to a string - * string(value) - * Example: string(314) Result: '314' + * Converts the value argument to a string + * string(value) + * Example: string(314) Result: '314' */ @Test - public void testFunctionString() throws Exception { - String sXml = "<foo><bar><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">1</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath =getQuery("testFunctionString",0); - m_xc.selectPath(sXPath); - assertEquals(1,m_xc.getSelectionCount()); + void testFunctionString() throws Exception { + String sXPath = getQuery("testFunctionString", 0); + String sXml = + "<foo><bar><price at=\"val0\">3.00</price>" + + "<price at=\"val1\">1</price></bar><bar1>3.00</bar1></foo>"; + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(1, m_xc.getSelectionCount()); + } - System.out.println(" Test 0 Pass=================="); /* m_xc.clearSelections(); sXPath="xf:concat(xf:string(//foo/bar/price[1]),xf:string(//foo/bar/price[last()]))"; @@ -257,99 +306,99 @@ public abstract class XPathFunctionTest /** * string-length() - * Returns the number of characters in a string - * number=string-length(string) - * Example: string-length('Beatles') Result: 7 - */ - @Test - public void testFunctionStringLength() throws Exception { - String sXml = "<foo><bar>" + - "<price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>" + - "<bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath = getQuery("testFunctionStringLength",0); - m_xc.selectPath(sXPath); - assertEquals(2, m_xc.getSelectionCount()); - - System.out.println(" Test 0 Pass=================="); - m_xc.clearSelections(); + * Returns the number of characters in a string + * number=string-length(string) + * Example: string-length('Beatles') Result: 7 + */ + @Test + void testFunctionStringLength() throws Exception { + String sXPath = getQuery("testFunctionStringLength", 0); + String sXml = + "<foo><bar>" + + "<price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>" + + "<bar1>3.00</bar1></foo>"; + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(2, m_xc.getSelectionCount()); + m_xc.clearSelections(); + } } /** * substring() - * Returns a part of the string in the string argument - * string=substring(string,start,length) - * Example: substring('Beatles',1,4) Result: 'Beat' + * Returns a part of the string in the string argument + * string=substring(string,start,length) + * Example: substring('Beatles',1,4) Result: 'Beat' */ @Test - public void testFunctionSubString() throws Exception { + void testFunctionSubString() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath = getQuery("testFunctionSubString",0); - m_xc.selectPath(sXPath); - assertEquals(2, m_xc.getSelectionCount()); + String sXPath = getQuery("testFunctionSubString", 0); - m_xc.clearSelections(); - System.out.println(" Test 0 Pass=================="); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(2, m_xc.getSelectionCount()); + m_xc.clearSelections(); + } } /** * substring-after() - * Returns the part of the string in the string argument that occurs after the substring in the substr argument - * string=substring-after(string,substr) - * Example: substring-after('12/10','/') Result: '10' + * Returns the part of the string in the string argument that occurs after the substring in the substr argument + * string=substring-after(string,substr) + * Example: substring-after('12/10','/') Result: '10' */ @Test - public void testFunctionSubStringAfter() throws Exception { + void testFunctionSubStringAfter() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath = getQuery("testFunctionSubStringAfter",0); - m_xc.selectPath(sXPath); - assertEquals(2, m_xc.getSelectionCount()); + String sXPath = getQuery("testFunctionSubStringAfter", 0); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(2, m_xc.getSelectionCount()); + } } /** * substring-before() - * Returns the part of the string in the string argument that occurs before the substring in the substr argument - * string=substring-before(string,substr) - * Example: substring-before('12/10','/') Result: '12' + * Returns the part of the string in the string argument that occurs before the substring in the substr argument + * string=substring-before(string,substr) + * Example: substring-before('12/10','/') Result: '12' */ @Test - public void testFunctionSubStringBefore() throws Exception { + void testFunctionSubStringBefore() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price>" + - "<price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); + "<price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; + String sXPath = getQuery("testFunctionSubStringBefore", 0); - String sXPath = getQuery("testFunctionSubStringBefore",0); - m_xc.selectPath(sXPath); - assertEquals(2, m_xc.getSelectionCount()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(2, m_xc.getSelectionCount()); + } } /** * translate() - * Performs a character by character replacement. It looks in the value argument for characters - * contained in string1, and replaces each character for the one in the same position in the string2 - * string=translate(value,string1,string2) - * Examples: translate('12:30','30','45') Result: '12:45' - * translate('12:30','03','54') Result: '12:45' - * translate('12:30','0123','abcd') Result: 'bc:da' + * Performs a character by character replacement. It looks in the value argument for characters + * contained in string1, and replaces each character for the one in the same position in the string2 + * string=translate(value,string1,string2) + * Examples: translate('12:30','30','45') Result: '12:45' + * translate('12:30','03','54') Result: '12:45' + * translate('12:30','0123','abcd') Result: 'bc:da' */ @Test - public void testFunctionTranslate() throws Exception { + void testFunctionTranslate() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - //TODO: is this a bug in XQRL? - String sXPath =getQuery("testFunctionTranslate",0); - m_xc.selectPath(sXPath); - assertEquals(2, m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals("<price at=\"val0\">3.00</price>", m_xc.xmlText()); - m_xc.toNextSelection(); - assertEquals("<price at=\"val1\">2</price>", m_xc.xmlText()); + String sXPath = getQuery("testFunctionTranslate", 0); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(2, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals("<price at=\"val0\">3.00</price>", m_xc.xmlText()); + m_xc.toNextSelection(); + assertEquals("<price at=\"val1\">2</price>", m_xc.xmlText()); + } } // Number Functions @@ -377,68 +426,66 @@ public abstract class XPathFunctionTest /** * number() - * Converts the value argument to a number - * number=number(value) - * Example: number('100') Result: 100 + * Converts the value argument to a number + * number=number(value) + * Example: number('100') Result: 100 */ @Test - public void testFunctionNumber() throws Exception { + void testFunctionNumber() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.00</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); //really wanted . here...seems like number function doesn't //recognize context? - String sXPath =getQuery("testFunctionNumber",0); - m_xc.selectPath(sXPath); - assertEquals(1, m_xc.getSelectionCount()); - - System.out.println(" Test 0 Pass=================="); - m_xc.clearSelections(); + String sXPath = getQuery("testFunctionNumber", 0); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.clearSelections(); + } } /** * round() - * Rounds the number argument to the nearest integer - * integer=round(number) - * Example: round(3.14) Result: 3 + * Rounds the number argument to the nearest integer + * integer=round(number) + * Example: round(3.14) Result: 3 */ @Test - public void testFunctionRound() throws Exception { + void testFunctionRound() throws Exception { String sXml = "<foo><bar><price at=\"val0\">3.15</price>" + - "<price at=\"val1\">2.87</price></bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath = getQuery("testFunctionRound",0); - m_xc.selectPath(sXPath); //"//bar//*[round(text())=3]" - assertEquals(2, m_xc.getSelectionCount()); - m_xc.toNextSelection(); - assertEquals("<price at=\"val0\">3.15</price>", m_xc.xmlText()); - m_xc.toNextSelection(); - assertEquals("<price at=\"val1\">2.87</price>", m_xc.xmlText()); - m_xc.clearSelections(); + "<price at=\"val1\">2.87</price></bar><bar1>3.00</bar1></foo>"; + String sXPath = getQuery("testFunctionRound", 0); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); //"//bar//*[round(text())=3]" + assertEquals(2, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals("<price at=\"val0\">3.15</price>", m_xc.xmlText()); + m_xc.toNextSelection(); + assertEquals("<price at=\"val1\">2.87</price>", m_xc.xmlText()); + m_xc.clearSelections(); + } } /** * sum() - * Returns the total value of a set of numeric values in a node-set - * number=sum(nodeset) - * Example: sum(/cd/price) + * Returns the total value of a set of numeric values in a node-set + * number=sum(nodeset) + * Example: sum(/cd/price) */ @Test - public void testFunctionSum() throws Exception { + void testFunctionSum() throws Exception { String sXml = "<foo><bar><price at=\"val0\">" + - "3.00</price><price at=\"val1\">2</price>" + - "</bar><bar1>3.00</bar1></foo>"; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - - String sXPath = getQuery("testFunctionSum",0); - m_xc.selectPath(sXPath); - assertEquals(1, m_xc.getSelectionCount()); - m_xc.toNextSelection(); - String exp = "<bar><price at=\"val0\">" + - "3.00</price><price at=\"val1\">2</price>" + - "</bar>"; - assertEquals(exp, m_xc.xmlText()); - m_xc.clearSelections(); + "3.00</price><price at=\"val1\">2</price>" + + "</bar><bar1>3.00</bar1></foo>"; + String sXPath = getQuery("testFunctionSum", 0); + String exp = "<bar><price at=\"val0\">3.00</price><price at=\"val1\">2</price></bar>"; + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(1, m_xc.getSelectionCount()); + m_xc.toNextSelection(); + assertEquals(exp, m_xc.xmlText()); + m_xc.clearSelections(); + } } // Boolean Functions @@ -446,63 +493,61 @@ public abstract class XPathFunctionTest /** * boolean() - * Converts the value argument to Boolean and returns true or false - * bool=boolean(value) + * Converts the value argument to Boolean and returns true or false + * bool=boolean(value) * a number is true if and only if it is neither positive or negative zero nor NaN * a node-set is true if and only if it is non-empty * a string is true if and only if its length is non-zero * an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type */ @Test - public void testFunctionBoolean() throws Exception { - m_xc = - XmlObject.Factory.parse( - "<foo><price at=\"val0\">3.00</price></foo>") - .newCursor(); + void testFunctionBoolean() throws Exception { + String sXml = "<foo><price at=\"val0\">3.00</price></foo>"; String sXPath = getQuery("testFunctionBoolean", 0); - m_xc.selectPath(sXPath); - m_xc.toNextSelection(); - assertEquals(1, m_xc.getSelectionCount()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(1, m_xc.getSelectionCount()); + } } /** * false() - * Returns false false() - * Example: number(false()) Result: 0 + * Returns false false() + * Example: number(false()) Result: 0 */ @Test - public void testFunctionFalse() throws Exception { - m_xc = - XmlObject.Factory.parse( - "<foo><price at=\"val0\">3.00</price></foo>") - .newCursor(); + void testFunctionFalse() throws Exception { + String sXml = "<foo><price at=\"val0\">3.00</price></foo>"; String sXPath = getQuery("testFunctionFalse", 0); - m_xc.selectPath(sXPath); - assertEquals(0, m_xc.getSelectionCount()); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(0, m_xc.getSelectionCount()); + } } /** * lang() - * Returns true if the language argument matches the language of the the - * xsl:lang element, otherwise it returns false - * bool=lang(language) + * Returns true if the language argument matches the language of the the + * xsl:lang element, otherwise it returns false + * bool=lang(language) */ @Test - public void testFunctionLang() throws Exception { - m_xc = - XmlObject.Factory.parse( - "<foo><div xml:lang=\"en\"><para/><price at=\"val0\">3.00</price></div></foo>") - .newCursor(); + void testFunctionLang() throws Exception { + String sXml = "<foo><div xml:lang=\"en\"><para/><price at=\"val0\">3.00</price></div></foo>"; String sXPath = getQuery("testFunctionLang", 0); - m_xc.selectPath(sXPath); - String sExpected = "<price at=\"val0\">3.00</price>"; - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); - m_xc.clearSelections(); - - sXPath= getQuery("testFunctionLang",1); - m_xc.selectPath(sXPath); - assertEquals(0, m_xc.getSelectionCount()); + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + String sExpected = "<price at=\"val0\">3.00</price>"; + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + m_xc.clearSelections(); + + sXPath = getQuery("testFunctionLang", 1); + m_xc.selectPath(sXPath); + assertEquals(0, m_xc.getSelectionCount()); + } } // /** @@ -518,22 +563,101 @@ public abstract class XPathFunctionTest /** * true() - * Returns true - * true() - * Example: number(true()) Result: 1 - */ - @Test - public void testFunctionTrue() throws Exception { - m_xc = - XmlObject.Factory.parse( - "<foo><price at=\"val0\">3.00</price></foo>") - .newCursor(); - - String sXPath= getQuery("testFunctionTrue",0); - m_xc.selectPath(sXPath); - String sExpected = "<price at=\"val0\">3.00</price>"; - m_xc.toNextSelection(); - assertEquals(sExpected, m_xc.xmlText()); + * Returns true + * true() + * Example: number(true()) Result: 1 + */ + @Test + void testFunctionTrue() throws Exception { + String sXml = "<foo><price at=\"val0\">3.00</price></foo>"; + String sXPath = getQuery("testFunctionTrue", 0); + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + String sExpected = "<price at=\"val0\">3.00</price>"; + m_xc.toNextSelection(); + assertEquals(sExpected, m_xc.xmlText()); + } + } + + @Test + void testErrorMessages() throws Exception { + //do nothing for Jaxen + } + + //ensure Jaxen is not in the classpath + @Test + @Disabled("Saxon will always jump in, so ignoring it") + public void testAntiJaxenTest() throws XmlException { + String sXml = "<foo><price at=\"val0\">3.00</price></foo>"; + try (XmlCursor m_xc = cur(sXml)) { + // XQRL shouldn't handle absolute paths + assertThrows(Throwable.class, () -> m_xc.selectPath("//*")); + } + } + + @Disabled + public void testExternalVariable() throws Exception { + + } + + @Test + void testExternalFunction() throws Exception { + String query = + "declare function local:toc($book-or-section as element()) as element()* { $book-or-section/section }; " + + "local:toc(book)"; + + String input = + "<book>\n" + + " <title>Data on the Web</title>\n" + + " <author>Serge Abiteboul</author>\n" + + " <author>Peter Buneman</author>\n" + + " <author>Dan Suciu</author>\n" + + " <section id=\"intro\" difficulty=\"easy\" >\n" + + " <title>Introduction</title>\n" + + " <p>Text ... </p>\n" + + " <section>\n" + + " <title>Audience</title>\n" + + " <p>Text ... </p>\n" + + " </section>\n" + + " <section>\n" + + " <title>Web Data and the Two Cultures</title>\n" + + " <p>Text ... </p>\n" + + " <figure height=\"400\" width=\"400\">\n" + + " <title>Traditional client/server architecture</title>\n" + + " <image source=\"csarch.gif\"/>\n" + + " </figure>\n" + + " <p>Text ... </p>\n" + + " </section>\n" + + " </section>\n" + + " <section id=\"syntax\" difficulty=\"medium\" >\n" + + " <title>A Syntax For Data</title>\n" + + " <p>Text ... </p>\n" + + " <figure height=\"200\" width=\"500\">\n" + + " <title>Graph representations of structures</title>\n" + + " <image source=\"graphs.gif\"/>\n" + + " </figure>\n" + + " <p>Text ... </p>\n" + + " <section>\n" + + " <title>Base Types</title>\n" + + " <p>Text ... </p>\n" + + " </section>\n" + + " <section>\n" + + " <title>Representing Relational Databases</title>\n" + + " <p>Text ... </p>\n" + + " <figure height=\"250\" width=\"400\">\n" + + " <title>Examples of Relations</title>\n" + + " <image source=\"relations.gif\"/>\n" + + " </figure>\n" + + " </section>\n" + + " <section>\n" + + " <title>Representing Object Databases</title>\n" + + " <p>Text ... </p>\n" + + " </section> \n" + + " </section>\n" + + "</book>"; + XmlObject o = obj(input); + XmlObject[] res = o.execQuery(query); + assertEquals(2, res.length); } } Copied: xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodeTest.java (from r1897794, xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodetestTest.java) URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodeTest.java?p2=xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodeTest.java&p1=xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodetestTest.java&r1=1897794&r2=1897795&rev=1897795&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodetestTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathNodeTest.java Sun Feb 6 01:51:55 2022 @@ -16,110 +16,200 @@ package xmlcursor.xpath.common; +import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; -import org.junit.Test; -import xmlcursor.common.BasicCursorTestCase; +import org.junit.jupiter.api.Test; import xmlcursor.common.Common; -import static org.junit.Assert.assertEquals; +import javax.xml.namespace.QName; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static xmlcursor.common.BasicCursorTestCase.cur; +import static xmlcursor.common.BasicCursorTestCase.obj; /** * Verifies XPath nodetest functions */ -public class XPathNodetestTest extends BasicCursorTestCase { +public class XPathNodeTest { + + private static String fixPath(String path) { + return "." + path; + } + + public void testAllNodes() { + //e.g //A/B/*: tested by Zvon + } + + @Test + void testComment() throws Exception { + try (XmlCursor m_xc = cur(Common.XML_FOO_NS_PREFIX)) { + String sExpected = "<xml-fragment xmlns:edi=\"http://ecommerce.org/schema\"><!-- the 'price' element's namespace is http://ecommerce.org/schema -->" + Common.XMLFRAG_ENDTAG;//the comment string + String sXPath = "//comment()"; + m_xc.selectPath(fixPath(sXPath)); + m_xc.toNextSelection(); + assertEquals(m_xc.xmlText(), sExpected); + } + } + + @Test + void testNode() throws Exception { + String sXPath = "//foo/node()"; + String[] sExpected = { + Common.XMLFRAG_BEGINTAG + " " + Common.XMLFRAG_ENDTAG, + "<node>foo</node>", + Common.XMLFRAG_BEGINTAG + "txt" + Common.XMLFRAG_ENDTAG + }; + + // TODO: add asserts + try (XmlCursor m_xc = cur("<foo> <node>foo</node>txt</foo>")) { + m_xc.selectPath(fixPath(sXPath)); + int i = 0; + // assertEquals("node() failed", sExpected.length, m_xc.getSelectionCount()); + while (m_xc.hasNextSelection()) { + m_xc.toNextSelection(); + //assertEquals(m_xc.xmlText(), sExpected[i++]); + } + } + } + + @Test + void testPI() throws Exception { + String sXPath1 = "//processing-instruction()"; + String sXPath2 = "//processing-instruction(\"xml-stylesheet\")"; + String sXPath3 = "//processing-instruction(\"xsl\")"; + String sExpected = Common.XMLFRAG_BEGINTAG + "<?xml-stylesheet type=\"text/xsl\" xmlns=\"http://openuri.org/shipping/\"?>" + Common.XMLFRAG_ENDTAG; + + try (XmlCursor m_xc = cur(Common.XML_FOO_PROCINST)) { + m_xc.selectPath(fixPath(sXPath1)); + assertEquals(m_xc.getSelectionCount(), 1); + m_xc.toNextSelection(); + assertEquals(m_xc.xmlText(), sExpected); + + m_xc.clearSelections(); + m_xc.selectPath(fixPath(sXPath2)); + assertEquals(m_xc.xmlText(), sExpected); + + m_xc.clearSelections(); + //shouldn't select any nodes + m_xc.selectPath(fixPath(sXPath3)); + assertEquals(m_xc.getSelectionCount(), 0); + } + } + + @Test + void testText() throws Exception { + String sXml = "<?xml-stylesheet type=\"text/xsl\" xmlns=\"http://openuri.org/shipping/\"?><br>foo<foo>text</foo></br>"; + String sXPath = "//text()"; + String sExpected1 = Common.XMLFRAG_BEGINTAG + "foo" + Common.XMLFRAG_ENDTAG; + String sExpected2 = Common.XMLFRAG_BEGINTAG + "text" + Common.XMLFRAG_ENDTAG; + + try (XmlCursor m_xc = cur(sXml)) { + m_xc.selectPath(sXPath); + assertEquals(m_xc.getSelectionCount(), 2); + m_xc.toNextSelection(); + assertEquals(m_xc.xmlText(), sExpected1); + m_xc.toNextSelection(); + assertEquals(m_xc.xmlText(), sExpected2); + } + } - private static String fixPath(String path){ - return "."+path; - } - - - public void testAllNodes(){ - //e.g //A/B/*: tested by Zvon - } - - @Test - public void testComment()throws Exception { - String sXml = Common.XML_FOO_NS_PREFIX; - m_xc = XmlObject.Factory.parse(sXml).newCursor(); - String sExpected = "<xml-fragment xmlns:edi=\"http://ecommerce.org/schema\"><!-- the 'price' element's namespace is http://ecommerce.org/schema -->" + Common.XMLFRAG_ENDTAG;//the comment string - String sXPath = "//comment()"; - m_xc.selectPath(fixPath(sXPath)); - m_xc.toNextSelection(); - assertEquals(m_xc.xmlText(), sExpected); - } - - @Test - public void testNode() throws Exception { - String sInput = "<foo> <node>foo</node>txt</foo>"; - m_xc = XmlObject.Factory.parse(sInput).newCursor(); - String sXPath = "//foo/node()"; - String[] sExpected = { - Common.XMLFRAG_BEGINTAG + " " + Common.XMLFRAG_ENDTAG, - "<node>foo</node>", - Common.XMLFRAG_BEGINTAG + "txt" + Common.XMLFRAG_ENDTAG - }; - m_xc.selectPath(fixPath(sXPath)); - int i = 0; - // assertEquals("node() failed", sExpected.length, m_xc.getSelectionCount()); - while (m_xc.hasNextSelection()) { - m_xc.toNextSelection(); - //assertEquals(m_xc.xmlText(), sExpected[i++]); - } - } - - @Test - public void testPI() throws Exception { - String sInput = Common.XML_FOO_PROCINST; - m_xc = XmlObject.Factory.parse(sInput).newCursor(); - String sXPath1 = "//processing-instruction()"; - String sXPath2 = "//processing-instruction(\"xml-stylesheet\")"; - String sXPath3 = "//processing-instruction(\"xsl\")"; - String sExpected1 = Common.XMLFRAG_BEGINTAG + "<?xml-stylesheet type=\"text/xsl\" xmlns=\"http://openuri.org/shipping/\"?>" + Common.XMLFRAG_ENDTAG; - String sExpected2 = ""; - m_xc.selectPath(fixPath(sXPath1)); - assertEquals(m_xc.getSelectionCount(), 1); - m_xc.toNextSelection(); - assertEquals(m_xc.xmlText(), sExpected1); - - - m_xc.clearSelections(); - m_xc.selectPath(fixPath(sXPath2)); - assertEquals(m_xc.xmlText(), sExpected1); - - m_xc.clearSelections(); - //shouldn't select any nodes - m_xc.selectPath(fixPath(sXPath3)); - assertEquals(m_xc.getSelectionCount(), 0); - } - - @Test - public void testText() throws Exception { - String sInput = "<?xml-stylesheet type=\"text/xsl\" xmlns=\"http://openuri.org/shipping/\"?><br>foo<foo>text</foo></br>"; - m_xc = XmlObject.Factory.parse(sInput).newCursor(); - String sXPath = "//text()"; - String sExpected1 = Common.XMLFRAG_BEGINTAG + "foo" + Common.XMLFRAG_ENDTAG; - String sExpected2 = Common.XMLFRAG_BEGINTAG + "text" + Common.XMLFRAG_ENDTAG; - m_xc.selectPath(sXPath); - assertEquals(m_xc.getSelectionCount(), 2); - m_xc.toNextSelection(); - assertEquals(m_xc.xmlText(), sExpected1); - m_xc.toNextSelection(); - assertEquals(m_xc.xmlText(), sExpected2); - } - - @Test - public void testTextObject() throws Exception { - String sInput = "<?xml-stylesheet type=\"text/xsl\" xmlns=\"http://openuri.org/shipping/\"?><br>foo<foo>text</foo></br>"; - m_xo = XmlObject.Factory.parse(sInput); - String sXPath = "//text()"; - String sExpected1 = Common.XMLFRAG_BEGINTAG + "foo<foo>text</foo>" + Common.XMLFRAG_ENDTAG; - String sExpected2 = Common.XMLFRAG_BEGINTAG + "text" + Common.XMLFRAG_ENDTAG; - XmlObject[] res = m_xo.selectPath(sXPath); - assertEquals(res.length, 2); - assertEquals(res[0].xmlText(), sExpected1); - assertEquals(res[1].xmlText(), sExpected2); - } + @Test + void testTextObject() throws Exception { + String sXml = "<?xml-stylesheet type=\"text/xsl\" xmlns=\"http://openuri.org/shipping/\"?><br>foo<foo>text</foo></br>"; + String sXPath = "//text()"; + String sExpected1 = Common.XMLFRAG_BEGINTAG + "foo<foo>text</foo>" + Common.XMLFRAG_ENDTAG; + String sExpected2 = Common.XMLFRAG_BEGINTAG + "text" + Common.XMLFRAG_ENDTAG; + XmlObject m_xo = obj(sXml); + XmlObject[] res = m_xo.selectPath(sXPath); + assertEquals(res.length, 2); + assertEquals(res[0].xmlText(), sExpected1); + assertEquals(res[1].xmlText(), sExpected2); + } + + @Test + void testNodeEquality() throws Exception { + try (XmlCursor c = cur("<root><book isbn='012345' id='09876'/></root>")) { + c.selectPath("//book[@isbn='012345'] is //book[@id='09876']"); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), c.xmlText()); + } + } + + @Test + void testNodeOrder() throws Exception { + try (XmlCursor c = cur("<root><book isbn='012345'/><book id='09876'/></root>")) { + c.selectPath("//book[@isbn='012345'] << //book[@id='09876']"); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), c.xmlText()); + + c.selectPath("//book[@isbn='012345'] >> //book[@id='09876']"); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), c.xmlText()); + } + } + + @Test + void testParent() throws Exception { + String sXml = "<A><B><C></C></B></A>"; + XmlObject o; + try (XmlCursor c = cur(sXml)) { + c.toFirstContentToken(); + c.toFirstChild(); + c.toFirstChild(); + o = c.getObject(); + } + assertEquals("<C/>", xmlText(o)); + XmlObject[] res = o.selectPath(".."); + assertEquals(1, res.length); + assertEquals("<B><C/></B>", xmlText(res[0])); + } + + private String xmlText(XmlObject o) { + try (XmlCursor c = o.newCursor()) { + return c.xmlText(); + } + } + + @Test + void testParent1() throws Exception { + String sXml = + "<AttributeCertificate " + + "xmlns=\"http://www.eurecom.fr/security/xac#\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + + "<Content>" + + "<Validity>" + + "<ValidityFrom>2005-02-10T11:02:57.590+01:00</ValidityFrom>" + + "<ValidityTo>2006-02-10T11:02:57.590+01:00</ValidityTo>" + + "</Validity></Content></AttributeCertificate>"; + + XmlObject o; + try (XmlCursor c = cur(sXml)) { + c.toFirstContentToken(); + c.toFirstChild(); + c.toFirstChild(); + o = c.getObject(); + } + + QName qn = getName(o); + assertEquals("http://www.eurecom.fr/security/xac#", qn.getNamespaceURI()); + assertEquals("Validity", qn.getLocalPart()); + XmlObject[] res = o.selectPath(".."); + assertEquals(1, res.length); + XmlObject x = res[0]; + qn = getName(x); + assertEquals("http://www.eurecom.fr/security/xac#", qn.getNamespaceURI()); + assertEquals("Content", qn.getLocalPart()); + } + + private QName getName(XmlObject o) { + try (XmlCursor c = o.newCursor()) { + return c.getName(); + } + } } Copied: xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathTestBase.java (from r1897794, xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/BaseXPathTest.java) URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathTestBase.java?p2=xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathTestBase.java&p1=xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/BaseXPathTest.java&r1=1897794&r2=1897795&rev=1897795&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/BaseXPathTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathTestBase.java Sun Feb 6 01:51:55 2022 @@ -14,98 +14,73 @@ */ package xmlcursor.xpath.common; -import org.junit.Before; -import org.junit.Ignore; -import xmlcursor.common.BasicCursorTestCase; +public interface XPathTestBase { -import java.util.HashMap; - -@Ignore("abstract class") -public abstract class BaseXPathTest extends BasicCursorTestCase { - protected final HashMap<String,String[]> testMap = new HashMap<String,String[]>(); - - @Before - public void setUp() throws Exception { - String[] add = new String[]{"//price[position()=0+1]"}; - String[] sub = new String[]{"//price[position()=4-2]"}; - String[] mult = new String[]{"//price[position()=2*1]"}; - String[] div = new String[]{ - "//price[ position()= last() div 2 ]", - "//price[ position()= 4 div 2 ]", - "//price[ position()=10 div 0 ]", - "//price[position()=round(22 div 7)-1]" - }; - - String[] mod = new String[]{ - "//price[ position() mod 2 = 0 ]", - "//price[position() = 5 mod 3]" - }; - - String[] eq = new String[]{"//bar[price=5.00]"}; - - String[] eq_ns = new String[]{"//bar[price=3]"}; - - String[] neq = new String[]{"//bar[price!=3]"}; - - String[] lt = new String[]{"//bar[price < 2 ]"}; - String[] lteq = new String[]{"//bar[price <=2 ]"}; - - String[] gt = new String[]{"//bar[price > 2 ]"}; - - String[] gteq = new String[]{"//bar[price >= 2 ]"}; - - String[] or = new String[]{"//price[text()>3 or @at=\"val1\"]"}; - - String[] and = new String[]{"//price[text()>2 and @at=\"val1\"]"}; - - testMap.put("testAddition", add); - testMap.put("testSubtraction", sub); - testMap.put("testMultiplication", mult); - testMap.put("testDiv", div); - testMap.put("testMod", mod); - testMap.put("testEqual", eq); - testMap.put("testEqualityNodeset", eq_ns); - testMap.put("testNotEqual", neq); - testMap.put("testLessThan", lt); - testMap.put("testLessOrEqual", lteq); - testMap.put("testGreaterThan", gt); - testMap.put("testGreaterOrEqual", gteq); - testMap.put("testOr", or); - testMap.put("testAnd", and); - - testMap.put("testFunctionId", new String[]{ - "id(\"bobdylan\")", - "id(\"foobar\")", - "//child::cd[position()=3]"}); - - testMap.put("testFunctionLast", new String[]{ - "/catalog/cd[last()]"}); - - testMap.put("testFunctionNamespaceURI", new String[]{ - "//*[namespace-uri(.)=\"uri.org\"]"}); - - testMap.put("testFunctionNumber", new String[]{ - "/foo/bar[number(price)+1=4]"}); - - testMap.put("testFunctionRound", new String[]{ - "//bar//*[round(text())=3]"}); - - testMap.put("testFunctionSum", new String[]{ - "//bar[position()=sum(price)-4]"}); - - testMap.put("testFunctionBoolean", new String[]{ - "/foo[boolean(.//@at)]"}); - - testMap.put("testFunctionFalse", new String[]{ - "//foo[boolean(price)=false()]"}); - - testMap.put("testFunctionLang", new String[]{ - "//price[xf:lang(\"en\")=true()]", - "//foo[xf:lang(\"en\")=true()]"}); - - testMap.put("testFunctionTrue", new String[]{ - "//*[xf:boolean(@at)=true()]"}); + static String getQuery(String testName, int testCase) { + switch (testName) { + case "testAddition": + return "//price[position()=0+1]"; + case "testSubtraction": + return "//price[position()=4-2]"; + case "testMultiplication": + return "//price[position()=2*1]"; + case "testDiv": + return new String[]{ + "//price[ position()= last() div 2 ]", + "//price[ position()= 4 div 2 ]", + "//price[ position()=10 div 0 ]", + "//price[position()=round(22 div 7)-1]" + }[testCase]; + case "testMod": + return new String[]{ + "//price[ position() mod 2 = 0 ]", + "//price[position() = 5 mod 3]" + }[testCase]; + case "testEqual": + return "//bar[price=5.00]"; + case "testEqualityNodeset": + return "//bar[price=3]"; + case "testNotEqual": + return "//bar[price!=3]"; + case "testLessThan": + return "//bar[price < 2 ]"; + case "testLessOrEqual": + return "//bar[price <=2 ]"; + case "testGreaterThan": + return "//bar[price > 2 ]"; + case "testGreaterOrEqual": + return "//bar[price >= 2 ]"; + case "testOr": + return "//price[text()>3 or @at=\"val1\"]"; + case "testAnd": + return "//price[text()>2 and @at=\"val1\"]"; + case "testFunctionId": + return new String[]{ + "id(\"bobdylan\")", + "id(\"foobar\")", + "//child::cd[position()=3]"}[testCase]; + case "testFunctionLast": + return "/catalog/cd[last()]"; + case "testFunctionNamespaceURI": + return "//*[namespace-uri(.)=\"uri.org\"]"; + case "testFunctionNumber": + return "/foo/bar[number(price)+1=4]"; + case "testFunctionRound": + return "//bar//*[round(text())=3]"; + case "testFunctionSum": + return "//bar[position()=sum(price)-4]"; + case "testFunctionBoolean": + return "/foo[boolean(.//@at)]"; + case "testFunctionFalse": + return "//foo[boolean(price)=false()]"; + case "testFunctionLang": + return new String[]{ + "//price[xf:lang(\"en\")=true()]", + "//foo[xf:lang(\"en\")=true()]"}[testCase]; + case "testFunctionTrue": + return "//*[xf:boolean(@at)=true()]"; + default: + return null; + } } - - public abstract String getQuery(String testName, int testCase); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
