Clover Coverage Report - jmxmonitor 1.0.2
Coverage timestamp: Wed Feb 10 2010 07:36:51 GMT
25   109   6   6.25
4   71   0.24   2
4     1.5  
2    
 
  JmxMonitorLocalMonitoringTest       Line # 46 24 0% 5 0 100% 1.0
  JmxMonitorLocalMonitoringTest.RunningJmxMonitor       Line # 96 1 0% 1 0 100% 1.0
 
  (1)
 
1    /*
2    * Copyright 2009 Ben Gidley
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
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    * Created by IntelliJ IDEA. User: ben Date: Dec 30, 2009 Time: 8:12:56 AM
45    */
 
46    public class JmxMonitorLocalMonitoringTest {
47    private ByteArrayOutputStream outputStream;
48    private PrintStream out;
49   
 
50  1 toggle @BeforeMethod
51    public void setupMonitorConsole() {
52  1 outputStream = new ByteArrayOutputStream();
53  1 out = System.out;
54  1 System.setOut(new PrintStream(outputStream));
55    }
56   
 
57  1 toggle @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    // Wait for it to actually start
63    // Jmx Monitor should start and go into a loop doing nothing
64    // Check for startup within 10 seconds
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    // Shutdown
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   
 
96    private class RunningJmxMonitor implements Runnable {
 
97  1 toggle @Override
98    public void run() {
99    // Now start JMX Monitor configured to run against local host
100  1 JmxMonitor.main(
101    new String[] { "jmxmonitor", "-c", "src/test/resources/jmxLocalMonitoringTestConfiguration.properties" });
102    }
103    }
104   
 
105  1 toggle @AfterMethod
106    public void tearDownMonitorConsole() {
107  1 System.setOut(out);
108    }
109    }