From dc81abe20cf4f13fec73c35cb94869ec1b48ae03 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Tue, 7 May 2024 16:21:28 +0200 Subject: [PATCH] WIP incorporate marc21XmlEncoder into marcXmlEncoder --- .../biblio/marc21/AbstractMarcXmlEncoder.java | 10 - .../biblio/marc21/Marc21XmlEncoder.java | 115 ---------- .../biblio/marc21/MarcXmlEncoder.java | 216 ++++++++++++------ .../marc21/MarcXmlEncoderInterface.java | 49 ---- .../biblio/marc21/Marc21XmlEncoderTest.java | 37 --- .../biblio/marc21/MarcXmlEncoderTest.java | 36 ++- 6 files changed, 175 insertions(+), 288 deletions(-) delete mode 100644 metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/AbstractMarcXmlEncoder.java delete mode 100644 metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21XmlEncoder.java delete mode 100644 metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoderInterface.java delete mode 100644 metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21XmlEncoderTest.java diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/AbstractMarcXmlEncoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/AbstractMarcXmlEncoder.java deleted file mode 100644 index a54d5b455..000000000 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/AbstractMarcXmlEncoder.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.metafacture.biblio.marc21; - -import org.metafacture.framework.ObjectReceiver; -import org.metafacture.framework.helpers.DefaultStreamPipe; - -public abstract class AbstractMarcXmlEncoder extends DefaultStreamPipe> implements MarcXmlEncoderInterface { - - protected void onResetStream() { - } -} diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21XmlEncoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21XmlEncoder.java deleted file mode 100644 index 503705200..000000000 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21XmlEncoder.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2024 hbz - * - * 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.biblio.marc21; - -import org.metafacture.framework.FluxCommand; -import org.metafacture.framework.StreamReceiver; -import org.metafacture.framework.annotations.Description; -import org.metafacture.framework.annotations.In; -import org.metafacture.framework.annotations.Out; - -/** - * Acts as a wrapper: pipes input to Marc21Encoder which output is piped to Marc21Decoder which output is piped to MarcXmlEncoder. - * - * @author Pascal Christoph (dr0i) - * - */ -@In(StreamReceiver.class) -@Out(String.class) -@Description("Encodes MARC21 records as MARCXML. It wraps `encode-marc21 | decode-marc21 | encode-marcxml` to generate MARCXML more safely, especially when building the `leader`.") -@FluxCommand("encode-marc21xml") -public final class Marc21XmlEncoder extends AbstractMarcXmlEncoder { - private final Marc21Decoder marc21Decoder = new Marc21Decoder(); - private final Marc21Encoder marc21Encoder = new Marc21Encoder(); - private final MarcXmlEncoder marcXmlEncoder = new MarcXmlEncoder(); - - /** - * Creates an instance of {@link Marc21XmlEncoder}. - */ - public Marc21XmlEncoder() { - marc21Decoder.setEmitLeaderAsWhole(true); - - marc21Encoder.setReceiver(marc21Decoder); - marc21Decoder.setReceiver(marcXmlEncoder); - } - - @Override - protected void onSetReceiver() { - marcXmlEncoder.setReceiver(getReceiver()); - } - - @Override - public void startRecord(final String identifier) { - marc21Encoder.startRecord(identifier); - } - - @Override - public void endRecord() { - marc21Encoder.endRecord(); - } - - @Override - public void startEntity(final String name) { - marc21Encoder.startEntity(name); - } - - @Override - public void endEntity() { - marc21Encoder.endEntity(); - } - - @Override - public void literal(final String name, final String value) { - marc21Encoder.literal(name, value); - } - - @Override - protected void onCloseStream() { - marc21Encoder.closeStream(); - } - - @Override - public void onResetStream() { - marc21Encoder.resetStream(); - } - - @Override - public void setEmitNamespace(final boolean emitNamespace) { - marcXmlEncoder.setEmitNamespace(emitNamespace); - } - - @Override - public void omitXmlDeclaration(final boolean currentOmitXmlDeclaration) { - marcXmlEncoder.omitXmlDeclaration(currentOmitXmlDeclaration); - } - - @Override - public void setXmlVersion(final String xmlVersion) { - marcXmlEncoder.setXmlVersion(xmlVersion); - } - - @Override - public void setXmlEncoding(final String xmlEncoding) { - marcXmlEncoder.setXmlEncoding(xmlEncoding); - } - - @Override - public void setFormatted(final boolean formatted) { - marcXmlEncoder.setFormatted(formatted); - } -} - diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java index 40288f2d0..1d3da894c 100644 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java +++ b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java @@ -18,10 +18,12 @@ import org.metafacture.commons.XmlUtil; import org.metafacture.framework.FluxCommand; import org.metafacture.framework.MetafactureException; +import org.metafacture.framework.ObjectReceiver; import org.metafacture.framework.StreamReceiver; import org.metafacture.framework.annotations.Description; import org.metafacture.framework.annotations.In; import org.metafacture.framework.annotations.Out; +import org.metafacture.framework.helpers.DefaultStreamPipe; import java.util.Arrays; import java.util.Collections; @@ -34,17 +36,18 @@ * @author Pascal Christoph (dr0i) dug it up again */ -@Description("Encodes a stream into MARCXML. Use this only if you can ensure valid MARC21. Also, the leader must be correct and set as one literal. You may want to use `encode-marc21xml` instead (which can cope with e.g. an irregular leader).") +@Description("Encodes a stream into MARCXML. If you can't ensure valid MARC21 (e.g. the leader isn't correct or not set as one literal) then set the parameter `ensureCorrectMarc21Xml` to `true`.") @In(StreamReceiver.class) @Out(String.class) @FluxCommand("encode-marcxml") -public final class MarcXmlEncoder extends AbstractMarcXmlEncoder { +public final class MarcXmlEncoder extends DefaultStreamPipe> { public static final String NAMESPACE_NAME = "marc"; public static final String XML_ENCODING = "UTF-8"; public static final String XML_VERSION = "1.0"; public static final boolean PRETTY_PRINTED = true; public static final boolean OMIT_XML_DECLARATION = false; + public static final boolean ENSURE_CORRECT_MARC21_XML = false; private static final String ROOT_OPEN = ""; private static final String ROOT_CLOSE = ""; @@ -120,10 +123,22 @@ public String close(final Object[] args) { private boolean formatted = PRETTY_PRINTED; private int recordAttributeOffset; + private boolean ensureCorrectMarc21Xml = ENSURE_CORRECT_MARC21_XML; + private final Marc21Decoder marc21Decoder = new Marc21Decoder(); + private final Marc21Encoder marc21Encoder = new Marc21Encoder(); + private MarcXmlEncoder marcXmlEncoder = null; + public static boolean isInstanced = false; /** * Creates an instance of {@link MarcXmlEncoder}. */ public MarcXmlEncoder() { + if (! isInstanced) { + isInstanced = true; + marcXmlEncoder = new MarcXmlEncoder(); + marc21Decoder.setEmitLeaderAsWhole(true); + marc21Encoder.setReceiver(marc21Decoder); + marc21Decoder.setReceiver(marcXmlEncoder); + } } /** @@ -135,11 +150,12 @@ public MarcXmlEncoder() { public void setEmitNamespace(final boolean emitNamespace) { this.emitNamespace = emitNamespace; namespacePrefix = new Object[]{emitNamespace ? NAMESPACE_PREFIX : EMPTY}; + if (marcXmlEncoder !=null) marcXmlEncoder.setEmitNamespace(emitNamespace); + } /** * Sets the flag to decide whether to omit the XML declaration. - * * Default value: {@value #OMIT_XML_DECLARATION} * * @param currentOmitXmlDeclaration true if the XML declaration is omitted, otherwise @@ -147,145 +163,197 @@ public void setEmitNamespace(final boolean emitNamespace) { */ public void omitXmlDeclaration(final boolean currentOmitXmlDeclaration) { omitXmlDeclaration = currentOmitXmlDeclaration; + if (marcXmlEncoder !=null) marcXmlEncoder.omitXmlDeclaration(currentOmitXmlDeclaration); + } /** * Sets the XML version. - * * Default value: {@value #XML_VERSION} * * @param xmlVersion the XML version */ public void setXmlVersion(final String xmlVersion) { this.xmlVersion = xmlVersion; + this.xmlVersion = xmlVersion; + if (marcXmlEncoder !=null) marcXmlEncoder.setXmlVersion(xmlVersion); + } /** * Sets the XML encoding. - * * Default value: {@value #XML_ENCODING} * * @param xmlEncoding the XML encoding */ public void setXmlEncoding(final String xmlEncoding) { this.xmlEncoding = xmlEncoding; + if (marcXmlEncoder !=null) marcXmlEncoder.setXmlEncoding(xmlEncoding); + } /** - * Formats the resulting xml by indentation. Aka "pretty printing". + * Sets to ensure correct MARC21 XML. + * If true, the input data is validated to ensure correct MARC21. Also the leader may be generated. + * It acts as a wrapper: the input is piped to {@link org.metafacture.biblio.marc21.Marc21Encoder}, whose output is piped to {@link org.metafacture.biblio.marc21.Marc21Decoder}, whose output is piped to {@link org.metafacture.biblio.marc21.MarcXmlEncoder}. + * This validation and treatment of the leader is more safe but comes with a performance impact. * + * Default value: {@value #ENSURE_CORRECT_MARC21_XML} + * + * @param ensureCorrectMarc21Xml if true the input data is validated to ensure correct MARC21. Also the leader may be generated. + */ + public void setEnsureCorrectMarc21Xml(final boolean ensureCorrectMarc21Xml) { + this.ensureCorrectMarc21Xml = ensureCorrectMarc21Xml; + } + + /** + * Formats the resulting xml by indentation. Aka "pretty printing". * Default value: {@value #PRETTY_PRINTED} * * @param formatted true if formatting is activated, otherwise false */ public void setFormatted(final boolean formatted) { this.formatted = formatted; + if (marcXmlEncoder !=null) marcXmlEncoder.setFormatted(formatted); + } + + @Override + protected void onSetReceiver() { + if (marcXmlEncoder !=null ) { + marcXmlEncoder.setReceiver(getReceiver()); + } } @Override public void startRecord(final String identifier) { - if (atStreamStart) { - if (!omitXmlDeclaration) { - writeHeader(); + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.startRecord(identifier); + } else { + if (atStreamStart) { + if (!omitXmlDeclaration) { + writeHeader(); + prettyPrintNewLine(); + } + writeTag(Tag.collection::open, emitNamespace ? NAMESPACE_SUFFIX : EMPTY, emitNamespace ? SCHEMA_ATTRIBUTES : EMPTY); prettyPrintNewLine(); + incrementIndentationLevel(); } - writeTag(Tag.collection::open, emitNamespace ? NAMESPACE_SUFFIX : EMPTY, emitNamespace ? SCHEMA_ATTRIBUTES : EMPTY); + atStreamStart = false; + + prettyPrintIndentation(); + writeTag(Tag.record::open); + recordAttributeOffset = builder.length() - 1; prettyPrintNewLine(); + incrementIndentationLevel(); } - atStreamStart = false; - - prettyPrintIndentation(); - writeTag(Tag.record::open); - recordAttributeOffset = builder.length() - 1; - prettyPrintNewLine(); - - incrementIndentationLevel(); } @Override public void endRecord() { - if (builderLeader.length() > 0) { - writeLeader(); + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.endRecord(); + } else { + if (builderLeader.length() > 0) { + writeLeader(); + } + decrementIndentationLevel(); + prettyPrintIndentation(); + writeTag(Tag.record::close); + prettyPrintNewLine(); + sendAndClearData(); } - decrementIndentationLevel(); - prettyPrintIndentation(); - writeTag(Tag.record::close); - prettyPrintNewLine(); - sendAndClearData(); } @Override public void startEntity(final String name) { - currentEntity = name; - if (!name.equals(Marc21EventNames.LEADER_ENTITY)) { - if (name.length() != LEADER_ENTITY_LENGTH) { - final String message = String.format("Entity too short." + "Got a string ('%s') of length %d." + - "Expected a length of " + LEADER_ENTITY_LENGTH + " (field + indicators).", name, name.length()); - throw new MetafactureException(message); - } + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.startEntity(name); + } else { + currentEntity = name; + if (!name.equals(Marc21EventNames.LEADER_ENTITY)) { + if (name.length() != LEADER_ENTITY_LENGTH) { + final String message = String.format("Entity too short." + "Got a string ('%s') of length %d." + + "Expected a length of " + LEADER_ENTITY_LENGTH + " (field + indicators).", name, name.length()); + throw new MetafactureException(message); + } - final String tag = name.substring(TAG_BEGIN, TAG_END); - final String ind1 = name.substring(IND1_BEGIN, IND1_END); - final String ind2 = name.substring(IND2_BEGIN, IND2_END); - prettyPrintIndentation(); - writeTag(Tag.datafield::open, tag, ind1, ind2); - prettyPrintNewLine(); - incrementIndentationLevel(); + final String tag = name.substring(TAG_BEGIN, TAG_END); + final String ind1 = name.substring(IND1_BEGIN, IND1_END); + final String ind2 = name.substring(IND2_BEGIN, IND2_END); + prettyPrintIndentation(); + writeTag(Tag.datafield::open, tag, ind1, ind2); + prettyPrintNewLine(); + incrementIndentationLevel(); + } } } @Override public void endEntity() { - if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) { - decrementIndentationLevel(); - prettyPrintIndentation(); - writeTag(Tag.datafield::close); - prettyPrintNewLine(); + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.endEntity(); + } else { + if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) { + decrementIndentationLevel(); + prettyPrintIndentation(); + writeTag(Tag.datafield::close); + prettyPrintNewLine(); + } + currentEntity = ""; } - currentEntity = ""; } @Override public void literal(final String name, final String value) { - if ("".equals(currentEntity)) { - if (name.equals(Marc21EventNames.MARCXML_TYPE_LITERAL)) { - if (value != null) { - builder.insert(recordAttributeOffset, String.format(ATTRIBUTE_TEMPLATE, name, value)); + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.literal(name, value); + } else { + if ("".equals(currentEntity)) { + if (name.equals(Marc21EventNames.MARCXML_TYPE_LITERAL)) { + if (value != null) { + builder.insert(recordAttributeOffset, String.format(ATTRIBUTE_TEMPLATE, name, value)); + } + } else if (!writeLeader(name, value)) { + prettyPrintIndentation(); + writeTag(Tag.controlfield::open, name); + if (value != null) { + writeEscaped(value.trim()); + } + writeTag(Tag.controlfield::close); + prettyPrintNewLine(); } - } - else if (!writeLeader(name, value)) { + } else if (!writeLeader(currentEntity, value)) { prettyPrintIndentation(); - writeTag(Tag.controlfield::open, name); - if (value != null) { - writeEscaped(value.trim()); - } - writeTag(Tag.controlfield::close); + writeTag(Tag.subfield::open, name); + writeEscaped(value.trim()); + writeTag(Tag.subfield::close); prettyPrintNewLine(); } } - else if (!writeLeader(currentEntity, value)) { - prettyPrintIndentation(); - writeTag(Tag.subfield::open, name); - writeEscaped(value.trim()); - writeTag(Tag.subfield::close); - prettyPrintNewLine(); - } } @Override protected void onResetStream() { - if (!atStreamStart) { - writeFooter(); + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.resetStream(); + } else { + if (!atStreamStart) { + writeFooter(); + } + sendAndClearData(); + atStreamStart = true; } - sendAndClearData(); - atStreamStart = true; } @Override protected void onCloseStream() { - writeFooter(); - sendAndClearData(); + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marc21Encoder.closeStream(); + } else { + writeFooter(); + sendAndClearData(); + } } /** Increments the indentation level by one */ @@ -373,9 +441,13 @@ private void prettyPrintNewLine() { } private void sendAndClearData() { - getReceiver().process(builder.toString()); - builder.delete(0, builder.length()); - recordAttributeOffset = 0; + if (ENSURE_CORRECT_MARC21_XML && marcXmlEncoder !=null ) { + marcXmlEncoder.sendAndClearData(); + } else { + getReceiver().process(builder.toString()); + builder.delete(0, builder.length()); + recordAttributeOffset = 0; + } } } diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoderInterface.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoderInterface.java deleted file mode 100644 index b3e2f8840..000000000 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoderInterface.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.metafacture.biblio.marc21; - -public interface MarcXmlEncoderInterface { - - /** - * Sets the flag to decide whether to emit the {@value MarcXmlEncoder#NAMESPACE_NAME} - * namespace - * - * @param emitNamespace true if the namespace is emitted, otherwise false - */ - void setEmitNamespace(boolean emitNamespace); - - /** - * Sets the flag to decide whether to omit the XML declaration. - * - * Default value: {@value MarcXmlEncoder#OMIT_XML_DECLARATION} - * - * @param currentOmitXmlDeclaration true if the XML declaration is omitted, otherwise - * false - */ - void omitXmlDeclaration(boolean currentOmitXmlDeclaration); - - /** - * Sets the XML version. - * - * Default value: {@value MarcXmlEncoder#XML_VERSION} - * - * @param xmlVersion the XML version - */ - void setXmlVersion(String xmlVersion); - - /** - * Sets the XML encoding. - * - * Default value: {@value MarcXmlEncoder#XML_ENCODING} - * - * @param xmlEncoding the XML encoding - */ - void setXmlEncoding(String xmlEncoding); - - /** - * Formats the resulting xml by indentation. Aka "pretty printing". - * - * Default value: {@value MarcXmlEncoder#PRETTY_PRINTED} - * - * @param formatted true if formatting is activated, otherwise false - */ - void setFormatted(boolean formatted); -} diff --git a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21XmlEncoderTest.java b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21XmlEncoderTest.java deleted file mode 100644 index 9ef77f973..000000000 --- a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21XmlEncoderTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.metafacture.biblio.marc21; - -import org.junit.Before; -import org.junit.Test; -import org.metafacture.framework.FormatException; -import org.metafacture.framework.MissingIdException; - -public class Marc21XmlEncoderTest { - MarcXmlEncoderTest marcXmlEncoderTest = new MarcXmlEncoderTest(); - - @Before - public void setUp() { - marcXmlEncoderTest.encoder=new Marc21XmlEncoder(); - marcXmlEncoderTest.initializeEncoder(); - } - - @Test(expected = FormatException.class) - public void createAnRecordWithLeader() { - marcXmlEncoderTest.createAnRecordWithLeader(); - } - - @Test(expected = FormatException.class) - public void issue336_createRecordWithTopLevelLeader() { - marcXmlEncoderTest.issue336_createRecordWithTopLevelLeader(); - } - - @Test - public void issue336_createRecordWithTopLevelLeader_Marc21Xml() { - marcXmlEncoderTest.issue336_createRecordWithTopLevelLeader_Marc21Xml(); - } - - @Test(expected = MissingIdException.class) - public void issue527ShouldEmitLeaderAlwaysAsWholeString() { - marcXmlEncoderTest.issue527ShouldEmitLeaderAlwaysAsWholeString(); - } - -} diff --git a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java index aa8ab45ea..6e870649e 100644 --- a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java +++ b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Before; -import org.junit.ComparisonFailure; import org.junit.Test; import static org.metafacture.biblio.marc21.Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL; import static org.metafacture.biblio.marc21.Marc21EventNames.CATALOGING_FORM_LITERAL; @@ -30,7 +29,10 @@ import static org.metafacture.biblio.marc21.Marc21EventNames.RECORD_STATUS_LITERAL; import static org.metafacture.biblio.marc21.Marc21EventNames.RECORD_TYPE_LITERAL; import static org.metafacture.biblio.marc21.Marc21EventNames.TYPE_OF_CONTROL_LITERAL; + +import org.metafacture.framework.FormatException; import org.metafacture.framework.MetafactureException; +import org.metafacture.framework.MissingIdException; import org.metafacture.framework.helpers.DefaultObjectReceiver; /** @@ -56,10 +58,11 @@ public class MarcXmlEncoderTest { private static final String RECORD_ID = "92005291"; private static StringBuilder resultCollector; - AbstractMarcXmlEncoder encoder; + private static MarcXmlEncoder encoder; @Before public void setUp() { + MarcXmlEncoder.isInstanced = false; encoder = new MarcXmlEncoder(); initializeEncoder(); } @@ -79,7 +82,7 @@ public void process(final String obj) { public void tearDown() { } - private void addOneRecord(AbstractMarcXmlEncoder encoder) { + private void addOneRecord(MarcXmlEncoder encoder) { encoder.startRecord(RECORD_ID); encoder.literal("001", RECORD_ID); encoder.startEntity("010 "); @@ -212,6 +215,14 @@ public void createAnRecordWithLeader() { assertEquals(expected, actual); } + @Test(expected = FormatException.class) + public void createAnRecordWithLeader_ensureCorrectMarc21Xml() { + setUp(); + encoder.setEnsureCorrectMarc21Xml(true); + this.createAnRecordWithLeader(); + } + + @Test public void issue336_createRecordWithTopLevelLeader() { encoder.startRecord("1"); @@ -224,8 +235,16 @@ public void issue336_createRecordWithTopLevelLeader() { assertEquals(expected, actual); } - @Test(expected = ComparisonFailure.class) - public void issue336_createRecordWithTopLevelLeader_Marc21Xml() { + @Test(expected = FormatException.class) + public void issue336_createRecordWithTopLevelLeader_ensureCorrectMarc21Xml() { + encoder.setEnsureCorrectMarc21Xml(true); + this.issue336_createRecordWithTopLevelLeader(); + } + + @Test + public void issue336_createRecordWithTopLevelLeader_correctMarc21Xml() { + setUp(); + encoder.setEnsureCorrectMarc21Xml(true); encoder.startRecord("1"); encoder.literal("001", "8u3287432"); encoder.literal(Marc21EventNames.LEADER_ENTITY, "00000naa a2200000uc 4500"); @@ -260,6 +279,13 @@ public void issue527ShouldEmitLeaderAlwaysAsWholeString() { assertEquals(expected, actual); } + @Test(expected = MissingIdException.class) + public void issue527ShouldEmitLeaderAlwaysAsWholeString_ensureCorrectMarc21Xml() { + setUp(); + encoder.setEnsureCorrectMarc21Xml(true); + this.issue527ShouldEmitLeaderAlwaysAsWholeString(); + } + @Test public void sendDataAndClearWhenRecordStartedAndStreamResets() { encoder.startRecord("1");