|
21 | 21 | */
|
22 | 22 | package org.exist.xquery.update;
|
23 | 23 |
|
| 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; |
24 | 30 | import org.exist.storage.DBBroker;
|
25 |
| -import org.exist.test.ExistXmldbEmbeddedServer; |
| 31 | +import org.exist.storage.txn.Txn; |
| 32 | +import org.exist.test.ExistEmbeddedServer; |
26 | 33 | 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; |
28 | 38 | import org.junit.Before;
|
29 | 39 | import org.junit.ClassRule;
|
30 | 40 | 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; |
38 | 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; |
39 | 47 | import static org.exist.util.PropertiesBuilder.propertiesBuilder;
|
40 | 48 | import static org.junit.Assert.assertEquals;
|
| 49 | +import static org.junit.Assert.assertTrue; |
41 | 50 |
|
42 | 51 | public class UpdateInsertTriggersDefragTest {
|
43 | 52 |
|
44 | 53 | @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); |
64 | 55 |
|
65 | 56 | @Before
|
66 | 57 | 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()) { |
72 | 61 |
|
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 | + } |
76 | 69 | }
|
77 | 70 |
|
78 | 71 | @Test
|
79 | 72 | 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 | + ); |
83 | 77 |
|
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 | + ); |
86 | 82 | }
|
87 | 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 | + } |
88 | 98 | }
|
0 commit comments