From 69555880f11464e724a744a7446cc6d00d4cb1ac Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Sun, 20 Oct 2024 13:32:59 +0200 Subject: [PATCH] [refactor] Use Embedded API directly. We should not use XML:DB API in tests unless we are intending to explicitly test the XML:DB API implementation --- .../exist/test/ExistXmldbEmbeddedServer.java | 11 --- .../UpdateInsertTriggersDefragTest.java | 86 +++++++++++-------- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/exist-core/src/main/java/org/exist/test/ExistXmldbEmbeddedServer.java b/exist-core/src/main/java/org/exist/test/ExistXmldbEmbeddedServer.java index cd54c9e7b4d..8e1b46546a6 100644 --- a/exist-core/src/main/java/org/exist/test/ExistXmldbEmbeddedServer.java +++ b/exist-core/src/main/java/org/exist/test/ExistXmldbEmbeddedServer.java @@ -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(); diff --git a/exist-core/src/test/java/org/exist/xquery/update/UpdateInsertTriggersDefragTest.java b/exist-core/src/test/java/org/exist/xquery/update/UpdateInsertTriggersDefragTest.java index d9e4fa14578..291b493d63d 100644 --- a/exist-core/src/test/java/org/exist/xquery/update/UpdateInsertTriggersDefragTest.java +++ b/exist-core/src/test/java/org/exist/xquery/update/UpdateInsertTriggersDefragTest.java @@ -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("initial"); - 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("initial"), 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 new node 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 new node 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 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(); + } + } }