Skip to content

Commit 6955588

Browse files
committed
[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
1 parent 9ef85e6 commit 6955588

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

exist-core/src/main/java/org/exist/test/ExistXmldbEmbeddedServer.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,6 @@ public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAuto
9999
this.asGuest = asGuest;
100100
}
101101

102-
/**
103-
* @param asGuest Use the guest account, default is the admin account
104-
* @param disableAutoDeploy Whether auto-deployment of XARs should be disabled
105-
* @param useTemporaryStorage Whether the data and journal folder should use temporary storage
106-
* @param settings set properties
107-
*/
108-
public ExistXmldbEmbeddedServer(final boolean asGuest, final boolean disableAutoDeploy, final boolean useTemporaryStorage, final Properties settings) {
109-
this.existEmbeddedServer = new ExistEmbeddedServer(null, null, settings, disableAutoDeploy, useTemporaryStorage);
110-
this.asGuest = asGuest;
111-
}
112-
113102
@Override
114103
protected void before() throws Throwable {
115104
startDb();

exist-core/src/test/java/org/exist/xquery/update/UpdateInsertTriggersDefragTest.java

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,78 @@
2121
*/
2222
package org.exist.xquery.update;
2323

24-
import org.exist.test.ExistXmldbEmbeddedServer;
25-
import org.junit.After;
24+
import com.evolvedbinary.j8fu.function.Consumer2E;
25+
import org.exist.EXistException;
26+
import org.exist.collections.Collection;
27+
import org.exist.security.PermissionDeniedException;
28+
import org.exist.source.StringSource;
29+
import org.exist.storage.BrokerPool;
30+
import org.exist.storage.DBBroker;
31+
import org.exist.storage.txn.Txn;
32+
import org.exist.test.ExistEmbeddedServer;
33+
import org.exist.test.TestConstants;
34+
import org.exist.util.MimeType;
35+
import org.exist.util.StringInputSource;
36+
import org.exist.xquery.XPathException;
37+
import org.exist.xquery.value.Sequence;
2638
import org.junit.Before;
2739
import org.junit.ClassRule;
2840
import org.junit.Test;
29-
import org.xmldb.api.base.Collection;
30-
import org.xmldb.api.base.ResourceSet;
31-
import org.xmldb.api.modules.CollectionManagementService;
32-
import org.xmldb.api.modules.XMLResource;
33-
import org.xmldb.api.modules.XQueryService;
3441

42+
import java.io.IOException;
43+
import java.util.Optional;
44+
45+
import static org.exist.test.Util.executeQuery;
46+
import static org.exist.test.Util.withCompiledQuery;
3547
import static org.exist.util.PropertiesBuilder.propertiesBuilder;
36-
import static org.exist.storage.DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR;
37-
import static org.exist.test.TestConstants.TEST_COLLECTION_URI;
38-
import static org.exist.test.TestConstants.TEST_XML_URI;
3948
import static org.junit.Assert.assertEquals;
49+
import static org.junit.Assert.assertTrue;
4050

4151
public class UpdateInsertTriggersDefragTest {
4252

4353
@ClassRule
44-
public static final ExistXmldbEmbeddedServer exist = new ExistXmldbEmbeddedServer(false, true, true,
45-
propertiesBuilder().put(PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build());
46-
47-
private final String path = TEST_COLLECTION_URI + "/" + TEST_XML_URI.toString();
48-
private Collection testCollection;
49-
private CollectionManagementService collectionService;
54+
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(propertiesBuilder().put(DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build(), true, true);
5055

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

55-
testCollection = collectionService.createCollection(TEST_COLLECTION_URI.lastSegment().toString());
56-
try (final XMLResource doc = (XMLResource) testCollection.createResource(TEST_XML_URI.toString(), XMLResource.RESOURCE_TYPE)) {
57-
58-
doc.setContent("<list><item>initial</item></list>");
59-
testCollection.storeResource(doc);
60-
};
61-
}
62+
// store the test document in the test collection
63+
try (final Collection testCollection = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
64+
broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource("<list><item>initial</item></list>"), MimeType.XML_TYPE, testCollection);
65+
}
6266

63-
@After
64-
public void tearDown() throws Exception {
65-
testCollection.close();
67+
transaction.commit();
68+
}
6669
}
6770

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

72-
final String update = "update insert <item>new node</item> into doc('" + path + "')//list";
73-
final ResourceSet updateResult = queryService.queryResource(path, update);
74-
assertEquals("Update expression returns an empty sequence", 0, updateResult.getSize());
75-
76-
final ResourceSet itemResult = queryService.queryResource(path, "//item");
77-
assertEquals("Both items are returned", 2, itemResult.getSize());
78+
final String searchQuery = "doc('" + TestConstants.TEST_COLLECTION_URI + "/" + TestConstants.TEST_XML_URI + "')//item";
79+
assertQuery(searchQuery, searchResults ->
80+
assertEquals("Both items are returned", 2, searchResults.getItemCount())
81+
);
7882
}
7983

84+
private void assertQuery(final String query, final Consumer2E<Sequence, XPathException, PermissionDeniedException> assertions) throws EXistException, XPathException, PermissionDeniedException, IOException {
85+
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
86+
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
87+
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
88+
89+
withCompiledQuery(broker, new StringSource(query), compiledQuery -> {
90+
final Sequence results = executeQuery(broker, compiledQuery);
91+
assertions.accept(results);
92+
return null;
93+
});
94+
95+
transaction.commit();
96+
}
97+
}
8098
}

0 commit comments

Comments
 (0)