| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package uk.co.gidley.jmxmonitor; |
| 18 |
|
|
| 19 |
|
import org.testng.annotations.AfterMethod; |
| 20 |
|
import org.testng.annotations.BeforeMethod; |
| 21 |
|
import org.testng.annotations.Test; |
| 22 |
|
import uk.co.gidley.jmxmonitor.services.ThreadManager; |
| 23 |
|
|
| 24 |
|
import java.io.ByteArrayOutputStream; |
| 25 |
|
import java.io.IOException; |
| 26 |
|
import java.io.PrintStream; |
| 27 |
|
import java.io.PrintWriter; |
| 28 |
|
import java.lang.management.ManagementFactory; |
| 29 |
|
import java.lang.management.ThreadInfo; |
| 30 |
|
import java.lang.management.ThreadMXBean; |
| 31 |
|
import java.net.Socket; |
| 32 |
|
import java.util.Date; |
| 33 |
|
|
| 34 |
|
import static org.hamcrest.Matchers.*; |
| 35 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
|
|
|
| 94.3% |
Uncovered Elements: 3 (53) |
Complexity: 9 |
Complexity Density: 0.22 |
|
| 40 |
|
public class JmxMonitorTest { |
| 41 |
|
private ByteArrayOutputStream outputStream; |
| 42 |
|
private PrintStream out; |
| 43 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 44 |
4
|
@BeforeMethod... |
| 45 |
|
public void setupMonitorConsole() { |
| 46 |
4
|
outputStream = new ByteArrayOutputStream(); |
| 47 |
4
|
out = System.out; |
| 48 |
4
|
System.setOut(new PrintStream(outputStream)); |
| 49 |
|
} |
| 50 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 51 |
1
|
@Test... |
| 52 |
|
public void testJmxMonitorMissingArg() { |
| 53 |
|
|
| 54 |
1
|
JmxMonitor.main(new String[] { "jmxmonitor", "-c" }); |
| 55 |
1
|
assertThat(outputStream.toString(), containsString("usage: jmxMonitor\n" + |
| 56 |
|
" -c <arg> Configuration Path")); |
| 57 |
|
|
| 58 |
|
} |
| 59 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 60 |
1
|
@Test... |
| 61 |
|
public void testJmxMonitorMissingOption() { |
| 62 |
|
|
| 63 |
1
|
JmxMonitor.main(new String[] { "jmxmonitor" }); |
| 64 |
1
|
assertThat(outputStream.toString(), containsString("usage: jmxMonitor\n" + |
| 65 |
|
" -c <arg> Configuration Path")); |
| 66 |
|
|
| 67 |
|
} |
| 68 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 69 |
1
|
@Test... |
| 70 |
|
public void testJmxMonitorSpareOption() { |
| 71 |
|
|
| 72 |
1
|
JmxMonitor.main(new String[] { "jmxmonitor", "-c", "out.txt", "-d" }); |
| 73 |
1
|
assertThat(outputStream.toString(), containsString("usage: jmxMonitor\n" + |
| 74 |
|
" -c <arg> Configuration Path")); |
| 75 |
|
|
| 76 |
|
} |
| 77 |
|
|
|
|
|
| 91.9% |
Uncovered Elements: 3 (37) |
Complexity: 4 |
Complexity Density: 0.13 |
1
PASS
|
|
| 78 |
1
|
@Test... |
| 79 |
|
public void testJmxMonitorValidOption() throws InterruptedException, IOException { |
| 80 |
1
|
Thread jmxMonitor = new Thread(new RunningJmxMonitor(), "JmxMonitor"); |
| 81 |
1
|
jmxMonitor.start(); |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
1
|
long currentTime = (new Date()).getTime(); |
| 86 |
1
|
final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); |
| 87 |
1
|
boolean threadFound = false; |
| 88 |
|
|
| 89 |
21
|
while (currentTime + 10 * 1000 > (new Date()).getTime()) { |
| 90 |
20
|
ThreadInfo[] threadInfos = thbean.getThreadInfo(thbean.getAllThreadIds()); |
| 91 |
20
|
for (ThreadInfo threadInfo : threadInfos) { |
| 92 |
38
|
if (threadInfo.getThreadName().equals(ThreadManager.SHUTDOWN_MONITOR_THREAD)) { |
| 93 |
19
|
threadFound = true; |
| 94 |
19
|
break; |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
20
|
Thread.sleep(500); |
| 98 |
|
} |
| 99 |
1
|
assertThat(threadFound, is(true)); |
| 100 |
1
|
String output = outputStream.toString(); |
| 101 |
1
|
assertThat(output, not(containsString("usage: jmxMonitor\n" + |
| 102 |
|
" -c <arg> Configuration Path"))); |
| 103 |
1
|
assertThat(output, containsString("ConfigurationFile is src/test/resources/noopConfiguration.properties")); |
| 104 |
|
|
| 105 |
|
|
| 106 |
1
|
Socket socket = new Socket("localhost", 18001); |
| 107 |
1
|
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); |
| 108 |
1
|
printWriter.write("stop"); |
| 109 |
1
|
printWriter.flush(); |
| 110 |
|
|
| 111 |
|
|
| 112 |
1
|
currentTime = (new Date()).getTime(); |
| 113 |
1
|
boolean stopped = false; |
| 114 |
|
|
| 115 |
1
|
jmxMonitor.join(10000); |
| 116 |
1
|
assertThat(jmxMonitor.isAlive(), is(false)); |
| 117 |
|
|
| 118 |
|
|
| 119 |
1
|
threadFound = false; |
| 120 |
1
|
ThreadInfo[] threadInfos = thbean.getThreadInfo(thbean.getAllThreadIds()); |
| 121 |
1
|
for (ThreadInfo threadInfo : threadInfos) { |
| 122 |
12
|
if (threadInfo.getThreadName().equals(ThreadManager.SHUTDOWN_MONITOR_THREAD)) { |
| 123 |
0
|
threadFound = true; |
| 124 |
0
|
break; |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
1
|
assertThat(threadFound, is(not(true))); |
| 128 |
|
|
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 132 |
|
private class RunningJmxMonitor implements Runnable { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 133 |
1
|
@Override... |
| 134 |
|
public void run() { |
| 135 |
1
|
JmxMonitor.main(new String[] { "jmxmonitor", "-c", "src/test/resources/noopConfiguration.properties" }); |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 139 |
4
|
@AfterMethod... |
| 140 |
|
public void tearDownMonitorConsole() { |
| 141 |
4
|
System.setOut(out); |
| 142 |
|
} |
| 143 |
|
} |