| 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 javax.management.InstanceAlreadyExistsException; |
| 25 |
|
import javax.management.MBeanRegistrationException; |
| 26 |
|
import javax.management.MalformedObjectNameException; |
| 27 |
|
import javax.management.NotCompliantMBeanException; |
| 28 |
|
import java.io.ByteArrayOutputStream; |
| 29 |
|
import java.io.IOException; |
| 30 |
|
import java.io.PrintStream; |
| 31 |
|
import java.io.PrintWriter; |
| 32 |
|
import java.lang.management.ManagementFactory; |
| 33 |
|
import java.lang.management.ThreadInfo; |
| 34 |
|
import java.lang.management.ThreadMXBean; |
| 35 |
|
import java.net.Socket; |
| 36 |
|
import java.util.Date; |
| 37 |
|
|
| 38 |
|
import static org.hamcrest.Matchers.containsString; |
| 39 |
|
import static org.hamcrest.Matchers.is; |
| 40 |
|
import static org.hamcrest.Matchers.not; |
| 41 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 5 |
Complexity Density: 0.21 |
|
| 46 |
|
public class JmxMonitorLocalMonitoringTest { |
| 47 |
|
private ByteArrayOutputStream outputStream; |
| 48 |
|
private PrintStream out; |
| 49 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 50 |
1
|
@BeforeMethod... |
| 51 |
|
public void setupMonitorConsole() { |
| 52 |
1
|
outputStream = new ByteArrayOutputStream(); |
| 53 |
1
|
out = System.out; |
| 54 |
1
|
System.setOut(new PrintStream(outputStream)); |
| 55 |
|
} |
| 56 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 3 |
Complexity Density: 0.15 |
1
PASS
|
|
| 57 |
1
|
@Test... |
| 58 |
|
public void testLocalMonitoring() throws IOException, InterruptedException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException, MalformedObjectNameException { |
| 59 |
1
|
Thread jmxMonitor = new Thread(new RunningJmxMonitor(), "JmxMonitor"); |
| 60 |
1
|
jmxMonitor.start(); |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
1
|
long currentTime = (new Date()).getTime(); |
| 66 |
1
|
final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); |
| 67 |
1
|
boolean threadFound = false; |
| 68 |
|
|
| 69 |
21
|
while (currentTime + 10 * 1000 > (new Date()).getTime()) { |
| 70 |
20
|
ThreadInfo[] threadInfos = thbean.getThreadInfo(thbean.getAllThreadIds()); |
| 71 |
20
|
for (ThreadInfo threadInfo : threadInfos) { |
| 72 |
85
|
if (threadInfo.getThreadName().equals(ThreadManager.SHUTDOWN_MONITOR_THREAD)) { |
| 73 |
18
|
threadFound = true; |
| 74 |
18
|
break; |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
20
|
Thread.sleep(500); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
1
|
String output = outputStream.toString(); |
| 81 |
1
|
assertThat(output, not(containsString("usage: jmxMonitor\n" + |
| 82 |
|
" -c <arg> Configuration Path"))); |
| 83 |
1
|
assertThat(output, containsString( |
| 84 |
|
"ConfigurationFile is src/test/resources/jmxLocalMonitoringTestConfiguration.properties")); |
| 85 |
1
|
assertThat(threadFound, is(true)); |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
1
|
Socket socket = new Socket("localhost", 18001); |
| 91 |
1
|
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); |
| 92 |
1
|
printWriter.write("stop"); |
| 93 |
1
|
printWriter.flush(); |
| 94 |
|
} |
| 95 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 96 |
|
private class RunningJmxMonitor implements Runnable { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 97 |
1
|
@Override... |
| 98 |
|
public void run() { |
| 99 |
|
|
| 100 |
1
|
JmxMonitor.main( |
| 101 |
|
new String[] { "jmxmonitor", "-c", "src/test/resources/jmxLocalMonitoringTestConfiguration.properties" }); |
| 102 |
|
} |
| 103 |
|
} |
| 104 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 105 |
1
|
@AfterMethod... |
| 106 |
|
public void tearDownMonitorConsole() { |
| 107 |
1
|
System.setOut(out); |
| 108 |
|
} |
| 109 |
|
} |