|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.xml |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test |
| 22 | + |
| 23 | +class XmlUtilSerializeOptionsTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + void defaultOptionsMatchExistingBehaviour() { |
| 27 | + def xml = '<root><item>value</item></root>' |
| 28 | + def withoutOptions = XmlUtil.serialize(xml) |
| 29 | + def withOptions = XmlUtil.serialize(xml, new SerializeOptions()) |
| 30 | + assert withoutOptions == withOptions |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void encodingInXmlDeclaration() { |
| 35 | + def xml = '<root><item>value</item></root>' |
| 36 | + def result = XmlUtil.serialize(xml, new SerializeOptions(encoding: 'ISO-8859-1')) |
| 37 | + assert result.contains('encoding="ISO-8859-1"') |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void defaultEncodingIsUtf8() { |
| 42 | + def xml = '<root><item>value</item></root>' |
| 43 | + def result = XmlUtil.serialize(xml, new SerializeOptions()) |
| 44 | + assert result.contains('encoding="UTF-8"') |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void customIndent() { |
| 49 | + def xml = '<root><item>value</item></root>' |
| 50 | + def twoSpaces = XmlUtil.serialize(xml, new SerializeOptions(indent: 2)) |
| 51 | + def fourSpaces = XmlUtil.serialize(xml, new SerializeOptions(indent: 4)) |
| 52 | + assert twoSpaces.contains(' <item>') |
| 53 | + assert fourSpaces.contains(' <item>') |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void serializeNodeWithOptions() { |
| 58 | + def node = new XmlParser().parseText('<root><item>value</item></root>') |
| 59 | + def result = XmlUtil.serialize(node, new SerializeOptions(encoding: 'ISO-8859-1')) |
| 60 | + assert result.contains('encoding="ISO-8859-1"') |
| 61 | + assert result.contains('<item>value</item>') |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void serializeGPathResultWithOptions() { |
| 66 | + def gpath = new XmlSlurper().parseText('<root><item>value</item></root>') |
| 67 | + def result = XmlUtil.serialize(gpath, new SerializeOptions(encoding: 'ISO-8859-1')) |
| 68 | + assert result.contains('encoding="ISO-8859-1"') |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void serializeToOutputStreamWithEncoding() { |
| 73 | + def xml = '<root><item>value</item></root>' |
| 74 | + def baos = new ByteArrayOutputStream() |
| 75 | + XmlUtil.serialize(xml, baos, new SerializeOptions(encoding: 'ISO-8859-1')) |
| 76 | + def result = baos.toString('ISO-8859-1') |
| 77 | + assert result.contains('encoding="ISO-8859-1"') |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void serializeOptionsNamedParams() { |
| 82 | + def opts = new SerializeOptions(encoding: 'UTF-16', indent: 4, allowDocTypeDeclaration: true) |
| 83 | + assert opts.encoding == 'UTF-16' |
| 84 | + assert opts.indent == 4 |
| 85 | + assert opts.allowDocTypeDeclaration == true |
| 86 | + } |
| 87 | +} |
0 commit comments