コード例 #1
0
ファイル: QueryTest.java プロジェクト: runeengh/basex
 /**
  * Creates a container for the specified node values.
  *
  * @param doc document
  */
 protected static void create(final String doc) {
   try {
     new CreateDB(Util.className(SandboxTest.class), doc).execute(context);
   } catch (final BaseXException ex) {
     Util.notExpected(ex);
   }
 }
コード例 #2
0
ファイル: BaseX.java プロジェクト: james-jw/basex
  /** Launches the console mode, which reads and executes user input. */
  private void console() {
    Util.outln(header() + NL + TRY_MORE_X);
    verbose = true;

    // create console reader
    try (final ConsoleReader cr = ConsoleReader.get()) {
      // loop until console is set to false (may happen in server mode)
      while (console) {
        // get next line
        final String in = cr.readLine(PROMPT);
        // end of input: break loop
        if (in == null) break;
        // skip empty lines
        if (in.isEmpty()) continue;

        try {
          if (!execute(new CommandParser(in, context).pwReader(cr.pwReader()))) {
            // show goodbye message if method returns false
            Util.outln(BYE[new Random().nextInt(4)]);
            break;
          }
        } catch (final IOException ex) {
          // output error messages
          Util.errln(ex);
        }
      }
    }
  }
コード例 #3
0
ファイル: Command.java プロジェクト: plutext/basex
  /**
   * Runs the command without permission, data and concurrency checks.
   *
   * @param ctx database context
   * @param os output stream
   * @return result of check
   */
  public boolean run(final Context ctx, final OutputStream os) {
    perf = new Performance();
    context = ctx;
    prop = ctx.prop;
    mprop = ctx.mprop;
    out = PrintOutput.get(os);

    try {
      return run();
    } catch (final ProgressException ex) {
      // process was interrupted by the user or server
      abort();
      return error(INTERRUPTED);
    } catch (final Throwable ex) {
      // unexpected error
      Performance.gc(2);
      abort();
      if (ex instanceof OutOfMemoryError) {
        Util.debug(ex);
        return error(OUT_OF_MEM + (createWrite() ? H_OUT_OF_MEM : ""));
      }
      return error(Util.bug(ex) + NL + info.toString());
    } finally {
      // flushes the output
      try {
        if (out != null) out.flush();
      } catch (final IOException ignored) {
      }
    }
  }
コード例 #4
0
ファイル: IndexOptimizeTest.java プロジェクト: fpapai/basex
 /**
  * Checks if specified query was rewritten for index access, and checks the query result.
  *
  * @param query query to be tested
  * @param result result or {@code null} for no comparison
  */
 private static void check(final String query, final String result) {
   check(
       query,
       result,
       "exists(/descendant-or-self::*"
           + "[self::"
           + Util.className(ValueAccess.class)
           + "|self::"
           + Util.className(FTIndexAccess.class)
           + "])");
 }
コード例 #5
0
ファイル: DiskBuilder.java プロジェクト: dirkk/basex
  @Override
  public DiskData build() throws IOException {
    meta.assign(parser);
    meta.dirty = true;

    // calculate optimized output buffer sizes to reduce disk fragmentation
    final Runtime rt = Runtime.getRuntime();
    final long max = Math.min(1 << 22, rt.maxMemory() - rt.freeMemory() >> 2);
    int bs = (int) Math.min(meta.filesize, max);
    bs = Math.max(IO.BLOCKSIZE, bs - bs % IO.BLOCKSIZE);

    // drop old database (if available) and create new one
    DropDB.drop(dbname, sopts);
    sopts.dbpath(dbname).md();

    elemNames = new Names(meta);
    attrNames = new Names(meta);
    try {
      tout = new DataOutput(new TableOutput(meta, DATATBL));
      xout = new DataOutput(meta.dbfile(DATATXT), bs);
      vout = new DataOutput(meta.dbfile(DATAATV), bs);
      sout = new DataOutput(meta.dbfile(DATATMP), bs);

      final Performance perf = Prop.debug ? new Performance() : null;
      Util.debug(tit() + DOTS);
      parse();
      if (Prop.debug) Util.errln(" " + perf + " (" + Performance.getMemory() + ')');

    } catch (final IOException ex) {
      try {
        close();
      } catch (final IOException ignored) {
      }
      throw ex;
    }
    close();

    // copy temporary values into database table
    try (final DataInput in = new DataInput(meta.dbfile(DATATMP))) {
      final TableAccess ta = new TableDiskAccess(meta, true);
      for (; spos < ssize; ++spos) ta.write4(in.readNum(), 8, in.readNum());
      ta.close();
    }
    meta.dbfile(DATATMP).delete();

    // return database instance
    return new DiskData(meta, elemNames, attrNames, path, ns);
  }
