|
| 1 | +(: |
| 2 | + : eXist-db Open Source Native XML Database |
| 3 | + : Copyright (C) 2001 The eXist-db Authors |
| 4 | + : |
| 5 | + : info@exist-db.org |
| 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 | +xquery version "3.1"; |
| 23 | + |
| 24 | +(:~ |
| 25 | + : Tests for xmldb:store() with a binary (xs:base64Binary) content value. |
| 26 | + : |
| 27 | + : Regression for the case where a binary content value is stored under an XML-type |
| 28 | + : mime (explicit mime="application/xml" or inferred from an .xml resource name): |
| 29 | + : the store used to fail with java.lang.NullPointerException |
| 30 | + : ("Cannot invoke \"String.length()\" because \"<parameter1>\" is null") because the |
| 31 | + : binary was bound to an XML resource that had no character/byte stream to parse. |
| 32 | + : The bytes should instead be parsed as an XML document. |
| 33 | + :) |
| 34 | +module namespace sbt = "http://exist-db.org/xquery/test/xmldb-store-binary"; |
| 35 | + |
| 36 | +declare namespace test = "http://exist-db.org/xquery/xqsuite"; |
| 37 | +declare namespace util = "http://exist-db.org/xquery/util"; |
| 38 | + |
| 39 | +declare variable $sbt:collection-name := "xmldb-store-binary-test"; |
| 40 | +declare variable $sbt:collection := "/db/" || $sbt:collection-name; |
| 41 | + |
| 42 | +declare |
| 43 | + %test:setUp |
| 44 | +function sbt:setup() as empty-sequence() { |
| 45 | + let $_ := xmldb:create-collection("/db", $sbt:collection-name) |
| 46 | + return () |
| 47 | +}; |
| 48 | + |
| 49 | +declare |
| 50 | + %test:tearDown |
| 51 | +function sbt:tear-down() as empty-sequence() { |
| 52 | + let $_ := xmldb:remove($sbt:collection) |
| 53 | + return () |
| 54 | +}; |
| 55 | + |
| 56 | +(: binary content + explicit application/xml mime -> parsed and stored as XML :) |
| 57 | +declare |
| 58 | + %test:assertEquals("1") |
| 59 | +function sbt:binary-with-xml-mime-is-parsed() { |
| 60 | + let $bin := util:string-to-binary("<doc><a>1</a></doc>") |
| 61 | + let $stored := xmldb:store($sbt:collection, "explicit.xml", $bin, "application/xml") |
| 62 | + return doc($stored)/doc/a/string() |
| 63 | +}; |
| 64 | + |
| 65 | +(: binary content, mime inferred from the .xml resource name -> parsed and stored as XML :) |
| 66 | +declare |
| 67 | + %test:assertEquals("1") |
| 68 | +function sbt:binary-with-inferred-xml-mime-is-parsed() { |
| 69 | + let $bin := util:string-to-binary("<doc><a>1</a></doc>") |
| 70 | + let $stored := xmldb:store($sbt:collection, "inferred.xml", $bin) |
| 71 | + return doc($stored)/doc/a/string() |
| 72 | +}; |
| 73 | + |
| 74 | +(: the parsed document is a real XML document, not a binary resource :) |
| 75 | +declare |
| 76 | + %test:assertEquals("false") |
| 77 | +function sbt:binary-with-xml-mime-is-not-binary() { |
| 78 | + let $bin := util:string-to-binary("<doc><a>1</a></doc>") |
| 79 | + let $stored := xmldb:store($sbt:collection, "as-xml.xml", $bin, "application/xml") |
| 80 | + return string(util:binary-doc-available($stored)) |
| 81 | +}; |
| 82 | + |
| 83 | +(: XML encoding declaration in the bytes is honored (parser reads it from the stream) :) |
| 84 | +declare |
| 85 | + %test:assertEquals("ä") |
| 86 | +function sbt:binary-with-xml-mime-honors-encoding() { |
| 87 | + let $bin := util:string-to-binary("<?xml version='1.0' encoding='UTF-8'?><doc>ä</doc>") |
| 88 | + let $stored := xmldb:store($sbt:collection, "encoded.xml", $bin, "application/xml") |
| 89 | + return doc($stored)/doc/string() |
| 90 | +}; |
| 91 | + |
| 92 | +(: control: binary content + a binary mime is still stored byte-for-byte as a binary resource :) |
| 93 | +declare |
| 94 | + %test:assertEquals("true") |
| 95 | +function sbt:binary-with-binary-mime-stays-binary() { |
| 96 | + let $bin := util:string-to-binary("<doc><a>1</a></doc>") |
| 97 | + let $stored := xmldb:store($sbt:collection, "raw.bin", $bin, "application/octet-stream") |
| 98 | + return string(util:binary-doc-available($stored)) |
| 99 | +}; |
| 100 | + |
| 101 | +(: malformed XML bytes under an XML mime fail with a clean store/parse error, NOT an NPE :) |
| 102 | +declare |
| 103 | + %test:assertError("storing document") |
| 104 | +function sbt:binary-malformed-xml-mime-reports-parse-error() { |
| 105 | + let $bin := util:string-to-binary("not well-formed <") |
| 106 | + return xmldb:store($sbt:collection, "broken.xml", $bin, "application/xml") |
| 107 | +}; |
0 commit comments