|
21 | 21 | */
|
22 | 22 | package org.exist.xquery.update;
|
23 | 23 |
|
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; |
26 | 38 | import org.junit.Before;
|
27 | 39 | import org.junit.ClassRule;
|
28 | 40 | 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; |
34 | 41 |
|
| 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; |
35 | 47 | 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; |
39 | 48 | import static org.junit.Assert.assertEquals;
|
| 49 | +import static org.junit.Assert.assertTrue; |
40 | 50 |
|
41 | 51 | public class UpdateInsertTriggersDefragTest {
|
42 | 52 |
|
43 | 53 | @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); |
50 | 55 |
|
51 | 56 | @Before
|
52 | 57 | 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()) { |
54 | 61 |
|
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 | + } |
62 | 66 |
|
63 |
| - @After |
64 |
| - public void tearDown() throws Exception { |
65 |
| - testCollection.close(); |
| 67 | + transaction.commit(); |
| 68 | + } |
66 | 69 | }
|
67 | 70 |
|
68 | 71 | @Test
|
69 | 72 | 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 | + ); |
71 | 77 |
|
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 | + ); |
78 | 82 | }
|
79 | 83 |
|
| 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 | + } |
80 | 98 | }
|
0 commit comments