From a74d265a0da1fd84ef332643c02e1ced0dd2e3aa Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Mon, 13 May 2024 13:49:38 +0200 Subject: [PATCH] Take hints from @blackwinter into account (#531) --- .../biblio/marc21/MarcXmlEncoder.java | 10 +++--- .../biblio/marc21/MarcXmlEncoderTest.java | 32 ++++++++----------- 2 files changed, 19 insertions(+), 23 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 42fca105d..50e933576 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 @@ -335,7 +335,7 @@ public void literal(final String name, final String value) { builder.insert(recordAttributeOffset, String.format(ATTRIBUTE_TEMPLATE, name, value)); } } - else if (!writeLeader(name, value)) { + else if (!appendLeader(name, value)) { prettyPrintIndentation(); writeTag(Tag.controlfield::open, name); if (value != null) { @@ -345,7 +345,7 @@ else if (!writeLeader(name, value)) { prettyPrintNewLine(); } } - else if (!writeLeader(currentEntity, value)) { + else if (!appendLeader(currentEntity, value)) { prettyPrintIndentation(); writeTag(Tag.subfield::open, name); writeEscaped(value.trim()); @@ -414,7 +414,7 @@ private void writeRaw(final String str) { * * @param str the unescaped sequence to be written */ - private void writeRawLeader(final String str) { + private void appendLeader(final String str) { builderLeader.append(str); } @@ -435,9 +435,9 @@ private void writeLeader() { prettyPrintNewLine(); } - private boolean writeLeader(final String name, final String value) { + private boolean appendLeader(final String name, final String value) { if (name.equals(Marc21EventNames.LEADER_ENTITY)) { - writeRawLeader(value); + appendLeader(value); return true; } else { 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 f57b87e3f..237a15d31 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; @@ -58,15 +57,13 @@ public class MarcXmlEncoderTest { private static final String RECORD_ID = "92005291"; private static StringBuilder resultCollector; - private static MarcXmlEncoder encoder; - private static MarcXmlEncoder encoder_ensureCorrectMarc21Xml; + private final MarcXmlEncoder encoder = new MarcXmlEncoder(); + private final MarcXmlEncoder encoder_ensureCorrectMarc21Xml = new MarcXmlEncoder(); @Before public void setUp() { - encoder = new MarcXmlEncoder(); initializeEncoder(encoder); - encoder_ensureCorrectMarc21Xml = new MarcXmlEncoder(); encoder_ensureCorrectMarc21Xml.setEnsureCorrectMarc21Xml(true); initializeEncoder(encoder_ensureCorrectMarc21Xml); } @@ -243,21 +240,15 @@ public void issue336_createRecordWithTopLevelLeader() { @Test public void issue336_createRecordWithTopLevelLeader_ensureCorrectMarc21Xml() { - issue336_createRecordWithTopLevelLeader_correctMarc21Xml(encoder_ensureCorrectMarc21Xml); + createRecordWithTopLevelLeader(encoder_ensureCorrectMarc21Xml, "00048naa a2200037uc 4500"); } - @Test(expected = NullPointerException.class) - public void setParameterAfterSettingReceiver_ensureCorrectMarc21Xml() { - encoder_ensureCorrectMarc21Xml.setEnsureCorrectMarc21Xml(true); - issue336_createRecordWithTopLevelLeader_correctMarc21Xml(encoder_ensureCorrectMarc21Xml); - } - - @Test(expected = ComparisonFailure.class) - public void issue336_createRecordWithTopLevelLeader_correctMarc21Xml() { - issue336_createRecordWithTopLevelLeader_correctMarc21Xml(encoder); + @Test + public void issue336_createRecordWithTopLevelLeader_defaultMarc21Xml() { + createRecordWithTopLevelLeader(encoder,"00000naa a2200000uc 4500"); } - private void issue336_createRecordWithTopLevelLeader_correctMarc21Xml(MarcXmlEncoder encoder) { + private void createRecordWithTopLevelLeader(final MarcXmlEncoder encoder, final String expectedLeader) { encoder.startRecord("1"); encoder.literal("001", "8u3287432"); encoder.literal(Marc21EventNames.LEADER_ENTITY, "00000naa a2200000uc 4500"); @@ -265,11 +256,17 @@ private void issue336_createRecordWithTopLevelLeader_correctMarc21Xml(MarcXmlEnc encoder.closeStream(); String expected = XML_DECLARATION + XML_ROOT_OPEN + "8u3287432" + - "00048naa a2200037uc 4500" + XML_MARC_COLLECTION_END_TAG; + "" + expectedLeader + "" + XML_MARC_COLLECTION_END_TAG; String actual = resultCollector.toString(); assertEquals(expected, actual); } + @Test(expected = NullPointerException.class) + public void setParameterAfterSettingReceiver() { + encoder_ensureCorrectMarc21Xml.setEnsureCorrectMarc21Xml(true); + createRecordWithTopLevelLeader(encoder_ensureCorrectMarc21Xml,"ignored"); + } + @Test public void issue527ShouldEmitLeaderAlwaysAsWholeString() { issue527ShouldEmitLeaderAlwaysAsWholeString(encoder); @@ -285,7 +282,6 @@ private void issue527ShouldEmitLeaderAlwaysAsWholeString(MarcXmlEncoder encoder) encoder.startRecord("1"); encoder.startEntity(Marc21EventNames.LEADER_ENTITY); encoder.literal(RECORD_STATUS_LITERAL, "a"); - encoder.literal(RECORD_TYPE_LITERAL, "o"); encoder.literal(BIBLIOGRAPHIC_LEVEL_LITERAL, "a"); encoder.literal(TYPE_OF_CONTROL_LITERAL, " ");