Refactored test suite to have a single query read function in TestSimple
Change-Id: Ib2bf531927666e221629b35fb55b2a6ade99a353
diff --git a/src/test/java/de/ids_mannheim/korap/TestSimple.java b/src/test/java/de/ids_mannheim/korap/TestSimple.java
index 041c381..91b54d5 100644
--- a/src/test/java/de/ids_mannheim/korap/TestSimple.java
+++ b/src/test/java/de/ids_mannheim/korap/TestSimple.java
@@ -117,7 +117,12 @@
StringBuilder contentBuilder = new StringBuilder();
try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(
+ new FileInputStream(URLDecoder.decode(path, "UTF-8")),
+ "UTF-8"
+ )
+ );
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
diff --git a/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java b/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java
index c4fabc3..04768b0 100644
--- a/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java
+++ b/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java
@@ -4,7 +4,7 @@
import static org.junit.Assert.fail;
import java.io.*;
-import java.net.URLDecoder;
+import static de.ids_mannheim.korap.TestSimple.*;
import org.apache.lucene.search.spans.SpanQuery;
import org.junit.Test;
@@ -28,10 +28,8 @@
@Test
public void queryJSONpoly1 () throws QueryException, IOException {
- String jsonPath = URLDecoder.decode(
- getClass().getResource("/queries/poly1.json").getFile(),
- "UTF-8");
- String jsonQuery = readFile(jsonPath);
+ String jsonPath = getClass().getResource("/queries/poly1.json").getFile();
+ String jsonQuery = getJsonString(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonQuery);
SpanNextQuery sq = (SpanNextQuery) sqwi.toQuery();
@@ -64,10 +62,8 @@
@Test
public void queryJSONpoly4 () throws QueryException, IOException {
- String jsonPath = URLDecoder.decode(
- getClass().getResource("/queries/poly4.json").getFile(),
- "UTF-8");
- String jsonQuery = readFile(jsonPath);
+ String jsonPath = getClass().getResource("/queries/poly4.json").getFile();
+ String jsonQuery = getJsonString(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonQuery);
SpanQuery sq = sqwi.toQuery();
@@ -94,21 +90,4 @@
//fail("Tests have to be updated");
}
-
-
- private String readFile (String path) {
- StringBuilder sb = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
- String str;
- while ((str = in.readLine()) != null) {
- sb.append(str);
- };
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- }
- return sb.toString();
- }
}
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
index f3c15a6..82b1dcb 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
@@ -5,7 +5,7 @@
import java.util.*;
import java.io.*;
-import java.net.URLDecoder;
+import static de.ids_mannheim.korap.TestSimple.*;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.spans.SpanQuery;
@@ -244,9 +244,7 @@
ki.addDoc(getClass().getResourceAsStream("/wiki/00001.json.gz"), true);
ki.commit();
- String jsonPath = URLDecoder.decode(
- getClass().getResource("/queries/cosmas1.json").getFile(),
- "UTF-8");
+ String jsonPath = getClass().getResource("/queries/cosmas1.json").getFile();
SpanQueryWrapper sqwi = jsonQuery(jsonPath);
kr = ki.search(sqwi.toQuery(), (short) 10);
@@ -274,9 +272,8 @@
assertEquals(kr.getTotalResults(), 1);
assertEquals("[[ecebdc]]", kr.getMatch(0).getSnippetBrackets());
- String jsonPath = URLDecoder.decode(getClass()
- .getResource("/queries/distances/in-same-t.jsonld").getFile(),
- "UTF-8");
+ String jsonPath = getClass()
+ .getResource("/queries/distances/in-same-t.jsonld").getFile();
sqwi = jsonQuery(jsonPath);
assertEquals(
@@ -288,29 +285,13 @@
assertEquals("[[ec]]ebdc", kr.getMatch(0).getSnippetBrackets());
}
-
- public static String getString (String path) {
- StringBuilder contentBuilder = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
- String str;
- while ((str = in.readLine()) != null) {
- contentBuilder.append(str);
- }
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- }
- return contentBuilder.toString();
- }
-
-
+ // TODO:
+ // probably replace with getJSONQuery()
public static SpanQueryWrapper jsonQuery (String jsonFile) {
SpanQueryWrapper sqwi;
try {
- String json = getString(jsonFile);
+ String json = getJsonString(jsonFile);
sqwi = new KrillQuery("tokens").fromKoral(json);
}
catch (QueryException e) {
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
index b94475d..81a6ef0 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
@@ -4,7 +4,7 @@
import static org.junit.Assert.fail;
import java.io.*;
-import java.net.URLDecoder;
+import static de.ids_mannheim.korap.TestSimple.*;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.RegexpQuery;
@@ -296,10 +296,8 @@
ki.addDoc(getClass().getResourceAsStream("/wiki/00001.json.gz"), true);
ki.addDoc(getClass().getResourceAsStream("/wiki/00002.json.gz"), true);
ki.commit();
- String jsonPath = URLDecoder.decode(
- getClass().getResource("/queries/poly3.json").getFile(),
- "UTF-8");
- String jsonQuery = readFile(jsonPath);
+ String jsonPath = getClass().getResource("/queries/poly3.json").getFile();
+ String jsonQuery = getJsonString(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonQuery);
SpanQuery sq = sqwi.toQuery();
@@ -396,10 +394,9 @@
ki.addDoc(createFieldDoc3());
ki.addDoc(createFieldDoc4());
ki.commit();
- String jsonPath = URLDecoder.decode(getClass()
- .getResource("/queries/bugs/expansion_bug_3.jsonld").getFile(),
- "UTF-8");
- String json = readFile(jsonPath);
+ String jsonPath = getClass()
+ .getResource("/queries/bugs/expansion_bug_3.jsonld").getFile();
+ String json = getJsonString(jsonPath);
KrillQuery kq = new KrillQuery("base");
SpanQuery sq = kq.fromKoral(json).toQuery();
assertEquals(sq.toString(),
@@ -571,22 +568,6 @@
return fd;
}
- private String readFile (String path) {
- StringBuilder sb = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
- String str;
- while ((str = in.readLine()) != null) {
- sb.append(str);
- }
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- }
- return sb.toString();
- }
-
private Krill _newKrill (SpanQueryWrapper query) {
Krill ks = new Krill(query);
ks.getMeta().getContext().left.setToken(true).setLength(1);
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
index 8fd2e6b..5e06cfa 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
@@ -5,6 +5,7 @@
import java.io.*;
import java.net.URLDecoder;
+import static de.ids_mannheim.korap.TestSimple.*;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.spans.SpanQuery;
@@ -1102,10 +1103,8 @@
// contains(<s>, (es wird | wird es))
@Test
public void queryJSONpoly2 () throws QueryException, IOException {
- String jsonPath = URLDecoder.decode(
- getClass().getResource("/queries/poly2.json").getFile(),
- "UTF-8");
- String jsonPQuery = readFile(jsonPath);
+ String jsonPath = getClass().getResource("/queries/poly2.json").getFile();
+ String jsonPQuery = getJsonString(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonPQuery);
SpanWithinQuery sq = (SpanWithinQuery) sqwi.toQuery();
@@ -1140,10 +1139,9 @@
at de.ids_mannheim.korap.Krill.apply(Krill.java:304)
*/
- String jsonPath = URLDecoder.decode(getClass()
- .getResource("/queries/bugs/span_or_bug.jsonld").getFile(),
- "UTF-8");
- String jsonPQuery = readFile(jsonPath);
+ String jsonPath = getClass()
+ .getResource("/queries/bugs/span_or_bug.jsonld").getFile();
+ String jsonPQuery = getJsonString(jsonPath);
SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonPQuery);
SpanWithinQuery sq = (SpanWithinQuery) sqwi.toQuery();
@@ -1163,22 +1161,4 @@
Result kr = ki.search(sq, (short) 1);
assertEquals(1, kr.getTotalResults());
}
-
-
-
- private String readFile (String path) {
- StringBuilder sb = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
- String str;
- while ((str = in.readLine()) != null) {
- sb.append(str);
- };
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- }
- return sb.toString();
- };
};
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index d2d1f6b..3d65c62 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -2,9 +2,7 @@
import java.util.*;
import java.io.*;
-import java.net.URLDecoder;
-import java.nio.file.Path;
-import java.nio.file.Files;
+import static de.ids_mannheim.korap.TestSimple.*;
import org.apache.lucene.search.spans.SpanQuery;
import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
@@ -418,7 +416,7 @@
public void queryJSONunderspecifiedTokenBug () {
// ((MORPH(APPR) ODER MORPH(APPRART)) /+w1 Urlaub
try {
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/bugs/underspecified_token.jsonld")
.getFile());
new KrillQuery("tokens").fromKoral(json);
@@ -489,21 +487,21 @@
public void queryJSONflags2 () throws QueryException {
// buchstabe/i
try {
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/flags/unknown1.jsonld").getFile());
KrillQuery kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(),
"tokens:s:buchstabe");
assertEquals(kq.getWarning(0).getCode(), 748);
- json = getString(getClass()
+ json = getJsonString(getClass()
.getResource("/queries/flags/unknown2.jsonld").getFile());
kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(),
"tokens:i:buchstabe");
assertEquals(kq.getWarning(0).getCode(), 748);
- json = getString(getClass()
+ json = getJsonString(getClass()
.getResource("/queries/flags/unknown3.jsonld").getFile());
kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(),
@@ -521,7 +519,7 @@
public void queryJSONelement () throws QueryException {
// <base/s=s>
try {
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/element/simple-element.jsonld")
.getFile());
KrillQuery kq = new KrillQuery("tokens");
@@ -539,7 +537,7 @@
public void queryJSONinfiniteExpansion () throws QueryException {
// der []*
try {
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/bugs/expansion_bug_3.jsonld")
.getFile());
KrillQuery kq = new KrillQuery("tokens");
@@ -557,7 +555,7 @@
public void queryJSONcomplexSpanOrTerm () throws QueryException {
// startsWith(<base/s=s>, { lassen | laufen })
try {
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/bugs/span_or_bug.jsonld").getFile());
KrillQuery kq = new KrillQuery("tokens");
@@ -574,7 +572,7 @@
public void queryJSONdistancesWithRegexes () throws QueryException {
// "der" []{2,3} [opennlp/p="NN"]
try {
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource(
"/queries/bugs/distances_with_regex_bug.jsonld")
.getFile());
@@ -592,7 +590,7 @@
@Test
public void queryJSONregexRewrite1 () throws QueryException {
// "der" [.+?]
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/sequence/regex-rewrite-1.jsonld")
.getFile());
KrillQuery kq = new KrillQuery("tokens");
@@ -605,7 +603,7 @@
@Test
public void queryJSONmerge () throws QueryException {
// treat merging gracefully
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/merge.jsonld")
.getFile());
KrillQuery kq = new KrillQuery("tokens");
@@ -615,29 +613,12 @@
};
- public static String getString (String path) {
- StringBuilder contentBuilder = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
-
- String str;
- while ((str = in.readLine()) != null) {
- contentBuilder.append(str);
- };
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- }
- return contentBuilder.toString();
- };
-
-
+ // TODO: Probably replace with getJSONQuery
public static SpanQueryWrapper jsonQuery (String jsonFile) {
SpanQueryWrapper sqwi;
try {
- String json = getString(jsonFile);
+ String json = getJsonString(jsonFile);
sqwi = new KrillQuery("tokens").fromKoral(json);
}
catch (QueryException e) {
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestResult.java b/src/test/java/de/ids_mannheim/korap/search/TestResult.java
index 875bf9a..ac4f775 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestResult.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestResult.java
@@ -2,7 +2,7 @@
import java.util.*;
import java.io.*;
-import java.net.URLDecoder;
+import static de.ids_mannheim.korap.TestSimple.*;
import org.apache.lucene.search.spans.SpanQuery;
@@ -110,7 +110,7 @@
ki.addDoc(fd);
ki.commit();
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/bugs/optionality_warning.jsonld")
.getFile());
Krill ks = new Krill(json);
@@ -148,7 +148,7 @@
// Commit!
ki.commit();
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/bsp-result-check.jsonld").getFile());
Krill ks = new Krill(json);
Result kr = ks.apply(ki);
@@ -260,22 +260,4 @@
assertEquals(1, res.at("/matches/2/tokens/1/0").asInt());
assertEquals(2, res.at("/matches/2/tokens/1/1").asInt());
};
-
-
- public static String getString (String path) {
- StringBuilder contentBuilder = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
-
- String str;
- while ((str = in.readLine()) != null) {
- contentBuilder.append(str);
- };
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- }
- return contentBuilder.toString();
- };
};
diff --git a/src/test/java/de/ids_mannheim/korap/server/TestResource.java b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
index 6c0e343..afecf1c 100644
--- a/src/test/java/de/ids_mannheim/korap/server/TestResource.java
+++ b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
@@ -9,6 +9,8 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Entity;
+import static de.ids_mannheim.korap.TestSimple.*;
+
import org.glassfish.grizzly.http.server.HttpServer;
import static org.junit.Assert.*;
@@ -17,7 +19,6 @@
import org.junit.Test;
import java.io.FileInputStream;
-import java.net.URLDecoder;
import de.ids_mannheim.korap.server.Node;
import de.ids_mannheim.korap.response.Result;
@@ -248,7 +249,7 @@
public void testCollection () throws IOException {
// mate/l:sein
- String json = getString(getClass()
+ String json = getJsonString(getClass()
.getResource("/queries/bsp-uid-example.jsonld").getFile());
try {
@@ -264,21 +265,4 @@
};
};
-
-
- public static String getString (String path) {
- StringBuilder contentBuilder = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(URLDecoder.decode(path, "UTF-8")), "UTF-8"));
- String str;
- while ((str = in.readLine()) != null) {
- contentBuilder.append(str);
- };
- in.close();
- }
- catch (IOException e) {
- fail(e.getMessage());
- };
- return contentBuilder.toString();
- };
};