blob: c83d87deaedb20b391115ab320ed7c0fa27ea061 [file] [log] [blame]
Eliza Margarethac0b7a462016-11-15 21:26:54 +01001package de.ids_mannheim.korap;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.ByteArrayOutputStream;
6import java.io.File;
7import java.io.IOException;
8import java.io.PrintStream;
9
10import org.junit.After;
11import org.junit.Before;
12import org.junit.Test;
13import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
16import de.ids_mannheim.korap.index.Indexer;
17
18/**
19 * @author margaretha
20 *
21 */
22public 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",
Akron81829f12019-04-09 23:06:34 +020031 "-i", "src/test/resources/bzk"});
32 assertEquals("Added or updated 1 file.\n", outputStream.toString());
Eliza Margarethac0b7a462016-11-15 21:26:54 +010033 }
34
35 @Test
36 public void testOutputArgument () throws IOException {
37 Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
Akron81829f12019-04-09 23:06:34 +020038 "-i", "src/test/resources/bzk", "-o", "test-output"});
39 assertEquals("Added or updated 1 file.\n", outputStream.toString());
Eliza Margarethac0b7a462016-11-15 21:26:54 +010040 }
41
42 @Test
43 public void testMultipleInputFiles () throws IOException {
44 Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
Akron81829f12019-04-09 23:06:34 +020045 "-i", "src/test/resources/wiki"});
Akron90e05f22019-12-05 16:42:43 +010046 assertEquals("Added or updated 19 files.\n", outputStream.toString());
Eliza Margarethac0b7a462016-11-15 21:26:54 +010047 }
48
Akronf0e36532019-03-06 11:43:21 +010049
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",
Akron81829f12019-04-09 23:06:34 +020055 "-a"});
Akronf0e36532019-03-06 11:43:21 +010056 logger.info(outputStream.toString());
Akron81829f12019-04-09 23:06:34 +020057 assertEquals(outputStream.toString(), "Added 1 file.\n");
Akronf0e36532019-03-06 11:43:21 +010058 }
59
60
Eliza Margarethac0b7a462016-11-15 21:26:54 +010061 @Test
62 public void testMultipleInputDirectories () throws IOException {
63 Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
Akron81829f12019-04-09 23:06:34 +020064 "-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 Margarethac0b7a462016-11-15 21:26:54 +010068 }
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",
Akron81829f12019-04-09 23:06:34 +020081 "-o test-index"});
Eliza Margarethac0b7a462016-11-15 21:26:54 +010082 logger.info(outputStream.toString());
83 assertEquals(true, outputStream.toString().startsWith(info));
84 }
Akronf0e36532019-03-06 11:43:21 +010085
Eliza Margarethac0b7a462016-11-15 21:26:54 +010086 @Test
87 public void testMissingInput () throws IOException {
88 Indexer.main(new String[] { "-c", "src/test/resources/krill.properties",
Akron81829f12019-04-09 23:06:34 +020089 "-o", "test-index"});
Eliza Margarethac0b7a462016-11-15 21:26:54 +010090 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}