Search in sources :

Example 16 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class NodeTest method tearDown.

@AfterClass
public static void tearDown() throws EXistException, PermissionDeniedException, IOException, TriggerException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        root = broker.getOrCreateCollection(transaction, XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test"));
        assertNotNull(root);
        broker.removeCollection(transaction, root);
        transact.commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 17 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class NodeTest method document.

@Test
public void document() throws EXistException, LockException, PermissionDeniedException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final LockedDocument lockedDoc = root.getDocumentWithLock(broker, XmldbURI.create("test.xml"), LockMode.READ_LOCK)) {
        final NodeList children = lockedDoc.getDocument().getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            final IStoredNode node = (IStoredNode<?>) children.item(i);
            node.getNodeId();
            node.getNodeName();
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) BrokerPool(org.exist.storage.BrokerPool)

Example 18 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class AuditTrailSessionListenerTest method sessionDestroyed.

/**
 * Ensures that AuditTrailSessionListener releases any locks
 * on the XQuery document when destroying a session
 */
@Test
public void sessionDestroyed() throws EXistException, PermissionDeniedException {
    final HttpSessionEvent httpSessionEvent = createMock(HttpSessionEvent.class);
    final HttpSession httpSession = createMock(HttpSession.class);
    expect(httpSessionEvent.getSession()).andReturn(httpSession);
    expect(httpSession.getId()).andReturn("mock-session");
    replay(httpSessionEvent, httpSession);
    final AuditTrailSessionListener listener = new AuditTrailSessionListener();
    listener.sessionDestroyed(httpSessionEvent);
    verify(httpSessionEvent, httpSession);
    final XmldbURI docUri = XmldbURI.create(DESTROYED_SCRIPT_PATH);
    try (final DBBroker broker = existEmbeddedServer.getBrokerPool().getBroker();
        final LockedDocument lockedResource = broker.getXMLResource(docUri, Lock.LockMode.NO_LOCK)) {
        // ensure that AuditTrailSessionListener released the lock
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        assertFalse(lockManager.isDocumentLockedForRead(docUri));
        assertFalse(lockManager.isDocumentLockedForWrite(docUri));
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) LockedDocument(org.exist.dom.persistent.LockedDocument) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 19 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class AuditTrailSessionListenerTest method sessionCreated.

/**
 * Ensures that AuditTrailSessionListener releases any locks
 * on the XQuery document when creating a session
 */
@Test
public void sessionCreated() throws EXistException, PermissionDeniedException {
    final HttpSessionEvent httpSessionEvent = createMock(HttpSessionEvent.class);
    final HttpSession httpSession = createMock(HttpSession.class);
    expect(httpSessionEvent.getSession()).andReturn(httpSession);
    expect(httpSession.getId()).andReturn("mock-session");
    replay(httpSessionEvent, httpSession);
    final AuditTrailSessionListener listener = new AuditTrailSessionListener();
    listener.sessionCreated(httpSessionEvent);
    verify(httpSessionEvent, httpSession);
    final XmldbURI docUri = XmldbURI.create(CREATE_SCRIPT_PATH);
    try (final DBBroker broker = existEmbeddedServer.getBrokerPool().getBroker();
        final LockedDocument lockedResource = broker.getXMLResource(docUri, Lock.LockMode.NO_LOCK)) {
        // ensure that AuditTrailSessionListener released the lock
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        assertFalse(lockManager.isDocumentLockedForRead(docUri));
        assertFalse(lockManager.isDocumentLockedForWrite(docUri));
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) LockedDocument(org.exist.dom.persistent.LockedDocument) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 20 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class AbstractRealmTest method updateGroup_calls_assertCanModifyGroup.

/*
    @Test
    public void updateAccount_calls_assertCanModifyAccount() throws PermissionDeniedException, EXistException {
        SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
        Configuration mockConfiguration = EasyMock.createMock(Configuration.class);
        Database mockDatabase = EasyMock.createMock(Database.class);
        Subject mockSubject = EasyMock.createMock(Subject.class);

        Account mockAccount = EasyMock.createMockBuilder(AbstractAccount.class)
                .addMockedMethod("getName", new Class[0])
                .addMockedMethod("getGroups", new Class[0])
                .addMockedMethod("assertCanModifyAccount", new Class[]{Account.class})
                .addMockedMethod("getRealm", new Class[0])
                .createNiceMock();
        final String accountName = "someAccount";

        AbstractRealm mockRealm = EasyMock
                .createMockBuilder(AbstractRealm.class)
                .withConstructor(SecurityManager.class, Configuration.class)
                .withArgs(mockSecurityManager, mockConfiguration)
                .addMockedMethod("getDatabase", new Class[0])
                .addMockedMethod("getAccount", new Class[]{Subject.class, String.class})
                .createNiceMock();

        Account mockUpdatingAccount = EasyMock.createMock(Account.class);

        //expectations
        expect(mockRealm.getDatabase()).andReturn(mockDatabase);
        expect(mockDatabase.getCurrentSubject()).andReturn(mockSubject);
        mockAccount.assertCanModifyAccount(mockSubject);
        expect(mockAccount.getName()).andReturn(accountName);
        expect(mockRealm.getAccount(null, accountName)).andReturn(mockUpdatingAccount);
        expect(mockAccount.getGroups()).andReturn(new String[0]);
        expect(mockUpdatingAccount.getGroups()).andReturn(new String[0]);

        replay(mockRealm, mockDatabase, mockSubject, mockUpdatingAccount, mockAccount);

        mockRealm.updateAccount(null, mockAccount);

        verify(mockRealm, mockDatabase, mockSubject, mockUpdatingAccount, mockAccount);
    } */
@Test
public void updateGroup_calls_assertCanModifyGroup() throws PermissionDeniedException, EXistException {
    SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
    Configuration mockConfiguration = EasyMock.createMock(Configuration.class);
    Database mockDatabase = EasyMock.createMock(Database.class);
    DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    Subject mockSubject = EasyMock.createMock(Subject.class);
    Group mockGroup = EasyMock.createMockBuilder(AbstractGroup.class).addMockedMethod("getName", new Class[0]).addMockedMethod("getManagers", new Class[0]).addMockedMethod("assertCanModifyGroup", new Class[] { Account.class }).addMockedMethod("getMetadataKeys", new Class[0]).createNiceMock();
    final String groupName = "someGroup";
    AbstractRealm mockRealm = EasyMock.createMockBuilder(AbstractRealm.class).withConstructor(SecurityManager.class, Configuration.class).withArgs(mockSecurityManager, mockConfiguration).addMockedMethod("getDatabase", new Class[0]).addMockedMethod("getGroup", new Class[] { String.class }).createNiceMock();
    Group mockUpdatingGroup = EasyMock.createNiceMock(Group.class);
    // expectations
    expect(mockRealm.getDatabase()).andReturn(mockDatabase);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker);
    expect(mockBroker.getCurrentSubject()).andReturn(mockSubject);
    mockGroup.assertCanModifyGroup(mockSubject);
    expect(mockGroup.getName()).andReturn(groupName);
    expect(mockRealm.getGroup(groupName)).andReturn(mockUpdatingGroup);
    expect(mockGroup.getManagers()).andReturn(Collections.emptyList());
    expect(mockUpdatingGroup.getManagers()).andReturn(Collections.emptyList());
    expect(mockGroup.getMetadataKeys()).andReturn(Collections.emptySet());
    mockGroup.save();
    replay(mockRealm, mockDatabase, mockBroker, mockGroup, mockSubject, mockUpdatingGroup);
    mockRealm.updateGroup(mockGroup);
    verify(mockRealm, mockDatabase, mockBroker, mockGroup, mockSubject, mockUpdatingGroup);
}
Also used : DBBroker(org.exist.storage.DBBroker) Configuration(org.exist.config.Configuration) Database(org.exist.Database) Test(org.junit.Test)

Aggregations

DBBroker (org.exist.storage.DBBroker)468 BrokerPool (org.exist.storage.BrokerPool)304 Txn (org.exist.storage.txn.Txn)219 Sequence (org.exist.xquery.value.Sequence)185 Test (org.junit.Test)170 XQuery (org.exist.xquery.XQuery)108 Collection (org.exist.collections.Collection)93 TransactionManager (org.exist.storage.txn.TransactionManager)70 EXistException (org.exist.EXistException)66 StringInputSource (org.exist.util.StringInputSource)66 PermissionDeniedException (org.exist.security.PermissionDeniedException)44 Source (org.exist.source.Source)42 StringSource (org.exist.source.StringSource)41 XmldbURI (org.exist.xmldb.XmldbURI)41 CompiledXQuery (org.exist.xquery.CompiledXQuery)39 IOException (java.io.IOException)38 QName (org.exist.dom.QName)37 LockedDocument (org.exist.dom.persistent.LockedDocument)36 Database (org.exist.Database)35 XPathException (org.exist.xquery.XPathException)30