private <R> R modifyCollection(
      DBBroker broker,
      XmldbURI collectionURI,
      DatabaseItemModifier<org.exist.collections.Collection, R> modifier)
      throws XMLDBException, LockException, PermissionDeniedException, IOException, EXistException,
          TriggerException, SyntaxException {
    final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
    final Txn transaction = transact.beginTransaction();

    org.exist.collections.Collection coll = null;

    try {
      coll = broker.openCollection(collectionURI, Lock.WRITE_LOCK);
      if (coll == null) {
        throw new XMLDBException(
            ErrorCodes.INVALID_COLLECTION, "Collection " + collectionURI.toString() + " not found");
      }

      final R result = modifier.modify(coll);

      broker.saveCollection(transaction, coll);
      transact.commit(transaction);
      broker.flush();

      return result;

    } catch (final EXistException ee) {
      transact.abort(transaction);
      throw ee;
    } catch (final XMLDBException xmldbe) {
      transact.abort(transaction);
      throw xmldbe;
    } catch (final LockException le) {
      transact.abort(transaction);
      throw le;
    } catch (final PermissionDeniedException pde) {
      transact.abort(transaction);
      throw pde;
    } catch (final IOException ioe) {
      transact.abort(transaction);
      throw ioe;
    } catch (final TriggerException te) {
      transact.abort(transaction);
      throw te;
    } catch (final SyntaxException se) {
      transact.abort(transaction);
      throw se;
    } finally {
      transact.close(transaction);
      if (coll != null) {
        coll.release(Lock.WRITE_LOCK);
      }
    }
  }
  @BeforeClass
  public static void setUp() throws Exception {
    TransactionManager transact = null;
    Txn transaction = null;
    try {
      pool = startDB();
      broker = pool.get(pool.getSecurityManager().getSystemSubject());
      transact = pool.getTransactionManager();
      transaction = transact.beginTransaction();

      root =
          broker.getOrCreateCollection(
              transaction, XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test"));
      broker.saveCollection(transaction, root);

      String existHome = System.getProperty("exist.home");
      File existDir = existHome == null ? new File(".") : new File(existHome);
      String directory = "samples/shakespeare";
      File dir = new File(existDir, directory);

      // store some documents.
      for (File f : dir.listFiles(new XMLFilenameFilter())) {
        IndexInfo info =
            root.validateXMLResource(
                transaction,
                broker,
                XmldbURI.create(f.getName()),
                new InputSource(f.toURI().toASCIIString()));
        root.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false);
      }

      IndexInfo info =
          root.validateXMLResource(transaction, broker, XmldbURI.create("nested.xml"), NESTED_XML);
      root.store(transaction, broker, info, NESTED_XML, false);
      transact.commit(transaction);

      // for the tests
      docs = root.allDocs(broker, new DefaultDocumentSet(), true);
      seqSpeech = executeQuery(broker, "//SPEECH", 2628, null);

    } catch (Exception e) {
      if (pool != null) {
        pool.release(broker);
        BrokerPool.stopAll(false);
        pool = null;
        root = null;
      }
      throw e;
    }
  }