Skip to content

Commit 2d3976e

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 c530532 commit 2d3976e

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
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: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,68 +21,78 @@
2121
*/
2222
package org.exist.xquery.update;
2323

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;
2430
import org.exist.storage.DBBroker;
25-
import org.exist.test.ExistXmldbEmbeddedServer;
31+
import org.exist.storage.txn.Txn;
32+
import org.exist.test.ExistEmbeddedServer;
2633
import org.exist.test.TestConstants;
27-
import org.junit.After;
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;
2838
import org.junit.Before;
2939
import org.junit.ClassRule;
3040
import org.junit.Test;
31-
import org.xmldb.api.base.Collection;
32-
import org.xmldb.api.base.ResourceSet;
33-
import org.xmldb.api.base.XMLDBException;
34-
import org.xmldb.api.modules.CollectionManagementService;
35-
import org.xmldb.api.modules.XMLResource;
36-
import org.xmldb.api.modules.XPathQueryService;
37-
import org.xmldb.api.modules.XQueryService;
3841

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;
3947
import static org.exist.util.PropertiesBuilder.propertiesBuilder;
4048
import static org.junit.Assert.assertEquals;
49+
import static org.junit.Assert.assertTrue;
4150

4251
public class UpdateInsertTriggersDefragTest {
4352

4453
@ClassRule
45-
public static final ExistXmldbEmbeddedServer exist = new ExistXmldbEmbeddedServer(false, true, true, propertiesBuilder().put(DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build());
46-
47-
private Collection testCollection;
48-
private XQueryService queryService;
49-
private CollectionManagementService collectionService;
50-
51-
/**
52-
* stores XML String and get Query Service
53-
*
54-
* @param documentName to be stored in the DB
55-
* @param content to be stored in the DB
56-
* @throws XMLDBException if an error occurs storing the document
57-
*/
58-
private void storeXML(final String documentName, final String content) throws XMLDBException {
59-
try (final XMLResource doc = testCollection.createResource(documentName, XMLResource.class)) {
60-
doc.setContent(content);
61-
testCollection.storeResource(doc);
62-
}
63-
}
54+
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(propertiesBuilder().put(DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build(), true, true);
6455

6556
@Before
6657
public void setUp() throws Exception {
67-
collectionService = exist.getRoot().getService(CollectionManagementService.class);
68-
testCollection = collectionService.createCollection(TestConstants.TEST_COLLECTION_URI.toString());
69-
queryService = (XQueryService) testCollection.getService(XPathQueryService.class);
70-
storeXML(TestConstants.TEST_XML_URI.toString(), "<list><item>initial</item></list>");
71-
}
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()) {
7261

73-
@After
74-
public void tearDown() throws Exception {
75-
testCollection.close();
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+
}
66+
67+
transaction.commit();
68+
}
7669
}
7770

7871
@Test
7972
public void triggerDefragAfterUpdate() throws Exception {
80-
final String update = "update insert <item>new node</item> into doc('" + TestConstants.TEST_COLLECTION_URI + "/" + TestConstants.TEST_XML_URI.toString() + "')//list";
81-
final ResourceSet updateResult = queryService.queryResource(TestConstants.TEST_XML_URI.toString(), update);
82-
assertEquals("Update expression returns an empty sequence", 0, updateResult.getSize());
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+
);
8377

84-
final ResourceSet itemResult = queryService.queryResource(TestConstants.TEST_XML_URI.toString(), "//item");
85-
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+
);
8682
}
8783

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+
}
8898
}

0 commit comments

Comments
 (0)