| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap;
|
| 2 |
|
| 3 | import static org.junit.Assert.assertEquals;
|
| 4 |
|
| 5 | import java.io.ByteArrayOutputStream;
|
| 6 | import java.io.File;
|
| 7 | import java.io.IOException;
|
| 8 | import java.io.PrintStream;
|
| 9 |
|
| 10 | import org.junit.After;
|
| 11 | import org.junit.Before;
|
| 12 | import org.junit.Test;
|
| 13 | import org.slf4j.Logger;
|
| 14 | import org.slf4j.LoggerFactory;
|
| 15 |
|
| 16 | import de.ids_mannheim.korap.index.Indexer;
|
| 17 |
|
| 18 | /**
|
| 19 | * @author margaretha
|
| 20 | *
|
| 21 | */
|
| 22 | public class TestIndexer {
|
| 23 | private Logger logger = LoggerFactory.getLogger(TestIndexer.class);
|
| 24 | private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
| 25 | private String info = "usage: Krill indexer";
|
| 26 | private File outputDirectory = new File("test-index");
|
| 27 |
|
| 28 | @Test
|
| 29 | public void testArguments () throws IOException {
|
| 30 | Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 31 | "-i", "src/test/resources/bzk"});
|
| 32 | assertEquals("Added or updated 1 file.\n", outputStream.toString());
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 33 | }
|
| 34 |
|
| 35 | @Test
|
| 36 | public void testOutputArgument () throws IOException {
|
| 37 | Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 38 | "-i", "src/test/resources/bzk", "-o", "test-output"});
|
| 39 | assertEquals("Added or updated 1 file.\n", outputStream.toString());
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 40 | }
|
| 41 |
|
| 42 | @Test
|
| 43 | public void testMultipleInputFiles () throws IOException {
|
| 44 | Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 45 | "-i", "src/test/resources/wiki"});
|
| Akron | 90e05f2 | 2019-12-05 16:42:43 +0100 | [diff] [blame] | 46 | assertEquals("Added or updated 19 files.\n", outputStream.toString());
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 47 | }
|
| 48 |
|
| Akron | f0e3653 | 2019-03-06 11:43:21 +0100 | [diff] [blame] | 49 |
|
| 50 | @Test
|
| 51 | public void testAdding () throws IOException {
|
| 52 | Indexer.main(new String[] {
|
| 53 | "-c", "src/test/resources/krill.properties",
|
| 54 | "-i", "src/test/resources/bzk",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 55 | "-a"});
|
| Akron | f0e3653 | 2019-03-06 11:43:21 +0100 | [diff] [blame] | 56 | logger.info(outputStream.toString());
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 57 | assertEquals(outputStream.toString(), "Added 1 file.\n");
|
| Akron | f0e3653 | 2019-03-06 11:43:21 +0100 | [diff] [blame] | 58 | }
|
| 59 |
|
| 60 |
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 61 | @Test
|
| 62 | public void testMultipleInputDirectories () throws IOException {
|
| 63 | Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 64 | "-i",
|
| 65 | "src/test/resources/bzk;src/test/resources/goe;src/test/resources/sgbr",
|
| 66 | "-o", "test-index"});
|
| 67 | assertEquals("Added or updated 5 files.\n", outputStream.toString());
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 68 | }
|
| 69 |
|
| 70 | @Test
|
| 71 | public void testEmptyArgument () throws IOException {
|
| 72 | Indexer.main(new String[] {});
|
| 73 | logger.info(outputStream.toString());
|
| 74 | assertEquals(true, outputStream.toString().startsWith(info));
|
| 75 | }
|
| 76 |
|
| 77 |
|
| 78 | @Test
|
| 79 | public void testMissingConfig () throws IOException {
|
| 80 | Indexer.main(new String[] { "-i", "src/test/resources/bzk",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 81 | "-o test-index"});
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 82 | logger.info(outputStream.toString());
|
| 83 | assertEquals(true, outputStream.toString().startsWith(info));
|
| 84 | }
|
| Akron | f0e3653 | 2019-03-06 11:43:21 +0100 | [diff] [blame] | 85 |
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 86 | @Test
|
| 87 | public void testMissingInput () throws IOException {
|
| 88 | Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
|
| Akron | 81829f1 | 2019-04-09 23:06:34 +0200 | [diff] [blame] | 89 | "-o", "test-index"});
|
| Eliza Margaretha | c0b7a46 | 2016-11-15 21:26:54 +0100 | [diff] [blame] | 90 | logger.info(outputStream.toString());
|
| 91 | assertEquals(true, outputStream.toString().startsWith(info));
|
| 92 | }
|
| 93 |
|
| 94 | @Before
|
| 95 | public void setOutputStream () {
|
| 96 | System.setOut(new PrintStream(outputStream));
|
| 97 | }
|
| 98 |
|
| 99 | @After
|
| 100 | public void cleanOutputStream () {
|
| 101 | System.setOut(null);
|
| 102 | }
|
| 103 |
|
| 104 | @Before
|
| 105 | public void cleanOutputDirectory () {
|
| 106 |
|
| 107 | if (outputDirectory.exists()) {
|
| 108 | logger.debug("Output directory exists");
|
| 109 | deleteFile(outputDirectory);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private void deleteFile (File path) {
|
| 114 | if (path.isDirectory()) {
|
| 115 | File file;
|
| 116 | for (String filename : path.list()) {
|
| 117 | file = new File(path + "/" + filename);
|
| 118 | deleteFile(file);
|
| 119 | logger.debug(file.getAbsolutePath());
|
| 120 | }
|
| 121 | }
|
| 122 | path.delete();
|
| 123 | }
|
| 124 | }
|