Пример #1
0
  public void testGetBoolean() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'true'");
    xqs.next();
    try {
      xqs.getBoolean();
      fail("A-XQIA-1.1: conversion to boolean should fail");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:boolean('true')");
    try {
      xqs.getBoolean();
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:boolean('true')");
    xqs.next();
    xqs.close();
    try {
      xqs.getBoolean();
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:boolean('true')");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getBoolean();
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:boolean('true')");
    xqs.next();
    try {
      assertEquals("A-XQIA-1.6: getBoolean on xs:boolean failed", true, xqs.getBoolean());
    } catch (final XQException e) {
      fail("A-XQIA-1.6: getBoolean on xs:boolean failed with message: " + e.getMessage());
    }
    xqe.close();
  }
Пример #2
0
  public void testGetNodeUri() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    xqs.next();
    try {
      xqs.getNodeUri();
      fail("A-XQIA-5.1: getNodeUri() should fail if current item is not a node");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    try {
      xqs.getNodeUri();
      fail("A-XQIA-5.2: getNodeUri() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    xqs.close();
    try {
      xqs.getNodeUri();
      fail("A-XQIA-5.3: closed item accessor supports getNodeUri()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    try {
      xqs.getNodeUri(); // returned value is implementation defined
    } catch (final XQException e) {
      fail("A-XQIA-5.4: getNodeUri() failed with message: " + e.getMessage());
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getNodeUri(); // returned value is implementation defined
    } catch (final XQException e) {
      fail("A-XQIA-5.5: getNodeUri() failed with message: " + e.getMessage());
    }
    xqe.close();
  }
Пример #3
0
  public void testGetAtomicValue() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    try {
      xqs.getAtomicValue();
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    xqs.next();
    xqs.close();
    try {
      xqs.getAtomicValue();
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getAtomicValue();
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("1, 1.1");
    xqs.next();
    try {
      assertEquals("A-XQIA-2.1: getAtomicValue on xs:string failed", "1", xqs.getAtomicValue());
    } catch (final XQException e) {
      fail("A-XQIA-2.1: getAtomicValue on xs:string failed with message: " + e.getMessage());
    }
    xqs.next();
    try {
      assertEquals("A-XQIA-2.1: getAtomicValue on xs:decimal failed", "1.1", xqs.getAtomicValue());
    } catch (final XQException e) {
      fail("A-XQIA-2.1: getAtomicValue on xs:decimal failed with message: " + e.getMessage());
    }
    xqe.close();
  }
Пример #4
0
  public void testGetItemType() throws XQException {
    XQExpression xqe;
    XQSequence xqs;
    XQItemType xqtype;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    try {
      xqs.getItemType();
      fail("A-XQIA-6.1: getItemType() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    xqs.next();
    xqs.close();
    try {
      xqs.getItemType();
      fail("A-XQIA-6.2: closed item accessor supports getItemType()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'Hello world!'");
    xqs.next();
    try {
      xqtype = xqs.getItemType();
      assertNotNull("A-XQIA-6.3: getItemType() failed", xqtype);
      assertEquals(
          "A-XQIA-6.3: getItemType() failed", XQItemType.XQITEMKIND_ATOMIC, xqtype.getItemKind());
      assertEquals(
          "A-XQIA-6.3: getItemType() failed", XQItemType.XQBASETYPE_STRING, xqtype.getBaseType());
    } catch (final XQException e) {
      fail("A-XQIA-6.3: getItemType() failed with message: " + e.getMessage());
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("1");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getItemType();
    } catch (final XQException e) {
      fail("A-XQIA-6.4: getItemType() failed with message: " + e.getMessage());
    }
    xqe.close();
  }
Пример #5
0
  public void testGetItemAsStream() throws XQException {
    XQExpression xqe;
    XQSequence xqs;
    XMLStreamReader xmlReader = null;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    try {
      xqs.getItemAsStream();
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.close();
    try {
      xqs.getItemAsStream();
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getItemAsStream();
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e a='Hello world!'/>/@*");
    xqs.next();
    try {
      xmlReader = xqs.getItemAsStream();
      while (xmlReader.hasNext()) xmlReader.next();
      fail("A-XQIA-8.1: serialization process fails when sequence contains a top-level attribute");
    } catch (final XQException xq) {
      // Expect an XQException or XMLStreamException
    } catch (final XMLStreamException xml) {
      // Expect an XQException or XMLStreamException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    try {
      xmlReader = xqs.getItemAsStream();
    } catch (final XQException e) {
      fail("A-XQIA-9.1: getItemAsStream failed with message: " + e.getMessage());
    }
    try {
      assertNotNull("A-XQIA-9.1: getItemAsStream returned a null XMLStreamReader", xmlReader);
      assertEquals(
          "A-XQIA-9.1: unexpected first event returned by XMLStreamReader",
          XMLStreamReader.START_DOCUMENT,
          xmlReader.getEventType());
      assertEquals(
          "A-XQIA-9.1: unexpected second event returned by XMLStreamReader",
          XMLStreamReader.START_ELEMENT,
          xmlReader.next());
      assertEquals(
          "A-XQIA-9.1: unexpected third event returned by XMLStreamReader",
          XMLStreamReader.CHARACTERS,
          xmlReader.next());
      assertEquals(
          "A-XQIA-9.1: unexpected fourth event returned by XMLStreamReader",
          XMLStreamReader.END_ELEMENT,
          xmlReader.next());
      assertEquals(
          "A-XQIA-9.1: unexpected fifth event returned by XMLStreamReader",
          XMLStreamReader.END_DOCUMENT,
          xmlReader.next());
    } catch (final XMLStreamException e) {
      fail("A-XQIA-9.1: XMLStreamReader.next() failed with message: " + e.getMessage());
    }
    xqe.close();
  }
Пример #6
0
  public void testGetByte() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    xqs.next();
    try {
      xqs.getByte();
      fail("A-XQIA-1.1: conversion to byte should fail");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:byte('1')");
    try {
      xqs.getByte();
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:byte('1')");
    xqs.next();
    xqs.close();
    try {
      xqs.getByte();
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:byte('1')");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getByte();
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:decimal('1.1')");
    xqs.next();
    try {
      xqs.getByte();
      fail("A-XQIA-1.5: getByte on xs:decimal out of value space of byte should fail");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("xs:byte('1'), 10.0");
    xqs.next();
    try {
      assertEquals("A-XQIA-1.6: getByte on xs:byte failed", 1, xqs.getByte());
    } catch (final XQException e) {
      fail("A-XQIA-1.6: getByte on xs:byte failed with message: " + e.getMessage());
    }
    xqs.next();
    try {
      assertEquals("A-XQIA-1.6: getByte on xs:decimal failed", 10, xqs.getByte());
    } catch (final XQException e) {
      fail("A-XQIA-1.6: getByte on xs:decimal failed with message: " + e.getMessage());
    }
    xqe.close();
  }
Пример #7
0
  public void testGetObject() throws XQException {
    XQExpression xqe;
    XQSequence xqs;
    Object object = null;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    try {
      xqs.getObject();
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    xqs.close();
    try {
      xqs.getObject();
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getObject();
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>, 'Hello world!'");
    xqs.next();
    try {
      object = xqs.getObject();
    } catch (final XQException e) {
      fail("A-XQIA-4.1: getObject on element() failed with message: " + e.getMessage());
    }
    assertEquals(
        "A-XQIA-4.1: getObject on element() failed", true, object instanceof org.w3c.dom.Element);
    assertEquals(
        "A-XQIA-4.1: getObject on element() failed",
        "e",
        ((org.w3c.dom.Node) object).getLocalName());
    xqs.next();
    try {
      object = xqs.getObject();
    } catch (final XQException e) {
      fail("A-XQIA-4.1: getObject on xs:string failed with message: " + e.getMessage());
    }
    assertEquals("A-XQIA-4.1: getObject on xs:string failed", true, object instanceof String);
    assertEquals("A-XQIA-4.1: getObject on xs:string failed", "Hello world!", (String) object);
    xqe.close();
  }
Пример #8
0
  public void testGetNode() throws XQException {
    XQExpression xqe;
    XQSequence xqs;
    org.w3c.dom.Node node = null;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("'1'");
    xqs.next();
    try {
      xqs.getNode();
      fail("A-XQIA-1.1: conversion to org.w3c.dom.Node should fail");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    try {
      xqs.getNode();
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    xqs.close();
    try {
      xqs.getNode();
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getNode();
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e/>, <e a=''/>/@*");
    xqs.next();
    try {
      node = xqs.getNode();
    } catch (final XQException e) {
      fail("A-XQIA-3.1: getNode on element() failed with message: " + e.getMessage());
    }
    assertEquals(
        "A-XQIA-3.1: getNode on element() failed", true, node instanceof org.w3c.dom.Element);
    assertEquals("A-XQIA-3.1: getNode on element() failed", "e", node.getLocalName());
    xqs.next();
    try {
      node = xqs.getNode();
    } catch (final XQException e) {
      fail("A-XQIA-3.1: getNode on attribute() failed with message: " + e.getMessage());
    }
    assertEquals(
        "A-XQIA-3.1: getNode on attribute() failed", true, node instanceof org.w3c.dom.Attr);
    assertEquals("A-XQIA-3.1: getNode on attribute() failed", "a", node.getLocalName());
    xqe.close();
  }
Пример #9
0
  public void testWriteItemToResult() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    try {
      xqs.writeItemToResult(new StreamResult(new StringWriter()));
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.close();
    try {
      xqs.writeItemToResult(new StreamResult(new StringWriter()));
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.writeItemToResult(new StreamResult(new StringWriter()));
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    // The spec doesn't say serialization is performed according to XSLT 2.0 and XQuery 1.0
    // serialization.
    // As such we don't test here if the serialization process can fail.
    //
    //   xqe = xqc.createExpression();
    //   xqs = xqe.executeQuery("<e a='Hello world!'/>/@*");
    //   xqs.next();
    //   try {
    //     xqs.writeItemToResult(new StreamResult(new StringWriter()));
    //     fail("A-XQIA-8.1: serialization process fails when sequence contains a top-level
    // attribute");
    //   } catch (XQException xq) {
    //     // Expect an XQException
    //   }
    //   xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    try {
      xqs.writeItemToResult((Result) null);
      fail("A-XQIA-11.3: writeItemToResult accepts a null buffer as first argument");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    final StringWriter result = new StringWriter();
    try {
      xqs.writeItemToResult(new StreamResult(result));
    } catch (final XQException e) {
      fail("A-XQIA-11.1: writeItemToResult failed with message: " + e.getMessage());
    }
    assertTrue(
        "A-XQIA-11.1: Expects serialized result contains '<e>Hello world!</e>', but it is '"
            + result.toString()
            + '\'',
        result.toString().indexOf("<e>Hello world!</e>") != -1);
    xqe.close();
  }
Пример #10
0
  public void testWriteItemToSAX() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    try {
      xqs.writeItemToSAX(new DefaultHandler());
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.close();
    try {
      xqs.writeItemToSAX(new DefaultHandler());
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.writeItemToSAX(new DefaultHandler());
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e a='Hello world!'/>/@*");
    xqs.next();
    try {
      xqs.writeItemToSAX(new DefaultHandler());
      fail("A-XQIA-8.1: serialization process fails when sequence contains a top-level attribute");
    } catch (final XQException xq) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    try {
      xqs.writeItemToSAX((ContentHandler) null);
      fail("A-XQIA-11.3: writeItem accepts a null buffer as first argument");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    final TestContentHandler result = new TestContentHandler();
    try {
      xqs.writeItemToSAX(result);
    } catch (final XQException e) {
      fail("A-XQIA-11.1: writeItem failed with message: " + e.getMessage());
    }
    assertTrue(
        "A-XQIA-11.1: Expects serialized result contains '<e>Hello world!</e>', but it is '"
            + result.buffer.toString()
            + '\'',
        result.buffer.toString().indexOf("<e>Hello world!</e>") != -1);
    xqe.close();
  }
Пример #11
0
  public void testWriteItem_OutputStream() throws XQException, UnsupportedEncodingException {

    // We don't expect this method ever to throw UnsupportedEncodingException, as we only request
    // the "UTF-8" encoding.
    // However, in order to make the compiler happy, and to play it safe, add
    // UnsupportedEncodingException to the throws clause.

    XQExpression xqe;
    XQSequence xqs;

    final Properties prop = new Properties();
    prop.setProperty("encoding", "UTF-8");

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    try {
      xqs.writeItem(new ByteArrayOutputStream(), prop);
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.close();
    try {
      xqs.writeItem(new ByteArrayOutputStream(), prop);
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.writeItem(new ByteArrayOutputStream(), prop);
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e a='Hello world!'/>/@*");
    xqs.next();
    try {
      xqs.writeItem(new ByteArrayOutputStream(), prop);
      fail("A-XQIA-8.1: serialization process fails when sequence contains a top-level attribute");
    } catch (final XQException xq) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    try {
      xqs.writeItem((OutputStream) null, prop);
      fail("A-XQIA-11.3: writeItem accepts a null buffer as first argument");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    final ByteArrayOutputStream result = new ByteArrayOutputStream();
    try {
      xqs.writeItem(result, prop);
    } catch (final XQException e) {
      fail("A-XQIA-11.1: writeItem failed with message: " + e.getMessage());
    }
    assertTrue(
        "A-XQIA-11.1: Expects serialized result contains '<e>Hello world!</e>', but it is '"
            + result.toString("UTF-8")
            + '\'',
        result.toString("UTF-8").indexOf("<e>Hello world!</e>") != -1);
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    final ByteArrayOutputStream otherResult = new ByteArrayOutputStream();
    try {
      xqs.writeItem(otherResult, prop);
    } catch (final XQException e) {
      fail("A-XQIA-11.2: writeItem failed with message: " + e.getMessage());
    }
    assertEquals(
        "A-XQIA-11.2: null properties argument is equivalent to empty properties argument",
        result.toString("UTF-8"),
        otherResult.toString("UTF-8"));
    xqe.close();
  }
Пример #12
0
  public void testGetItemAsString() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    try {
      xqs.getItemAsString(new Properties());
      fail("A-XQIA-1.2: getXXX() should fail when not positioned on an item");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.close();
    try {
      xqs.getItemAsString(new Properties());
      fail("A-XQIA-1.3: closed item accessor supports getXXX()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    xqs.getItem();
    try {
      xqs.getItemAsString(new Properties());
      fail("A-XQIA-1.4: SCROLLTYPE_FORWARD_ONLY sequence supports getting item twice()");
    } catch (final XQException e) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e a='Hello world!'/>/@*");
    xqs.next();
    try {
      xqs.getItemAsString(new Properties());
      fail("A-XQIA-8.1: serialization process fails when sequence contains a top-level attribute");
    } catch (final XQException xq) {
      // Expect an XQException
    }
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    String result = null;
    try {
      result = xqs.getItemAsString(new Properties());
    } catch (final XQException e) {
      fail("A-XQIA-10.1: getItemAsString failed with message: " + e.getMessage());
    }
    assertTrue(
        "A-XQIA-10.1: Expects serialized result contains '<e>Hello world!</e>', but it is '"
            + result
            + '\'',
        result.indexOf("<e>Hello world!</e>") != -1);
    xqe.close();

    xqe = xqc.createExpression();
    xqs = xqe.executeQuery("<e>Hello world!</e>");
    xqs.next();
    try {
      assertEquals(
          "A-XQIA-10.2: null properties argument is equivalent to empty properties argument",
          result,
          xqs.getItemAsString(null));
    } catch (final XQException e) {
      fail("A-XQIA-10.2: getItemAsString failed with message: " + e.getMessage());
    }
    xqe.close();
  }