Skip to content

Commit

Permalink
Take hints from @blackwinter into account (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed May 13, 2024
1 parent fde6ba1 commit 3611be6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down Expand Up @@ -414,10 +414,20 @@ 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);
}

private boolean appendLeader(final String name, final String value) {
if (name.equals(Marc21EventNames.LEADER_ENTITY)) {
appendLeader(value);
return true;
}
else {
return false;
}
}

/**
* Writes an escaped sequence.
*
Expand All @@ -435,16 +445,6 @@ private void writeLeader() {
prettyPrintNewLine();
}

private boolean writeLeader(final String name, final String value) {
if (name.equals(Marc21EventNames.LEADER_ENTITY)) {
writeRawLeader(value);
return true;
}
else {
return false;
}
}

private void writeTag(final Function<Object[], String> function, final Object... args) {
final Object[] allArgs = Arrays.copyOf(namespacePrefix, namespacePrefix.length + args.length);
System.arraycopy(args, 0, allArgs, namespacePrefix.length, args.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -243,33 +240,33 @@ 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");
encoder.endRecord();
encoder.closeStream();
String expected = XML_DECLARATION + XML_ROOT_OPEN
+ "<marc:record><marc:controlfield tag=\"001\">8u3287432</marc:controlfield>" +
"<marc:leader>00048naa a2200037uc 4500</marc:leader></marc:record>" + XML_MARC_COLLECTION_END_TAG;
"<marc:leader>" + expectedLeader + "</marc:leader></marc:record>" + 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);
Expand All @@ -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, " ");
Expand Down

0 comments on commit 3611be6

Please sign in to comment.