|
| 1 | +/* |
| 2 | + * Copyright 2014 Deutsche Nationalbibliothek |
| 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 | + |
| 17 | +package org.metafacture.biblio.pica; |
| 18 | + |
| 19 | +import org.metafacture.framework.StreamReceiver; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.mockito.InOrder; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.mockito.MockitoAnnotations; |
| 28 | +import org.xml.sax.SAXException; |
| 29 | +import org.xml.sax.helpers.AttributesImpl; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for class {@link PicaXmlHandler}. |
| 33 | + * |
| 34 | + * @author Tobias Bülte |
| 35 | + * |
| 36 | + */ |
| 37 | +public final class PicaXmlHandlerTest { |
| 38 | + |
| 39 | + private static final String NAMESPACE = "info:srw/schema/5/picaXML-v1.0"; |
| 40 | + private static final String RECORD = "record"; |
| 41 | + private static final String DATAFIELD = "datafield"; |
| 42 | + private static final String SUBFIELD = "subfield"; |
| 43 | + |
| 44 | + private PicaXmlHandler picaXmlHandler; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private StreamReceiver receiver; |
| 48 | + |
| 49 | + public PicaXmlHandlerTest() { |
| 50 | + } |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setup() { |
| 54 | + MockitoAnnotations.initMocks(this); |
| 55 | + picaXmlHandler = new PicaXmlHandler(); |
| 56 | + picaXmlHandler.setReceiver(receiver); |
| 57 | + } |
| 58 | + |
| 59 | + @After |
| 60 | + public void cleanup() { |
| 61 | + picaXmlHandler.closeStream(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void shouldLabelDataFieldWithAndWithoutOccuence() |
| 66 | + throws SAXException { |
| 67 | + final AttributesImpl attributes = new AttributesImpl(); |
| 68 | + |
| 69 | + final String fieldValue1 = "1234"; |
| 70 | + final String fieldValue2 = "utf-8"; |
| 71 | + |
| 72 | + picaXmlHandler.startElement(NAMESPACE, RECORD, "", attributes); |
| 73 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 74 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 75 | + attributes.clear(); |
| 76 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 77 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 78 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 79 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 80 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 81 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "201U"); |
| 82 | + attributes.addAttribute(null, "occurence", "occurence", "CDATA", "01"); |
| 83 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 84 | + attributes.clear(); |
| 85 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 86 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 87 | + picaXmlHandler.characters(fieldValue2.toCharArray(), 0, fieldValue2.length()); |
| 88 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 89 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 90 | + picaXmlHandler.endElement(NAMESPACE, RECORD, ""); |
| 91 | + |
| 92 | + |
| 93 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 94 | + ordered.verify(receiver).startRecord(""); |
| 95 | + ordered.verify(receiver).startEntity("003@"); |
| 96 | + ordered.verify(receiver).literal("0", fieldValue1); |
| 97 | + ordered.verify(receiver).endEntity(); |
| 98 | + ordered.verify(receiver).startEntity("201U01"); |
| 99 | + ordered.verify(receiver).literal("0", fieldValue2); |
| 100 | + ordered.verify(receiver).endEntity(); |
| 101 | + ordered.verify(receiver).endRecord(); |
| 102 | + ordered.verifyNoMoreInteractions(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void shouldRecognizeRecordsWithNamespace() |
| 107 | + throws SAXException { |
| 108 | + final AttributesImpl attributes = new AttributesImpl(); |
| 109 | + |
| 110 | + final String fieldValue1 = "1234"; |
| 111 | + |
| 112 | + picaXmlHandler.startElement(NAMESPACE, RECORD, "", attributes); |
| 113 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 114 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 115 | + attributes.clear(); |
| 116 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 117 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 118 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 119 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 120 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 121 | + |
| 122 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 123 | + ordered.verify(receiver).startRecord(""); |
| 124 | + ordered.verify(receiver).startEntity("003@"); |
| 125 | + ordered.verify(receiver).literal("0", fieldValue1); |
| 126 | + ordered.verify(receiver).endEntity(); |
| 127 | + ordered.verify(receiver).endRecord(); |
| 128 | + ordered.verifyNoMoreInteractions(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void shouldNotRecognizeRecordsWithoutNamespace() |
| 133 | + throws SAXException { |
| 134 | + final AttributesImpl attributes = new AttributesImpl(); |
| 135 | + |
| 136 | + final String fieldValue1 = "1234"; |
| 137 | + |
| 138 | + picaXmlHandler.startElement(null, RECORD, "", attributes); |
| 139 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 140 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 141 | + attributes.clear(); |
| 142 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 143 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 144 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 145 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 146 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 147 | + picaXmlHandler.endElement(null, RECORD, ""); |
| 148 | + |
| 149 | + Mockito.verifyNoMoreInteractions(receiver); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void shouldRecognizeRecordsWithoutNamespace() |
| 154 | + throws SAXException { |
| 155 | + final AttributesImpl attributes = new AttributesImpl(); |
| 156 | + |
| 157 | + final String fieldValue1 = "1234"; |
| 158 | + |
| 159 | + picaXmlHandler.setNamespace(""); |
| 160 | + picaXmlHandler.startElement(null, RECORD, "", attributes); |
| 161 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 162 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 163 | + attributes.clear(); |
| 164 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 165 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 166 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 167 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 168 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 169 | + picaXmlHandler.endElement(null, RECORD, ""); |
| 170 | + |
| 171 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 172 | + ordered.verify(receiver).startRecord(""); |
| 173 | + ordered.verify(receiver).startEntity("003@"); |
| 174 | + ordered.verify(receiver).literal("0", fieldValue1); |
| 175 | + ordered.verify(receiver).endEntity(); |
| 176 | + ordered.verify(receiver).endRecord(); |
| 177 | + ordered.verifyNoMoreInteractions(); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace() |
| 182 | + throws SAXException { |
| 183 | + final AttributesImpl attributes = new AttributesImpl(); |
| 184 | + |
| 185 | + final String fieldValue1 = "1234"; |
| 186 | + |
| 187 | + picaXmlHandler.setNamespace(""); |
| 188 | + picaXmlHandler.startElement(NAMESPACE, RECORD, "", attributes); |
| 189 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 190 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 191 | + attributes.clear(); |
| 192 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 193 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 194 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 195 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 196 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 197 | + picaXmlHandler.endElement(null, RECORD, ""); |
| 198 | + |
| 199 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 200 | + ordered.verify(receiver).startRecord(""); |
| 201 | + ordered.verify(receiver).startEntity("003@"); |
| 202 | + ordered.verify(receiver).literal("0", fieldValue1); |
| 203 | + ordered.verify(receiver).endEntity(); |
| 204 | + ordered.verify(receiver).endRecord(); |
| 205 | + ordered.verifyNoMoreInteractions(); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace() |
| 210 | + throws SAXException { |
| 211 | + final AttributesImpl attributes = new AttributesImpl(); |
| 212 | + |
| 213 | + final String fieldValue1 = "1234"; |
| 214 | + |
| 215 | + picaXmlHandler.setIgnoreNamespace(true); |
| 216 | + picaXmlHandler.startElement(NAMESPACE, RECORD, "", attributes); |
| 217 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 218 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 219 | + attributes.clear(); |
| 220 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 221 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 222 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 223 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 224 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 225 | + picaXmlHandler.endElement(null, RECORD, ""); |
| 226 | + |
| 227 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 228 | + ordered.verify(receiver).startRecord(""); |
| 229 | + ordered.verify(receiver).startEntity("003@"); |
| 230 | + ordered.verify(receiver).literal("0", fieldValue1); |
| 231 | + ordered.verify(receiver).endEntity(); |
| 232 | + ordered.verify(receiver).endRecord(); |
| 233 | + ordered.verifyNoMoreInteractions(); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void issue569ShouldRecognizeRecordsWithAndWithoutNamespaceOrderIndependently() |
| 238 | + throws SAXException { |
| 239 | + final AttributesImpl attributes = new AttributesImpl(); |
| 240 | + |
| 241 | + final String fieldValue1 = "1234"; |
| 242 | + |
| 243 | + picaXmlHandler.setIgnoreNamespace(true); |
| 244 | + picaXmlHandler.setNamespace(""); |
| 245 | + picaXmlHandler.startElement(null, RECORD, "", attributes); |
| 246 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 247 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 248 | + attributes.clear(); |
| 249 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 250 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 251 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 252 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 253 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 254 | + picaXmlHandler.endElement(NAMESPACE, RECORD, ""); |
| 255 | + |
| 256 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 257 | + ordered.verify(receiver).startRecord(""); |
| 258 | + ordered.verify(receiver).startEntity("003@"); |
| 259 | + ordered.verify(receiver).literal("0", fieldValue1); |
| 260 | + ordered.verify(receiver).endEntity(); |
| 261 | + ordered.verify(receiver).endRecord(); |
| 262 | + ordered.verifyNoMoreInteractions(); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + public void issue569ShouldNotRecognizeRecordsWithAndWithoutNamespace() |
| 267 | + throws SAXException { |
| 268 | + final AttributesImpl attributes = new AttributesImpl(); |
| 269 | + |
| 270 | + final String fieldValue1 = "1234"; |
| 271 | + |
| 272 | + picaXmlHandler.setIgnoreNamespace(false); |
| 273 | + picaXmlHandler.startElement(null, RECORD, "", attributes); |
| 274 | + attributes.addAttribute(null, "tag", "tag", "CDATA", "003@"); |
| 275 | + picaXmlHandler.startElement(null, DATAFIELD, "", attributes); |
| 276 | + attributes.clear(); |
| 277 | + attributes.addAttribute(null, "code", "code", "CDATA", "0"); |
| 278 | + picaXmlHandler.startElement(null, SUBFIELD, "", attributes); |
| 279 | + picaXmlHandler.characters(fieldValue1.toCharArray(), 0, fieldValue1.length()); |
| 280 | + picaXmlHandler.endElement(null, SUBFIELD, ""); |
| 281 | + picaXmlHandler.endElement(null, DATAFIELD, ""); |
| 282 | + picaXmlHandler.endElement(NAMESPACE, RECORD, ""); |
| 283 | + |
| 284 | + Mockito.verify(receiver).endRecord(); |
| 285 | + |
| 286 | + Mockito.verifyNoMoreInteractions(receiver); |
| 287 | + } |
| 288 | + |
| 289 | +} |
0 commit comments