示例#1
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();
  }
示例#2
0
 /**
  * Creates new databases.
  *
  * @throws IOException I/O exception
  */
 @Test
 public final void create() throws IOException {
   session.create(NAME, new ArrayInput(""));
   assertEqual("", session.query("db:open('" + NAME + "')").execute());
   session.create(NAME, new ArrayInput("<X/>"));
   assertEqual("<X/>", session.query("db:open('" + NAME + "')").execute());
 }
示例#3
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());
 }
示例#4
0
 /**
  * Stores binary content.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void storeBinary() throws IOException {
   session.execute("create db " + NAME);
   session.store("X", new ArrayInput(new byte[] {-128, -2, -1, 0, 1, 127}));
   assertEqual(
       "-128 -2 -1 0 1 127",
       session.query(_CONVERT_BINARY_TO_BYTES.args(_DB_RETRIEVE.args(NAME, "X"))).execute());
 }
示例#5
0
 /**
  * Adds documents to a database.
  *
  * @throws IOException I/O exception
  */
 @Test
 public final void add() throws IOException {
   session.execute("create db " + NAME);
   session.add(NAME, new ArrayInput("<X/>"));
   assertEqual("1", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute());
   for (int i = 0; i < 9; i++) session.add(NAME, new ArrayInput("<X/>"));
   assertEqual("10", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute());
 }
示例#6
0
 /** Stops a session. */
 @After
 public final void stopSession() {
   try {
     if (cleanup) session.execute(new DropDB(NAME));
     session.close();
   } catch (final IOException ex) {
     fail(Util.message(ex));
   }
 }
示例#7
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());
 }
示例#8
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();
  }
示例#9
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();
 }
示例#10
0
 /** Clean up method. */
 @After
 public void cleanUp() {
   try {
     testSession.close();
     adminSession.execute(new DropDB(RENAMED));
     adminSession.execute(new DropDB(NAME));
     adminSession.close();
     // give the server some time to clean up the sessions before next test
     Performance.sleep(100);
   } catch (final Exception ex) {
     fail(Util.message(ex));
   }
 }
示例#11
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();
 }
示例#12
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();
 }
示例#13
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();
 }
示例#14
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();
 }
示例#15
0
 /**
  * Performs the specified query n times and and returns the result.
  *
  * @param query query to be evaluated
  * @param n number of runs
  * @return resulting string
  * @throws IOException I/O exception
  */
 protected static String eval(final int n, final String query) throws IOException {
   // loop through number of runs for a single query
   check();
   String result = "";
   for (int rn = 0; rn < n; ++rn) result = session.execute(new XQuery(query));
   return result;
 }
示例#16
0
 /**
  * Assumes that this command fails.
  *
  * @param cmd command reference
  * @param s session
  */
 private static void no(final Command cmd, final Session s) {
   try {
     s.execute(cmd);
     fail("\"" + cmd + "\" was supposed to fail.");
   } catch (final IOException ignored) {
   }
 }
示例#17
0
  /**
   * Runs a query and checks the updating flag.
   *
   * @throws IOException I/O exception
   */
  @Test
  public void queryUpdating() throws IOException {
    // test non-updating query
    Query query = session.query("12345678");
    assertFalse(query.updating());
    assertEqual("12345678", query.execute());
    assertFalse(query.updating());
    query.close();

    // test updating query
    query = session.query("insert node <a/> into <b/>");
    assertTrue(query.updating());
    assertEqual("", query.execute());
    assertTrue(query.updating());
    query.close();
  }
示例#18
0
 /**
  * Assumes that this command is successful.
  *
  * @param cmd command reference
  * @param s session
  */
 private static void ok(final Command cmd, final Session s) {
   try {
     s.execute(cmd);
   } catch (final IOException ex) {
     fail(Util.message(ex));
   }
 }
示例#19
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();
 }
示例#20
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();
 }
示例#21
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindDynamic() throws IOException {
   final Query query = session.query("declare variable $a as xs:integer external; $a");
   query.bind("a", "1");
   assertEqual("1", query.execute());
   query.close();
 }
示例#22
0
 /**
  * Runs 5 queries in parallel.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryParallel2() throws IOException {
   final int size = 8;
   final Query[] cqs = new Query[size];
   for (int q = 0; q < size; q++) cqs[q] = session.query(Integer.toString(q));
   for (int q = 0; q < size; q++) assertEqual(q, cqs[q].next());
   for (final Query query : cqs) query.close();
 }
示例#23
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();
 }
示例#24
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());
 }
示例#25
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());
 }
示例#26
0
 /**
  * Runs a query and checks the serialization parameters.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryOptions() throws IOException {
   final Query query = session.query("declare option output:encoding 'US-ASCII';()");
   query.execute();
   final SerializerOptions sp = new SerializerOptions();
   sp.parse(query.options());
   assertEquals("US-ASCII", sp.get(SerializerOptions.ENCODING));
   query.close();
 }
示例#27
0
 /**
  * Replaces documents in a database.
  *
  * @throws IOException I/O exception
  */
 @Test
 public final void replace() throws IOException {
   session.execute("create db " + NAME);
   assertEqual("0", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute());
   session.replace(NAME, new ArrayInput("<X/>"));
   assertEqual("1", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute());
   session.replace(NAME + '2', new ArrayInput("<X/>"));
   assertEqual("2", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute());
   session.replace(NAME + '2', new ArrayInput("<X/>"));
   assertEqual("2", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute());
 }
示例#28
0
  /**
   * Initializes the benchmark.
   *
   * @throws IOException I/O exception
   */
  @BeforeClass
  public static void init() throws IOException {
    // check if server is (not) running
    final int sp = context.soptions.get(StaticOptions.SERVERPORT);
    server = local || BaseXServer.ping(S_LOCALHOST, sp) ? null : createServer();
    session = local ? new LocalSession(context) : createClient();

    // create test database
    session.execute(new Set(MainOptions.QUERYINFO, true));
  }
示例#29
0
 /**
  * Stores binary content in the database.
  *
  * @throws IOException I/O exception
  */
 @Test
 public final void store() throws IOException {
   session.execute("create db " + NAME);
   session.store("X", new ArrayInput("!"));
   assertEqual("true", session.query(_DB_IS_RAW.args(NAME, "X")).execute());
   session.store("X", new ArrayInput(""));
   assertEqual("", session.query(_DB_RETRIEVE.args(NAME, "X")).execute());
   session.store("X", new ArrayInput(new byte[] {0, 1, -1}));
   assertEqual(
       "0001FF", session.query("xs:hexBinary(" + _DB_RETRIEVE.args(NAME, "X") + ')').execute());
   session.execute("drop db " + NAME);
 }
示例#30
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();
 }