|
| 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 org.exist.storage.DBBroker; |
| 25 | +import org.exist.test.ExistXmldbEmbeddedServer; |
| 26 | +import org.exist.test.TestConstants; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.ClassRule; |
| 30 | +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 | + |
| 39 | +import static org.exist.util.PropertiesBuilder.propertiesBuilder; |
| 40 | +import static org.junit.Assert.assertEquals; |
| 41 | + |
| 42 | +public class UpdateInsertTriggersDefrag { |
| 43 | + @ClassRule |
| 44 | + public static final ExistXmldbEmbeddedServer exist = new ExistXmldbEmbeddedServer(false, true, true, propertiesBuilder().put(DBBroker.PROPERTY_XUPDATE_FRAGMENTATION_FACTOR, -1).build()); |
| 45 | + final String path = TestConstants.TEST_COLLECTION_URI + "/" + TestConstants.TEST_XML_URI.toString(); |
| 46 | + final String xml = "<list><item>initial</item></list>"; |
| 47 | + Collection testCollection; |
| 48 | + XQueryService queryService; |
| 49 | + 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 | + final XMLResource doc = testCollection.createResource(documentName, XMLResource.class); |
| 60 | + doc.setContent(content); |
| 61 | + testCollection.storeResource(doc); |
| 62 | + } |
| 63 | + |
| 64 | + @Before |
| 65 | + public void setUp() throws Exception { |
| 66 | + collectionService = exist.getRoot().getService(CollectionManagementService.class); |
| 67 | + testCollection = collectionService.createCollection(TestConstants.TEST_COLLECTION_URI.toString()); |
| 68 | + queryService = (XQueryService) testCollection.getService(XPathQueryService.class); |
| 69 | + storeXML(TestConstants.TEST_XML_URI.toString(), xml); |
| 70 | + } |
| 71 | + |
| 72 | + @After |
| 73 | + public void tearDown() throws Exception { |
| 74 | + collectionService.removeCollection(testCollection.getName()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void triggerDefragAfterUpdate() throws Exception { |
| 79 | + final String update = "update insert <item>new node</item> into doc('" + path + "')//list"; |
| 80 | + final ResourceSet updateResult = queryService.queryResource(TestConstants.TEST_XML_URI.toString(), update); |
| 81 | + assertEquals("Update expression returns an empty sequence", 0, updateResult.getSize()); |
| 82 | + |
| 83 | + final ResourceSet itemResult = queryService.queryResource(TestConstants.TEST_XML_URI.toString(), "//item"); |
| 84 | + assertEquals("Both items are returned", 2, itemResult.getSize()); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments