1 package net.avcompris.commons3.it.utils;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static org.apache.commons.lang3.StringUtils.isBlank;
5 import static org.apache.commons.lang3.StringUtils.join;
6
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.util.Properties;
12
13 public abstract class IntegrationTestUtils {
14
15 private static final String[] RESOURCE_PATHS = { "env.properties",
16 "test.properties",
17 };
18
19
20
21
22 public static String getTestProperty(final String propertyName) throws IOException {
23
24 checkNotNull(propertyName, "propertyName");
25
26 final Properties properties = new Properties();
27
28 try (InputStream is = getTestResourceAsStream()) {
29
30 properties.load(is);
31 }
32
33 final String value = properties.getProperty(propertyName);
34
35 if (isBlank(value)) {
36
37 throw new IOException("value shoud not be blank for property: " + propertyName + ": " + value);
38
39 } else if (value.startsWith("$") || value.contains("{")) {
40
41 throw new IllegalStateException(
42 "Value has not been processed for property: " + propertyName + ": " + value);
43 }
44
45 return value;
46 }
47
48
49
50
51 public static int getIntTestProperty(final String propertyName) throws IOException {
52
53 final String valueAsString = getTestProperty(propertyName);
54
55 try {
56
57 return Integer.parseInt(valueAsString);
58
59 } catch (final NumberFormatException e) {
60
61 throw new IOException("Cannot parse value to int for property: " + propertyName + ": " + valueAsString);
62 }
63 }
64
65 private static InputStream getTestResourceAsStream() throws IOException {
66
67 for (final String resourcePath : RESOURCE_PATHS) {
68
69 final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
70
71 if (is != null) {
72
73 System.out.println("Using: " + resourcePath);
74
75 return is;
76 }
77 }
78
79 throw new FileNotFoundException("Cannot find resource in: " + join(RESOURCE_PATHS, ", "));
80 }
81
82 public static File getFilteredFile(final String fileName) throws IOException {
83
84 checkNotNull(fileName, "fileName");
85
86 final File[] files = new File[] {
87 new File("target/classes", fileName),
88 new File("target/test-classes", fileName),
89 };
90
91 for (final File file : files) {
92
93 if (file.isFile()) {
94
95 System.out.println("Using: " + file.getCanonicalPath());
96
97 return file;
98 }
99 }
100
101 final String[] filePaths = new String[files.length];
102
103 for (int i = 0; i < files.length; ++i) {
104
105 filePaths[i] = files[i].getCanonicalPath();
106 }
107
108 throw new FileNotFoundException(join(filePaths, ", or: "));
109 }
110 }