Modified: xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/IsXXXTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/IsXXXTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/IsXXXTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/IsXXXTest.java Thu Jan 6 17:54:30 2022 @@ -102,25 +102,26 @@ public abstract class IsXXXTest { @Before public void setUp() throws Exception { - XmlCursor cur = XmlObject.Factory.newInstance().newCursor(); - cur.toNextToken(); + try (XmlCursor cur = XmlObject.Factory.newInstance().newCursor()) { + cur.toNextToken(); - cur.insertAttributeWithValue(new QName("foo.org", "at0", "pre"), - "val0"); - cur.insertAttributeWithValue(new QName("", "at1", "pre"), "val1"); - cur.insertNamespace("pre", "foons.bar.org"); - cur.beginElement(new QName("foo.org", "foo", "")); - cur.insertAttribute("localName"); - cur.insertChars("some text"); - cur.toNextToken(); - cur.toNextToken();//end elt - cur.insertProcInst("xml-stylesheet", "http://foobar"); - cur.insertChars("\t"); - cur.insertComment(" some comment "); + cur.insertAttributeWithValue(new QName("foo.org", "at0", "pre"), + "val0"); + cur.insertAttributeWithValue(new QName("", "at1", "pre"), "val1"); + cur.insertNamespace("pre", "foons.bar.org"); + cur.beginElement(new QName("foo.org", "foo", "")); + cur.insertAttribute("localName"); + cur.insertChars("some text"); + cur.toNextToken(); + cur.toNextToken();//end elt + cur.insertProcInst("xml-stylesheet", "http://foobar"); + cur.insertChars("\t"); + cur.insertComment(" some comment "); - cur.toStartDoc(); + cur.toStartDoc(); - m_stream = getStream(cur); + m_stream = getStream(cur); + } } @After
Modified: xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/NamespaceTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/NamespaceTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/NamespaceTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/NamespaceTest.java Thu Jan 6 17:54:30 2022 @@ -249,26 +249,27 @@ public abstract class NamespaceTest { @Before public void setUp() throws Exception { - XmlCursor cur = XmlObject.Factory.newInstance().newCursor(); - cur.toNextToken(); + try (XmlCursor cur = XmlObject.Factory.newInstance().newCursor()) { + cur.toNextToken(); - cur.insertAttributeWithValue(new QName("foo.org", "at0", "pre"), - "val0"); - cur.insertNamespace("pre0", "bea.com"); - - cur.beginElement(new QName("foo.org", "foo", "")); - cur.insertNamespace("pre", "foons.bar.org"); - cur.insertNamespace("pre1", "foons1.bar1.org1"); - cur.insertChars("some text"); - cur.toNextToken(); - cur.toNextToken();//end elt - cur.insertProcInst("xml-stylesheet", "http://foobar"); - cur.insertChars("\t"); - cur.insertComment(" some comment "); + cur.insertAttributeWithValue(new QName("foo.org", "at0", "pre"), + "val0"); + cur.insertNamespace("pre0", "bea.com"); + + cur.beginElement(new QName("foo.org", "foo", "")); + cur.insertNamespace("pre", "foons.bar.org"); + cur.insertNamespace("pre1", "foons1.bar1.org1"); + cur.insertChars("some text"); + cur.toNextToken(); + cur.toNextToken();//end elt + cur.insertProcInst("xml-stylesheet", "http://foobar"); + cur.insertChars("\t"); + cur.insertComment(" some comment "); - cur.toStartDoc(); + cur.toStartDoc(); - m_stream = getStream(cur); + m_stream = getStream(cur); + } } @After Modified: xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/PITest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/PITest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/PITest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/jsr173/common/PITest.java Thu Jan 6 17:54:30 2022 @@ -62,12 +62,13 @@ public abstract class PITest { @Before public void setUp() throws Exception { - XmlCursor cur = XmlObject.Factory.newInstance().newCursor(); - cur.toNextToken(); - cur.insertProcInst("xml-stylesheet", "http://foobar"); - cur.insertElement("foobar"); - cur.toStartDoc(); - m_stream = getStream(cur); + try (XmlCursor cur = XmlObject.Factory.newInstance().newCursor()) { + cur.toNextToken(); + cur.insertProcInst("xml-stylesheet", "http://foobar"); + cur.insertElement("foobar"); + cur.toStartDoc(); + m_stream = getStream(cur); + } } @After Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathCommon.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathCommon.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathCommon.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathCommon.java Thu Jan 6 17:54:30 2022 @@ -96,12 +96,18 @@ public class XPathCommon { } public static void compare(XmlObject rObj, XmlObject rSet) { - check(rObj.newCursor(), rSet.newCursor()); + try (XmlCursor cObj = rObj.newCursor(); + XmlCursor cSet = rSet.newCursor()) { + check(cObj, cSet); + } } public static void compare(XmlObject[] rObj, XmlObject[] rSet) { for (int i=0; i < Math.min(rObj.length,rSet.length); i++) { - check(rObj[i].newCursor(), rSet[i].newCursor()); + try (XmlCursor cObj = rObj[i].newCursor(); + XmlCursor cSet = rSet[i].newCursor()) { + check(cObj, cSet); + } } Assert.assertEquals(rSet.length, rObj.length); @@ -110,7 +116,9 @@ public class XPathCommon { public static void compare(XmlCursor rObj, XmlObject[] rSet) { int curLen = rObj.getSelectionCount(); for (int i=0; i < Math.min(curLen,rSet.length) && rObj.toNextSelection(); i++) { - check(rObj, rSet[i].newCursor()); + try (XmlCursor cSet = rSet[i].newCursor()) { + check(rObj, cSet); + } } Assert.assertEquals(rSet.length, curLen); 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=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionAuxTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionAuxTest.java Thu Jan 6 17:54:30 2022 @@ -46,11 +46,11 @@ public class XPathFunctionAuxTest extend XmlObject[] exXml1 = new XmlObject[]{XmlObject.Factory.parse(ex1R1)}; System.out.println("Test 1: " + ex1Simple); - XmlCursor x1 = xDoc.newCursor(); - x1.selectPath(ex1Simple); - XPathCommon.display(x1); - XPathCommon.compare(x1, exXml1); - x1.dispose(); + try (XmlCursor x1 = xDoc.newCursor()) { + x1.selectPath(ex1Simple); + XPathCommon.display(x1); + XPathCommon.compare(x1, exXml1); + } } @Test @@ -199,44 +199,45 @@ public class XPathFunctionAuxTest extend "<price at=\"val1\">2</price>" + "</bar><bar1>3.00</bar1></foo>"; m_xc = XmlObject.Factory.parse(sXml).newCursor(); - XmlCursor _startPos = m_xc.newCursor(); - String sXPath = "boolean(//foo/text())";//"boolean(//foo/text())"; - m_xc.push(); - m_xc.selectPath(sXPath); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); - m_xc.clearSelections(); - - //need to reset cursor since it's on a bool outside the doc - m_xc.pop(); - //number - m_xc.selectPath("boolean(//price/text())"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); - m_xc.clearSelections(); - - - //number - assertTrue(m_xc.toCursor(_startPos)); - //boolean of Nan is false - m_xc.selectPath("boolean(number(name(//price[position()=last()])))"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); - m_xc.clearSelections(); - - //node-set - m_xc.toCursor(_startPos); - m_xc.selectPath("boolean(//price)"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); - m_xc.clearSelections(); - - m_xc.toCursor(_startPos); - m_xc.selectPath("boolean(//barK)"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); - m_xc.clearSelections(); + try (XmlCursor _startPos = m_xc.newCursor()) { + String sXPath = "boolean(//foo/text())";//"boolean(//foo/text())"; + m_xc.push(); + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); + m_xc.clearSelections(); + + //need to reset cursor since it's on a bool outside the doc + m_xc.pop(); + //number + m_xc.selectPath("boolean(//price/text())"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); + m_xc.clearSelections(); + + + //number + assertTrue(m_xc.toCursor(_startPos)); + //boolean of Nan is false + m_xc.selectPath("boolean(number(name(//price[position()=last()])))"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); + m_xc.clearSelections(); + + //node-set + m_xc.toCursor(_startPos); + m_xc.selectPath("boolean(//price)"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); + m_xc.clearSelections(); + + m_xc.toCursor(_startPos); + m_xc.selectPath("boolean(//barK)"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); + m_xc.clearSelections(); + } } @Test 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=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/common/XPathFunctionTest.java Thu Jan 6 17:54:30 2022 @@ -44,31 +44,30 @@ public abstract class XPathFunctionTest String ex0Simple =getQuery("testFunctionCount",0) ; String ex0Simple1 =getQuery("testFunctionCount",1) ; System.out.println("Test 0: " + ex0Simple); - XmlCursor x0 = XmlObject.Factory.parse( + try (XmlCursor x0 = XmlObject.Factory.parse( "<foo><cd>1</cd><cd>2</cd></foo>") .newCursor(); - 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>" ; - - x01.selectPath(ex0Simple1); - assertEquals(1, x01.getSelectionCount()); - x01.toNextSelection(); - assertEquals(sExpectedResult, x01.xmlText()); - x01.dispose(); - - sExpectedResult = "<xml-fragment>2</xml-fragment>"; - x0.selectPath(ex0Simple); - XPathCommon.display(x0); - assertEquals(1, x0.getSelectionCount()); - x0.toNextSelection(); - //XPathCommon.compare(x0, new XmlObject[]{XmlObject.Factory.parse("<a>foo</a>")}); - assertEquals(sExpectedResult, x0.xmlText()); - x0.dispose(); + 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>" ; + + x01.selectPath(ex0Simple1); + assertEquals(1, x01.getSelectionCount()); + x01.toNextSelection(); + assertEquals(sExpectedResult, x01.xmlText()); + + sExpectedResult = "<xml-fragment>2</xml-fragment>"; + x0.selectPath(ex0Simple); + XPathCommon.display(x0); + assertEquals(1, x0.getSelectionCount()); + x0.toNextSelection(); + //XPathCommon.compare(x0, new XmlObject[]{XmlObject.Factory.parse("<a>foo</a>")}); + assertEquals(sExpectedResult, x0.xmlText()); + } } @@ -98,30 +97,30 @@ public abstract class XPathFunctionTest System.out.println("Test 1: " + ex1Simple); - 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()); - XPathCommon.compare(x1, exXml1); - //assertEquals(ex1R1, x1.xmlText()); - x1.dispose(); + 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()); + XPathCommon.compare(x1, exXml1); + //assertEquals(ex1R1, x1.xmlText()); + } System.out.println("Test 2: " + ex2Simple); - XmlCursor x2 = xDoc.newCursor(); - x2.selectPath(ex2Simple); - XPathCommon.display(x2); - assertFalse(x2.toNextSelection()); - x2.dispose(); + try (XmlCursor x2 = xDoc.newCursor()) { + x2.selectPath(ex2Simple); + XPathCommon.display(x2); + assertFalse(x2.toNextSelection()); + } System.out.println("Test 3: " + ex3Simple); - XmlCursor x3 = xDoc.newCursor(); - x3.selectPath(ex3Simple); - XPathCommon.display(x3); - XPathCommon.compare(x3, exXml3); - x3.dispose(); + try (XmlCursor x3 = xDoc.newCursor()) { + x3.selectPath(ex3Simple); + XPathCommon.display(x3); + XPathCommon.compare(x3, exXml3); + } } /** @@ -143,11 +142,11 @@ public abstract class XPathFunctionTest XmlObject[] exXml1 = new XmlObject[]{XmlObject.Factory.parse(ex1R1)}; System.out.println("Test 1: " + ex1Simple); - XmlCursor x1 = xDoc.newCursor(); - x1.selectPath(ex1Simple); - XPathCommon.display(x1); - XPathCommon.compare(x1, exXml1); - x1.dispose(); + try (XmlCursor x1 = xDoc.newCursor()) { + x1.selectPath(ex1Simple); + XPathCommon.display(x1); + XPathCommon.compare(x1, exXml1); + } } /** Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java Thu Jan 6 17:54:30 2022 @@ -47,23 +47,25 @@ public class ContainerCommentTest { String m_namespaceDeclaration = "declare namespace xq='http://xmlbeans.apache.org/samples/xquery/employees';"; - XmlCursor cursor = employees.newCursor(); - cursor.toNextToken(); + try (XmlCursor cursor = employees.newCursor()) { + cursor.toNextToken(); - cursor.selectPath(m_namespaceDeclaration + "$this//xq:employee"); - if (cursor.getSelectionCount() > 0) { - cursor.toNextSelection(); + cursor.selectPath(m_namespaceDeclaration + "$this//xq:employee"); + if (cursor.getSelectionCount() > 0) { + cursor.toNextSelection(); - String[] names = new String[cursor.getSelectionCount()]; + String[] names = new String[cursor.getSelectionCount()]; - for (int i = 0; i < cursor.getSelectionCount(); i++) { - XmlCursor nameCursor = cursor.newCursor(); - nameCursor.selectPath(m_namespaceDeclaration + - "$this/xq:name/text()"); - nameCursor.toNextSelection(); - names[i] = nameCursor.getTextValue(); - cursor.toNextSelection(); - System.out.println(names[i]); + for (int i = 0; i < cursor.getSelectionCount(); i++) { + try (XmlCursor nameCursor = cursor.newCursor()) { + nameCursor.selectPath(m_namespaceDeclaration + + "$this/xq:name/text()"); + nameCursor.toNextSelection(); + names[i] = nameCursor.getTextValue(); + } + cursor.toNextSelection(); + System.out.println(names[i]); + } } } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTests.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTests.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTests.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTests.java Thu Jan 6 17:54:30 2022 @@ -125,8 +125,7 @@ public class XPathTests { @Test public void testConformance() { - XmlCursor actual = doc.newCursor(); - try { + try (XmlCursor actual = doc.newCursor()) { actual.selectPath(xpath); if (actual.getSelectionCount() == 0) { @@ -136,8 +135,6 @@ public class XPathTests { XmlObject[] expXO = Stream.of(expected).map(XPathTests::parse).toArray(XmlObject[]::new); XPathCommon.compare(actual, expXO); - } finally { - actual.dispose(); } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTestsMisc.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTestsMisc.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTestsMisc.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/checkin/XPathTestsMisc.java Thu Jan 6 17:54:30 2022 @@ -25,10 +25,11 @@ public class XPathTestsMisc { public void testDelete() throws Exception { String query = "*"; - XmlCursor xc = XmlObject.Factory.parse(XPathTests.XML).newCursor(); - xc.selectPath(query); - while (xc.toNextSelection()) { - System.out.println(xc.xmlText()); + try (XmlCursor xc = XmlObject.Factory.parse(XPathTests.XML).newCursor()) { + xc.selectPath(query); + while (xc.toNextSelection()) { + System.out.println(xc.xmlText()); + } } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/DeclareNamespaceTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/DeclareNamespaceTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/DeclareNamespaceTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/DeclareNamespaceTest.java Thu Jan 6 17:54:30 2022 @@ -37,18 +37,21 @@ public class DeclareNamespaceTest { //"for $e in ./a return <doc>{ $e } </doc>" */ String query = "declare namespace ack='abc'; .//@ack:attr"; - XmlCursor s1 = s.newCursor(); - s1.selectPath(query); - assertEquals(1, s1.getSelectionCount()); - s1.toNextSelection(); - assertEquals(s1.xmlText(), - "<xml-fragment ack:attr=\"val1\" xmlns:ack=\"abc\"/>"); + try (XmlCursor s1 = s.newCursor()) { + s1.selectPath(query); + assertEquals(1, s1.getSelectionCount()); + s1.toNextSelection(); + assertEquals(s1.xmlText(), + "<xml-fragment ack:attr=\"val1\" xmlns:ack=\"abc\"/>"); + } res = s.execQuery(query); - XmlCursor c1 = s.newCursor(); - c1.toFirstContentToken(); + XmlObject o; + try (XmlCursor c1 = s.newCursor()) { + c1.toFirstContentToken(); + o = c1.getObject(); + } - XmlObject o = c1.getObject(); assertNotSame(o, res[0]); assertEquals(res[0].xmlText(), "<xml-fragment ack:attr=\"val1\" xmlns:ack=\"abc\"/>"); @@ -69,12 +72,12 @@ public class DeclareNamespaceTest { assertEquals( s1.xmlText(),"<b xmlns=\"abc\">bar</b>"); */ res = s.execQuery(query); - XmlCursor c1 = s.newCursor(); - c1.toFirstContentToken(); - - XmlObject o = c1.getObject(); - assertNotSame(o, res[0]); - assertEquals(res[0].xmlText(), "<abc:b xmlns:abc=\"abc\">bar</abc:b>"); + try (XmlCursor c1 = s.newCursor()) { + c1.toFirstContentToken(); + XmlObject o = c1.getObject(); + assertNotSame(o, res[0]); + assertEquals(res[0].xmlText(), "<abc:b xmlns:abc=\"abc\">bar</abc:b>"); + } } @Test @@ -120,22 +123,24 @@ public class DeclareNamespaceTest { @Test public void testSequenceIntersect() throws Exception { - XmlCursor o = XmlObject.Factory.parse("<a><b>1</b>1</a>").newCursor(); - o.selectPath("//b intersect //b"); - assertEquals(1, o.getSelectionCount()); - o.toNextSelection(); - assertEquals("<b>1</b>", o.xmlText()); + try (XmlCursor o = XmlObject.Factory.parse("<a><b>1</b>1</a>").newCursor()) { + o.selectPath("//b intersect //b"); + assertEquals(1, o.getSelectionCount()); + o.toNextSelection(); + assertEquals("<b>1</b>", o.xmlText()); + } } @Test public void testSequenceExcept() throws Exception { - XmlCursor o = XmlObject.Factory.parse("<a><b>1</b>1</a>").newCursor(); - o.selectPath("/a except /a"); - assertEquals(0, o.getSelectionCount()); - o.selectPath("//* except //b"); - assertEquals(1, o.getSelectionCount()); - o.toNextSelection(); - assertEquals("<a><b>1</b>1</a>", o.xmlText()); + try (XmlCursor o = XmlObject.Factory.parse("<a><b>1</b>1</a>").newCursor()) { + o.selectPath("/a except /a"); + assertEquals(0, o.getSelectionCount()); + o.selectPath("//* except //b"); + assertEquals(1, o.getSelectionCount()); + o.toNextSelection(); + assertEquals("<a><b>1</b>1</a>", o.xmlText()); + } } //If an operand of union, intersect, or except @@ -143,8 +148,9 @@ public class DeclareNamespaceTest { @Test(expected = RuntimeException.class) public void testSequenceTypeError() throws XmlException { - XmlCursor o = XmlObject.Factory.parse("<a/>").newCursor(); - o.selectPath("(0 to 4) except (0 to 4)"); - o.toNextSelection(); + try (XmlCursor o = XmlObject.Factory.parse("<a/>").newCursor()) { + o.selectPath("(0 to 4) except (0 to 4)"); + o.toNextSelection(); + } } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/NodeCopyTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/NodeCopyTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/NodeCopyTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/NodeCopyTest.java Thu Jan 6 17:54:30 2022 @@ -35,14 +35,18 @@ public class NodeCopyTest { assertEquals( res[0].xmlText(),"<xml-fragment ack:attr=\"val1\" xmlns:ack=\"abc\">foo<b>bar</b></xml-fragment>"); //"for $e in ./a return <doc>{ $e } </doc>" */ - XmlCursor s1 = s.newCursor().execQuery("./a"); - assertEquals("<a ack:attr=\"val1\" xmlns:ack=\"abc\">foo<b>bar</b></a>", s1.xmlText()); + try (XmlCursor c = s.newCursor(); + XmlCursor s1 = c.execQuery("./a")) { + assertEquals("<a ack:attr=\"val1\" xmlns:ack=\"abc\">foo<b>bar</b></a>", s1.xmlText()); + } res = s.execQuery("./a"); - XmlCursor c1 = s.newCursor(); - c1.toFirstContentToken(); + XmlObject o; + try (XmlCursor c1 = s.newCursor()) { + c1.toFirstContentToken(); + o = c1.getObject(); + } - XmlObject o = c1.getObject(); assertNotSame(o, res[0]); assertEquals("<a ack:attr=\"val1\" xmlns:ack=\"abc\">foo<b>bar</b></a>", res[0].xmlText()); } @@ -87,23 +91,21 @@ public class NodeCopyTest { @Test public void testDeleteMe() throws Exception { XmlObject t = XmlObject.Factory.parse("<a><b/><b/></a>"); - XmlCursor cursor = - t.newCursor(); - System.out.println(cursor.getObject()); - // use xpath to select elements - cursor.selectPath("*/*"); - - System.out.println("cnt " + cursor.getSelectionCount()); - // iterate over the selection - while (cursor.toNextSelection()) { - // two views of the same data: - // move back and forth between XmlObject <-> XmlCursor - XmlObject trans = cursor.getObject(); - - System.out.println("Trans " + trans.xmlText()); - System.out.println("xmlText " + cursor.xmlText()); - + try (XmlCursor cursor = t.newCursor()) { + System.out.println(cursor.getObject()); + // use xpath to select elements + cursor.selectPath("*/*"); + + System.out.println("cnt " + cursor.getSelectionCount()); + // iterate over the selection + while (cursor.toNextSelection()) { + // two views of the same data: + // move back and forth between XmlObject <-> XmlCursor + XmlObject trans = cursor.getObject(); + + System.out.println("Trans " + trans.xmlText()); + System.out.println("xmlText " + cursor.xmlText()); + } } - } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathExpressionTestImpl.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathExpressionTestImpl.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathExpressionTestImpl.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathExpressionTestImpl.java Thu Jan 6 17:54:30 2022 @@ -81,80 +81,85 @@ public class XPathExpressionTestImpl ext "<xml-fragment>Suciu</xml-fragment>", "<title>Data on the Web</title>" }; - XmlCursor c = XmlObject.Factory.parse(sXml).newCursor(); - c.selectPath(query); - verifySelection(c, exp); + try (XmlCursor c = XmlObject.Factory.parse(sXml).newCursor()) { + c.selectPath(query); + verifySelection(c, exp); + } } @Test public void testFor_1() throws Exception { - XmlCursor c = XmlObject.Factory.parse("<a/>").newCursor(); - String query = "for $i in (10, 20),\n" + - " $j in (1, 2)\n" + - "return ($i + $j)"; - c.selectPath(query); - String[] expected = new String[] { - Common.wrapInXmlFrag("11"), - Common.wrapInXmlFrag("12"), - Common.wrapInXmlFrag("21"), - Common.wrapInXmlFrag("22") - }; - verifySelection(c, expected); + try (XmlCursor c = XmlObject.Factory.parse("<a/>").newCursor()) { + String query = "for $i in (10, 20),\n" + + " $j in (1, 2)\n" + + "return ($i + $j)"; + c.selectPath(query); + String[] expected = new String[] { + Common.wrapInXmlFrag("11"), + Common.wrapInXmlFrag("12"), + Common.wrapInXmlFrag("21"), + Common.wrapInXmlFrag("22") + }; + verifySelection(c, expected); + } } @Test public void testFor_2() throws Exception { - XmlCursor c = XmlObject.Factory.parse("<a/>").newCursor(); - 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()); + try (XmlCursor c = XmlObject.Factory.parse("<a/>").newCursor()) { + 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 public void testIf() throws Exception { - XmlCursor c = XmlObject.Factory.parse("<root>" + - "<book price='20'>Pooh</book>" + - "<cd price='25'>Pooh</cd>" + - "<book price='50'>Maid</book>" + - "<cd price='25'>Maid</cd>" + - "</root>").newCursor(); - String query = "if (//book[1]/@price) " + - " then //book[1] " + - " else 0"; - c.selectPath(query); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals("<book price=\"20\">Pooh</book>", c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse("<root>" + + "<book price='20'>Pooh</book>" + + "<cd price='25'>Pooh</cd>" + + "<book price='50'>Maid</book>" + + "<cd price='25'>Maid</cd>" + + "</root>").newCursor()) { + String query = "if (//book[1]/@price) " + + " then //book[1] " + + " else 0"; + c.selectPath(query); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals("<book price=\"20\">Pooh</book>", c.xmlText()); - query = "for $b1 in //book, $b2 in //cd " + - "return " + - "if ( $b1/@price < $b2/@price )" + - " then $b1" + - " else $b2"; - c.selectPath(query); - 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()); + query = "for $b1 in //book, $b2 in //cd " + + "return " + + "if ( $b1/@price < $b2/@price )" + + " then $b1" + + " else $b2"; + c.selectPath(query); + 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 public void testQuantifiedExpression() throws Exception { - XmlCursor c = XmlObject.Factory.parse("<root></root>").newCursor(); - String query = - "some $x in (1, 2, 3), $y in (2, 3, 4) " + - "satisfies $x + $y = 4"; - c.selectPath(query); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals("<xml-fragment>true</xml-fragment>", c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse("<root></root>").newCursor()) { + String query = + "some $x in (1, 2, 3), $y in (2, 3, 4) " + + "satisfies $x + $y = 4"; + 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/complex/detailed/XPathFunctionAuxTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathFunctionAuxTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathFunctionAuxTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathFunctionAuxTest.java Thu Jan 6 17:54:30 2022 @@ -47,11 +47,11 @@ public class XPathFunctionAuxTest extend XmlObject[] exXml1 = new XmlObject[]{XmlObject.Factory.parse(ex1R1)}; System.out.println("Test 1: " + ex1Simple); - XmlCursor x1 = xDoc.newCursor(); - x1.selectPath(ex1Simple); - XPathCommon.display(x1); - XPathCommon.compare(x1, exXml1); - x1.dispose(); + try (XmlCursor x1 = xDoc.newCursor()) { + x1.selectPath(ex1Simple); + XPathCommon.display(x1); + XPathCommon.compare(x1, exXml1); + } } @Test @@ -201,45 +201,46 @@ public class XPathFunctionAuxTest extend "<price at=\"val1\">2</price>" + "</bar><bar1>3.00</bar1></foo>"; m_xc = XmlObject.Factory.parse(sXml).newCursor(); - XmlCursor _startPos = m_xc.newCursor(); - String sXPath = "boolean(//foo/text())";//"boolean(//foo/text())"; - m_xc.push(); - m_xc.selectPath(sXPath); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("false"), - m_xc.xmlText()); - m_xc.clearSelections(); - - //need to reset cursor since it's on a bool outside the doc - m_xc.pop(); - //number - m_xc.selectPath("boolean(//price/text())"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); - m_xc.clearSelections(); - - - //number - assertTrue(m_xc.toCursor(_startPos)); - //boolean of Nan is false - m_xc.selectPath("boolean(number(name(//price[position()=last()])))"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); - m_xc.clearSelections(); - - //node-set - m_xc.toCursor(_startPos); - m_xc.selectPath("boolean(//price)"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); - m_xc.clearSelections(); - - m_xc.toCursor(_startPos); - m_xc.selectPath("boolean(//barK)"); - m_xc.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); - m_xc.clearSelections(); + try (XmlCursor _startPos = m_xc.newCursor()) { + String sXPath = "boolean(//foo/text())";//"boolean(//foo/text())"; + m_xc.push(); + m_xc.selectPath(sXPath); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), + m_xc.xmlText()); + m_xc.clearSelections(); + + //need to reset cursor since it's on a bool outside the doc + m_xc.pop(); + //number + m_xc.selectPath("boolean(//price/text())"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); + m_xc.clearSelections(); + + + //number + assertTrue(m_xc.toCursor(_startPos)); + //boolean of Nan is false + m_xc.selectPath("boolean(number(name(//price[position()=last()])))"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); + m_xc.clearSelections(); + + //node-set + m_xc.toCursor(_startPos); + m_xc.selectPath("boolean(//price)"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), m_xc.xmlText()); + m_xc.clearSelections(); + + m_xc.toCursor(_startPos); + m_xc.selectPath("boolean(//barK)"); + m_xc.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("false"), m_xc.xmlText()); + m_xc.clearSelections(); + } } @Test Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathNodeTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathNodeTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathNodeTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathNodeTest.java Thu Jan 6 17:54:30 2022 @@ -26,44 +26,53 @@ import static org.junit.Assert.assertEqu public class XPathNodeTest { @Test public void testNodeEquality() throws Exception { - XmlCursor c = XmlObject.Factory.parse( - "<root><book isbn='012345' id='09876'/></root>") - .newCursor(); - c.selectPath("//book[@isbn='012345'] is //book[@id='09876']"); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(Common.wrapInXmlFrag("true"), c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse( + "<root><book isbn='012345' id='09876'/></root>") + .newCursor()) { + c.selectPath("//book[@isbn='012345'] is //book[@id='09876']"); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(Common.wrapInXmlFrag("true"), c.xmlText()); + } } @Test public void testNodeOrder() throws Exception { - XmlCursor c = XmlObject.Factory.parse( - "<root><book isbn='012345'/><book id='09876'/></root>") - .newCursor(); - 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()); + try (XmlCursor c = XmlObject.Factory.parse( + "<root><book isbn='012345'/><book id='09876'/></root>") + .newCursor()) { + 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 public void testParent() throws Exception { String input = "<A><B><C></C></B></A>"; XmlObject o; - XmlCursor c = XmlObject.Factory.parse(input).newCursor(); - c.toFirstContentToken(); - c.toFirstChild(); - c.toFirstChild(); - o = c.getObject(); - assertEquals("<C/>", o.newCursor().xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(input).newCursor()) { + 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>", res[0].newCursor().xmlText()); + assertEquals("<B><C/></B>", xmlText(res[0])); + } + + private String xmlText(XmlObject o) { + try (XmlCursor c = o.newCursor()) { + return c.xmlText(); + } } @Test @@ -79,18 +88,27 @@ public class XPathNodeTest { "</Validity></Content></AttributeCertificate>"; XmlObject o; - XmlCursor c = XmlObject.Factory.parse(input).newCursor(); - c.toFirstContentToken(); - c.toFirstChild(); - c.toFirstChild(); - o = c.getObject(); - QName qn = o.newCursor().getName(); + try (XmlCursor c = XmlObject.Factory.parse(input).newCursor()) { + 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); - qn = res[0].newCursor().getName(); + 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(); + } + } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/complex/detailed/XPathTest.java Thu Jan 6 17:54:30 2022 @@ -204,8 +204,7 @@ public class XPathTest extends BasicCurs @Test public void zvonExample() throws IOException, XmlException { XmlObject xDoc = XmlObject.Factory.parse(JarUtil.getResourceFromJar("xbean/xmlcursor/xpath/zvon"+dataset+".xml")); - XmlCursor x1 = xDoc.newCursor(); - try { + try (XmlCursor x1 = xDoc.newCursor()) { x1.selectPath(xpath); XmlObject[] exp = new XmlObject[expected.length]; @@ -214,8 +213,6 @@ public class XPathTest extends BasicCurs } XPathCommon.compare(x1, exp); - } finally { - x1.dispose(); } } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/AxesTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/AxesTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/AxesTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/AxesTest.java Thu Jan 6 17:54:30 2022 @@ -56,19 +56,21 @@ public class AxesTest { String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); - - sQuery1 = "$this/foo/child::bar"; - c.clearSelections(); - c.toStartDoc(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + + sQuery1 = "$this/foo/child::bar"; + c.clearSelections(); + c.toStartDoc(); + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @@ -77,12 +79,13 @@ public class AxesTest { String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @@ -91,19 +94,23 @@ public class AxesTest { String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test public void testChildAxisDNE() throws XmlException { String sQuery1 = "$this/foo/./baz"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - c.selectPath(sQuery1, options); - assertEquals(0, c.getSelectionCount()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + c.selectPath(sQuery1, options); + assertEquals(0, c.getSelectionCount()); + } } @Test @@ -112,15 +119,16 @@ public class AxesTest { String sQuery1 = "./descendant::foo"; String sExpected = "<foo at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</foo>"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); - - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); + + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @@ -128,14 +136,15 @@ public class AxesTest { String sQuery1 = ".//foo"; String sExpected = "<foo at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</foo>"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @@ -144,236 +153,249 @@ public class AxesTest { String sQuery1 = "$this/descendant::foo/."; String sExpected = "<foo at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</foo>"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testDescAxisDNE() throws XmlException { String sQuery1 = "$this/descendant::baz"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(0, c.getSelectionCount()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1, options); + assertEquals(0, c.getSelectionCount()); + } } @Test public void testChildAttribute() throws XmlException { String sExpected = "<xml-fragment at0=\"val0\" xmlns:pre=\"http://uri.com\"/>"; String sQuery1 = "$this/foo/bar/attribute::at0"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test public void testChildAttributeAbbrev() throws XmlException { String sExpected = "<xml-fragment at0=\"val0\" xmlns:pre=\"http://uri.com\"/>"; String sQuery1 = "$this/foo/bar/@at0"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test public void testDescAttribute() throws XmlException { String sExpected = "<xml-fragment at0=\"val0\" xmlns:pre=\"http://uri.com\"/>"; String sQuery1 = "$this//attribute::at0"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testDescendantOrSelfAxis() throws XmlException { - String sQuery1 = "./descendant-or-self::foo"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - String[] sExpected = { - c.xmlText() - , "<foo at0=\"val0\" xmlns:pre=\"http://uri.com\">" + - "<pre:baz baz:at0=\"val1\"" + - " xmlns:baz=\"http://uri\"/>txt child</foo>" - }; - - - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); - - c.selectPath(sQuery1, options); - assertEquals(2, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected[0], c.xmlText()); - c.toNextSelection(); - assertEquals(sExpected[1], c.xmlText()); - - } - - @Test - @Ignore - public void testDescendantOrSelfAxisDot() throws XmlException { - String sQuery1 = "./descendant-or-self::foo"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - String[] sExpected = new String[] - { + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + String[] sExpected = { c.xmlText() , "<foo at0=\"val0\" xmlns:pre=\"http://uri.com\">" + "<pre:baz baz:at0=\"val1\"" + " xmlns:baz=\"http://uri\"/>txt child</foo>" }; + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); + c.selectPath(sQuery1, options); + assertEquals(2, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected[0], c.xmlText()); + c.toNextSelection(); + assertEquals(sExpected[1], c.xmlText()); + } + } + + @Test + @Ignore + public void testDescendantOrSelfAxisDot() throws XmlException { + String sQuery1 = "./descendant-or-self::foo"; - c.selectPath(sQuery1, options); - assertEquals(2, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected[0], c.xmlText()); - c.toNextSelection(); - assertEquals(sExpected[1], c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + String[] sExpected = new String[] + { + c.xmlText() + , "<foo at0=\"val0\" xmlns:pre=\"http://uri.com\">" + + "<pre:baz baz:at0=\"val1\"" + + " xmlns:baz=\"http://uri\"/>txt child</foo>" + }; + + + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1, options); + + c.selectPath(sQuery1, options); + assertEquals(2, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected[0], c.xmlText()); + c.toNextSelection(); + assertEquals(sExpected[1], c.xmlText()); + } } @Test @Ignore public void testDescendantOrSelfAxisDNE() throws XmlException { - String sQuery1 = "$this/descendant-or-self::baz"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(0, c.getSelectionCount()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1, options); + assertEquals(0, c.getSelectionCount()); + } } @Test @Ignore public void testSelfAxis() throws XmlException { String sQuery1 = "$this/self::foo"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - String sExpected = - c.xmlText(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + String sExpected = c.xmlText(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); + + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test public void testSelfAxisAbbrev() throws XmlException { String sQuery1 = "."; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = - c.xmlText(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = c.xmlText(); + + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testSelfAxisDot() throws XmlException { - String sQuery1 = "./self::foo"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - String sExpected = - c.xmlText(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + String sExpected = c.xmlText(); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); + + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testSelfAxisDNE() throws XmlException { - String sQuery1 = "$this/self::baz"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(0, c.getSelectionCount()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1, options); + assertEquals(0, c.getSelectionCount()); + } } @Test @Ignore public void testNamespaceAxis() throws XmlException { - String sQuery1 = "$this/namespace::http://uri.com"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - String sExpected = - c.xmlText(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals(XmlCursor.TokenType.TEXT, c.toNextToken()); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); - - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + String sExpected = c.xmlText(); + + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals(XmlCursor.TokenType.TEXT, c.toNextToken()); + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); + + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testNamespaceAxisDot() throws XmlException { - String sQuery1 = "./*/namespace::http://uri.com"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - String sExpected = - c.xmlText(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + String sExpected = c.xmlText(); + + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); - c.selectPath(sQuery1, options); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + c.selectPath(sQuery1, options); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testNamespaceAxisDNE() throws XmlException { - String sQuery1 = "$this/namespace::*"; - XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals(XmlCursor.TokenType.TEXT, c.toNextToken()); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - //to namespace - assertEquals(XmlCursor.TokenType.NAMESPACE, c.toNextToken()); - c.selectPath(sQuery1, options); - assertEquals(0, c.getSelectionCount()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlDesc).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals(XmlCursor.TokenType.TEXT, c.toNextToken()); + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + //to namespace + assertEquals(XmlCursor.TokenType.NAMESPACE, c.toNextToken()); + c.selectPath(sQuery1, options); + assertEquals(0, c.getSelectionCount()); + } } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/NodeTest.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/NodeTest.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/NodeTest.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xpath/xbean_xpath/detailed/NodeTest.java Thu Jan 6 17:54:30 2022 @@ -51,71 +51,75 @@ public class NodeTest { @Test public void testNameTestStar() throws XmlException { String sQuery1 = "./*"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = c.xmlText(); - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = c.xmlText(); + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test public void testNameTestNCName() throws XmlException { String sQuery1 = "$this//*"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = "<pre:baz baz:at0=\"val1\" " + - "xmlns:baz=\"http://uri\" xmlns:pre=\"http://uri.com\"/>"; - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.toNextToken(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("bar", c.getName().getLocalPart()); - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = "<pre:baz baz:at0=\"val1\" " + + "xmlns:baz=\"http://uri\" xmlns:pre=\"http://uri.com\"/>"; + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.toNextToken(); + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("bar", c.getName().getLocalPart()); + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test public void testNameTestQName_1() throws XmlException { String sQuery1 = "declare namespace pre=\"http://uri.com\"; $this//pre:*"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = - "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\" xmlns:pre=\"http://uri.com\"/>"; - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - assertEquals("foo", c.getName().getLocalPart()); - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\" xmlns:pre=\"http://uri.com\"/>"; + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + assertEquals("foo", c.getName().getLocalPart()); + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } //test a QName that DNE @Test public void testNameTestQName_2() throws XmlException { String sQuery1 = "declare namespace pre=\"http://uri\"; $this//pre:baz"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1); - assertEquals(0, c.getSelectionCount()); - + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1); + assertEquals(0, c.getSelectionCount()); + } } @Test public void testNameTestQName_3() throws XmlException { String sQuery1 = "$this//bar"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + - "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @@ -127,13 +131,15 @@ public class NodeTest { @Test public void testNodeTypeNodeAbbrev() throws XmlException { String sQuery1 = "$this/foo/*"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + - "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } /** @@ -143,59 +149,65 @@ public class NodeTest { @Ignore public void testNodeTypeNode() throws XmlException { String sQuery1 = "$this/foo/node()"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + - "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = "<bar at0=\"val0\" xmlns:pre=\"http://uri.com\">" + + "<pre:baz baz:at0=\"val1\" xmlns:baz=\"http://uri\"/>txt child</bar>"; + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testNodeTypePI() throws XmlException { - - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = "<foo><?xml-stylesheet target=\"http://someuri\"?></foo>"; - String sQuery = "./foo/processing-instruction()"; - c.selectPath(sQuery); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = "<foo><?xml-stylesheet target=\"http://someuri\"?></foo>"; + String sQuery = "./foo/processing-instruction()"; + c.selectPath(sQuery); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testNodeTypeText() throws XmlException { String sQuery1 = "$this//text()"; - XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor(); - String sExpected = " "; - assertEquals(XmlCursor.TokenType.START, c.toNextToken()); - c.selectPath(sQuery1); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + + try (XmlCursor c = XmlObject.Factory.parse(sXmlChild).newCursor()) { + String sExpected = " "; + assertEquals(XmlCursor.TokenType.START, c.toNextToken()); + c.selectPath(sQuery1); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testPI() throws XmlException { - XmlCursor c = XmlObject.Factory.parse(sXmlPI).newCursor(); - String sExpected = "<?xml-stylesheet target=\"http://someuri\"?>"; - String sQuery = "./foo/processing-instruction('xml-stylesheet')"; - c.selectPath(sQuery); - assertEquals(1, c.getSelectionCount()); - c.toNextSelection(); - assertEquals(sExpected, c.xmlText()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlPI).newCursor()) { + String sExpected = "<?xml-stylesheet target=\"http://someuri\"?>"; + String sQuery = "./foo/processing-instruction('xml-stylesheet')"; + c.selectPath(sQuery); + assertEquals(1, c.getSelectionCount()); + c.toNextSelection(); + assertEquals(sExpected, c.xmlText()); + } } @Test @Ignore public void testPIDNE() throws XmlException { - XmlCursor c = XmlObject.Factory.parse(sXmlPI).newCursor(); - String sQuery = "./foo/processing-instruction('stylesheet')"; - c.selectPath(sQuery); - assertEquals(0, c.getSelectionCount()); + try (XmlCursor c = XmlObject.Factory.parse(sXmlPI).newCursor()) { + String sQuery = "./foo/processing-instruction('stylesheet')"; + c.selectPath(sQuery); + assertEquals(0, c.getSelectionCount()); + } } } Modified: xmlbeans/trunk/src/test/java/xmlcursor/xquery/detailed/StoreTestsXqrl.java URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/xmlcursor/xquery/detailed/StoreTestsXqrl.java?rev=1896764&r1=1896763&r2=1896764&view=diff ============================================================================== --- xmlbeans/trunk/src/test/java/xmlcursor/xquery/detailed/StoreTestsXqrl.java (original) +++ xmlbeans/trunk/src/test/java/xmlcursor/xquery/detailed/StoreTestsXqrl.java Thu Jan 6 17:54:30 2022 @@ -26,10 +26,11 @@ import static org.junit.Assert.assertTru //Used to be a checkin public class StoreTestsXqrl { private void doTokenTest(String xml) throws Exception { - XmlCursor c = XmlObject.Factory.parse(xml).newCursor(); - //String s = c.execQuery( "$this" ).xmlText(); - String s = c.execQuery(".").xmlText(); - assertEquals(s, xml); + try (XmlCursor c = XmlObject.Factory.parse(xml).newCursor(); + XmlCursor cq = c.execQuery(".")) { + String s = cq.xmlText(); + assertEquals(s, xml); + } } private void doSaveTest(String xml) throws Exception { @@ -53,21 +54,21 @@ public class StoreTestsXqrl { XmlObject x = XmlObject.Factory.parse("<foo xmlns:a='a.com'><bar xmlns:a='b.com'/></foo>"); - XmlCursor c = x.newCursor(); + try (XmlCursor c = x.newCursor()) { + c.toFirstChild(); + c.toFirstChild(); - c.toFirstChild(); - c.toFirstChild(); - - assertEquals("<bar xmlns:a=\"b.com\"/>", c.xmlText()); + assertEquals("<bar xmlns:a=\"b.com\"/>", c.xmlText()); + } x = XmlObject.Factory.parse("<foo xmlns:a='a.com'><bar/></foo>"); - c = x.newCursor(); - - c.toFirstChild(); - c.toFirstChild(); + try (XmlCursor c = x.newCursor()) { + c.toFirstChild(); + c.toFirstChild(); - assertEquals("<bar xmlns:a=\"a.com\"/>", c.xmlText()); + assertEquals("<bar xmlns:a=\"a.com\"/>", c.xmlText()); + } } public void testTextFrag(String actual, String expected) { @@ -88,24 +89,26 @@ public class StoreTestsXqrl { @Test public void testXQuery() throws Exception { - XmlCursor c = - XmlObject.Factory.parse( + try (XmlCursor c = XmlObject.Factory.parse( "<foo><bar>1</bar><bar>2</bar></foo>").newCursor(); + XmlCursor cq = c.execQuery("for $b in //bar order by ($b) descending return $b")) { + String s = cq.xmlText(); - String s = - c.execQuery("for $b in //bar order by ($b) " + - "descending return $b").xmlText(); - - testTextFrag(s, "<bar>2</bar><bar>1</bar>"); - - c = XmlObject.Factory.parse("<foo></foo>").newCursor(); - c.toNextToken(); - c.toNextToken(); - c.insertElement("boo", "boo.com"); - c.toStartDoc(); + testTextFrag(s, "<bar>2</bar><bar>1</bar>"); + } - assertEquals("<foo><boo:boo xmlns:boo=\"boo.com\"/></foo>", - c.execQuery(".").xmlText()); + try (XmlCursor c = XmlObject.Factory.parse("<foo></foo>").newCursor()) { + c.toNextToken(); + c.toNextToken(); + c.insertElement("boo", "boo.com"); + c.toStartDoc(); + + try (XmlCursor cq = c.execQuery(".")) { + String s = cq.xmlText(); + assertEquals("<foo><boo:boo xmlns:boo=\"boo.com\"/></foo>", + s); + } + } } @Test @@ -114,63 +117,63 @@ public class StoreTestsXqrl { XmlObject.Factory.parse( "<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>"); - XmlCursor c = x.newCursor(); - - c.selectPath("//bar"); + try (XmlCursor c = x.newCursor()) { + c.selectPath("//bar"); - assertTrue(c.toNextSelection()); - assertEquals("<bar>1</bar>", c.xmlText()); + assertTrue(c.toNextSelection()); + assertEquals("<bar>1</bar>", c.xmlText()); - assertTrue(c.toNextSelection()); - assertEquals("<bar>2</bar>", c.xmlText()); + assertTrue(c.toNextSelection()); + assertEquals("<bar>2</bar>", c.xmlText()); - assertTrue(c.toNextSelection()); - assertEquals("<bar>3</bar>", c.xmlText()); + assertTrue(c.toNextSelection()); + assertEquals("<bar>3</bar>", c.xmlText()); - assertTrue(!c.toNextSelection()); + assertTrue(!c.toNextSelection()); - x = - XmlObject.Factory.parse( - "<foo><bar x='1'/><bar x='2'/><bar x='3'/></foo>"); - - c = x.newCursor(); + x = + XmlObject.Factory.parse( + "<foo><bar x='1'/><bar x='2'/><bar x='3'/></foo>"); + } - //c.selectPath( "$this//@x" ); - c.selectPath(".//@x"); + try (XmlCursor c = x.newCursor()) { + //c.selectPath( "$this//@x" ); + c.selectPath(".//@x"); - assertTrue(c.toNextSelection()); - assertTrue(c.currentTokenType().isAttr()); - assertEquals("1", c.getTextValue()); + assertTrue(c.toNextSelection()); + assertTrue(c.currentTokenType().isAttr()); + assertEquals("1", c.getTextValue()); - assertTrue(c.toNextSelection()); - assertTrue(c.currentTokenType().isAttr()); - assertEquals("2", c.getTextValue()); + assertTrue(c.toNextSelection()); + assertTrue(c.currentTokenType().isAttr()); + assertEquals("2", c.getTextValue()); - assertTrue(c.toNextSelection()); - assertTrue(c.currentTokenType().isAttr()); - assertEquals("3", c.getTextValue()); + assertTrue(c.toNextSelection()); + assertTrue(c.currentTokenType().isAttr()); + assertEquals("3", c.getTextValue()); - assertTrue(!c.toNextSelection()); + assertTrue(!c.toNextSelection()); - x = XmlObject.Factory.parse( - "<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>"); - - c = x.newCursor(); + x = XmlObject.Factory.parse( + "<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>"); + } - c.selectPath("//text()"); + try (XmlCursor c = x.newCursor()) { + c.selectPath("//text()"); - assertTrue(c.toNextSelection()); - assertTrue(c.currentTokenType().isText()); - assertEquals("1", c.getChars()); + assertTrue(c.toNextSelection()); + assertTrue(c.currentTokenType().isText()); + assertEquals("1", c.getChars()); - assertTrue(c.toNextSelection()); - assertTrue(c.currentTokenType().isText()); - assertEquals("2", c.getChars()); + assertTrue(c.toNextSelection()); + assertTrue(c.currentTokenType().isText()); + assertEquals("2", c.getChars()); - assertTrue(c.toNextSelection()); - assertTrue(c.currentTokenType().isText()); - assertEquals("3", c.getChars()); + assertTrue(c.toNextSelection()); + assertTrue(c.currentTokenType().isText()); + assertEquals("3", c.getChars()); - assertTrue(!c.toNextSelection()); + assertTrue(!c.toNextSelection()); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
