Exemplo n.º 1
0
 /**
  * Runs a query, omitting more().
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryNoMore() throws IOException {
   final Query query = session.query("1 to 2");
   assertEqual("1", query.next());
   assertEqual("2", query.next());
   assertNull(query.next());
   query.close();
 }
Exemplo n.º 2
0
 /**
  * Queries empty content.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryEmptyString() throws IOException {
   final Query q = session.query("'',1");
   assertTrue(q.more());
   assertEqual("", q.next());
   assertTrue(q.more());
   assertEqual("1", q.next());
   assertNull(q.next());
 }
Exemplo n.º 3
0
 /**
  * Queries empty content.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryEmptyBinary() throws IOException {
   session.execute("create db " + NAME);
   session.store("X", new ArrayInput(""));
   assertEqual("", session.execute("xquery " + RAW + _DB_RETRIEVE.args(NAME, "X")));
   assertEqual("", session.query(RAW + _DB_RETRIEVE.args(NAME, "X")).execute());
   final Query q = session.query(RAW + _DB_RETRIEVE.args(NAME, "X"));
   assertTrue(q.more());
   assertEqual("", q.next());
   assertNull(q.next());
 }
Exemplo n.º 4
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBind() throws IOException {
   final Query query = session.query("declare variable $a external; $a");
   query.bind("$a", "4");
   assertEqual("4", query.execute());
   query.bind("$a", "5");
   assertEqual("5", query.next());
   query.bind("$a", "6");
   assertEqual("6", query.next());
   query.close();
 }
Exemplo n.º 5
0
  /**
   * Runs a query with an external variable declaration.
   *
   * @throws IOException I/O exception
   */
  @Test
  public void queryBindInt() throws IOException {
    Query query = session.query("declare variable $a as xs:integer external; $a");
    query.bind("a", "5", "xs:integer");
    assertEqual("5", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", Int.get(1), "xs:integer");
    assertEqual("1", query.next());
    query.close();
  }
Exemplo n.º 6
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindURI() throws IOException {
   final Query query = session.query("declare variable $a external; $a");
   query.bind("$a", "X", "xs:anyURI");
   assertEqual("X", query.next());
   query.close();
 }
Exemplo n.º 7
0
 /**
  * Runs a query with a bound context value.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryContextVar() throws IOException {
   final Query query = session.query("declare variable $a := .; $a");
   query.context("<a/>", "element()");
   assertEqual("<a/>", query.next());
   query.close();
 }
Exemplo n.º 8
0
 /**
  * Runs a query with a bound context value.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryContext() throws IOException {
   final Query query = session.query(".");
   query.context("5");
   assertEqual("5", query.next());
   query.close();
 }
Exemplo n.º 9
0
 /**
  * Runs a query with a bound context value.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryContextInt() throws IOException {
   final Query query = session.query(". * 2");
   query.context("6", "xs:integer");
   assertEqual("12", query.next());
   query.close();
 }
Exemplo n.º 10
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindEmptySequence() throws IOException {
   final Query query = session.query("declare variable $a external; $a");
   query.bind("a", "()", "empty-sequence()");
   assertNull(query.next());
   query.close();
 }
Exemplo n.º 11
0
 /**
  * Runs a query and retrieves multiple results as string.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryMore() throws IOException {
   final Query query = session.query("1 to 3");
   int c = 0;
   while (query.more()) assertEqual(++c, query.next());
   query.close();
 }
Exemplo n.º 12
0
 /**
  * Runs a query with additional serialization parameters.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void querySerial1() throws IOException {
   session.execute("set serializer wrap-prefix=db,wrap-uri=ns");
   final Query query = session.query(WRAPPER + "()");
   assertTrue("Result expected.", query.more());
   assertEqual("<db:results xmlns:db=\"ns\"/>", query.next());
   assertFalse("No result expected.", query.more());
 }
Exemplo n.º 13
0
 /**
  * Runs a query with additional serialization parameters.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void querySerial2() throws IOException {
   final Query query = session.query(WRAPPER + "1 to 2");
   assertTrue("Result expected.", query.more());
   assertEqual(
       "<db:results xmlns:db=\"ns\">  <db:result>1</db:result>"
           + "  <db:result>2</db:result></db:results>",
       query.next());
 }
Exemplo n.º 14
0
  /**
   * Runs a query with an external variable declaration.
   *
   * @throws IOException I/O exception
   */
  @Test
  public void queryBindSequence() throws IOException {
    Query query = session.query("declare variable $a external; $a");
    query.bind("a", "1\u00012", "xs:integer");
    assertEqual("1", query.next());
    assertEqual("2", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", "09\u0002xs:hexBinary\u00012", "xs:integer");
    assertEqual("09", query.next());
    assertEqual("2", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", Seq.get(new Item[] {Int.get(1), Str.get("X")}, 2));
    assertEqual("1", query.next());
    assertEqual("X", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", IntSeq.get(new long[] {1, 2}, AtomType.INT));
    assertEqual("1", query.next());
    assertEqual("2", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", IntSeq.get(new long[] {1, 2}, AtomType.INT), "xs:integer");
    assertEqual("1", query.next());
    assertEqual("2", query.next());
    query.close();
  }
Exemplo n.º 15
0
 /**
  * Queries binary content (works only if output stream is specified).
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBinary() throws IOException {
   if (out == null) return;
   session.execute("create db " + NAME);
   final byte[] tmp = {0, 1, 2, 127, 0, -1, -2, -128};
   session.store("X", new ArrayInput(tmp));
   final String retr = _DB_RETRIEVE.args(NAME, "X");
   // check command
   session.execute("xquery " + RAW + retr + ',' + retr);
   assertTrue(eq(out.toArray(), concat(tmp, tmp)));
   out.reset();
   // check query execution
   session.query(RAW + retr + ',' + retr).execute();
   assertTrue(eq(out.toArray(), concat(tmp, tmp)));
   out.reset();
   // check iterator
   final Query q = session.query(RAW + retr + ',' + retr);
   q.next();
   assertTrue(eq(out.toArray(), tmp));
   out.reset();
   q.next();
   assertTrue(eq(out.toArray(), tmp));
   assertNull(q.next());
 }
Exemplo n.º 16
0
 /**
  * Runs two queries in parallel.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryParallel() throws IOException {
   final Query query1 = session.query("1 to 2");
   final Query query2 = session.query("reverse(3 to 4)");
   assertEqual("1", query1.next());
   assertEqual("4", query2.next());
   assertEqual("2", query1.next());
   assertEqual("3", query2.next());
   assertNull(query1.next());
   assertNull(query2.next());
   query1.close();
   query2.close();
 }
Exemplo n.º 17
0
 /**
  * Runs an erroneous query.
  *
  * @throws IOException expected exception
  */
 @Test(expected = BaseXException.class)
 public void queryError3() throws IOException {
   final Query query = session.query("(1,'a')[. eq 1]");
   assertEqual("1", query.next());
   query.next();
 }
Exemplo n.º 18
0
 /**
  * Runs a query and retrieves XML entities as string.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryEntities() throws IOException {
   final Query query = session.query("'&amp;&lt;&gt;&apos;&quot;'");
   assertEqual("&<>'\"", query.next());
 }
Exemplo n.º 19
0
 /**
  * Runs a query and retrieves the result as string.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void query2() throws IOException {
   final Query query = session.query("1");
   if (!query.more()) fail("No result returned");
   assertEqual("1", query.next());
 }