コード例 #6
0
ファイル: GUICommands.java プロジェクト: JosuaKrause/basex
    @Override
    public void execute(final GUI gui) {
      final DialogExport dialog = new DialogExport(gui);
      if (!dialog.ok()) return;

      final IOFile root = new IOFile(dialog.path());

      // check if existing files will be overwritten
      if (root.exists()) {
        IO file = null;
        boolean overwrite = false;
        final Data d = gui.context.data();
        final IntList il = d.resources.docs();
        final int is = il.size();
        for (int i = 0; i < is; i++) {
          file = root.merge(Token.string(d.text(il.get(i), true)));
          if (file.exists()) {
            if (overwrite) {
              // more than one file will be overwritten; check remaining tests
              file = null;
              break;
            }
            overwrite = true;
          }
        }
        if (overwrite) {
          // show message for overwriting files or directories
          final String msg = file == null ? FILES_REPLACE_X : FILE_EXISTS_X;
          if (file == null) file = root;
          if (!BaseXDialog.confirm(gui, Util.info(msg, file))) return;
        }
      }
      DialogProgress.execute(gui, new Export(root.path()));
    }
コード例 #7
0
ファイル: PermissionTest.java プロジェクト: LukasK/basex
 /**
  * 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));
   }
 }
コード例 #8
0
ファイル: DiskData.java プロジェクト: LukasK/basex
  @Override
  public synchronized void finishUpdate(final MainOptions opts) {
    // remove updating file
    final boolean auto = opts.get(MainOptions.AUTOFLUSH);
    if (auto) {
      final IOFile uf = meta.updateFile();
      if (!uf.exists()) throw Util.notExpected("%: lock file does not exist.", meta.name);
      if (!uf.delete()) throw Util.notExpected("%: could not delete lock file.", meta.name);
    }

    // db:optimize(..., true) will close the database before this function is called
    if (!closed) {
      flush(auto);
      if (!table.lock(false)) throw Util.notExpected("Database '%': could not unlock.", meta.name);
    }
  }
コード例 #9
0
ファイル: BaseX.java プロジェクト: james-jw/basex
 /**
  * Main method, launching the standalone mode. Command-line arguments are listed with the {@code
  * -h} argument.
  *
  * @param args command-line arguments
  */
 public static void main(final String... args) {
   try {
     new BaseX(args);
   } catch (final IOException ex) {
     Util.errln(ex);
     System.exit(1);
   }
 }
コード例 #10
0
ファイル: ProjectList.java プロジェクト: LukasK/basex
 /** Open all selected files externally. */
 private void openExternal() {
   for (final IOFile file : selectedValues()) {
     try {
       file.open();
     } catch (final IOException ex) {
       BaseXDialog.error(project.gui, Util.info(FILE_NOT_OPENED_X, file));
     }
   }
 }
コード例 #11
0
ファイル: DiskBuilder.java プロジェクト: dirkk/basex
 @Override
 public void abort() {
   try {
     close();
   } catch (final IOException ex) {
     Util.debug(ex);
   }
   if (meta != null) DropDB.drop(meta.name, sopts);
 }
コード例 #12
0
ファイル: SessionTest.java プロジェクト: runeengh/basex
 /** 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));
   }
 }
コード例 #13
0
ファイル: DiskData.java プロジェクト: LukasK/basex
 @Override
 public void startUpdate(final MainOptions opts) throws IOException {
   if (!table.lock(true)) throw new BaseXException(Text.DB_PINNED_X, meta.name);
   if (opts.get(MainOptions.AUTOFLUSH)) {
     final IOFile uf = meta.updateFile();
     if (uf.exists()) throw new BaseXException(Text.DB_UPDATED_X, meta.name);
     if (!uf.touch()) throw Util.notExpected("%: could not create lock file.", meta.name);
   }
 }
コード例 #14
0
ファイル: QueryTest.java プロジェクト: runeengh/basex
  /** Tests the specified instance. */
  @Test
  public void test() {
    final StringBuilder sb = new StringBuilder();
    int fail = 0;

    for (final Object[] qu : queries) {
      final boolean correct = qu.length == 3;
      final String query = qu[correct ? 2 : 1].toString();
      final Value cmp = correct ? (Value) qu[1] : null;

      final QueryProcessor qp = new QueryProcessor(query, context);
      try {
        final Value val = qp.value();
        if (!correct || !new DeepCompare().equal(val, cmp)) {
          sb.append("[" + qu[0] + "] " + query);
          String s = correct && cmp.size() != 1 ? "#" + cmp.size() : "";
          sb.append("\n[E" + s + "] ");
          if (correct) {
            final String cp = cmp.toString();
            sb.append('\'');
            sb.append(cp.length() > 1000 ? cp.substring(0, 1000) + "..." : cp);
            sb.append('\'');
          } else {
            sb.append("error");
          }
          final TokenBuilder types = new TokenBuilder();
          for (final Item it : val) types.add(it.type.toString()).add(" ");
          s = val.size() == 1 ? "" : "#" + val.size();
          sb.append("\n[F" + s + "] '" + val + "', " + types + details() + '\n');
          ++fail;
        }
      } catch (final Exception ex) {
        final String msg = ex.getMessage();
        if (correct || msg == null || msg.contains("mailman")) {
          final String cp = correct && cmp.data() != null ? cmp.toString() : "()";
          sb.append(
              "["
                  + qu[0]
                  + "] "
                  + query
                  + "\n[E] "
                  + cp
                  + "\n[F] "
                  + (msg == null ? Util.className(ex) : msg.replaceAll("\r\n?|\n", " "))
                  + ' '
                  + details()
                  + '\n');
          ex.printStackTrace();
          ++fail;
        }
      } finally {
        qp.close();
      }
    }
    if (fail != 0) fail(fail + " Errors. [E] = expected, [F] = found:\n" + sb.toString().trim());
  }
コード例 #15
0
ファイル: RewritingsTest.java プロジェクト: runeengh/basex
  /**
   * Checks if the count function is pre-compiled.
   *
   * @throws Exception exception
   */
  @Test
  public void preEval() throws Exception {
    check("count(1)", "1", "exists(//" + Util.className(Int.class) + ')');

    new CreateDB(NAME, "<xml><a x='y'>1</a><a>2 3</a><a/></xml>").execute(context);
    check("count(//a)", "3", "exists(//" + Util.className(Int.class) + ')');
    check("count(/xml/a)", "3", "exists(//" + Util.className(Int.class) + ')');
    check("count(//text())", "2", "exists(//" + Util.className(Int.class) + ')');
    check("count(//*)", "4", "exists(//" + Util.className(Int.class) + ')');
    check("count(//node())", "6", "exists(//" + Util.className(Int.class) + ')');
    check("count(//comment())", "0", "exists(//" + Util.className(Int.class) + ')');
    check("count(/self::document-node())", "1", "exists(//" + Util.className(Int.class) + ')');
    new DropDB(NAME).execute(context);
  }
