Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.util

File Log4jTest.java

 

Code metrics

16
54
10
2
199
152
23
0.43
5.4
5
2.3

Classes

Class Line # Actions
Log4jTest 39 44 16
0.00%
Log4jTest.Worker 43 10 7
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
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   
 
39    public class Log4jTest
40    {
41    private static final int TIMEOUT = 10;
42   
 
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   
 
53  0 toggle private Worker(Process process)
54    {
55  0 this.process = process;
56    }
57   
 
58  0 toggle @Override
59    public void run()
60    {
61  0 try
62    {
63  0 exited = process.waitFor(TIMEOUT, TimeUnit.SECONDS);
64    } catch (InterruptedException ignore)
65    {
66  0 return;
67    }
68  0 this.interrupt();
69  0 this.process.destroy();
70    }
71   
 
72  0 toggle public BufferedReader getOutputReader()
73    {
74  0 return outputReader;
75    }
76   
 
77  0 toggle public void setOutputReader(BufferedReader outputReader)
78    {
79  0 this.outputReader = outputReader;
80    }
81   
 
82  0 toggle public BufferedReader getErrorReader()
83    {
84  0 return errorReader;
85    }
86   
 
87  0 toggle public void setErrorReader(BufferedReader errorReader)
88    {
89  0 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   
 
99  0 toggle public synchronized static String getClassPath()
100    {
101  0 if (scanner == null)
102    {
103  0 scanner = new ClassGraph();
104  0 ScanResult scan = scanner.scan();
105  0 classpath = scan.getClasspath();
106  0 java_exe = System.getProperty("java.home") + File.separator + "bin"
107    + File.separator + "java";
108   
109    }
110  0 while (classpath == null)
111    {
112  0 try
113    {
114  0 Thread.sleep(10);
115    } catch (InterruptedException x)
116    {
117   
118    }
119    }
120  0 return classpath;
121    }
122   
 
123  0 toggle private Worker getJalviewDesktopRunner(String appArgs)
124    {
125  0 String classpath = getClassPath();
126  0 String cmd = java_exe + " " + " -classpath " + classpath + " "
127    + " jalview.bin.Jalview " + " "
128    + "--props=test/jalview/util/log4jTestProps.jvprops " + appArgs;
129  0 Process proc = null;
130  0 Worker worker = null;
131  0 try
132    {
133  0 proc = Runtime.getRuntime().exec(cmd);
134    } catch (Throwable e)
135    {
136  0 e.printStackTrace();
137    }
138  0 if (proc != null)
139    {
140  0 BufferedReader outputReader = new BufferedReader(
141    new InputStreamReader(proc.getInputStream()));
142  0 BufferedReader errorReader = new BufferedReader(
143    new InputStreamReader(proc.getErrorStream()));
144  0 worker = new Worker(proc);
145  0 worker.start();
146  0 worker.setOutputReader(outputReader);
147  0 worker.setErrorReader(errorReader);
148    }
149  0 return worker;
150    }
151   
 
152  0 toggle @BeforeTest(alwaysRun = true)
153    public void initialize()
154    {
155  0 new Log4jTest();
156    }
157   
 
158  0 toggle @Test(groups = { "Functional" })
159    public void testLog4j()
160    {
161  0 String appArgs = " -open examples/uniref50.fa -nosplash -nonews -noquestionnaire -nousagestats -nowebservicediscovery";
162   
163  0 Worker worker = getJalviewDesktopRunner(appArgs);
164  0 assertNotNull(worker, "worker is null");
165   
166  0 String ln = null;
167  0 int count = 0;
168  0 boolean logTestFound = false;
169  0 try
170    {
171  0 while ((ln = worker.getErrorReader().readLine()) != null)
172    {
173  0 if (++count > 500)
174    {
175  0 break;
176    }
177  0 if (ln.contains(Console.LOGGING_TEST_MESSAGE))
178    {
179  0 logTestFound = true;
180  0 break;
181    }
182    }
183    } catch (IOException e)
184    {
185  0 e.printStackTrace();
186    }
187  0 if (worker != null && worker.exited == false)
188    {
189  0 worker.interrupt();
190  0 worker.process.destroy();
191    }
192  0 if (!logTestFound)
193    {
194  0 Assert.fail("Did not find Log4j Test message line '"
195    + Console.LOGGING_TEST_MESSAGE + "'");
196    }
197    }
198   
199    }