1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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 |
|
|
41 |
|
|
|
|
| 83.3% |
Uncovered Elements: 6 (36) |
Complexity: 7 |
Complexity Density: 0.21 |
|
42 |
|
public class InternalJmxTest { |
43 |
|
|
44 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
1
PASS
|
|
45 |
1
|
@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 |
|
|
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 |
|
|
69 |
1
|
internalJmx.registryDidShutdown(); |
70 |
|
|
71 |
|
|
72 |
|
} |
73 |
|
|
|
|
| 79.2% |
Uncovered Elements: 5 (24) |
Complexity: 5 |
Complexity Density: 0.21 |
1
PASS
|
|
74 |
1
|
@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 |
|
|
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 |
|
|
108 |
1
|
internalJmx.registryDidShutdown(); |
109 |
|
|
110 |
|
|
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 |
|
} |