|
| 1 | +/* |
| 2 | + * Copyright 2013 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 | +package org.culturegraph.mf.stream.sink; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertArrayEquals; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.util.Arrays; |
| 25 | + |
| 26 | +import org.apache.commons.io.IOUtils; |
| 27 | +import org.culturegraph.mf.util.FileCompression; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.rules.TemporaryFolder; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.Parameterized; |
| 33 | +import org.junit.runners.Parameterized.Parameters; |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * |
| 38 | + * Tests for file compression in {@link ObjectFileWriter}. |
| 39 | + * |
| 40 | + * @author Christoph Böhme |
| 41 | + * |
| 42 | + */ |
| 43 | +@RunWith(Parameterized.class) |
| 44 | +public final class ObjectFileWriterCompressionTest { |
| 45 | + |
| 46 | + private static final String DATA = "This could have been a remarkable sentence."; |
| 47 | + |
| 48 | + private static final String FILENAME_NONE = "compressed.txt"; |
| 49 | + private static final String FILENAME_BZ2 = "compressed.txt.bz2"; |
| 50 | + private static final String FILENAME_BZIP2 = "compressed.txt.bzip2"; |
| 51 | + private static final String FILENAME_GZ = "compressed.txt.gz"; |
| 52 | + private static final String FILENAME_GZIP = "compressed.txt.gzip"; |
| 53 | + private static final String FILENAME_XZ = "compressed.txt.xz"; |
| 54 | + |
| 55 | + private static final byte[] MAGIC_BYTES_NONE = { 'T', 'h', 'i', 's' }; |
| 56 | + private static final byte[] MAGIC_BYTES_BZIP2 = { 'B', 'Z', 'h' }; |
| 57 | + private static final byte[] MAGIC_BYTES_GZIP = { (byte)0x1f, (byte)0x8b }; |
| 58 | + private static final byte[] MAGIC_BYTES_XZ = { (byte)0xfd, '7', 'z', 'X', 'Z', (byte)0x00 }; |
| 59 | + |
| 60 | + // NO CHECKSTYLE VisibilityModifier FOR 3 LINES: |
| 61 | + // JUnit requires rules to be public |
| 62 | + @Rule |
| 63 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 64 | + |
| 65 | + private final String fileName; |
| 66 | + private final FileCompression compression; |
| 67 | + private final byte[] magicBytes; |
| 68 | + |
| 69 | + public ObjectFileWriterCompressionTest(final String fileName, final FileCompression compression, |
| 70 | + final byte[] magicBytes) { |
| 71 | + this.fileName = fileName; |
| 72 | + this.compression = compression; |
| 73 | + this.magicBytes = magicBytes; |
| 74 | + } |
| 75 | + |
| 76 | + @Parameters |
| 77 | + public static Iterable<Object[]> data() { |
| 78 | + return Arrays.asList(new Object[][] { |
| 79 | + { FILENAME_NONE, FileCompression.AUTO, MAGIC_BYTES_NONE }, |
| 80 | + { FILENAME_BZ2, FileCompression.AUTO, MAGIC_BYTES_BZIP2 }, |
| 81 | + { FILENAME_BZIP2, FileCompression.AUTO, MAGIC_BYTES_BZIP2 }, |
| 82 | + { FILENAME_GZ, FileCompression.AUTO, MAGIC_BYTES_GZIP }, |
| 83 | + { FILENAME_GZIP, FileCompression.AUTO, MAGIC_BYTES_GZIP }, |
| 84 | + { FILENAME_XZ, FileCompression.AUTO, MAGIC_BYTES_XZ }, |
| 85 | + { FILENAME_NONE, FileCompression.NONE, MAGIC_BYTES_NONE }, |
| 86 | + { FILENAME_BZ2, FileCompression.BZIP2, MAGIC_BYTES_BZIP2 }, |
| 87 | + { FILENAME_GZ, FileCompression.GZIP, MAGIC_BYTES_GZIP }, |
| 88 | + { FILENAME_XZ, FileCompression.XZ, MAGIC_BYTES_XZ }, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testWriteCompressedFiles() throws IOException { |
| 94 | + // This test only looks at the magic byte sequences in the |
| 95 | + // files to decide whether a compressed file was written. |
| 96 | + |
| 97 | + final File file = tempFolder.newFile(fileName); |
| 98 | + |
| 99 | + final ObjectFileWriter<String> writer = new ObjectFileWriter<String>(file.getAbsolutePath()); |
| 100 | + writer.setCompression(compression); |
| 101 | + writer.process(DATA); |
| 102 | + writer.closeStream(); |
| 103 | + |
| 104 | + final InputStream stream = new FileInputStream(file); |
| 105 | + final byte[] fileHeader; |
| 106 | + try { fileHeader = IOUtils.toByteArray(stream, magicBytes.length); } |
| 107 | + finally { stream.close(); } |
| 108 | + |
| 109 | + assertArrayEquals(magicBytes, fileHeader); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments