예제 #1
0
  public void testGetObject_AllTypes() throws XQException {
    XQExpression xqe;
    XQSequence xqs;

    xqe = xqc.createExpression();
    xqs =
        xqe.executeQuery(
            "xs:anyURI('http://www.foo.org'),"
                + "xs:base64Binary('AAAA'),"
                + "xs:boolean('true'),"
                + "xs:byte('1'),"
                + "xs:date('2000-12-31'),"
                + "xs:dateTime('2000-12-31T12:00:00'),"
                + "xs:decimal('1'),"
                + "xs:double('1'),"
                +
                //                           "xs:duration()" +
                "xs:ENTITY('AAA'),"
                + "xs:float('1'),"
                + "xs:gDay('---11'),"
                + "xs:gMonth('--11'),"
                + "xs:gMonthDay('--01-01'),"
                + "xs:gYear('2000'),"
                + "xs:gYearMonth('2000-01'),"
                + "xs:hexBinary('AA'),"
                + "xs:ID('AA'),"
                + "xs:IDREF('AA'),"
                + "xs:int('1'),"
                + "xs:integer('1'),"
                + "xs:language('en-US'),"
                + "xs:long('1'),"
                + "xs:Name('AAA'),"
                + "xs:NCName('AAA'),"
                + "xs:negativeInteger('-1'),"
                + "xs:NMTOKEN('AAA'),"
                + "xs:nonNegativeInteger('1'),"
                + "xs:nonPositiveInteger('-1'),"
                + "xs:normalizedString('AAA'),"
                +
                //                           "xs:NOTATION()," +
                "xs:positiveInteger('1'),"
                + "xs:QName('AAA'),"
                + "xs:short('1'),"
                + "xs:string('AAA'),"
                + "xs:time('12:12:12'),"
                + "xs:token('AAA'),"
                + "xs:unsignedByte('1'),"
                + "xs:unsignedInt('1'),"
                + "xs:unsignedLong('1'),"
                + "xs:unsignedShort('1'),"
                + "xs:dayTimeDuration('PT5H'),"
                + "xs:untypedAtomic('AAA'),"
                + "xs:yearMonthDuration('P1M'),"
                + "<e a=\"{''}\"/>/@a,"
                + "<!-- comment -->, "
                + "document{<e/>},"
                + "<e/>, "
                + "processing-instruction {'a'} {'b'},"
                + "<e>text</e>/text()");
    Class xmlGregorianCalendar = null;
    Class duration = null;
    try {
      xmlGregorianCalendar = Class.forName("javax.xml.datatype.XMLGregorianCalendar");
      duration = Class.forName("javax.xml.datatype.Duration");
    } catch (final Exception e) {
      // assume JDK 1.4
    }
    try {
      final String msg =
          "A-XQIA-4.1: getObject implements casting rules of '14.4 Mapping an XQuery Atomic Value to a Java Object Type' ";
      xqs.next();
      assertTrue(msg + "for xs:anyURI", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:base64Binary", xqs.getObject() instanceof byte[]);
      xqs.next();
      assertTrue(msg + "for xs:boolean", xqs.getObject() instanceof Boolean);
      xqs.next();
      assertTrue(msg + "for xs:byte", xqs.getObject() instanceof Byte);
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:date", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:dateTime", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      assertTrue(msg + "for xs:decimal", xqs.getObject() instanceof BigDecimal);
      xqs.next();
      assertTrue(msg + "for xs:double", xqs.getObject() instanceof Double);
      xqs.next();
      assertTrue(msg + "for xs:ENTITY", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:float", xqs.getObject() instanceof Float);
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:gDay", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:gMonth", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:MonthDay", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:Year", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:YearMonth", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      assertTrue(msg + "for xs:hexBinary", xqs.getObject() instanceof byte[]);
      xqs.next();
      assertTrue(msg + "for xs:ID", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:IDREF", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:int", xqs.getObject() instanceof Integer);
      xqs.next();
      assertTrue(msg + "for xs:integer", xqs.getObject() instanceof BigInteger);
      xqs.next();
      assertTrue(msg + "for xs:language", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:long", xqs.getObject() instanceof Long);
      xqs.next();
      assertTrue(msg + "for xs:Name", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:NCName", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:negativeInteger", xqs.getObject() instanceof BigInteger);
      xqs.next();
      assertTrue(msg + "for xs:NMTOKEN", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:nonNegativeInteger", xqs.getObject() instanceof BigInteger);
      xqs.next();
      assertTrue(msg + "for xs:nonPostiveInteger", xqs.getObject() instanceof BigInteger);
      xqs.next();
      assertTrue(msg + "for xs:normalizedString", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:postiveInteger", xqs.getObject() instanceof BigInteger);
      xqs.next();
      assertTrue(msg + "for xs:QName", xqs.getObject() instanceof QName);
      xqs.next();
      assertTrue(msg + "for xs:short", xqs.getObject() instanceof Short);
      xqs.next();
      assertTrue(msg + "for xs:string", xqs.getObject() instanceof String);
      xqs.next();
      if (xmlGregorianCalendar != null)
        assertTrue(msg + "for xs:time", xmlGregorianCalendar.isInstance(xqs.getObject()));
      xqs.next();
      assertTrue(msg + "for xs:token", xqs.getObject() instanceof String);
      xqs.next();
      assertTrue(msg + "for xs:unsignedByte", xqs.getObject() instanceof Short);
      xqs.next();
      assertTrue(msg + "for xs:unsignedInt", xqs.getObject() instanceof Long);
      xqs.next();
      assertTrue(msg + "for xs:unsignedLong", xqs.getObject() instanceof BigInteger);
      xqs.next();
      assertTrue(msg + "for xs:unsignedShort", xqs.getObject() instanceof Integer);
      xqs.next();
      if (duration != null)
        assertTrue(msg + "for xs:dayTimeDuration", duration.isInstance(xqs.getObject()));
      xqs.next();
      assertTrue(msg + "for xs:untypedAtomic", xqs.getObject() instanceof String);
      xqs.next();
      if (duration != null)
        assertTrue(msg + "for xs:yearMonthDuration", duration.isInstance(xqs.getObject()));
      xqs.next();
      assertTrue(msg + "for attribute", xqs.getObject() instanceof org.w3c.dom.Attr);
      xqs.next();
      assertTrue(msg + "for comment", xqs.getObject() instanceof org.w3c.dom.Comment);
      xqs.next();
      assertTrue(msg + "for document", xqs.getObject() instanceof org.w3c.dom.Document);
      xqs.next();
      assertTrue(msg + "for element", xqs.getObject() instanceof org.w3c.dom.Element);
      xqs.next();
      assertTrue(
          msg + "for processing instruction",
          xqs.getObject() instanceof org.w3c.dom.ProcessingInstruction);
      xqs.next();
      assertTrue(msg + "for text", xqs.getObject() instanceof org.w3c.dom.Text);
    } catch (final XQException e) {
      e.printStackTrace();
      fail("A-XQIA-4.1: getObject on element() failed with message: " + e.getMessage());
    }
  }
예제 #2
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();
  }