aboutsummaryrefslogtreecommitdiff
path: root/test/uk/ac/ox/cs/pagoda/util
diff options
context:
space:
mode:
Diffstat (limited to 'test/uk/ac/ox/cs/pagoda/util')
-rw-r--r--test/uk/ac/ox/cs/pagoda/util/TestUtil.java74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/uk/ac/ox/cs/pagoda/util/TestUtil.java b/test/uk/ac/ox/cs/pagoda/util/TestUtil.java
new file mode 100644
index 0000000..c3909d5
--- /dev/null
+++ b/test/uk/ac/ox/cs/pagoda/util/TestUtil.java
@@ -0,0 +1,74 @@
1package uk.ac.ox.cs.pagoda.util;
2
3import org.apache.log4j.Appender;
4import org.apache.log4j.FileAppender;
5import org.apache.log4j.Logger;
6
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.URL;
11import java.nio.file.Files;
12import java.nio.file.Path;
13import java.nio.file.Paths;
14import java.util.Enumeration;
15import java.util.Properties;
16
17import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
18
19/**
20 * A collection of utility methods for testing.
21 */
22public class TestUtil {
23
24 public static final String CONFIG_FILE = "test.properties";
25
26 private static boolean isConfigLoaded = false;
27 private static Properties config;
28
29 public static Properties getConfig() {
30 if(!isConfigLoaded) {
31 try(InputStream in = TestUtil.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
32 config = new java.util.Properties();
33 config.load(in);
34 in.close();
35 isConfigLoaded = true;
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40 return config;
41 }
42
43 public static String combinePaths(String path1, String path2) {
44 File file1 = new File(path1);
45 File file2 = new File(file1, path2);
46 return file2.getPath();
47 }
48
49 public static void copyFile(String src, String dst) throws IOException {
50 Files.copy(Paths.get(src), Paths.get(dst), REPLACE_EXISTING);
51 }
52
53 /**
54 * Get the log file, which is assumed unique.
55 * */
56 public static String getLogFileName() {
57 Enumeration e = Logger.getRootLogger().getAllAppenders();
58 while (e.hasMoreElements()){
59 Appender app = (Appender)e.nextElement();
60 if (app instanceof FileAppender){
61 return ((FileAppender)app).getFile();
62 }
63 }
64 return null;
65 }
66
67 public static Path getAnswersFilePath(String name) {
68 URL givenAnswersURL = TestUtil.class.getClassLoader()
69 .getResource(name);
70 if(givenAnswersURL == null) throw new RuntimeException("Missing answers file:" + name);
71 return Paths.get(givenAnswersURL.getPath());
72 }
73
74}