From 32beffbc5eb953105dd61283564d67964323369a Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Tue, 25 Jun 2024 12:39:29 +0200 Subject: [PATCH] Fix top-level MARC record leader not being reset between records. (#524, #538) --- .../biblio/marc21/MarcXmlEncoder.java | 1 + .../biblio/marc21/MarcXmlEncoderTest.java | 46 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) 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 76f871869..2271dafaa 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 @@ -463,6 +463,7 @@ private void prettyPrintNewLine() { private void sendAndClearData() { getReceiver().process(builder.toString()); builder.delete(0, builder.length()); + leaderBuilder.delete(0, leaderBuilder.length()); recordAttributeOffset = 0; } 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 b167e5bba..04e40652e 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 @@ -184,6 +184,21 @@ public void emitExceptionWhenEntityLengthNot5() { encoder.startEntity("123456"); } + private void createRecordWithLeader(final String id, final String... leader) { + encoder.startRecord(id); + encoder.startEntity(Marc21EventNames.LEADER_ENTITY); + encoder.literal(Marc21EventNames.RECORD_STATUS_LITERAL, leader[0]); + encoder.literal(Marc21EventNames.RECORD_TYPE_LITERAL, leader[1]); + encoder.literal(Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL, leader[2]); + encoder.literal(Marc21EventNames.TYPE_OF_CONTROL_LITERAL, leader[3]); + encoder.literal(Marc21EventNames.CHARACTER_CODING_LITERAL, leader[4]); + encoder.literal(Marc21EventNames.ENCODING_LEVEL_LITERAL, leader[5]); + encoder.literal(Marc21EventNames.CATALOGING_FORM_LITERAL, leader[6]); + encoder.literal(Marc21EventNames.MULTIPART_LEVEL_LITERAL, leader[7]); + encoder.endEntity(); + encoder.endRecord(); + } + @Test public void createRecordWithLeader() { encoder.startRecord("1"); @@ -242,33 +257,20 @@ private void issue336_createRecordWithTopLevelLeader(final MarcXmlEncoder encode @Test public void issue527_shouldEmitLeaderAlwaysAsWholeString() { - issue527_shouldEmitLeaderAlwaysAsWholeString(encoder); + createRecordWithLeader("1", "a", "o", "a", " ", "a", "z", "u", " "); + createRecordWithLeader("2", "d", "u", "m", " ", "m", "y", "#", " "); + encoder.closeStream(); + String expected = XML_DECLARATION + XML_ROOT_OPEN + + "aoa azu " + + "dum my# " + XML_MARC_COLLECTION_END_TAG; + String actual = resultCollector.toString(); + assertEquals(expected, actual); } @Test(expected = MissingIdException.class) public void issue527_shouldEmitLeaderAlwaysAsWholeString_ensureCorrectMarc21Xml() { encoder.setEnsureCorrectMarc21Xml(true); - issue527_shouldEmitLeaderAlwaysAsWholeString(encoder); - } - - private void issue527_shouldEmitLeaderAlwaysAsWholeString(final MarcXmlEncoder encoder) { - encoder.startRecord("1"); - encoder.startEntity(Marc21EventNames.LEADER_ENTITY); - encoder.literal(Marc21EventNames.RECORD_STATUS_LITERAL, "a"); - encoder.literal(Marc21EventNames.RECORD_TYPE_LITERAL, "o"); - encoder.literal(Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL, "a"); - encoder.literal(Marc21EventNames.TYPE_OF_CONTROL_LITERAL, " "); - encoder.literal(Marc21EventNames.CHARACTER_CODING_LITERAL, "a"); - encoder.literal(Marc21EventNames.ENCODING_LEVEL_LITERAL, "z"); - encoder.literal(Marc21EventNames.CATALOGING_FORM_LITERAL, "u"); - encoder.literal(Marc21EventNames.MULTIPART_LEVEL_LITERAL, " "); - encoder.endEntity(); - encoder.endRecord(); - encoder.closeStream(); - String expected = XML_DECLARATION + XML_ROOT_OPEN - + "aoa azu " + XML_MARC_COLLECTION_END_TAG; - String actual = resultCollector.toString(); - assertEquals(expected, actual); + issue527_shouldEmitLeaderAlwaysAsWholeString(); } @Test