Skip to content

Commit

Permalink
enforce-index-use always test
Browse files Browse the repository at this point in the history
  • Loading branch information
marmoure committed May 3, 2023
1 parent 216a13d commit b54a1a5
Showing 1 changed file with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package org.exist.indexing;

import org.exist.EXistException;
import org.exist.TestUtils;
import org.exist.collections.Collection;
import org.exist.collections.CollectionConfigurationException;
import org.exist.collections.CollectionConfigurationManager;
import org.exist.collections.triggers.TriggerException;
import org.exist.dom.persistent.DefaultDocumentSet;
import org.exist.dom.persistent.DocumentSet;
import org.exist.dom.persistent.MutableDocumentSet;
import org.exist.security.PermissionDeniedException;
import org.exist.storage.BrokerPool;
import org.exist.storage.DBBroker;
import org.exist.storage.txn.TransactionManager;
import org.exist.storage.txn.Txn;
import org.exist.test.ExistEmbeddedServer;
import org.exist.test.TestConstants;
import org.exist.util.LockException;
import org.exist.util.MimeType;
import org.exist.util.StringInputSource;
import org.exist.xmldb.XmldbURI;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQuery;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Item;

Check notice on line 26 in extensions/indexes/indexes-integration-tests/src/test/java/org/exist/indexing/EnforceIndexUseTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/indexes/indexes-integration-tests/src/test/java/org/exist/indexing/EnforceIndexUseTest.java#L26

Unused import - org.exist.xquery.value.Item.
import org.exist.xquery.value.Sequence;
import org.junit.*;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

import static org.exist.util.PropertiesBuilder.propertiesBuilder;
import static org.junit.Assert.assertEquals;

public class EnforceIndexUseTest {


@ClassRule
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(
propertiesBuilder()
.put(XQueryContext.PROPERTY_ENFORCE_INDEX_USE, "always") // add a comment about why we are changing this
.build(),
true,
true);
private static final String OLD_RANGE_COLLECTION_CONFIG =
"<collection xmlns=\"http://exist-db.org/collection-config/1.0\">\n" +
" <index xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
" <create qname=\"@bar\" type=\"xs:string\"/>\n" +
" </index>\n" +
"</collection>";
private static final String NEW_RANGE_COLLECTION_CONFIG =
"<collection xmlns=\"http://exist-db.org/collection-config/1.0\">\n" +
" <index xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
" <range>\n" +
" <create qname=\"@bar\" type=\"xs:string\"/>\n" +
" </range>\n" +
" </index>\n" +
"</collection>";

private static final String XML =
"<root>\n" +
" <foo bar=\"baz\"/>\n" +
"</root>";

private Collection test = null;

@Test
public void matchesWithDiffrentIndexStyles() throws CollectionConfigurationException, LockException, IOException, SAXException, PermissionDeniedException, EXistException, XPathException {
// store the xml data and configure it
configureAndStore(NEW_RANGE_COLLECTION_CONFIG, XML, "test.xml", "/new-range-index");
configureAndStore(OLD_RANGE_COLLECTION_CONFIG, XML, "test.xml", "/old-range-index");
configureAndStore(null, XML, "test.xml", "/no-range-index");

//query and expand
final String query = "for $hit in collection(\"" + TestConstants.TEST_COLLECTION_URI.toString() + "\")//foo[matches(@bar, \"^b\")]\n" +
"return $hit";

final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try(final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
final Sequence seq = xquery.execute(broker, query, null);
assertEquals(1, seq.getItemCount());
}
}

private DocumentSet configureAndStore(final String configuration, final String data, final String docName , String path) throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException {
final MutableDocumentSet docs = new DefaultDocumentSet();
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try(final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {

Collection collection = broker.getOrCreateCollection(transaction, XmldbURI.xmldbUriFor(TestConstants.TEST_COLLECTION_URI + path));
if (configuration != null) {
final CollectionConfigurationManager mgr = pool.getConfigurationManager();
mgr.addConfiguration(transaction, broker, collection, configuration);
}
broker.storeDocument(transaction, XmldbURI.create(docName), new StringInputSource(data), MimeType.XML_TYPE, collection);

docs.add(collection.getDocument(broker, XmldbURI.create(docName)));
transact.commit(transaction);
} catch (URISyntaxException e) {
throw new RuntimeException(e);

Check warning on line 106 in extensions/indexes/indexes-integration-tests/src/test/java/org/exist/indexing/EnforceIndexUseTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

extensions/indexes/indexes-integration-tests/src/test/java/org/exist/indexing/EnforceIndexUseTest.java#L106

Avoid throwing raw exception types.
}

return docs;
}

@Before
public void setup() throws EXistException, PermissionDeniedException, IOException, TriggerException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try(final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {

test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
Collection newRange = broker.getOrCreateCollection(transaction, XmldbURI.xmldbUriFor(TestConstants.TEST_COLLECTION_URI + "new-range-index"));
Collection oldRange = broker.getOrCreateCollection(transaction, XmldbURI.xmldbUriFor(TestConstants.TEST_COLLECTION_URI + "old-range-index"));
Collection noRange = broker.getOrCreateCollection(transaction, XmldbURI.xmldbUriFor(TestConstants.TEST_COLLECTION_URI + "no-range-index"));
broker.saveCollection(transaction, test);
broker.saveCollection(transaction, newRange);
broker.saveCollection(transaction, oldRange);
broker.saveCollection(transaction, noRange);

transact.commit(transaction);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@After
public void cleanup() throws EXistException, PermissionDeniedException, IOException, TriggerException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try(final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {

final Collection collConfig = broker.getOrCreateCollection(transaction,
XmldbURI.create(XmldbURI.CONFIG_COLLECTION + "/db"));
broker.removeCollection(transaction, collConfig);

if (test != null) {
broker.removeCollection(transaction, test);
}
transact.commit(transaction);
}
}

@AfterClass
public static void cleanupDb() throws LockException, TriggerException, PermissionDeniedException, EXistException, IOException {
TestUtils.cleanupDB();
}
}

0 comments on commit b54a1a5

Please sign in to comment.