コード例 #16
0
ファイル: PermissionTest.java プロジェクト: LukasK/basex
 /** 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));
   }
 }
コード例 #17
0
 @Override
 public void run() {
   try {
     for (int i = 0; i < runs; ++i) {
       Performance.sleep((long) (50 * RND.nextDouble()));
       // Return nth text of the database
       final int n = RND.nextInt() % MAX + 1;
       final String qu = Util.info(QUERY, n);
       new XQuery(qu).execute(context);
     }
   } catch (final BaseXException ex) {
     ex.printStackTrace();
   }
 }
コード例 #18
0
ファイル: DiskBuilder.java プロジェクト: dirkk/basex
  @Override
  protected void addElem(
      final int dist, final int name, final int asize, final int uri, final boolean ne)
      throws IOException {

    tout.write1(asize << 3 | Data.ELEM);
    tout.write2((ne ? 1 << 15 : 0) | name);
    tout.write1(uri);
    tout.write4(dist);
    tout.write4(asize);
    tout.write4(meta.size++);

    if (Prop.debug && (c++ & 0x7FFFF) == 0) Util.err(".");
  }
コード例 #19
0
ファイル: DiskData.java プロジェクト: LukasK/basex
 @Override
 public synchronized void flush(final boolean all) {
   try {
     table.flush(all);
     if (all) {
       write();
       texts.flush();
       values.flush();
       if (textIndex != null) ((DiskValues) textIndex).flush();
       if (attrIndex != null) ((DiskValues) attrIndex).flush();
     }
   } catch (final IOException ex) {
     Util.stack(ex);
   }
 }
コード例 #20
0
ファイル: DiskData.java プロジェクト: LukasK/basex
 @Override
 public synchronized void close() {
   if (closed) return;
   closed = true;
   try {
     write();
     table.close();
     texts.close();
     values.close();
     close(IndexType.TEXT);
     close(IndexType.ATTRIBUTE);
     close(IndexType.FULLTEXT);
   } catch (final IOException ex) {
     Util.stack(ex);
   }
 }
コード例 #21
0
ファイル: Data.java プロジェクト: JosuaKrause/basex
 /**
  * Returns the index reference for the specified index type.
  *
  * @param type index type
  * @return index
  */
 final Index index(final IndexType type) {
   switch (type) {
     case TAG:
       return tagindex;
     case ATTNAME:
       return atnindex;
     case TEXT:
       return txtindex;
     case ATTRIBUTE:
       return atvindex;
     case FULLTEXT:
       return ftxindex;
     case PATH:
       return paths;
     default:
       throw Util.notexpected();
   }
 }
コード例 #22
0
ファイル: PermissionTest.java プロジェクト: LukasK/basex
  /** Set up method. */
  @Before
  public void setUp() {
    try {
      adminSession = createClient();
      if (server.context.users.get(NAME) != null) {
        ok(new DropUser(NAME), adminSession);
      }

      ok(new CreateUser(NAME, NAME), adminSession);
      ok(new CreateDB(RENAMED), adminSession);
      server.context.soptions.set(StaticOptions.REPOPATH, REPO);
      testSession = createClient(NAME, NAME);

      ok(new CreateDB(NAME, "<xml/>"), adminSession);
      ok(new Close(), adminSession);
    } catch (final Exception ex) {
      fail(Util.message(ex));
    }
  }
コード例 #23
0
ファイル: FnHttpTest.java プロジェクト: james-jw/basex
  /**
   * Tests writing of request content when @src is set.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeFromResource() throws IOException {
    // Create a file form which will be read
    final IOFile file = new IOFile(Prop.TMP, Util.className(FnHttpTest.class));
    file.write(token("test"));

    // Request
    final HttpRequest req = new HttpRequest();
    req.payloadAttrs.put("src", file.url());
    req.payloadAttrs.put("method", "binary");
    // HTTP connection
    final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn.getOutputStream(), req);

    // Delete file
    file.delete();

    assertEquals(fakeConn.out.toString(Strings.UTF8), "test");
  }
コード例 #24
0
ファイル: DiskData.java プロジェクト: LukasK/basex
  @Override
  public void createIndex(final IndexType type, final MainOptions options, final Command cmd)
      throws IOException {

    // close existing index
    close(type);
    final IndexBuilder ib;
    switch (type) {
      case TEXT:
        ib = new DiskValuesBuilder(this, options, true);
        break;
      case ATTRIBUTE:
        ib = new DiskValuesBuilder(this, options, false);
        break;
      case FULLTEXT:
        ib = new FTBuilder(this, options);
        break;
      default:
        throw Util.notExpected();
    }
    if (cmd != null) cmd.proc(ib);
    set(type, ib.build());
  }
コード例 #25
0
ファイル: BaseX.java プロジェクト: james-jw/basex
 @Override
 public String header() {
   return Util.info(S_CONSOLE_X, local() ? S_STANDALONE : S_CLIENT);
 }
コード例 #26
0
ファイル: StringParser.java プロジェクト: phspaelti/basex
 /**
  * Parses a single command.
  *
  * @param cmd command definition
  * @return resulting command
  * @throws QueryException query exception
  */
 private Command parse(final Cmd cmd) throws QueryException {
   switch (cmd) {
     case CREATE:
       switch (consume(CmdCreate.class, cmd)) {
         case BACKUP:
           return new CreateBackup(glob(cmd));
         case DATABASE:
         case DB:
           return new CreateDB(name(cmd), remaining(null));
         case INDEX:
           return new CreateIndex(consume(CmdIndex.class, cmd));
         case USER:
           return new CreateUser(name(cmd), password());
         case EVENT:
           return new CreateEvent(name(cmd));
       }
       break;
     case COPY:
       return new Copy(name(cmd), name(cmd));
     case ALTER:
       switch (consume(CmdAlter.class, cmd)) {
         case DATABASE:
         case DB:
           return new AlterDB(name(cmd), name(cmd));
         case USER:
           return new AlterUser(name(cmd), password());
       }
       break;
     case OPEN:
       return new Open(name(cmd));
     case CHECK:
       return new Check(string(cmd));
     case ADD:
       String arg = key(S_TO, null) ? string(cmd) : null;
       return new Add(arg, remaining(cmd));
     case STORE:
       arg = key(S_TO, null) ? string(cmd) : null;
       return new Store(arg, remaining(cmd));
     case RETRIEVE:
       return new Retrieve(string(cmd));
     case DELETE:
       return new Delete(string(cmd));
     case RENAME:
       return new Rename(string(cmd), string(cmd));
     case REPLACE:
       return new Replace(string(cmd), remaining(cmd));
     case INFO:
       switch (consume(CmdInfo.class, cmd)) {
         case NULL:
           return new Info();
         case DATABASE:
         case DB:
           return new InfoDB();
         case INDEX:
           return new InfoIndex(consume(CmdIndexInfo.class, null));
         case STORAGE:
           String arg1 = number(null);
           final String arg2 = arg1 != null ? number(null) : null;
           if (arg1 == null) arg1 = xquery(null);
           return new InfoStorage(arg1, arg2);
       }
       break;
     case INSPECT:
       return new Inspect();
     case CLOSE:
       return new Close();
     case LIST:
       return new List(name(null), string(null));
     case DROP:
       switch (consume(CmdDrop.class, cmd)) {
         case DATABASE:
         case DB:
           return new DropDB(glob(cmd));
         case INDEX:
           return new DropIndex(consume(CmdIndex.class, cmd));
         case USER:
           return new DropUser(glob(cmd), key(ON, null) ? glob(cmd) : null);
         case BACKUP:
           return new DropBackup(glob(cmd));
         case EVENT:
           return new DropEvent(name(cmd));
       }
       break;
     case OPTIMIZE:
       switch (consume(CmdOptimize.class, cmd)) {
         case NULL:
           return new Optimize();
         case ALL:
           return new OptimizeAll();
       }
       break;
     case EXPORT:
       return new Export(string(cmd));
     case XQUERY:
       return new XQuery(xquery(cmd));
     case RUN:
       return new Run(string(cmd));
     case TEST:
       return new Test(string(cmd));
     case EXECUTE:
       return new Execute(string(cmd, false));
     case FIND:
       return new Find(string(cmd, false));
     case CS:
       return new Cs(xquery(cmd));
     case GET:
       return new Get(name(null));
     case SET:
       return new Set(name(cmd), string(null, false));
     case PASSWORD:
       return new Password(password());
     case HELP:
       return new Help(name(null));
     case EXIT:
       return new Exit();
     case FLUSH:
       return new Flush();
     case KILL:
       return new Kill(string(cmd));
     case RESTORE:
       return new Restore(name(cmd));
     case SHOW:
       switch (consume(CmdShow.class, cmd)) {
         case SESSIONS:
           return new ShowSessions();
         case USERS:
           return new ShowUsers(key(ON, null) ? name(cmd) : null);
         case BACKUPS:
           return new ShowBackups();
         case EVENTS:
           return new ShowEvents();
       }
       break;
     case GRANT:
       final CmdPerm perm = consume(CmdPerm.class, cmd);
       if (perm == null) throw help(null, cmd);
       final String db = key(ON, null) ? glob(cmd) : null;
       key(S_TO, cmd);
       return new Grant(perm, glob(cmd), db);
     case REPO:
       switch (consume(CmdRepo.class, cmd)) {
         case INSTALL:
           return new RepoInstall(string(cmd), new InputInfo(parser));
         case DELETE:
           return new RepoDelete(string(cmd), new InputInfo(parser));
         case LIST:
           return new RepoList();
       }
       break;
   }
   throw Util.notExpected("Command specified, but not implemented yet");
 }
コード例 #27
0
ファイル: RESTConcurrencyTest.java プロジェクト: dirkk/basex
 /** Start BaseX HTTP. */
 private void startBaseXHTTP() {
   Util.start(BaseXHTTP.class, "-U" + UserText.ADMIN, "-P" + UserText.ADMIN);
   Performance.sleep(TIMEOUT); // give the server some time to stop
 }
コード例 #28
0
ファイル: RESTConcurrencyTest.java プロジェクト: dirkk/basex
 /** Stop BaseX HTTP. */
 private void stopBaseXHTTP() {
   Util.start(BaseXHTTP.class, "stop");
   Performance.sleep(TIMEOUT); // give the server some time to stop
 }
コード例 #29
0
ファイル: PermissionTest.java プロジェクト: LukasK/basex
/**
 * This class tests user permissions.
 *
 * @author BaseX Team 2005-15, BSD License
 * @author Andreas Weiler
 */
public final class PermissionTest extends SandboxTest {
  /** Name of the database to be renamed. */
  private static final String RENAMED = Util.className(PermissionTest.class) + 'r';
  /** Test folder. */
  private static final String FOLDER = "src/test/resources/";
  /** Test repository. * */
  private static final String REPO = FOLDER + "repo/";

  /** Server reference. */
  private static BaseXServer server;
  /** Admin session. */
  private Session adminSession;
  /** Test session. */
  private Session testSession;

  /**
   * Starts the server.
   *
   * @throws IOException I/O exception
   */
  @BeforeClass
  public static void start() throws IOException {
    server = createServer();
  }

  /**
   * Stops the server.
   *
   * @throws IOException I/O exception
   */
  @AfterClass
  public static void stop() throws IOException {
    stopServer(server);
  }

  /** Set up method. */
  @Before
  public void setUp() {
    try {
      adminSession = createClient();
      if (server.context.users.get(NAME) != null) {
        ok(new DropUser(NAME), adminSession);
      }

      ok(new CreateUser(NAME, NAME), adminSession);
      ok(new CreateDB(RENAMED), adminSession);
      server.context.soptions.set(StaticOptions.REPOPATH, REPO);
      testSession = createClient(NAME, NAME);

      ok(new CreateDB(NAME, "<xml/>"), adminSession);
      ok(new Close(), adminSession);
    } catch (final Exception ex) {
      fail(Util.message(ex));
    }
  }

