Skip to content

Commit d32fc00

Browse files
committed
Reformat ; use more constants; add header
See #300.
1 parent a99a34d commit d32fc00

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

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

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

319
import static org.junit.Assert.assertEquals;
@@ -8,11 +24,29 @@
824
import org.junit.Test;
925
import org.metafacture.framework.helpers.DefaultObjectReceiver;
1026

11-
public class MarcXmlEncoderTest {
12-
13-
private StringBuilder resultCollector;
14-
private MarcXmlEncoder encoder;
27+
/**
28+
* Tests for class {@link MarcXmlEncoder}.
29+
*
30+
* @author some Jan (Eberhardt) did almost all
31+
* @author Pascal Christoph (dr0i) dug it up again
32+
*
33+
*/
1534

35+
public class MarcXmlEncoderTest {
36+
private static StringBuilder resultCollector ;
37+
private static MarcXmlEncoder encoder ;
38+
private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
39+
private static final String XML_1_DECLARATION = "<?xml version=\"1.1\" encoding=\"UTF-8\"?>";
40+
private static final String XML_16_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>";
41+
private static final String XML_ROOT_OPEN = "<marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" "
42+
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21"
43+
+ "/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\">";
44+
private static final String XML_RECORD = "<marc:record><marc:controlfield tag=\"001\">92005291</marc:controlfield>"
45+
+ "<marc:datafield tag=\"010\" ind1=\" \" ind2=\" \"><marc:subfield code=\"a\">92005291</marc:subfield>"
46+
+ "</marc:datafield></marc:record>";
47+
private static final String XML_MARC_COLLECTION_END_TAG = "</marc:collection>";
48+
private static final String RECORD_ID = "92005291";
49+
1650
@Before
1751
public void setUp() throws Exception {
1852
encoder = new MarcXmlEncoder();
@@ -23,18 +57,18 @@ public void process(final String obj) {
2357
resultCollector.append(obj);
2458
}
2559
});
26-
resultCollector = new StringBuilder();
60+
resultCollector= new StringBuilder();
2761
}
2862

2963
@After
3064
public void tearDown() throws Exception {
3165
}
3266

3367
private void addOneRecord(MarcXmlEncoder encoder) {
34-
encoder.startRecord("92005291");
35-
encoder.literal("001", "92005291");
68+
encoder.startRecord(RECORD_ID);
69+
encoder.literal("001", RECORD_ID);
3670
encoder.startEntity("010 ");
37-
encoder.literal("a", "92005291");
71+
encoder.literal("a", RECORD_ID);
3872
encoder.endEntity();
3973
encoder.endRecord();
4074
}
@@ -46,7 +80,7 @@ public void doNotOmitXmlDeclaration() throws Exception {
4680
encoder.closeStream();
4781

4882
String actual = resultCollector.toString();
49-
assertTrue(actual.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
83+
assertTrue(actual.startsWith(XML_DECLARATION));
5084
}
5185

5286
@Test
@@ -56,7 +90,7 @@ public void omitXmlDeclaration() throws Exception {
5690
encoder.closeStream();
5791
String actual = resultCollector.toString();
5892
assertTrue(actual.startsWith("<marc:collection"));
59-
assertTrue(actual.endsWith("</marc:collection>"));
93+
assertTrue(actual.endsWith(XML_MARC_COLLECTION_END_TAG));
6094
}
6195

6296
@Test
@@ -67,7 +101,7 @@ public void setXmlVersion() throws Exception {
67101
encoder.closeStream();
68102

69103
String actual = resultCollector.toString();
70-
assertTrue(actual.startsWith("<?xml version=\"1.1\" encoding=\"UTF-8\"?>"));
104+
assertTrue(actual.startsWith(XML_1_DECLARATION));
71105
}
72106

73107
@Test
@@ -78,15 +112,16 @@ public void setXmlEncoding() throws Exception {
78112
encoder.closeStream();
79113

80114
String actual = resultCollector.toString();
81-
assertTrue(actual.startsWith("<?xml version=\"1.0\" encoding=\"UTF-16\"?>"));
115+
assertTrue(actual.startsWith(XML_16_DECLARATION));
82116
}
83117

84118
@Test
85119
public void createAnEmptyRecord() throws Exception {
86120
encoder.startRecord("1");
87121
encoder.endRecord();
88122
encoder.closeStream();
89-
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:record></marc:record></marc:collection>";
123+
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record></marc:record>"
124+
+ XML_MARC_COLLECTION_END_TAG;
90125
String actual = resultCollector.toString();
91126
assertEquals(expected, actual);
92127
}
@@ -95,7 +130,7 @@ public void createAnEmptyRecord() throws Exception {
95130
public void createARecord() throws Exception {
96131
addOneRecord(encoder);
97132
encoder.closeStream();
98-
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:record><marc:controlfield tag=\"001\">92005291</marc:controlfield><marc:datafield tag=\"010\" ind1=\" \" ind2=\" \"><marc:subfield code=\"a\">92005291</marc:subfield></marc:datafield></marc:record></marc:collection>";
133+
String expected = XML_DECLARATION + XML_ROOT_OPEN + XML_RECORD + "</marc:collection>";
99134
String actual = resultCollector.toString();
100135
assertEquals(expected, actual);
101136
}
@@ -105,10 +140,8 @@ public void createTwoRecordsInOneCollection() throws Exception {
105140
addOneRecord(encoder);
106141
addOneRecord(encoder);
107142
encoder.closeStream();
108-
109-
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:record><marc:controlfield tag=\"001\">92005291</marc:controlfield><marc:datafield tag=\"010\" ind1=\" \" ind2=\" \"><marc:subfield code=\"a\">92005291</marc:subfield></marc:datafield></marc:record><marc:record><marc:controlfield tag=\"001\">92005291</marc:controlfield><marc:datafield tag=\"010\" ind1=\" \" ind2=\" \"><marc:subfield code=\"a\">92005291</marc:subfield></marc:datafield></marc:record></marc:collection>";
143+
String expected = XML_DECLARATION + XML_ROOT_OPEN + XML_RECORD + XML_RECORD + "</marc:collection>";
110144
String actual = resultCollector.toString();
111-
112145
assertEquals(expected, actual);
113146
}
114147

@@ -120,7 +153,8 @@ public void createAnRecordWithLeader() throws Exception {
120153
encoder.endEntity();
121154
encoder.endRecord();
122155
encoder.closeStream();
123-
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:record><marc:leader>dummy</marc:leader></marc:record></marc:collection>";
156+
String expected = XML_DECLARATION + XML_ROOT_OPEN + "<marc:record><marc:leader>dummy</marc:leader></marc:record>"
157+
+ XML_MARC_COLLECTION_END_TAG;
124158
String actual = resultCollector.toString();
125159
assertEquals(expected, actual);
126160
}

0 commit comments

Comments
 (0)