From a95bf99319b5f0afd4563b11cd8ed4879a6b92ea Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Tue, 27 Aug 2024 08:15:08 +0200 Subject: [PATCH] WIP --- .../org/metafacture/xml/XmlDecoderTest.java | 419 ++++++++++++++++++ 1 file changed, 419 insertions(+) create mode 100644 metafacture-xml/src/test/java/org/metafacture/xml/XmlDecoderTest.java diff --git a/metafacture-xml/src/test/java/org/metafacture/xml/XmlDecoderTest.java b/metafacture-xml/src/test/java/org/metafacture/xml/XmlDecoderTest.java new file mode 100644 index 00000000..5c406c41 --- /dev/null +++ b/metafacture-xml/src/test/java/org/metafacture/xml/XmlDecoderTest.java @@ -0,0 +1,419 @@ +/* + * Copyright 2013, 2014 Deutsche Nationalbibliothek + * + * Licensed under the Apache License, Version 2.0 the "License"; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.metafacture.xml; + +import org.junit.Before; +import org.junit.Test; +import org.metafacture.framework.XmlReceiver; +import org.metafacture.framework.helpers.DefaultObjectReceiver; +import org.metafacture.framework.helpers.DefaultXmlPipe; +import org.metafacture.framework.helpers.DefaultXmlReceiver; +import org.metafacture.framework.StreamReceiver; + + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Tests for class {@link XmlDecoder}. + * + * @author Pascal Christoph (dr0i) + * + */ +public final class XmlDecoderTest { + + private static final String TAG = "tag"; + private static final String VALUE = "value"; + + private XmlDecoder xmlDecoder; +private GenericXmlHandler xmlHandler; +private DefaultXmlPipe receiverDefaultXmlPipe; + + private StringBuilder resultCollector; + + @Before + public void initSystemUnderTest() { + xmlDecoder = new XmlDecoder(); + xmlDecoder.setReceiver( + new DefaultObjectReceiver() { + @Override + public void process(final String obj) { + receiverDefaultXmlPipe.setReceiver(resultCollector.append(obj)); + } + }); + resultCollector = new StringBuilder(); + } + + @Test + public void issue249_shouldNotEmitClosingRootTagOnCloseStreamIfNoOutputWasGenerated() { + xmlDecoder.closeStream(); + + assertTrue(getResultXml().isEmpty()); + } + + @Test + public void shouldNotEmitClosingRootTagOnResetStreamIfNoOutputWasGenerated() { + xmlDecoder.resetStream(); + + assertTrue(getResultXml().isEmpty()); + } + + @Test + public void shouldOnlyEscapeXmlReservedCharacters() { + final StringBuilder builder = new StringBuilder(); + + SimpleXmlEncoder.writeEscaped(builder , "&<>'\" üäö"); + + assertEquals("&<>'" üäö", builder.toString()); + } + + @Test + public void shouldWrapEachRecordInRootTagIfSeparateRootsIsTrue() { + xmlDecoder.setSeparateRoots(true); + + emitTwoRecords(); + + assertEquals("valuevalue", + getResultXml()); + } + + @Test + public void shouldWrapAllRecordsInOneRootTagtIfSeparateRootsIsFalse() { + simpleXmlEncoder.setSeparateRoots(false); + + emitTwoRecords(); + + assertEquals("valuevalue", + getResultXml()); + } + + @Test + public void shouldAddNamespaceToRootElement() { + final Map namespaces = new HashMap(); + namespaces.put("ns", "http://example.org/ns"); + simpleXmlEncoder.setNamespaces(namespaces); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddMultipleNamespacesFromParameterToRootElement() { + simpleXmlEncoder.setNamespaces("__default=http://default.org/ns\nns=http://example.org/ns\nns1=http://example.org/ns1"); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + @Test + public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRootTag() { + final Map namespaces = new HashMap(); + namespaces.put("", "http://example.org/ns"); + simpleXmlEncoder.setNamespaces(namespaces); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddNamespaceWithDefaultKeyAsDefaultNamespaceToRootTag() { + final Map namespaces = new HashMap(); + namespaces.put("__default", "http://example.org/ns"); + simpleXmlEncoder.setNamespaces(namespaces); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceToRootTag() { + simpleXmlEncoder.setNamespaceFile("org/metafacture/xml/SimpleXmlEncoderTest_namespaces.properties"); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldNotEmitRootTagIfWriteRootTagIsFalse() { + simpleXmlEncoder.setWriteRootTag(false); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddNamespacesToRecordTagIfWriteRootTagIsFalse() { + final Map namespaces = new HashMap(); + namespaces.put("ns", "http://example.org/ns"); + simpleXmlEncoder.setNamespaces(namespaces); + simpleXmlEncoder.setWriteRootTag(false); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRecordTag() { + final Map namespaces = new HashMap(); + namespaces.put("", "http://example.org/ns"); + simpleXmlEncoder.setNamespaces(namespaces); + simpleXmlEncoder.setWriteRootTag(false); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddNamespaceWithDefaultKeyAsDefaultNamespaceToRecordTag() { + final Map namespaces = new HashMap(); + namespaces.put("__default", "http://example.org/ns"); + simpleXmlEncoder.setNamespaces(namespaces); + simpleXmlEncoder.setWriteRootTag(false); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceToRecordTag() { + simpleXmlEncoder.setNamespaceFile("org/metafacture/xml/SimpleXmlEncoderTest_namespaces.properties"); + simpleXmlEncoder.setWriteRootTag(false); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + @Test + public void shouldAddNamespaceWithEmptyKeyFromParameterAsDefaultNamespaceToRecordTag() { + simpleXmlEncoder.setNamespaces("=http://example.org/ns"); + simpleXmlEncoder.setWriteRootTag(false); + + emitEmptyRecord(); + + assertEquals("", + getResultXml()); + } + + @Test + public void testShouldEncodeUnnamedLiteralsAsText() { + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal("", VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + @Test + public void testShouldStillEncodeUnnamedLiteralsAsTextWithConfiguredValueTagName() { + simpleXmlEncoder.setValueTag("data"); + + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal("", VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + // SimpleXmlEncoder.Element.writeElement() does not write child elements with empty name + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + @Test + public void testShouldNotEncodeLiteralsWithDifferentValueTagNameAsText() { + simpleXmlEncoder.setValueTag("data"); + + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + @Test + public void issue379_testShouldEncodeConfiguredValueLiteralsAsText() { + final String name = "data"; + simpleXmlEncoder.setValueTag(name); + + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal(name, VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + @Test + public void issue379_testShouldNotEncodeUnconfiguredValueLiteralsAsText() { + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal(DefaultXmlPipe.DEFAULT_VALUE_TAG, VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + @Test + public void testShouldEncodeMarkedLiteralsAsAttributes() { + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.literal("~attr", VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + @Test + public void testShouldNotEncodeMarkedEntitiesAsAttributes() { + simpleXmlEncoder.setAttributeMarker("*"); + + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.startEntity("~entity"); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.endEntity(); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "<~entity>" + + "value" + + "" + + "" + + "", + getResultXml()); + } + + @Test + public void testShouldNotEncodeLiteralsWithDifferentMarkerAsAttributes() { + simpleXmlEncoder.setAttributeMarker("*"); + + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.literal("~attr", VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "<~attr>value" + + "" + + "", + getResultXml()); + } + + @Test + public void testShouldEncodeMarkedLiteralsWithConfiguredMarkerAsAttributes() { + final String marker = "**"; + simpleXmlEncoder.setAttributeMarker(marker); + + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.literal(marker + "attr", VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + + assertEquals("" + + "" + + "" + + "value" + + "" + + "", + getResultXml()); + } + + private void emitTwoRecords() { + simpleXmlEncoder.startRecord("X"); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.startRecord("Y"); + simpleXmlEncoder.literal(TAG, VALUE); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + } + + private void emitEmptyRecord() { + simpleXmlEncoder.startRecord(""); + simpleXmlEncoder.endRecord(); + simpleXmlEncoder.closeStream(); + } + + private String getResultXml() { + return resultCollector.toString().replaceAll("[\\n\\t]", ""); + } + +}