  /** 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));
    }
  }

  /** Tests all commands where no permission is needed. */
  @Test
  public void noPermsNeeded() {
    ok(new Grant("none", NAME), adminSession);

    ok(new Password(NAME), testSession);
    ok(new Help("list"), testSession);
    ok(new Close(), testSession);
    no(new List(NAME), testSession);
    ok(new List(), testSession);
    no(new Open(NAME), testSession);
    no(new InfoDB(), testSession);
    no(new InfoIndex(), testSession);
    no(new InfoStorage(), testSession);
    no(new Get("DBPATH"), testSession);
    ok(new Get(MainOptions.QUERYINFO), testSession);
    ok(new Set(MainOptions.QUERYINFO, false), testSession);

    // repo stuff
    no(new RepoInstall(REPO + "/pkg3.xar", null), testSession);
    ok(new RepoList(), testSession);
    no(new RepoDelete("http://www.pkg3.com", null), testSession);

    // XQuery read
    no(new XQuery("//xml"), testSession);
    no(new Find(NAME), testSession);
    no(new Optimize(), testSession);
    // XQuery update
    no(
        new XQuery("for $item in doc('" + NAME + "')//xml " + "return rename node $item as 'null'"),
        testSession);
    no(new CreateDB(NAME, "<xml/>"), testSession);
    no(new Rename(RENAMED, RENAMED + '2'), testSession);
    no(new CreateIndex("SUMMARY"), testSession);
    no(new DropDB(NAME), testSession);
    no(new DropIndex("SUMMARY"), testSession);
    no(new CreateUser(NAME, NAME), testSession);
    no(new DropUser(NAME), testSession);
    no(new Kill("dada"), testSession);
    no(new ShowUsers("Users"), testSession);
    no(new Grant("read", NAME), testSession);
    no(new Grant("none", NAME), testSession);
    no(new AlterPassword(NAME, NAME), testSession);
    no(new AlterUser(NAME, "test2"), testSession);
    no(new Flush(), testSession);
  }

  /** Tests all commands where read permission is needed. */
  @Test
  public void readPermsNeeded() {
    ok(new Grant("read", NAME), adminSession);

    ok(new Open(NAME), testSession);
    ok(new List(NAME), testSession);
    ok(new InfoDB(), testSession);
    ok(new InfoStorage("1", "2"), testSession);
    no(new Get("DBPATH"), testSession);
    ok(new Get(MainOptions.QUERYINFO), testSession);
    ok(new Set(MainOptions.QUERYINFO, false), testSession);
    // XQuery read
    ok(new XQuery("//xml"), testSession);
    ok(new Find(NAME), testSession);

    // repo stuff
    no(new RepoInstall(REPO + "/pkg3.xar", null), testSession);
    ok(new RepoList(), testSession);
    no(new RepoDelete("http://www.pkg3.com", null), testSession);

    // XQuery update
    no(new XQuery("for $n in " + DOC.args(NAME) + "//xml return delete node $n"), testSession);
    no(new XQuery(_DB_CREATE.args(NAME)), testSession);
    no(new Optimize(), testSession);
    no(new CreateDB(NAME, "<xml/>"), testSession);
    no(new Replace(RENAMED, "<xml />"), testSession);
    no(new Rename(RENAMED, RENAMED + '2'), testSession);
    no(new CreateIndex("SUMMARY"), testSession);
    no(new DropDB(NAME), testSession);
    no(new DropIndex("SUMMARY"), testSession);
    no(new CreateUser(NAME, NAME), testSession);
    no(new DropUser(NAME), testSession);
    no(new Export(Prop.TMP + NAME), testSession);
    no(new Kill("dada"), testSession);
    no(new ShowUsers("Users"), testSession);
    no(new Grant("read", NAME), testSession);
    no(new Grant("none", NAME), testSession);
    no(new AlterPassword(NAME, NAME), testSession);
    no(new AlterUser(NAME, "test2"), testSession);
    no(new Flush(), testSession);
    ok(new Close(), testSession);
  }

