Clover Coverage Report - jmxmonitor 1.0.2
Coverage timestamp: Wed Feb 10 2010 07:36:51 GMT
34   129   7   17
0   70   0.21   2
2     3.5  
1    
 
  InternalJmxTest       Line # 42 34 0% 7 6 83.3% 0.8333333
 
  (2)
 
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.services;
18   
19    import org.apache.commons.configuration.Configuration;
20    import org.apache.commons.configuration.ConfigurationException;
21    import org.apache.commons.configuration.PropertiesConfiguration;
22    import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
23    import org.testng.annotations.Test;
24   
25    import javax.management.InstanceNotFoundException;
26    import javax.management.MBeanServerConnection;
27    import javax.management.MalformedObjectNameException;
28    import javax.management.remote.JMXConnector;
29    import javax.management.remote.JMXConnectorFactory;
30    import javax.management.remote.JMXServiceURL;
31    import java.io.IOException;
32    import java.lang.management.ManagementFactory;
33   
34    import static org.hamcrest.Matchers.*;
35    import static org.mockito.Mockito.*;
36    import static org.hamcrest.MatcherAssert.assertThat;
37   
38   
39    /**
40    * Created by IntelliJ IDEA. User: ben Date: Jan 7, 2010 Time: 3:30:57 PM
41    */
 
42    public class InternalJmxTest {
43   
44   
 
45  1 toggle @Test
46    public void testStartupShutdownNoop() throws MalformedObjectNameException, InitialisationException, ConfigurationException {
47   
48  1 MainConfiguration mainConfiguration = mock(MainConfiguration.class);
49  1 RegistryShutdownHub registryShutdownHub = mock(RegistryShutdownHub.class);
50   
51  1 Configuration configuration = new PropertiesConfiguration("src/test/resources/noopConfiguration.properties");
52  1 when(mainConfiguration.getConfiguration()).thenReturn(configuration);
53   
54   
55  1 InternalJmx internalJmx = new InternalJmx(mainConfiguration, registryShutdownHub);
56   
57  1 verify(registryShutdownHub).addRegistryShutdownListener(internalJmx);
58   
59    // Internal JMX should now be not running
60   
61  1 try {
62  1 ManagementFactory.getPlatformMBeanServer().getObjectInstance(internalJmx.getConnectorServerName());
63  0 assertThat("Exception Shourt have thrown", false);
64    } catch (InstanceNotFoundException e) {
65   
66    }
67   
68    // Fire shutdown anyhow
69  1 internalJmx.registryDidShutdown();
70   
71   
72    }
73   
 
74  1 toggle @Test
75    public void testStartupShutdown() throws MalformedObjectNameException, InitialisationException, ConfigurationException {
76   
77  1 MainConfiguration mainConfiguration = mock(MainConfiguration.class);
78  1 RegistryShutdownHub registryShutdownHub = mock(RegistryShutdownHub.class);
79   
80  1 Configuration configuration = new PropertiesConfiguration(
81    "src/test/resources/jmxLocalMonitoringTestConfiguration.properties");
82  1 when(mainConfiguration.getConfiguration()).thenReturn(configuration);
83   
84   
85  1 InternalJmx internalJmx = new InternalJmx(mainConfiguration, registryShutdownHub);
86   
87  1 verify(registryShutdownHub).addRegistryShutdownListener(internalJmx);
88   
89    // Internal JMX should now be not running
90   
91  1 try {
92  1 ManagementFactory.getPlatformMBeanServer().getObjectInstance(internalJmx.getConnectorServerName());
93    } catch (InstanceNotFoundException e) {
94  0 assertThat("Exception should not have thrown", false);
95    }
96   
97  1 try {
98  1 JMXServiceURL serviceUrl = new JMXServiceURL(configuration.getString("jmxmonitor.localJmx"));
99  1 JMXConnector jmxc = JMXConnectorFactory.connect(serviceUrl, null);
100  1 MBeanServerConnection mBeanServerConnection = jmxc.getMBeanServerConnection();
101  1 assertThat(mBeanServerConnection, notNullValue());
102    } catch (IOException e) {
103  0 assertThat("Exception should not have thrown", false);
104    }
105   
106   
107    // Fire shutdown anyhow
108  1 internalJmx.registryDidShutdown();
109   
110    // Check JMX actually closed
111  1 try {
112  1 ManagementFactory.getPlatformMBeanServer().getObjectInstance(internalJmx.getConnectorServerName());
113  0 assertThat("Exception should have thrown", false);
114    } catch (InstanceNotFoundException e) {
115   
116    }
117   
118  1 try {
119  1 JMXServiceURL serviceUrl = new JMXServiceURL(configuration.getString("jmxmonitor.localJmx"));
120  1 JMXConnector jmxc = JMXConnectorFactory.connect(serviceUrl, null);
121  0 MBeanServerConnection mBeanServerConnection = jmxc.getMBeanServerConnection();
122  0 assertThat("Exception should have thrown", false);
123    } catch (IOException e) {
124   
125    }
126   
127   
128    }
129    }