|
| 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.Test; |
| 26 | +import org.xmldb.api.base.XMLDBException; |
| 27 | + |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | + |
| 30 | +import static org.exist.util.ByteOrderMark.*; |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 35 | + */ |
| 36 | +public class ParsingFunctionsTest { |
| 37 | + |
| 38 | + private static final String UTF8_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
| 39 | + |
| 40 | + @ClassRule |
| 41 | + public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true); |
| 42 | + |
| 43 | + @Test |
| 44 | + public void parseWithoutBomWithoutDecl() throws XMLDBException { |
| 45 | + final String query = "parse-xml('<elem1/>')"; |
| 46 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 47 | + assertEquals("<elem1/>", result); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void parseWithoutBomWithDecl() throws XMLDBException { |
| 52 | + final String query = "parse-xml('" + UTF8_DECL + "<elem2/>')"; |
| 53 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 54 | + assertEquals("<elem2/>", result); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void parseWithUtf8BomWithoutDecl() throws XMLDBException { |
| 59 | + final String query = "parse-xml('" + UTF8_BOM + "<elem3/>')"; |
| 60 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 61 | + assertEquals("<elem3/>", result); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void parseWithUtf8BomWithDecl() throws XMLDBException { |
| 66 | + final String query = "parse-xml('" + UTF8_BOM + UTF8_DECL + "<elem4/>')"; |
| 67 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 68 | + assertEquals("<elem4/>", result); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void parseWithUtf16BEBomWithoutDecl() throws XMLDBException { |
| 73 | + final String query = "parse-xml('" + UTF16_BE_BOM + new String(new byte[]{0x00, 0x3c, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x35, 0x00, 0x2f, 0x00, 0x3e}, StandardCharsets.UTF_16BE) + "')"; |
| 74 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 75 | + assertEquals("<elem5/>", result); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void parseWithUtf16LEBomWithoutDecl() throws XMLDBException { |
| 80 | + final String query = "parse-xml('" + UTF16_LE_BOM + new String(new byte[]{0x3c, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x36, 0x00, 0x2f, 0x00, 0x3e, 0x00}, StandardCharsets.UTF_16LE) + "')"; |
| 81 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 82 | + assertEquals("<elem6/>", result); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void parseFragmentWithoutBomWithoutDecl() throws XMLDBException { |
| 87 | + final String query = "parse-xml-fragment('<elem1/>')"; |
| 88 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 89 | + assertEquals("<elem1/>", result); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void parseFragmentWithUtf8BomWithoutDecl() throws XMLDBException { |
| 94 | + final String query = "parse-xml-fragment('" + UTF8_BOM + "<elem3/>')"; |
| 95 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 96 | + assertEquals("<elem3/>", result); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void parseFragmentWithUtf16BEBomWithoutDecl() throws XMLDBException { |
| 101 | + final String query = "parse-xml-fragment('" + UTF16_BE_BOM + new String(new byte[]{0x00, 0x3c, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x35, 0x00, 0x2f, 0x00, 0x3e}, StandardCharsets.UTF_16BE) + "')"; |
| 102 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 103 | + assertEquals("<elem5/>", result); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void parseFragmentWithUtf16LEBomWithoutDecl() throws XMLDBException { |
| 108 | + final String query = "parse-xml-fragment('" + UTF16_LE_BOM + new String(new byte[]{0x3c, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x36, 0x00, 0x2f, 0x00, 0x3e, 0x00}, StandardCharsets.UTF_16LE) + "')"; |
| 109 | + final String result = existEmbeddedServer.executeOneValue(query); |
| 110 | + assertEquals("<elem6/>", result); |
| 111 | + } |
| 112 | +} |
0 commit comments