|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.xquery.update; |
| 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; |
| 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; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.ClassRule; |
| 40 | +import org.junit.Test; |
| 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; |
| 47 | +import static org.exist.util.PropertiesBuilder.propertiesBuilder; |
| 48 | +import static org.junit.Assert.assertEquals; |
| 49 | +import static org.junit.Assert.assertTrue; |
| 50 | + |
| 51 | +public class UpdateInsertTriggersDefragTest { |
| 52 | + |
| 53 | + @ClassRule |
| 54 | + public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(propertiesBuilder().put(DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build(), true, true); |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() throws Exception { |
| 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()) { |
| 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 | + } |
| 66 | + |
| 67 | + transaction.commit(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void triggerDefragAfterUpdate() throws Exception { |
| 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 | + ); |
| 77 | + |
| 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 | + ); |
| 82 | + } |
| 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 | + } |
| 98 | +} |
0 commit comments