Skip to content

Commit b2d7c5e

Browse files
authored
Merge pull request #3935 from adamretter/hotfix/cache-issues
Further tests for Cache Module and a small NPE avoidance fix
2 parents f464770 + 87004d7 commit b2d7c5e

File tree

5 files changed

+1728
-1
lines changed

5 files changed

+1728
-1
lines changed

extensions/modules/cache/src/main/java/org/exist/xquery/modules/cache/Cache.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public CacheConfig getConfig() {
5757
}
5858

5959
public Sequence put(final String key, final Sequence value) {
60-
return store.asMap().put(key, value);
60+
final Sequence previous = store.asMap().put(key, value);
61+
if (previous != null) {
62+
return previous;
63+
}
64+
return Sequence.EMPTY_SEQUENCE;
6165
}
6266

6367
public Sequence list(final String[] keys) throws XPathException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.XPathException;
31+
import org.exist.xquery.value.Sequence;
32+
import org.junit.ClassRule;
33+
import org.junit.Test;
34+
35+
import java.net.URISyntaxException;
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
38+
39+
import static org.junit.Assert.*;
40+
41+
public class LazyCacheTest {
42+
43+
private static Path getLazyConfig() {
44+
try {
45+
return Paths.get(LazyCacheTest.class.getResource("/lazy-cache-conf.xml").toURI());
46+
} catch (final URISyntaxException e) {
47+
throw new IllegalStateException("Unable to find: lazy-cache-conf.xml");
48+
}
49+
}
50+
51+
@ClassRule
52+
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(null, getLazyConfig(), null, true, true);
53+
54+
@Test
55+
public void putOnLazilyCreatedCache() throws XPathException, PermissionDeniedException, EXistException {
56+
Sequence result = executeQuery("cache:put('foo', 'bar', 'baz1')");
57+
assertNotNull(result);
58+
assertTrue(result.isEmpty());
59+
60+
result = executeQuery("cache:put('foo', 'bar', 'baz2')");
61+
assertNotNull(result);
62+
assertEquals(1, result.getItemCount());
63+
assertEquals("baz1", result.itemAt(0).getStringValue());
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)