|
| 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.assertEquals; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | + |
| 25 | +import org.apache.commons.io.IOUtils; |
| 26 | +import org.culturegraph.mf.types.Triple; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.rules.TemporaryFolder; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link TripleObjectWriter}. |
| 35 | + * |
| 36 | + * @author Christoph Böhme |
| 37 | + */ |
| 38 | +public final class TripleObjectWriterTest { |
| 39 | + |
| 40 | + private static final String SUBJECT1 = "subject1"; |
| 41 | + private static final String SUBJECT2 = "subject2"; |
| 42 | + private static final String STRUCTURED_SUBJECT_A = "a"; |
| 43 | + private static final String STRUCTURED_SUBJECT_B = "b"; |
| 44 | + private static final String STRUCTURED_SUBJECT = STRUCTURED_SUBJECT_A + "/" + STRUCTURED_SUBJECT_B; |
| 45 | + private static final String PREDICATE = "predicate"; |
| 46 | + private static final String OBJECT1 = "object-data 1"; |
| 47 | + private static final String OBJECT2 = "object-data 2"; |
| 48 | + |
| 49 | + private TripleObjectWriter tripleObjectWriter; |
| 50 | + |
| 51 | + // NO CHECKSTYLE VisibilityModifier|DeclarationOrder FOR 3 LINES: |
| 52 | + // JUnit requires rules to be public |
| 53 | + @Rule |
| 54 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 55 | + |
| 56 | + private String baseDir; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setup() throws IOException { |
| 60 | + baseDir = tempFolder.newFolder().getAbsolutePath(); |
| 61 | + tripleObjectWriter = new TripleObjectWriter(baseDir); |
| 62 | + } |
| 63 | + |
| 64 | + @After |
| 65 | + public void cleanup() { |
| 66 | + tripleObjectWriter.closeStream(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testShouldWriteObjectOfTripleIntoFile() throws IOException { |
| 71 | + tripleObjectWriter.process(new Triple(SUBJECT1, PREDICATE, OBJECT1)); |
| 72 | + tripleObjectWriter.process(new Triple(SUBJECT2, PREDICATE, OBJECT2)); |
| 73 | + |
| 74 | + final String filename1 = baseDir + File.separator + SUBJECT1 + File.separator + PREDICATE; |
| 75 | + final String filename2 = baseDir + File.separator + SUBJECT2 + File.separator + PREDICATE; |
| 76 | + assertEquals(get(filename1), OBJECT1); |
| 77 | + assertEquals(get(filename2), OBJECT2); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testShouldMapStructuredSubjectsToDirectories() throws IOException { |
| 82 | + tripleObjectWriter.process(new Triple(STRUCTURED_SUBJECT, PREDICATE, OBJECT1)); |
| 83 | + |
| 84 | + final String filename = baseDir |
| 85 | + + File.separator + STRUCTURED_SUBJECT_A + File.separator + STRUCTURED_SUBJECT_B |
| 86 | + + File.separator + PREDICATE; |
| 87 | + assertEquals(get(filename), OBJECT1); |
| 88 | + } |
| 89 | + |
| 90 | + private String get(final String filename) throws IOException { |
| 91 | + final InputStream stream = new FileInputStream(filename); |
| 92 | + return IOUtils.toString(stream, tripleObjectWriter.getEncoding()); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments