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
+
1
17
package org .metafacture .biblio .marc21 ;
2
18
3
19
import static org .junit .Assert .assertEquals ;
8
24
import org .junit .Test ;
9
25
import org .metafacture .framework .helpers .DefaultObjectReceiver ;
10
26
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
+ */
15
34
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
+
16
50
@ Before
17
51
public void setUp () throws Exception {
18
52
encoder = new MarcXmlEncoder ();
@@ -23,18 +57,18 @@ public void process(final String obj) {
23
57
resultCollector .append (obj );
24
58
}
25
59
});
26
- resultCollector = new StringBuilder ();
60
+ resultCollector = new StringBuilder ();
27
61
}
28
62
29
63
@ After
30
64
public void tearDown () throws Exception {
31
65
}
32
66
33
67
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 );
36
70
encoder .startEntity ("010 " );
37
- encoder .literal ("a" , "92005291" );
71
+ encoder .literal ("a" , RECORD_ID );
38
72
encoder .endEntity ();
39
73
encoder .endRecord ();
40
74
}
@@ -46,7 +80,7 @@ public void doNotOmitXmlDeclaration() throws Exception {
46
80
encoder .closeStream ();
47
81
48
82
String actual = resultCollector .toString ();
49
- assertTrue (actual .startsWith ("<?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?>" ));
83
+ assertTrue (actual .startsWith (XML_DECLARATION ));
50
84
}
51
85
52
86
@ Test
@@ -56,7 +90,7 @@ public void omitXmlDeclaration() throws Exception {
56
90
encoder .closeStream ();
57
91
String actual = resultCollector .toString ();
58
92
assertTrue (actual .startsWith ("<marc:collection" ));
59
- assertTrue (actual .endsWith ("</marc:collection>" ));
93
+ assertTrue (actual .endsWith (XML_MARC_COLLECTION_END_TAG ));
60
94
}
61
95
62
96
@ Test
@@ -67,7 +101,7 @@ public void setXmlVersion() throws Exception {
67
101
encoder .closeStream ();
68
102
69
103
String actual = resultCollector .toString ();
70
- assertTrue (actual .startsWith ("<?xml version= \" 1.1 \" encoding= \" UTF-8 \" ?>" ));
104
+ assertTrue (actual .startsWith (XML_1_DECLARATION ));
71
105
}
72
106
73
107
@ Test
@@ -78,15 +112,16 @@ public void setXmlEncoding() throws Exception {
78
112
encoder .closeStream ();
79
113
80
114
String actual = resultCollector .toString ();
81
- assertTrue (actual .startsWith ("<?xml version= \" 1.0 \" encoding= \" UTF-16 \" ?>" ));
115
+ assertTrue (actual .startsWith (XML_16_DECLARATION ));
82
116
}
83
117
84
118
@ Test
85
119
public void createAnEmptyRecord () throws Exception {
86
120
encoder .startRecord ("1" );
87
121
encoder .endRecord ();
88
122
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 ;
90
125
String actual = resultCollector .toString ();
91
126
assertEquals (expected , actual );
92
127
}
@@ -95,7 +130,7 @@ public void createAnEmptyRecord() throws Exception {
95
130
public void createARecord () throws Exception {
96
131
addOneRecord (encoder );
97
132
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>" ;
99
134
String actual = resultCollector .toString ();
100
135
assertEquals (expected , actual );
101
136
}
@@ -105,10 +140,8 @@ public void createTwoRecordsInOneCollection() throws Exception {
105
140
addOneRecord (encoder );
106
141
addOneRecord (encoder );
107
142
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>" ;
110
144
String actual = resultCollector .toString ();
111
-
112
145
assertEquals (expected , actual );
113
146
}
114
147
@@ -120,7 +153,8 @@ public void createAnRecordWithLeader() throws Exception {
120
153
encoder .endEntity ();
121
154
encoder .endRecord ();
122
155
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 ;
124
158
String actual = resultCollector .toString ();
125
159
assertEquals (expected , actual );
126
160
}
0 commit comments