Skip to content

Commit 8b7abdd

Browse files
committed
[bugfix] Fix integration tests
1 parent 1b0601a commit 8b7abdd

File tree

6 files changed

+206
-76
lines changed

6 files changed

+206
-76
lines changed

extensions/modules/sql/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@
142142
<exclude>src/main/java/org/exist/xquery/modules/sql/SQLModule.java</exclude>
143143
<exclude>src/test/resources/jndi.properties</exclude>
144144
<exclude>src/test/java/org/exist/xquery/modules/sql/ConnectionIT.java</exclude>
145+
<exclude>src/test/java/org/exist/xquery/modules/sql/ConnectionPoolIT.java</exclude>
145146
<exclude>src/test/java/org/exist/xquery/modules/sql/H2DatabaseResource.java</exclude>
146147
<exclude>src/test/java/org/exist/xquery/modules/sql/JndiConnectionIT.java</exclude>
148+
<exclude>src/test/java/org/exist/xquery/modules/sql/Util.java</exclude>
147149
</excludes>
148150
</licenseSet>
149151

@@ -158,8 +160,10 @@
158160
<include>src/main/java/org/exist/xquery/modules/sql/SQLModule.java</include>
159161
<include>src/test/resources/jndi.properties</include>
160162
<include>src/test/java/org/exist/xquery/modules/sql/ConnectionIT.java</include>
163+
<include>src/test/java/org/exist/xquery/modules/sql/ConnectionPoolIT.java</include>
161164
<include>src/test/java/org/exist/xquery/modules/sql/H2DatabaseResource.java</include>
162165
<include>src/test/java/org/exist/xquery/modules/sql/JndiConnectionIT.java</include>
166+
<include>src/test/java/org/exist/xquery/modules/sql/Util.java</include>
163167
</includes>
164168

165169
</licenseSet>
@@ -171,6 +175,14 @@
171175
<plugin>
172176
<groupId>org.apache.maven.plugins</groupId>
173177
<artifactId>maven-failsafe-plugin</artifactId>
178+
<executions>
179+
<execution>
180+
<goals>
181+
<goal>integration-test</goal>
182+
<goal>verify</goal>
183+
</goals>
184+
</execution>
185+
</executions>
174186
</plugin>
175187

176188
</plugins>

extensions/modules/sql/src/test/java/org/exist/xquery/modules/sql/ConnectionIT.java

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
*/
3333
package org.exist.xquery.modules.sql;
3434

35-
import com.evolvedbinary.j8fu.function.Function2E;
3635
import com.evolvedbinary.j8fu.tuple.Tuple2;
3736
import org.exist.EXistException;
3837
import org.exist.collections.Collection;
@@ -42,15 +41,12 @@
4241
import org.exist.source.StringSource;
4342
import org.exist.storage.BrokerPool;
4443
import org.exist.storage.DBBroker;
45-
import org.exist.storage.XQueryPool;
4644
import org.exist.storage.lock.Lock;
4745
import org.exist.storage.txn.Txn;
4846
import org.exist.test.ExistEmbeddedServer;
4947
import org.exist.util.LockException;
5048
import org.exist.xmldb.XmldbURI;
51-
import org.exist.xquery.CompiledXQuery;
5249
import org.exist.xquery.XPathException;
53-
import org.exist.xquery.XQuery;
5450
import org.exist.xquery.XQueryContext;
5551
import org.exist.xquery.modules.ModuleUtils;
5652
import org.exist.xquery.value.Sequence;
@@ -61,10 +57,11 @@
6157
import java.io.IOException;
6258
import java.util.Map;
6359
import java.util.Optional;
64-
import java.util.Properties;
6560

6661
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
6762
import static java.nio.charset.StandardCharsets.UTF_8;
63+
import static org.exist.xquery.modules.sql.Util.executeQuery;
64+
import static org.exist.xquery.modules.sql.Util.withCompiledQuery;
6865
import static org.junit.Assert.assertEquals;
6966
import static org.junit.Assert.assertTrue;
7067

