Clover Coverage Report - jmxmonitor 1.0.2
Coverage timestamp: Wed Feb 10 2010 07:36:51 GMT
42   143   10   6
6   94   0.24   3.5
7     1.43  
2    
 
  JmxMonitorTest       Line # 40 41 0% 9 3 94.3% 0.9433962
  JmxMonitorTest.RunningJmxMonitor       Line # 132 1 0% 1 0 100% 1.0
 
  (4)
 
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 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    * Created by IntelliJ IDEA. User: ben Date: Dec 22, 2009 Time: 7:48:44 PM
39    */
 
40    public class JmxMonitorTest {
41    private ByteArrayOutputStream outputStream;
42    private PrintStream out;
43   
 
44  4 toggle @BeforeMethod
45    public void setupMonitorConsole() {
46  4 outputStream = new ByteArrayOutputStream();
47  4 out = System.out;
48  4 System.setOut(new PrintStream(outputStream));
49    }
50   
 
51  1 toggle @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   
 
60  1 toggle @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   
 
69  1 toggle @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   
 
78  1 toggle @Test
79    public void testJmxMonitorValidOption() throws InterruptedException, IOException {
80  1 Thread jmxMonitor = new Thread(new RunningJmxMonitor(), "JmxMonitor");
81  1 jmxMonitor.start();
82   
83    // Jmx Monitor should start and go into a loop doing nothing
84    // Check for startup within 10 seconds
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    // The controlling process should stop in at most 5 seconds
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    // Finally verify the socket thread shut down
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   
 
132    private class RunningJmxMonitor implements Runnable {
 
133  1 toggle @Override
134    public void run() {
135  1 JmxMonitor.main(new String[] { "jmxmonitor", "-c", "src/test/resources/noopConfiguration.properties" });
136    }
137    }
138   
 
139  4 toggle @AfterMethod
140    public void tearDownMonitorConsole() {
141  4 System.setOut(out);
142    }
143    }