  /** Tests all commands where write permission is needed. */
  @Test
  public void writePermsNeeded() {
    ok(new Grant("write", NAME), adminSession);
    ok(new Open(RENAMED), testSession);
    ok(new Rename(RENAMED, RENAMED + '2'), testSession);
    ok(new Rename(RENAMED + '2', RENAMED), testSession);

    // replace Test
    ok(new Close(), testSession);
    ok(new Open(RENAMED), testSession);
    ok(new Add(NAME + ".xml", "<xml>1</xml>"), testSession);
    ok(new Optimize(), testSession);
    ok(new Replace(NAME + ".xml", "<xmlr>2</xmlr>"), testSession);

    // repo stuff
    no(new RepoInstall(REPO + "/pkg3.xar", null), testSession);
    ok(new RepoList(), testSession);
    no(new RepoDelete("http://www.pkg3.com", null), testSession);

    // XQuery Update
    ok(
        new XQuery("for $item in doc('" + NAME + "')//xml " + "return rename node $item as 'null'"),
        testSession);
    no(new XQuery(_DB_CREATE.args(NAME)), testSession);

    ok(new Optimize(), testSession);
    for (final CmdIndex cmd : CmdIndex.values()) {
      ok(new CreateIndex(cmd), testSession);
    }
    ok(new InfoIndex(), testSession);
    for (final CmdIndex cmd : CmdIndex.values()) {
      ok(new DropIndex(cmd), testSession);
    }
    ok(new Flush(), testSession);
    ok(new Close(), testSession);
    no(new CreateDB(NAME, "<xml/>"), testSession);
    no(new DropDB(NAME), testSession);
    no(new CreateUser(NAME, NAME), testSession);
    no(new DropUser(NAME), testSession);
    no(new Export(Prop.TMP + NAME), testSession);
    no(new Kill("dada"), testSession);
    no(new ShowUsers("Users"), testSession);
    no(new Grant("read", NAME), testSession);
    no(new Grant("none", NAME), testSession);
    no(new AlterPassword(NAME, NAME), testSession);
    no(new AlterUser(NAME, "test2"), testSession);
  }

  /** Tests all commands where create permission is needed. */
  @Test
  public void createPermsNeeded() {
    ok(new Grant("create", NAME), adminSession);
    ok(new XQuery(_DB_CREATE.args(NAME)), testSession);

    ok(new Close(), testSession);
    ok(new CreateDB(NAME, "<xml/>"), testSession);
    for (final CmdIndex cmd : CmdIndex.values()) {
      ok(new CreateIndex(cmd), testSession);
    }
    ok(new Export(Prop.TMP + NAME), testSession);

    // repo stuff
    ok(new RepoInstall(REPO + "/pkg3.xar", null), testSession);
    ok(new RepoList(), testSession);
    ok(new RepoDelete("http://www.pkg3.com", null), testSession);

    no(new CreateUser(NAME, NAME), testSession);
    no(new DropUser(NAME), testSession);
    no(new Kill("dada"), testSession);
    no(new ShowUsers("Users"), testSession);
    no(new Grant("read", NAME), testSession);
    no(new Grant("none", NAME), testSession);
    no(new AlterPassword(NAME, NAME), testSession);
    no(new org.basex.core.cmd.Test(FOLDER + "tests-ok.xqm"), testSession);
  }

  /** Tests all commands where admin permission is needed. */
  @Test
  public void adminPermsNeeded() {
    ok(new Grant(ADMIN, NAME), adminSession);
    if (server.context.users.get("test2") != null) {
      ok(new DropUser("test2"), testSession);
    }
    ok(new CreateUser("test2", NAME), testSession);
    ok(new CreateDB(NAME, "<xml/>"), testSession);
    ok(new ShowUsers(), testSession);
    ok(new Grant(ADMIN, "test2"), testSession);
    ok(new Grant("create", "test2"), testSession);
    ok(new AlterPassword(NAME, NAME), testSession);
    ok(new AlterUser("test2", "test4"), testSession);
    ok(new DropUser("test3"), testSession);
    ok(new Close(), testSession);
    ok(new Close(), adminSession);
    ok(new DropDB(NAME), adminSession);

    // repo stuff
    ok(new RepoInstall(REPO + "/pkg3.xar", null), testSession);
    ok(new RepoList(), testSession);
    ok(new RepoDelete("http://www.pkg3.com", null), testSession);
    ok(new org.basex.core.cmd.Test(FOLDER + "tests-ok.xqm"), testSession);
  }

  /** Drops users. */
  @Test
  public void dropUsers() {
    no(new DropUser(NAME), testSession);
    no(new DropUser(NAME), adminSession);
    ok(new Exit(), testSession);
    // give the server some time to close the client session
    Performance.sleep(50);
    ok(new DropUser(NAME), adminSession);
  }

  /**
   * 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));
    }
  }

  /**
   * 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) {
    }
  }
}