|
| 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.xmldb; |
| 23 | + |
| 24 | +import org.exist.TestUtils; |
| 25 | +import org.exist.test.ExistWebServer; |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.ClassRule; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.junit.runners.Parameterized; |
| 32 | +import org.xmldb.api.DatabaseManager; |
| 33 | +import org.xmldb.api.base.*; |
| 34 | +import org.xmldb.api.modules.CollectionManagementService; |
| 35 | +import org.xmldb.api.modules.XQueryService; |
| 36 | + |
| 37 | +import java.math.BigDecimal; |
| 38 | +import java.math.BigInteger; |
| 39 | +import java.util.Arrays; |
| 40 | + |
| 41 | +import static org.junit.Assert.assertEquals; |
| 42 | +import static org.junit.Assert.assertNotNull; |
| 43 | + |
| 44 | +@RunWith(Parameterized.class) |
| 45 | +public class DeclareVariableTest { |
| 46 | + |
| 47 | + private static final String TEST_COLLECTION_NAME = "xmldb-declare-variable-test"; |
| 48 | + |
| 49 | + @ClassRule |
| 50 | + public static final ExistWebServer existWebServer = new ExistWebServer(true, false, true, true); |
| 51 | + private static final String PORT_PLACEHOLDER = "${PORT}"; |
| 52 | + |
| 53 | + @Parameterized.Parameters(name = "{0}") |
| 54 | + public static java.util.Collection<Object[]> data() { |
| 55 | + return Arrays.asList(new Object[][] { |
| 56 | + { "local", "xmldb:exist://" }, |
| 57 | + { "remote", "xmldb:exist://localhost:" + PORT_PLACEHOLDER + "/xmlrpc" } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @Parameterized.Parameter |
| 62 | + public String apiName; |
| 63 | + |
| 64 | + @Parameterized.Parameter(value = 1) |
| 65 | + public String baseUri; |
| 66 | + |
| 67 | + private Collection testCollection; |
| 68 | + |
| 69 | + private final String getBaseUri() { |
| 70 | + return baseUri.replace(PORT_PLACEHOLDER, Integer.toString(existWebServer.getPort())); |
| 71 | + } |
| 72 | + |
| 73 | + @Before |
| 74 | + public void setUp() throws XMLDBException { |
| 75 | + final Collection root = DatabaseManager.getCollection(getBaseUri() + "/db", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD); |
| 76 | + final CollectionManagementService service = (CollectionManagementService) root.getService("CollectionManagementService", "1.0"); |
| 77 | + testCollection = service.createCollection(TEST_COLLECTION_NAME); |
| 78 | + assertNotNull(testCollection); |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void tearDown() throws XMLDBException { |
| 83 | + final Collection root = DatabaseManager.getCollection(getBaseUri() + "/db", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD); |
| 84 | + final CollectionManagementService service = (CollectionManagementService) root.getService("CollectionManagementService", "1.0"); |
| 85 | + service.removeCollection(TEST_COLLECTION_NAME); |
| 86 | + testCollection = null; |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void declareBigInteger() throws XMLDBException { |
| 91 | + final Resource result = executeQueryWithExternalVariable(new BigInteger("123456789123456789123456789")); |
| 92 | + assertEquals("123456789123456789123456789", result.getContent()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void declareBigDecimal() throws XMLDBException { |
| 97 | + final Resource result = executeQueryWithExternalVariable(new BigDecimal("1.1")); |
| 98 | + assertEquals("1.1", result.getContent()); |
| 99 | + } |
| 100 | + |
| 101 | + private Resource executeQueryWithExternalVariable(final Object value) throws XMLDBException { |
| 102 | + final XQueryService xqueryService = (XQueryService) testCollection.getService("XQueryService", "1.0"); |
| 103 | + xqueryService.declareVariable("x", value); |
| 104 | + |
| 105 | + final String query = |
| 106 | + "xquery version \"3.1\";\n" + |
| 107 | + "declare variable $x external;\n" + |
| 108 | + "$x"; |
| 109 | + |
| 110 | + final CompiledExpression compiled = xqueryService.compile(query); |
| 111 | + |
| 112 | + final ResourceSet resourceSet = xqueryService.execute(compiled); |
| 113 | + assertEquals(1, resourceSet.getSize()); |
| 114 | + final Resource resource = resourceSet.getResource(0); |
| 115 | + assertNotNull(resource); |
| 116 | + return resource; |
| 117 | + } |
| 118 | +} |
0 commit comments