Skip to content

Commit 510013c

Browse files
committed
Follow sonarcloud advices
- increase test coverage - add copyright header - use constant See #300.
1 parent 83c4d56 commit 510013c

File tree

3 files changed

+88
-27
lines changed

3 files changed

+88
-27
lines changed

metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* Copyright 2019 Pascal Christoph (hbz) and others
2+
*
3+
* Licensed under the Apache License, Version 2.0 the "License";
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
116
package org.metafacture.biblio.marc21;
217

318
import java.util.Collections;
@@ -126,7 +141,7 @@ public void endRecord() {
126141
@Override
127142
public void startEntity(final String name) {
128143
currentEntity = name;
129-
if (!name.equals("leader")) {
144+
if (!name.equals(Marc21EventNames.LEADER_ENTITY)) {
130145
if (name.length() != 5) {
131146
String message = String.format("Entity too short." + "Got a string ('%s') of length %d."
132147
+ "Expected a length of 5 (field + indicators).", name, name.length());
@@ -145,7 +160,7 @@ public void startEntity(final String name) {
145160

146161
@Override
147162
public void endEntity() {
148-
if (!currentEntity.equals("leader")) {
163+
if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) {
149164
decrementIndentationLevel();
150165
prettyPrintIndentation();
151166
writeRaw(DATAFIELD_CLOSE);
@@ -162,7 +177,7 @@ public void literal(final String name, final String value) {
162177
writeEscaped(value.trim());
163178
writeRaw(CONTROLFIELD_CLOSE);
164179
prettyPrintNewLine();
165-
} else if (!currentEntity.equals("leader")) {
180+
} else if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) {
166181
prettyPrintIndentation();
167182
writeRaw(String.format(SUBFIELD_OPEN_TEMPLATE, name));
168183
writeEscaped(value.trim());

metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.After;
2323
import org.junit.Before;
2424
import org.junit.Test;
25+
import org.metafacture.framework.MetafactureException;
2526
import org.metafacture.framework.helpers.DefaultObjectReceiver;
2627

2728
/**
@@ -33,8 +34,8 @@
3334
*/
3435

3536
public class MarcXmlEncoderTest {
36-
private static StringBuilder resultCollector ;
37-
private static MarcXmlEncoder encoder ;
37+
private static StringBuilder resultCollector;
38+
private static MarcXmlEncoder encoder;
3839
private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
3940
private static final String XML_1_DECLARATION = "<?xml version=\"1.1\" encoding=\"UTF-8\"?>";
4041
private static final String XML_16_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>";
@@ -46,9 +47,9 @@ public class MarcXmlEncoderTest {
4647
+ "</marc:datafield></marc:record>";
4748
private static final String XML_MARC_COLLECTION_END_TAG = "</marc:collection>";
4849
private static final String RECORD_ID = "92005291";
49-
50+
5051
@Before
51-
public void setUp() throws Exception {
52+
public void setUp() {
5253
encoder = new MarcXmlEncoder();
5354
encoder.setFormatted(false);
5455
encoder.setReceiver(new DefaultObjectReceiver<String>() {
@@ -57,11 +58,11 @@ public void process(final String obj) {
5758
resultCollector.append(obj);
5859
}
5960
});
60-
resultCollector= new StringBuilder();
61+
resultCollector = new StringBuilder();
6162
}
6263

6364
@After
64-
public void tearDown() throws Exception {
65+
public void tearDown() {
6566
}
6667

6768
private void addOneRecord(MarcXmlEncoder encoder) {
@@ -74,7 +75,7 @@ private void addOneRecord(MarcXmlEncoder encoder) {
7475
}
7576

7677
@Test
77-
public void doNotOmitXmlDeclaration() throws Exception {
78+
public void doNotOmitXmlDeclaration() {
7879
encoder.omitXmlDeclaration(false);
7980
addOneRecord(encoder);
8081
encoder.closeStream();
@@ -84,7 +85,7 @@ public void doNotOmitXmlDeclaration() throws Exception {
8485
}
8586

8687
@Test
87-
public void omitXmlDeclaration() throws Exception {
88+
public void omitXmlDeclaration() {
8889
encoder.omitXmlDeclaration(true);
8990
addOneRecord(encoder);
9091
encoder.closeStream();
@@ -94,7 +95,7 @@ public void omitXmlDeclaration() throws Exception {
9495
}
9596

9697
@Test
97-
public void setXmlVersion() throws Exception {
98+
public void setXmlVersion() {
9899
encoder.omitXmlDeclaration(false);
99100
encoder.setXmlVersion("1.1");
100101
addOneRecord(encoder);
@@ -105,7 +106,7 @@ public void setXmlVersion() throws Exception {
105106
}
106107

107108
@Test
108-
public void setXmlEncoding() throws Exception {
109+
public void setXmlEncoding() {
109110
encoder.omitXmlDeclaration(false);
110111
encoder.setXmlEncoding("UTF-16");
111112
addOneRecord(encoder);
@@ -116,45 +117,90 @@ public void setXmlEncoding() throws Exception {
116117
}
117118

118119
@Test
119-
public void createAnEmptyRecord() throws Exception {
120-
encoder.startRecord("1");
120+
public void createAnEmptyRecord() {
121+
encoder.startRecord(RECORD_ID);
121122
encoder.endRecord();
122123
encoder.closeStream();
123-
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record></marc:record>"
124-
+ XML_MARC_COLLECTION_END_TAG;
124+
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record></marc:record>" + XML_MARC_COLLECTION_END_TAG;
125125
String actual = resultCollector.toString();
126126
assertEquals(expected, actual);
127127
}
128128

129129
@Test
130-
public void createARecord() throws Exception {
130+
public void createARecordPrettyPrint() {
131+
encoder.setFormatted(true);
131132
addOneRecord(encoder);
132133
encoder.closeStream();
133-
String expected = XML_DECLARATION + XML_ROOT_OPEN + XML_RECORD + "</marc:collection>";
134+
String expected = XML_DECLARATION + "\n" + XML_ROOT_OPEN + "\n"// " <marc:record>\n"
135+
+ "\t<marc:record>\n"//
136+
+ "\t\t<marc:controlfield tag=\"001\">92005291</marc:controlfield>\n"//
137+
+ "\t\t<marc:datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"//
138+
+ "\t\t\t<marc:subfield code=\"a\">92005291</marc:subfield>\n"//
139+
+ "\t\t</marc:datafield>\n"
140+
+ "\t</marc:record>\n"//
141+
+ XML_MARC_COLLECTION_END_TAG;
134142
String actual = resultCollector.toString();
135143
assertEquals(expected, actual);
136144
}
137145

138146
@Test
139-
public void createTwoRecordsInOneCollection() throws Exception {
147+
public void createARecordWithEscapedSequences() {
148+
encoder.startRecord(RECORD_ID);
149+
encoder.literal("001", "&<>\"");
150+
encoder.endRecord();
151+
encoder.onResetStream();
152+
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record>"
153+
+ "<marc:controlfield tag=\"001\">&amp;&lt;&gt;&quot;</marc:controlfield>" + "</marc:record>"
154+
+ XML_MARC_COLLECTION_END_TAG;
155+
String actual = resultCollector.toString();
156+
assertEquals(expected, actual);
157+
}
158+
159+
@Test
160+
public void createTwoRecordsInOneCollection() {
140161
addOneRecord(encoder);
141162
addOneRecord(encoder);
142163
encoder.closeStream();
143-
String expected = XML_DECLARATION + XML_ROOT_OPEN + XML_RECORD + XML_RECORD + "</marc:collection>";
164+
String expected = XML_DECLARATION + XML_ROOT_OPEN + XML_RECORD + XML_RECORD + XML_MARC_COLLECTION_END_TAG;
144165
String actual = resultCollector.toString();
145166
assertEquals(expected, actual);
146167
}
147168

169+
@Test(expected = MetafactureException.class)
170+
public void emitExceptionWhenEntityLengthNot5() {
171+
encoder.startRecord(RECORD_ID);
172+
encoder.startEntity("123456");
173+
}
174+
148175
@Test
149-
public void createAnRecordWithLeader() throws Exception {
176+
public void createAnRecordWithLeader() {
150177
encoder.startRecord("1");
151-
encoder.startEntity("leader");
152-
encoder.literal("leader", "dummy");
178+
encoder.startEntity(Marc21EventNames.LEADER_ENTITY);
179+
encoder.literal(Marc21EventNames.LEADER_ENTITY, "dummy");
153180
encoder.endEntity();
154181
encoder.endRecord();
155182
encoder.closeStream();
156-
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record><marc:leader>dummy</marc:leader></marc:record>"
157-
+ XML_MARC_COLLECTION_END_TAG;
183+
String expected = XML_DECLARATION + XML_ROOT_OPEN
184+
+ "<marc:record><marc:leader>dummy</marc:leader></marc:record>" + XML_MARC_COLLECTION_END_TAG;
185+
String actual = resultCollector.toString();
186+
assertEquals(expected, actual);
187+
}
188+
189+
@Test
190+
public void sendDataAndClearWhenRecordStartedAndStreamResets() {
191+
encoder.startRecord("1");
192+
encoder.onResetStream();
193+
encoder.endRecord();
194+
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record>" + XML_MARC_COLLECTION_END_TAG
195+
+ "</marc:record>";
196+
String actual = resultCollector.toString();
197+
assertEquals(expected, actual);
198+
}
199+
200+
@Test
201+
public void sendAndClearDataWhenOnResetStream() {
202+
encoder.onResetStream();
203+
String expected = "";
158204
String actual = resultCollector.toString();
159205
assertEquals(expected, actual);
160206
}

metafacture-strings/src/main/java/org/metafacture/strings/LineRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@FluxCommand("lines-to-records")
3737
public final class LineRecorder implements ObjectPipe<String, ObjectReceiver<String>> {
3838

39-
private final static int SB_CAPACITY = 4096 * 7;
39+
private static final int SB_CAPACITY = 4096 * 7;
4040
// empty line is the default
4141
private String recordMarkerRegexp = "^\\s*$";
4242
private StringBuilder record = new StringBuilder(SB_CAPACITY);

0 commit comments

Comments
 (0)