1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
import static org.testng.Assert.assertNotNull; |
24 |
|
|
25 |
|
import java.io.BufferedReader; |
26 |
|
import java.io.File; |
27 |
|
import java.io.IOException; |
28 |
|
import java.io.InputStreamReader; |
29 |
|
import java.util.concurrent.TimeUnit; |
30 |
|
|
31 |
|
import org.testng.Assert; |
32 |
|
import org.testng.annotations.BeforeTest; |
33 |
|
import org.testng.annotations.Test; |
34 |
|
|
35 |
|
import io.github.classgraph.ClassGraph; |
36 |
|
import io.github.classgraph.ScanResult; |
37 |
|
import jalview.bin.Console; |
38 |
|
|
|
|
| 78.1% |
Uncovered Elements: 14 (64) |
Complexity: 16 |
Complexity Density: 0.36 |
|
39 |
|
public class Log4jTest |
40 |
|
{ |
41 |
|
private static final int TIMEOUT = 10; |
42 |
|
|
|
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 7 |
Complexity Density: 0.7 |
|
43 |
|
private static class Worker extends Thread |
44 |
|
{ |
45 |
|
private final Process process; |
46 |
|
|
47 |
|
private BufferedReader outputReader; |
48 |
|
|
49 |
|
private BufferedReader errorReader; |
50 |
|
|
51 |
|
private boolean exited; |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
1 |
private Worker(Process process)... |
54 |
|
{ |
55 |
1 |
this.process = process; |
56 |
|
} |
57 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
58 |
1 |
@Override... |
59 |
|
public void run() |
60 |
|
{ |
61 |
1 |
try |
62 |
|
{ |
63 |
1 |
exited = process.waitFor(TIMEOUT, TimeUnit.SECONDS); |
64 |
|
} catch (InterruptedException ignore) |
65 |
|
{ |
66 |
1 |
return; |
67 |
|
} |
68 |
0 |
this.interrupt(); |
69 |
0 |
this.process.destroy(); |
70 |
|
} |
71 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
0 |
public BufferedReader getOutputReader()... |
73 |
|
{ |
74 |
0 |
return outputReader; |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
1 |
public void setOutputReader(BufferedReader outputReader)... |
78 |
|
{ |
79 |
1 |
this.outputReader = outputReader; |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
3 |
public BufferedReader getErrorReader()... |
83 |
|
{ |
84 |
3 |
return errorReader; |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
87 |
1 |
public void setErrorReader(BufferedReader errorReader)... |
88 |
|
{ |
89 |
1 |
this.errorReader = errorReader; |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
private static ClassGraph scanner = null; |
94 |
|
|
95 |
|
private static String classpath = null; |
96 |
|
|
97 |
|
private static String java_exe = null; |
98 |
|
|
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
99 |
1 |
public synchronized static String getClassPath()... |
100 |
|
{ |
101 |
1 |
if (scanner == null) |
102 |
|
{ |
103 |
1 |
scanner = new ClassGraph(); |
104 |
1 |
ScanResult scan = scanner.scan(); |
105 |
1 |
classpath = scan.getClasspath(); |
106 |
1 |
java_exe = System.getProperty("java.home") + File.separator + "bin" |
107 |
|
+ File.separator + "java"; |
108 |
|
|
109 |
|
} |
110 |
1 |
while (classpath == null) |
111 |
|
{ |
112 |
0 |
try |
113 |
|
{ |
114 |
0 |
Thread.sleep(10); |
115 |
|
} catch (InterruptedException x) |
116 |
|
{ |
117 |
|
|
118 |
|
} |
119 |
|
} |
120 |
1 |
return classpath; |
121 |
|
} |
122 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 3 |
Complexity Density: 0.2 |
|
123 |
1 |
private Worker getJalviewDesktopRunner(String appArgs)... |
124 |
|
{ |
125 |
1 |
String classpath = getClassPath(); |
126 |
1 |
String cmd = java_exe + " " + " -classpath " + classpath + " " |
127 |
|
+ " jalview.bin.Jalview " + " " |
128 |
|
+ "--props=test/jalview/util/log4jTestProps.jvprops " + appArgs; |
129 |
1 |
Process proc = null; |
130 |
1 |
Worker worker = null; |
131 |
1 |
try |
132 |
|
{ |
133 |
1 |
proc = Runtime.getRuntime().exec(cmd); |
134 |
|
} catch (Throwable e) |
135 |
|
{ |
136 |
0 |
e.printStackTrace(); |
137 |
|
} |
138 |
1 |
if (proc != null) |
139 |
|
{ |
140 |
1 |
BufferedReader outputReader = new BufferedReader( |
141 |
|
new InputStreamReader(proc.getInputStream())); |
142 |
1 |
BufferedReader errorReader = new BufferedReader( |
143 |
|
new InputStreamReader(proc.getErrorStream())); |
144 |
1 |
worker = new Worker(proc); |
145 |
1 |
worker.start(); |
146 |
1 |
worker.setOutputReader(outputReader); |
147 |
1 |
worker.setErrorReader(errorReader); |
148 |
|
} |
149 |
1 |
return worker; |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
4 |
@BeforeTest(alwaysRun = true)... |
153 |
|
public void initialize() |
154 |
|
{ |
155 |
4 |
new Log4jTest(); |
156 |
|
} |
157 |
|
|
|
|
| 72.4% |
Uncovered Elements: 8 (29) |
Complexity: 8 |
Complexity Density: 0.42 |
1PASS
|
|
158 |
1 |
@Test(groups = { "Functional" })... |
159 |
|
public void testLog4j() |
160 |
|
{ |
161 |
1 |
String appArgs = " -open examples/uniref50.fa -nosplash -nonews -noquestionnaire -nousagestats -nowebservicediscovery"; |
162 |
|
|
163 |
1 |
Worker worker = getJalviewDesktopRunner(appArgs); |
164 |
1 |
assertNotNull(worker, "worker is null"); |
165 |
|
|
166 |
1 |
String ln = null; |
167 |
1 |
int count = 0; |
168 |
1 |
boolean logTestFound = false; |
169 |
1 |
try |
170 |
|
{ |
171 |
? |
while ((ln = worker.getErrorReader().readLine()) != null) |
172 |
|
{ |
173 |
3 |
if (++count > 500) |
174 |
|
{ |
175 |
0 |
break; |
176 |
|
} |
177 |
3 |
if (ln.contains(Console.LOGGING_TEST_MESSAGE)) |
178 |
|
{ |
179 |
1 |
logTestFound = true; |
180 |
1 |
break; |
181 |
|
} |
182 |
|
} |
183 |
|
} catch (IOException e) |
184 |
|
{ |
185 |
0 |
e.printStackTrace(); |
186 |
|
} |
187 |
1 |
if (worker != null && worker.exited == false) |
188 |
|
{ |
189 |
1 |
worker.interrupt(); |
190 |
1 |
worker.process.destroy(); |
191 |
|
} |
192 |
1 |
if (!logTestFound) |
193 |
|
{ |
194 |
0 |
Assert.fail("Did not find Log4j Test message line '" |
195 |
|
+ Console.LOGGING_TEST_MESSAGE + "'"); |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
} |