|
| 1 | +/* |
| 2 | + * Elemental |
| 3 | + * Copyright (C) 2024, Evolved Binary Ltd |
| 4 | + * |
| 5 | + |
| 6 | + * https://www.evolvedbinary.com | https://www.elemental.xyz |
| 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; version 2.1. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | +package org.exist.xquery.functions.fn; |
| 22 | + |
| 23 | +import org.exist.test.ExistXmldbEmbeddedServer; |
| 24 | +import org.junit.ClassRule; |
| 25 | +import org.junit.Ignore; |
| 26 | +import org.junit.Test; |
| 27 | +import org.xmldb.api.base.XMLDBException; |
| 28 | + |
| 29 | +import static org.junit.Assert.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 33 | + */ |
| 34 | +public class FunXmlToJsonTest { |
| 35 | + |
| 36 | + @ClassRule |
| 37 | + public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void arrayInFnNs() throws XMLDBException { |
| 41 | + final String query = |
| 42 | + "fn:xml-to-json(\n" + |
| 43 | + " <array xmlns=\"http://www.w3.org/2005/xpath-functions\">\n" + |
| 44 | + " <string>Curly</string>\n" + |
| 45 | + " <string>Larry</string>\n" + |
| 46 | + " <string>Moe</string>\n" + |
| 47 | + " </array>\n" + |
| 48 | + ")"; |
| 49 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 50 | + assertEquals("[\"Curly\",\"Larry\",\"Moe\"]", result); |
| 51 | + } |
| 52 | + |
| 53 | + @Ignore("Enable in FunXmlToJson#checkNamespace(String) see: https://github.com/eXist-db/exist/issues/5543") |
| 54 | + @Test |
| 55 | + public void arrayOutsideFnNs() { |
| 56 | + final String query = |
| 57 | + "fn:xml-to-json(\n" + |
| 58 | + " <array>\n" + |
| 59 | + " <string>Curly</string>\n" + |
| 60 | + " <string>Larry</string>\n" + |
| 61 | + " <string>Moe</string>\n" + |
| 62 | + " </array>\n" + |
| 63 | + ")"; |
| 64 | + try { |
| 65 | + existEmbeddedServer.executeOneValue(query); |
| 66 | + } catch (final XMLDBException e) { |
| 67 | + assertTrue(e.getMessage().startsWith("err:FOJS0006")); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + fail("Expected XPathException: err:FOJS0006"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void mapInFnNs() throws XMLDBException { |
| 76 | + final String query = |
| 77 | + "fn:xml-to-json(\n" + |
| 78 | + " <map xmlns=\"http://www.w3.org/2005/xpath-functions\">\n" + |
| 79 | + " <string key=\"Fruit\">Apple</string>\n" + |
| 80 | + " <string key=\"Vegetable\">Carrot</string>\n" + |
| 81 | + " </map>" + |
| 82 | + ")"; |
| 83 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 84 | + assertEquals("{\"Fruit\":\"Apple\",\"Vegetable\":\"Carrot\"}", result); |
| 85 | + } |
| 86 | + |
| 87 | + @Ignore("Enable in FunXmlToJson#checkNamespace(String) see: https://github.com/eXist-db/exist/issues/5543") |
| 88 | + @Test |
| 89 | + public void mapOutsideFnNs() throws XMLDBException { |
| 90 | + final String query = |
| 91 | + "fn:xml-to-json(\n" + |
| 92 | + " <map>\n" + |
| 93 | + " <string key=\"Fruit\">Apple</string>\n" + |
| 94 | + " <string key=\"Vegetable\">Carrot</string>\n" + |
| 95 | + " </map>" + |
| 96 | + ")"; |
| 97 | + try { |
| 98 | + existEmbeddedServer.executeOneValue(query); |
| 99 | + } catch (final XMLDBException e) { |
| 100 | + assertTrue(e.getMessage().startsWith("err:FOJS0006")); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + fail("Expected XPathException: err:FOJS0006"); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments