Skip to content

Commit

Permalink
[refactor] Use Embedded API directly. We should not use XML:DB API in…
Browse files Browse the repository at this point in the history
… tests unless we are intending to explicitly test the XML:DB API implementation
  • Loading branch information
adamretter committed Oct 24, 2024
1 parent 9ef85e6 commit 6955588
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,6 @@ public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAuto
this.asGuest = asGuest;
}

/**
* @param asGuest Use the guest account, default is the admin account
* @param disableAutoDeploy Whether auto-deployment of XARs should be disabled
* @param useTemporaryStorage Whether the data and journal folder should use temporary storage
* @param settings set properties
*/
public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAutoDeploy, final boolean useTemporaryStorage, final Properties settings) {
this.existEmbeddedServer = new ExistEmbeddedServer(null, null, settings, disableAutoDeploy, useTemporaryStorage);
this.asGuest = asGuest;
}

@Override
protected void before() throws Throwable {
startDb();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,78 @@
*/
package org.exist.xquery.update;

import org.exist.test.ExistXmldbEmbeddedServer;
import org.junit.After;
import com.evolvedbinary.j8fu.function.Consumer2E;
import org.exist.EXistException;
import org.exist.collections.Collection;
import org.exist.security.PermissionDeniedException;
import org.exist.source.StringSource;
import org.exist.storage.BrokerPool;
import org.exist.storage.DBBroker;
import org.exist.storage.txn.Txn;
import org.exist.test.ExistEmbeddedServer;
import org.exist.test.TestConstants;
import org.exist.util.MimeType;
import org.exist.util.StringInputSource;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.Sequence;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.modules.XQueryService;

import java.io.IOException;
import java.util.Optional;

import static org.exist.test.Util.executeQuery;
import static org.exist.test.Util.withCompiledQuery;
import static org.exist.util.PropertiesBuilder.propertiesBuilder;
import static org.exist.storage.DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR;
import static org.exist.test.TestConstants.TEST_COLLECTION_URI;
import static org.exist.test.TestConstants.TEST_XML_URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class UpdateInsertTriggersDefragTest {

@ClassRule
public static final ExistXmldbEmbeddedServer exist = new ExistXmldbEmbeddedServer(false, true, true,
propertiesBuilder().put(PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build());

private final String path = TEST_COLLECTION_URI + "/" + TEST_XML_URI.toString();
private Collection testCollection;
private CollectionManagementService collectionService;
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(propertiesBuilder().put(DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build(), true, true);

@Before
public void setUp() throws Exception {
collectionService = (CollectionManagementService) exist.getRoot().getService("CollectionManagementService","1.0");
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {

testCollection = collectionService.createCollection(TEST_COLLECTION_URI.lastSegment().toString());
try (final XMLResource doc = (XMLResource) testCollection.createResource(TEST_XML_URI.toString(), XMLResource.RESOURCE_TYPE)) {

doc.setContent("<list><item>initial</item></list>");
testCollection.storeResource(doc);
};
}
// store the test document in the test collection
try (final Collection testCollection = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource("<list><item>initial</item></list>"), MimeType.XML_TYPE, testCollection);
}

@After
public void tearDown() throws Exception {
testCollection.close();
transaction.commit();
}
}

@Test
public void triggerDefragAfterUpdate() throws Exception {
final XQueryService queryService = (XQueryService) testCollection.getService("XPathQueryService", "1.0");
final String updateQuery = "update insert <item>new node</item> into doc('" + TestConstants.TEST_COLLECTION_URI + "/" + TestConstants.TEST_XML_URI + "')//list";
assertQuery(updateQuery, updateResults ->
assertTrue("Update expression returns an empty sequence", updateResults.isEmpty())
);

final String update = "update insert <item>new node</item> into doc('" + path + "')//list";
final ResourceSet updateResult = queryService.queryResource(path, update);
assertEquals("Update expression returns an empty sequence", 0, updateResult.getSize());

final ResourceSet itemResult = queryService.queryResource(path, "//item");
assertEquals("Both items are returned", 2, itemResult.getSize());
final String searchQuery = "doc('" + TestConstants.TEST_COLLECTION_URI + "/" + TestConstants.TEST_XML_URI + "')//item";
assertQuery(searchQuery, searchResults ->
assertEquals("Both items are returned", 2, searchResults.getItemCount())
);
}

private void assertQuery(final String query, final Consumer2E<Sequence, XPathException, PermissionDeniedException> assertions) throws EXistException, XPathException, PermissionDeniedException, IOException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {

withCompiledQuery(broker, new StringSource(query), compiledQuery -> {
final Sequence results = executeQuery(broker, compiledQuery);
assertions.accept(results);
return null;
});

transaction.commit();
}
}
}

0 comments on commit 6955588

Please sign in to comment.