@@ -152,35 +149,6 @@ public void getConnectionFromModuleIsAutomaticallyClosed() throws EXistException
152149
}
153150
}
154151

155-
@Test
156-
public void getConnectionFromPoolIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
157-
// NOTE: pool-1 is configured in src/test/resources-filtered/conf.xml
158-
final String query =
159-
"import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" +
160-
"sql:get-connection-from-pool(\"pool-1\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")";
161-
162-
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
163-
final Source source = new StringSource(query);
164-
try (final DBBroker broker = pool.getBroker();
165-
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
166-
167-
// execute query
168-
final Tuple2<XQueryContext, Boolean> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
169-
final Sequence result = executeQuery(broker, compiledXQuery);
170-
return Tuple(compiledXQuery.getContext(), !result.isEmpty());
171-
});
172-
173-
// check that the handle for the sql connection that was created is valid
174-
assertTrue(contextAndResult._2);
175-
176-
// check the connections were closed
177-
final int connectionsCount = ModuleUtils.readContextMap(contextAndResult._1, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
178-
assertEquals(0, connectionsCount);
179-
180-
transaction.commit();
181-
}
182-
}
183-
184152
@Test
185153
public void getConnectionCanBeExplicitlyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
186154
final String query =
@@ -209,44 +177,4 @@ public void getConnectionCanBeExplicitlyClosed() throws EXistException, XPathExc
209177
transaction.commit();
210178
}
211179
}
212-
213-
private Sequence executeQuery(final DBBroker broker, final CompiledXQuery compiledXQuery) throws PermissionDeniedException, XPathException {
214-
final BrokerPool pool = broker.getBrokerPool();
215-
final XQuery xqueryService = pool.getXQueryService();
216-
return xqueryService.execute(broker, compiledXQuery, null, new Properties());
217-
}
218-
219-
private <T> T withCompiledQuery(final DBBroker broker, final Source source, final Function2E<CompiledXQuery, T, XPathException, PermissionDeniedException> op) throws XPathException, PermissionDeniedException, IOException {
220-
final BrokerPool pool = broker.getBrokerPool();
221-
final XQuery xqueryService = pool.getXQueryService();
222-
final XQueryPool xqueryPool = pool.getXQueryPool();
223-
final CompiledXQuery compiledQuery = compileQuery(broker, xqueryService, xqueryPool, source);
224-
try {
225-
return op.apply(compiledQuery);
226-
} finally {
227-
if (compiledQuery != null) {
228-
xqueryPool.returnCompiledXQuery(source, compiledQuery);
229-
}
230-
}
231-
}
232-
233-
private CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
234-
CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
235-
XQueryContext context;
236-
if (compiled == null) {
237-
context = new XQueryContext(broker.getBrokerPool());
238-
} else {
239-
context = compiled.getContext();
240-
context.prepareForReuse();
241-
}
242-
243-
if (compiled == null) {
244-
compiled = xqueryService.compile(broker, context, query);
245-
} else {
246-
compiled.getContext().updateContext(context);
247-
context.getWatchDog().reset();
248-
}
249-
250-
return compiled;
251-
}
252180
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2014, Evolved Binary Ltd
3+
*
4+
* This file was originally ported from FusionDB to eXist-db by
5+
* Evolved Binary, for the benefit of the eXist-db Open Source community.
6+
* Only the ported code as it appears in this file, at the time that
7+
* it was contributed to eXist-db, was re-licensed under The GNU
8+
* Lesser General Public License v2.1 only for use in eXist-db.
9+
*
10+
* This license grant applies only to a snapshot of the code as it
11+
* appeared when ported, it does not offer or infer any rights to either
12+
* updates of this source code or access to the original source code.
13+
*
14+
* The GNU Lesser General Public License v2.1 only license follows.
15+
*
16+
* ---------------------------------------------------------------------
17+
*
18+
* Copyright (C) 2014, Evolved Binary Ltd
19+
*
20+
* This library is free software; you can redistribute it and/or
21+
* modify it under the terms of the GNU Lesser General Public
22+
* License as published by the Free Software Foundation; version 2.1.
23+
*
24+
* This library is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27+
* Lesser General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU Lesser General Public
30+
* License along with this library; if not, write to the Free Software
31+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32+
*/
33+
package org.exist.xquery.modules.sql;
34+
35+
import com.evolvedbinary.j8fu.tuple.Tuple2;
36+
import org.exist.EXistException;
37+
import org.exist.security.PermissionDeniedException;
38+
import org.exist.source.Source;
39+
import org.exist.source.StringSource;
40+
import org.exist.storage.BrokerPool;
41+
import org.exist.storage.DBBroker;
42+
import org.exist.storage.txn.Txn;
43+
import org.exist.test.ExistEmbeddedServer;
44+
import org.exist.xquery.XPathException;
45+
import org.exist.xquery.XQueryContext;
46+
import org.exist.xquery.modules.ModuleUtils;
47+
import org.exist.xquery.value.Sequence;
48+
import org.junit.Rule;
49+
import org.junit.Test;
50+
51+
import java.io.IOException;
52+
import java.util.Map;
53+
54+
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
55+
import static org.exist.xquery.modules.sql.Util.executeQuery;
56+
import static org.exist.xquery.modules.sql.Util.withCompiledQuery;
57+
import static org.junit.Assert.assertEquals;
58+
import static org.junit.Assert.assertTrue;
59+
60+
public class ConnectionPoolIT {
61+
62+
@Rule
63+
public ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(true, true);
64+
65+
@Test
66+
public void getConnectionFromPoolIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
67+
// NOTE: pool-1 is configured in src/test/resources-filtered/conf.xml
68+
final String query =
69+
"import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" +
70+
"sql:get-connection-from-pool(\"pool-1\")";
71+
72+
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
73+
final Source source = new StringSource(query);
74+
try (final DBBroker broker = pool.getBroker();
75+
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
76+
77+
// execute query
78+
final Tuple2<XQueryContext, Boolean> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
79+
final Sequence result = executeQuery(broker, compiledXQuery);
80+
return Tuple(compiledXQuery.getContext(), !result.isEmpty());
81+
});
82+
83+
// check that the handle for the sql connection that was created is valid
84+
assertTrue(contextAndResult._2);
85+
86+
// check the connections were closed
87+
final int connectionsCount = ModuleUtils.readContextMap(contextAndResult._1, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
88+
assertEquals(0, connectionsCount);
89+
90+
transaction.commit();
91+
}
92+
}
93+
}

extensions/modules/sql/src/test/java/org/exist/xquery/modules/sql/H2DatabaseResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class H2DatabaseResource extends ExternalResource {
5252

5353
private static final Logger LOG = LogManager.getLogger(H2DatabaseResource.class);
5454

55-
private static final String DEFAULT_URL = "jdbc:h2:~/test";
55+
private static final String DEFAULT_URL = "jdbc:h2:mem:test-1";
5656
private static final String DEFAULT_USER = "sa";
5757
private static final String DEFAULT_PASSWORD = "sa";
5858

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (C) 2014, Evolved Binary Ltd
3+
*
4+
* This file was originally ported from FusionDB to eXist-db by
5+
* Evolved Binary, for the benefit of the eXist-db Open Source community.
6+
* Only the ported code as it appears in this file, at the time that
7+
* it was contributed to eXist-db, was re-licensed under The GNU
8+
* Lesser General Public License v2.1 only for use in eXist-db.
9+
*
10+
* This license grant applies only to a snapshot of the code as it
11+
* appeared when ported, it does not offer or infer any rights to either
12+
* updates of this source code or access to the original source code.
13+
*
14+
* The GNU Lesser General Public License v2.1 only license follows.
15+
*
16+
* ---------------------------------------------------------------------
17+
*
18+
* Copyright (C) 2014, Evolved Binary Ltd
19+
*
20+
* This library is free software; you can redistribute it and/or
21+
* modify it under the terms of the GNU Lesser General Public
22+
* License as published by the Free Software Foundation; version 2.1.
23+
*
24+
* This library is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27+
* Lesser General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU Lesser General Public
30+
* License along with this library; if not, write to the Free Software
31+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32+
*/
33+
package org.exist.xquery.modules.sql;
34+
35+
import com.evolvedbinary.j8fu.function.Function2E;
36+
import org.exist.security.PermissionDeniedException;
37+
import org.exist.source.Source;
38+
import org.exist.storage.BrokerPool;
39+
import org.exist.storage.DBBroker;
40+
import org.exist.storage.XQueryPool;
41+
import org.exist.xquery.CompiledXQuery;
42+
import org.exist.xquery.XPathException;
43+
import org.exist.xquery.XQuery;
44+
import org.exist.xquery.XQueryContext;
45+
import org.exist.xquery.value.Sequence;
46+
47+
import java.io.IOException;
48+
import java.util.Properties;
49+
50+
/**
51+
* Tests utility methods.
52+
*
53+
* @author <a href="mailto:[email protected]">Adam Retter</a>
54+
*/
55+
public class Util {
56+
static Sequence executeQuery(final DBBroker broker, final CompiledXQuery compiledXQuery) throws PermissionDeniedException, XPathException {
57+
final BrokerPool pool = broker.getBrokerPool();
58+
final XQuery xqueryService = pool.getXQueryService();
59+
return xqueryService.execute(broker, compiledXQuery, null, new Properties());
60+
}
61+
62+
static <T> T withCompiledQuery(final DBBroker broker, final Source source, final Function2E<CompiledXQuery, T, XPathException, PermissionDeniedException> op) throws XPathException, PermissionDeniedException, IOException {
63+
final BrokerPool pool = broker.getBrokerPool();
64+
final XQuery xqueryService = pool.getXQueryService();
65+
final XQueryPool xqueryPool = pool.getXQueryPool();
66+
final CompiledXQuery compiledQuery = compileQuery(broker, xqueryService, xqueryPool, source);
67+
try {
68+
return op.apply(compiledQuery);
69+
} finally {
70+
if (compiledQuery != null) {
71+
xqueryPool.returnCompiledXQuery(source, compiledQuery);
72+
}
73+
}
74+
}
75+
76+
static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
77+
CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
78+
XQueryContext context;
79+
if (compiled == null) {
80+
context = new XQueryContext(broker.getBrokerPool());
81+
} else {
82+
context = compiled.getContext();
83+
context.prepareForReuse();
84+
}
85+
86+
if (compiled == null) {
87+
compiled = xqueryService.compile(broker, context, query);
88+
} else {
89+
compiled.getContext().updateContext(context);
90+
context.getWatchDog().reset();
91+
}
92+
93+
return compiled;
94+
}
95+
}

extensions/modules/sql/src/test/resources-filtered/conf.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@
747747
<module uri="http://exist-db.org/xquery/sql" class="org.exist.xquery.modules.sql.SQLModule">
748748
<parameter name="pool.1.name" value="pool-1"/>
749749
<parameter name="pool.1.properties.dataSourceClassName" value="org.h2.jdbcx.JdbcDataSource"/>
750-
<parameter name="pool.1.properties.jdbcUrl" value="jdbc:h2:~/test"/>
750+
<parameter name="pool.1.properties.dataSource.url" value="jdbc:h2:mem:test-pool-1"/>
751+
<parameter name="pool.1.properties.dataSource.user" value="sa"/>
752+
<parameter name="pool.1.properties.dataSource.password" value="sa"/>
751753
<parameter name="pool.1.properties.maximumPoolSize" value="10"/>
752754
</module>
753755

0 commit comments

Comments
 (0)