forked from eXist-db/exist
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Add test for setting custom namespace for SQL Module result el…
…ements
- Loading branch information
1 parent
5556f02
commit 8e2473d
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
extensions/modules/sql/src/test/java/org/exist/xquery/modules/sql/ExecuteIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* eXist-db Open Source Native XML Database | ||
* Copyright (C) 2001 The eXist-db Authors | ||
* | ||
* info@exist-db.org | ||
* http://www.exist-db.org | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
package org.exist.xquery.modules.sql; | ||
|
||
import com.evolvedbinary.j8fu.tuple.Tuple2; | ||
import org.exist.EXistException; | ||
import org.exist.dom.memtree.ElementImpl; | ||
import org.exist.security.PermissionDeniedException; | ||
import org.exist.source.Source; | ||
import org.exist.source.StringSource; | ||
import org.exist.storage.BrokerPool; | ||
import org.exist.storage.DBBroker; | ||
import org.exist.storage.txn.Txn; | ||
import org.exist.test.ExistEmbeddedServer; | ||
import org.exist.xquery.XPathException; | ||
import org.exist.xquery.XQueryContext; | ||
import org.exist.xquery.value.Sequence; | ||
import org.exist.xquery.value.Type; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.w3c.dom.Element; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple; | ||
import static org.exist.xquery.modules.sql.Util.executeQuery; | ||
import static org.exist.xquery.modules.sql.Util.withCompiledQuery; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* SQL Execute Integration Tests. | ||
* | ||
* @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a> | ||
*/ | ||
public class ExecuteIT { | ||
|
||
@Rule | ||
public final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(true, true); | ||
|
||
@Rule | ||
public final H2DatabaseResource h2Database = new H2DatabaseResource(); | ||
|
||
@Test | ||
public void executeResultsInSqlNS() throws EXistException, XPathException, PermissionDeniedException, IOException { | ||
executeForNS(SQLModule.NAMESPACE_URI, SQLModule.PREFIX); | ||
} | ||
|
||
@Test | ||
public void executeResultsInCustomNS() throws EXistException, XPathException, PermissionDeniedException, IOException { | ||
executeForNS("http://custom/ns", "custom"); | ||
} | ||
|
||
private void executeForNS(final String namespace, final String prefix) throws EXistException, XPathException, PermissionDeniedException, IOException { | ||
final String mainQuery = | ||
"import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + | ||
"let $conn := sql:get-connection(\"" + h2Database.getDriverClass().getName() + "\", \"" + h2Database.getUrl() + "\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")\n" + | ||
"return\n" + | ||
" sql:execute($conn, \"SELECT 'Hello World' FROM DUAL;\", true(), \"" + namespace + "\", \"" + prefix + "\")"; | ||
|
||
final BrokerPool pool = existEmbeddedServer.getBrokerPool(); | ||
final Source mainQuerySource = new StringSource(mainQuery); | ||
try (final DBBroker broker = pool.getBroker(); | ||
final Txn transaction = pool.getTransactionManager().beginTransaction()) { | ||
|
||
final Tuple2<String, String> namespaceAndPrefix = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> { | ||
final XQueryContext mainQueryContext = mainCompiledQuery.getContext(); | ||
|
||
// execute the query | ||
final Sequence result = executeQuery(broker, mainCompiledQuery); | ||
|
||
// check that the namespace of the result element is in the 'sql' namespace | ||
assertEquals(1, result.getItemCount()); | ||
assertTrue(result.itemAt(0) instanceof Element); | ||
assertEquals(Type.ELEMENT, result.itemAt(0).getType()); | ||
final Element element = (ElementImpl) result.itemAt(0); | ||
|
||
return Tuple(element.getNamespaceURI(), element.getPrefix()); | ||
}); | ||
|
||
assertEquals(namespace, namespaceAndPrefix._1); | ||
assertEquals(prefix, namespaceAndPrefix._2); | ||
|
||
transaction.commit(); | ||
} | ||
} | ||
} |