|
| 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.modules.cache; |
| 23 | + |
| 24 | +import org.exist.EXistException; |
| 25 | +import org.exist.security.PermissionDeniedException; |
| 26 | +import org.exist.storage.BrokerPool; |
| 27 | +import org.exist.storage.DBBroker; |
| 28 | +import org.exist.storage.txn.Txn; |
| 29 | +import org.exist.test.ExistEmbeddedServer; |
| 30 | +import org.exist.xquery.ErrorCodes; |
| 31 | +import org.exist.xquery.XPathException; |
| 32 | +import org.exist.xquery.value.Sequence; |
| 33 | +import org.junit.ClassRule; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +import java.net.URISyntaxException; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.nio.file.Paths; |
| 39 | + |
| 40 | +import static org.junit.Assert.*; |
| 41 | + |
| 42 | +public class NonLazyCacheTest { |
| 43 | + |
| 44 | + private static Path getLazyConfig() { |
| 45 | + try { |
| 46 | + return Paths.get(NonLazyCacheTest.class.getResource("/non-lazy-cache-conf.xml").toURI()); |
| 47 | + } catch (final URISyntaxException e) { |
| 48 | + throw new IllegalStateException("Unable to find: non-lazy-cache-conf.xml"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @ClassRule |
| 53 | + public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(null, getLazyConfig(), null, true, true); |
| 54 | + |
| 55 | + @Test |
| 56 | + public void putOnNonLazilyCreatedCacheWithoutExplicitCreation() throws XPathException, PermissionDeniedException, EXistException { |
| 57 | + try { |
| 58 | + executeQuery("cache:put('foo', 'bar', 'baz1')"); |
| 59 | + fail("Should not be able to lazily create a cache when lazy creation is disabled"); |
| 60 | + } catch (final XPathException e) { |
| 61 | + final ErrorCodes.ErrorCode errorCode = e.getErrorCode(); |
| 62 | + assertEquals("Expected lazy creation disabled error", CacheModule.LAZY_CREATION_DISABLED, errorCode); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static Sequence executeQuery(final String query) throws EXistException, PermissionDeniedException, XPathException { |
| 67 | + final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool(); |
| 68 | + try (final DBBroker broker = brokerPool.getBroker(); |
| 69 | + final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) { |
| 70 | + |
| 71 | + final Sequence result = brokerPool.getXQueryService().execute(broker, query, null); |
| 72 | + |
| 73 | + transaction.commit(); |
| 74 | + |
| 75 | + return result; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments