Search in sources :

Example 41 with XdmNodeReadTrx

use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.

the class ChildAxisTest method testIterate.

@Test
public void testIterate() throws SirixException {
    final XdmNodeReadTrx rtx = holder.getReader();
    rtx.moveTo(1L);
    AbsAxisTest.testIAxisConventions(new ChildAxis(rtx), new long[] { 4L, 5L, 8L, 9L, 13L });
    rtx.moveTo(5L);
    AbsAxisTest.testIAxisConventions(new ChildAxis(rtx), new long[] { 6L, 7L });
    rtx.moveTo(13L);
    AbsAxisTest.testIAxisConventions(new ChildAxis(rtx), new long[] {});
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Test(org.junit.Test)

Example 42 with XdmNodeReadTrx

use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.

the class DescendantAxisTest method testIterate.

@Test
public void testIterate() throws SirixException {
    final XdmNodeReadTrx rtx = holder.getReader();
    rtx.moveToDocumentRoot();
    AbsAxisTest.testIAxisConventions(new DescendantAxis(rtx), new long[] { 1L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L });
    new IteratorTester<Long>(ITERATIONS, IteratorFeature.UNMODIFIABLE, ImmutableList.of(1L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L), null) {

        @Override
        protected Iterator<Long> newTargetIterator() {
            final XdmNodeReadTrx rtx = holder.getReader();
            rtx.moveToDocumentRoot();
            return new DescendantAxis(rtx);
        }
    }.test();
    rtx.moveTo(1L);
    AbsAxisTest.testIAxisConventions(new DescendantAxis(rtx), new long[] { 4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L });
    new IteratorTester<Long>(ITERATIONS, IteratorFeature.UNMODIFIABLE, ImmutableList.of(4L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L), null) {

        @Override
        protected Iterator<Long> newTargetIterator() {
            final XdmNodeReadTrx rtx = holder.getReader();
            rtx.moveTo(1L);
            return new DescendantAxis(rtx);
        }
    }.test();
    rtx.moveTo(9L);
    AbsAxisTest.testIAxisConventions(new DescendantAxis(rtx), new long[] { 11L, 12L });
    new IteratorTester<Long>(ITERATIONS, IteratorFeature.UNMODIFIABLE, ImmutableList.of(11L, 12L), null) {

        @Override
        protected Iterator<Long> newTargetIterator() {
            final XdmNodeReadTrx rtx = holder.getReader();
            rtx.moveTo(9L);
            return new DescendantAxis(rtx);
        }
    }.test();
    rtx.moveTo(13L);
    AbsAxisTest.testIAxisConventions(new DescendantAxis(rtx), new long[] {});
    new IteratorTester<Long>(ITERATIONS, IteratorFeature.UNMODIFIABLE, Collections.<Long>emptyList(), null) {

        @Override
        protected Iterator<Long> newTargetIterator() {
            final XdmNodeReadTrx rtx = holder.getReader();
            rtx.moveTo(13L);
            return new DescendantAxis(rtx);
        }
    }.test();
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 43 with XdmNodeReadTrx

use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.

the class ForAxisTest method testFor.

@Test
public void testFor() throws SirixException {
    final XdmNodeReadTrx rtx = holder.getReader();
    rtx.moveTo(1L);
    AbsAxisTest.testIAxisConventions(new XPathAxis(rtx, "for $a in child::text() return child::node()"), new long[] { 4L, 5L, 8L, 9L, 13L, 4L, 5L, 8L, 9L, 13L, 4L, 5L, 8L, 9L, 13L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(rtx, "for $a in child::node() return $a/node()"), new long[] { 6L, 7L, 11L, 12L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(rtx, "for $a in child::node() return $a/text()"), new long[] { 6L, 12L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(rtx, "for $a in child::node() return $a/c"), new long[] { 7L, 11L });
    // IAxisTest.testIAxisConventions(new XPathAxis(
    // rtx,
    // "for $a in child::node(), $b in /node(), $c in ., $d in /c return $a/c"),
    // new long[] {7L, 11L});
    AbsAxisTest.testIAxisConventions(new XPathAxis(rtx, "for $a in child::node() return $a[@p:x]"), new long[] { 9L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(rtx, "for $a in . return $a"), new long[] { 1L });
    final AbstractAxis axis = new XPathAxis(rtx, "for $i in (10, 20), $j in (1, 2) return ($i + $j)");
    assertEquals(true, axis.hasNext());
    axis.next();
    assertEquals("11.0", rtx.getValue());
    assertEquals(true, axis.hasNext());
    axis.next();
    assertEquals("12.0", rtx.getValue());
    assertEquals(true, axis.hasNext());
    axis.next();
    assertEquals("21.0", rtx.getValue());
    assertEquals(true, axis.hasNext());
    axis.next();
    assertEquals("22.0", rtx.getValue());
    assertEquals(false, axis.hasNext());
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) Test(org.junit.Test)

Example 44 with XdmNodeReadTrx

use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.

the class StAXSerializerTest method testStAXSerializer.

@Test
public void testStAXSerializer() {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final XMLSerializer xmlSerializer = new XMLSerializerBuilder(holder.getResourceManager(), out).emitXMLDeclaration().build();
        xmlSerializer.call();
        final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx();
        StAXSerializer serializer = new StAXSerializer(rtx);
        final StringBuilder strBuilder = new StringBuilder();
        boolean isEmptyElement = false;
        while (serializer.hasNext()) {
            XMLEvent event = serializer.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_DOCUMENT:
                    strBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    emitElement(event, strBuilder);
                    if (serializer.peek().getEventType() == XMLStreamConstants.END_ELEMENT) {
                        strBuilder.append("/>");
                        isEmptyElement = true;
                    } else {
                        strBuilder.append('>');
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    if (isEmptyElement) {
                        isEmptyElement = false;
                    } else {
                        emitQName(true, event, strBuilder);
                        strBuilder.append('>');
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    strBuilder.append(((Characters) event).getData());
                    break;
            }
        }
        assertEquals(out.toString(), strBuilder.toString());
        // Check getElementText().
        // ========================================================
        holder.getReader().moveToDocumentRoot();
        holder.getReader().moveToFirstChild();
        serializer = new StAXSerializer(holder.getReader());
        String elemText = null;
        // <p:a>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("oops1foooops2baroops3", elemText);
        // oops1
        checkForException(serializer);
        // <b>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("foo", elemText);
        // foo
        checkForException(serializer);
        // <c>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("", elemText);
        // </c>
        checkForException(serializer);
        // </b>
        checkForException(serializer);
        // oops2
        checkForException(serializer);
        // <b p:x='y'>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("bar", elemText);
        // <c>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("", elemText);
        // </c>
        checkForException(serializer);
        // bar
        checkForException(serializer);
        // </b>
        checkForException(serializer);
        // oops3
        checkForException(serializer);
        // </p:a>
        checkForException(serializer);
        rtx.close();
    } catch (final XMLStreamException e) {
        fail("XML error while parsing: " + e.getMessage());
    } catch (final SirixException e) {
        fail("Sirix exception occured: " + e.getMessage());
    } catch (final Exception e) {
        fail("Any exception occured: " + e.getMessage());
    }
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) SirixException(org.sirix.exception.SirixException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SirixException(org.sirix.exception.SirixException) XMLStreamException(javax.xml.stream.XMLStreamException) Test(org.junit.Test)

Example 45 with XdmNodeReadTrx

use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.

the class PreviousAxisTest method testAxis.

@Test
public void testAxis() throws SirixException {
    final XdmNodeReadTrx firstRtx = holder.getResourceManager().beginNodeReadTrx(1);
    final XdmNodeReadTrx secondRtx = holder.getResourceManager().beginNodeReadTrx(2);
    new IteratorTester<XdmNodeReadTrx>(ITERATIONS, IteratorFeature.UNMODIFIABLE, ImmutableList.of(firstRtx), null) {

        @Override
        protected Iterator<XdmNodeReadTrx> newTargetIterator() {
            return new PreviousAxis(secondRtx);
        }
    }.test();
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Iterator(java.util.Iterator) Test(org.junit.Test)

Aggregations

XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)150 Test (org.junit.Test)93 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)48 QNm (org.brackit.xquery.atomic.QNm)20 DescendantAxis (org.sirix.axis.DescendantAxis)17 Iterator (java.util.Iterator)16 ResourceManager (org.sirix.api.ResourceManager)14 Axis (org.sirix.api.Axis)13 DBNode (org.sirix.xquery.node.DBNode)12 QueryException (org.brackit.xquery.QueryException)11 FilterAxis (org.sirix.axis.filter.FilterAxis)11 IndexController (org.sirix.access.IndexController)10 IndexDef (org.sirix.index.IndexDef)10 Item (org.brackit.xquery.xdm.Item)7 Database (org.sirix.api.Database)7 SirixException (org.sirix.exception.SirixException)6 NonStructuralWrapperAxis (org.sirix.axis.NonStructuralWrapperAxis)5 SirixDeweyID (org.sirix.node.SirixDeweyID)5 Path (java.nio.file.Path)4 AbsAxisTest (org.sirix.axis.AbsAxisTest)4