From eb38a1bd29cb7a306561248da428dd60f3c2eaae Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Mon, 22 Apr 2024 11:55:29 +0200 Subject: [PATCH] Add test for #524 (#454) - fix typo - remove superflous endEntity - add method to avoid unnecessary char-string conversion --- .../biblio/marc21/Marc21Encoder.java | 23 +++++++++++-------- .../biblio/marc21/Marc21EncoderTest.java | 15 ++++++++++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java index 6be0a0d08..3cd536fe7 100644 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java +++ b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java @@ -199,16 +199,16 @@ public void literal(final String name, final String value) { private void processLeaderAsOneLiteral(final String value) { if (value.length() != Iso2709Constants.RECORD_LABEL_LENGTH) { throw new FormatException( - "leader literal must contain " + Iso2709Constants.RECORD_LABEL_LENGTH + " characters:" + value); + "leader literal must contain " + Iso2709Constants.RECORD_LABEL_LENGTH + " characters: " + value); } - processLeaderAsSubfields(Marc21EventNames.RECORD_STATUS_LITERAL, String.valueOf(value.charAt(Iso2709Constants.RECORD_STATUS_POS))); - processLeaderAsSubfields(Marc21EventNames.RECORD_TYPE_LITERAL, String.valueOf(value.charAt(Iso2709Constants.IMPL_CODES_START))); - processLeaderAsSubfields(Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.IMPL_CODES_START + 1))); - processLeaderAsSubfields(Marc21EventNames.TYPE_OF_CONTROL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.IMPL_CODES_START + 2))); - processLeaderAsSubfields(Marc21EventNames.CHARACTER_CODING_LITERAL, String.valueOf(value.charAt(Iso2709Constants.RECORD_STATUS_POS + Iso2709Constants.IMPL_CODES_LENGTH))); - processLeaderAsSubfields(Marc21EventNames.ENCODING_LEVEL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.SYSTEM_CHARS_START))); - processLeaderAsSubfields(Marc21EventNames.CATALOGING_FORM_LITERAL, String.valueOf(value.charAt(Iso2709Constants.SYSTEM_CHARS_START + 1))); - processLeaderAsSubfields(Marc21EventNames.MULTIPART_LEVEL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.SYSTEM_CHARS_START + 2))); + processLeaderAsSubfields(Marc21EventNames.RECORD_STATUS_LITERAL, value.charAt(Iso2709Constants.RECORD_STATUS_POS)); + processLeaderAsSubfields(Marc21EventNames.RECORD_TYPE_LITERAL, value.charAt(Iso2709Constants.IMPL_CODES_START)); + processLeaderAsSubfields(Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL, value.charAt(Iso2709Constants.IMPL_CODES_START + 1)); + processLeaderAsSubfields(Marc21EventNames.TYPE_OF_CONTROL_LITERAL, value.charAt(Iso2709Constants.IMPL_CODES_START + 2)); + processLeaderAsSubfields(Marc21EventNames.CHARACTER_CODING_LITERAL, value.charAt(Iso2709Constants.RECORD_STATUS_POS + Iso2709Constants.IMPL_CODES_LENGTH)); + processLeaderAsSubfields(Marc21EventNames.ENCODING_LEVEL_LITERAL, value.charAt(Iso2709Constants.SYSTEM_CHARS_START)); + processLeaderAsSubfields(Marc21EventNames.CATALOGING_FORM_LITERAL, value.charAt(Iso2709Constants.SYSTEM_CHARS_START + 1)); + processLeaderAsSubfields(Marc21EventNames.MULTIPART_LEVEL_LITERAL, value.charAt(Iso2709Constants.SYSTEM_CHARS_START + 2)); } private void processLeaderAsSubfields(final String name, final String value) { @@ -216,7 +216,10 @@ private void processLeaderAsSubfields(final String name, final String value) { throw new FormatException( "literal must only contain a single character:" + name); } - final char code = value.charAt(0); + processLeaderAsSubfields(name, value.charAt(0)); + } + + private void processLeaderAsSubfields(final String name, final char code) { switch (name) { case Marc21EventNames.RECORD_STATUS_LITERAL: requireValidCode(code, Marc21Constants.RECORD_STATUS_CODES); diff --git a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java index b766104a2..e8e70325d 100644 --- a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java +++ b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java @@ -120,7 +120,6 @@ public void issue454ShouldNotFailWhenProcessingEntityLeaderAsOneString() { marc21Encoder.startEntity(LEADER_ENTITY); marc21Encoder.literal(LEADER_ENTITY, "02602pam a2200529 c 4500"); marc21Encoder.endEntity(); - marc21Encoder.endEntity(); marc21Encoder.endRecord(); verify(receiver).process(matches("00026pam a2200025 c 4500\u001e\u001d")); @@ -130,10 +129,22 @@ public void issue454ShouldNotFailWhenProcessingEntityLeaderAsOneString() { public void issue454ShouldNotFailWhenProcessingLeaderAsOneString() { marc21Encoder.startRecord(""); marc21Encoder.literal(LEADER_ENTITY, "02602pam a2200529 c 4500"); - marc21Encoder.endEntity(); marc21Encoder.endRecord(); verify(receiver).process(matches("00026pam a2200025 c 4500\u001e\u001d")); } + @Test + public void issue524ShouldComputeValidLeader() { + marc21Encoder.startRecord(""); + marc21Encoder.literal(LEADER_ENTITY, "00000pam a7777777 c 4444"); + marc21Encoder.startEntity("021a "); + marc21Encoder.literal("v", "Fritz"); + marc21Encoder.literal("n", "Bauer"); + marc21Encoder.endEntity(); + marc21Encoder.endRecord(); + + verify(receiver).process(matches("00055pam a2200037 c 4500021001700000\u001e.*\u001d")); + } + }