17
17
package org .springframework .jmx .access ;
18
18
19
19
import java .beans .PropertyDescriptor ;
20
+ import java .lang .management .ManagementFactory ;
21
+ import java .lang .management .MemoryMXBean ;
22
+ import java .lang .management .ThreadMXBean ;
20
23
import java .lang .reflect .Method ;
21
24
import java .net .BindException ;
22
25
import java .util .HashMap ;
30
33
31
34
import org .junit .jupiter .api .Test ;
32
35
36
+ import org .springframework .aop .framework .ProxyFactory ;
33
37
import org .springframework .jmx .AbstractMBeanServerTests ;
34
38
import org .springframework .jmx .IJmxTestBean ;
35
39
import org .springframework .jmx .JmxException ;
50
54
* @author Sam Brannen
51
55
* @author Chris Beams
52
56
*/
53
- public class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
57
+ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
54
58
55
59
protected static final String OBJECT_NAME = "spring:test=proxy" ;
56
60
@@ -87,14 +91,14 @@ protected IJmxTestBean getProxy() throws Exception {
87
91
}
88
92
89
93
@ Test
90
- public void testProxyClassIsDifferent () throws Exception {
94
+ void testProxyClassIsDifferent () throws Exception {
91
95
assumeTrue (runTests );
92
96
IJmxTestBean proxy = getProxy ();
93
- assertThat (( proxy .getClass () != IJmxTestBean . class )) .as ("The proxy class should be different than the base class" ).isTrue ( );
97
+ assertThat (proxy .getClass ()) .as ("The proxy class should be different than the base class" ).isNotSameAs ( IJmxTestBean . class );
94
98
}
95
99
96
100
@ Test
97
- public void testDifferentProxiesSameClass () throws Exception {
101
+ void testDifferentProxiesSameClass () throws Exception {
98
102
assumeTrue (runTests );
99
103
IJmxTestBean proxy1 = getProxy ();
100
104
IJmxTestBean proxy2 = getProxy ();
@@ -104,79 +108,79 @@ public void testDifferentProxiesSameClass() throws Exception {
104
108
}
105
109
106
110
@ Test
107
- public void testGetAttributeValue () throws Exception {
111
+ void testGetAttributeValue () throws Exception {
108
112
assumeTrue (runTests );
109
113
IJmxTestBean proxy1 = getProxy ();
110
114
int age = proxy1 .getAge ();
111
115
assertThat (age ).as ("The age should be 100" ).isEqualTo (100 );
112
116
}
113
117
114
118
@ Test
115
- public void testSetAttributeValue () throws Exception {
119
+ void testSetAttributeValue () throws Exception {
116
120
assumeTrue (runTests );
117
121
IJmxTestBean proxy = getProxy ();
118
122
proxy .setName ("Rob Harrop" );
119
123
assertThat (target .getName ()).as ("The name of the bean should have been updated" ).isEqualTo ("Rob Harrop" );
120
124
}
121
125
122
126
@ Test
123
- public void testSetAttributeValueWithRuntimeException () throws Exception {
127
+ void testSetAttributeValueWithRuntimeException () throws Exception {
124
128
assumeTrue (runTests );
125
129
IJmxTestBean proxy = getProxy ();
126
130
assertThatIllegalArgumentException ().isThrownBy (() ->
127
131
proxy .setName ("Juergen" ));
128
132
}
129
133
130
134
@ Test
131
- public void testSetAttributeValueWithCheckedException () throws Exception {
135
+ void testSetAttributeValueWithCheckedException () throws Exception {
132
136
assumeTrue (runTests );
133
137
IJmxTestBean proxy = getProxy ();
134
138
assertThatExceptionOfType (ClassNotFoundException .class ).isThrownBy (() ->
135
139
proxy .setName ("Juergen Class" ));
136
140
}
137
141
138
142
@ Test
139
- public void testSetAttributeValueWithIOException () throws Exception {
143
+ void testSetAttributeValueWithIOException () throws Exception {
140
144
assumeTrue (runTests );
141
145
IJmxTestBean proxy = getProxy ();
142
146
assertThatIOException ().isThrownBy (() ->
143
147
proxy .setName ("Juergen IO" ));
144
148
}
145
149
146
150
@ Test
147
- public void testSetReadOnlyAttribute () throws Exception {
151
+ void testSetReadOnlyAttribute () throws Exception {
148
152
assumeTrue (runTests );
149
153
IJmxTestBean proxy = getProxy ();
150
154
assertThatExceptionOfType (InvalidInvocationException .class ).isThrownBy (() ->
151
155
proxy .setAge (900 ));
152
156
}
153
157
154
158
@ Test
155
- public void testInvokeNoArgs () throws Exception {
159
+ void testInvokeNoArgs () throws Exception {
156
160
assumeTrue (runTests );
157
161
IJmxTestBean proxy = getProxy ();
158
162
long result = proxy .myOperation ();
159
163
assertThat (result ).as ("The operation should return 1" ).isEqualTo (1 );
160
164
}
161
165
162
166
@ Test
163
- public void testInvokeArgs () throws Exception {
167
+ void testInvokeArgs () throws Exception {
164
168
assumeTrue (runTests );
165
169
IJmxTestBean proxy = getProxy ();
166
170
int result = proxy .add (1 , 2 );
167
171
assertThat (result ).as ("The operation should return 3" ).isEqualTo (3 );
168
172
}
169
173
170
174
@ Test
171
- public void testInvokeUnexposedMethodWithException () throws Exception {
175
+ void testInvokeUnexposedMethodWithException () throws Exception {
172
176
assumeTrue (runTests );
173
177
IJmxTestBean bean = getProxy ();
174
178
assertThatExceptionOfType (InvalidInvocationException .class ).isThrownBy (() ->
175
179
bean .dontExposeMe ());
176
180
}
177
181
178
182
@ Test
179
- public void testTestLazyConnectionToRemote () throws Exception {
183
+ void testTestLazyConnectionToRemote () throws Exception {
180
184
assumeTrue (runTests );
181
185
182
186
final int port = SocketUtils .findAvailableTcpPort ();
@@ -219,46 +223,25 @@ public void testTestLazyConnectionToRemote() throws Exception {
219
223
catch (JmxException ex ) {
220
224
// expected
221
225
}
222
-
223
- connector = JMXConnectorServerFactory .newJMXConnectorServer (url , null , getServer ());
224
- connector .start ();
225
-
226
- // should now be able to access data via the lazy proxy
227
- try {
228
- assertThat (bean .getName ()).isEqualTo ("Rob Harrop" );
229
- assertThat (bean .getAge ()).isEqualTo (100 );
230
- }
231
- finally {
232
- connector .stop ();
233
- }
234
226
}
235
227
236
- /*
237
228
public void testMXBeanAttributeAccess () throws Exception {
238
229
MBeanClientInterceptor interceptor = new MBeanClientInterceptor ();
239
230
interceptor .setServer (ManagementFactory .getPlatformMBeanServer ());
240
231
interceptor .setObjectName ("java.lang:type=Memory" );
241
232
interceptor .setManagementInterface (MemoryMXBean .class );
242
233
MemoryMXBean proxy = ProxyFactory .getProxy (MemoryMXBean .class , interceptor );
243
- assertTrue (proxy.getHeapMemoryUsage().getMax() > 0);
234
+ assertThat (proxy .getHeapMemoryUsage ().getMax ()). isGreaterThan ( 0 );
244
235
}
245
236
246
237
public void testMXBeanOperationAccess () throws Exception {
247
238
MBeanClientInterceptor interceptor = new MBeanClientInterceptor ();
248
239
interceptor .setServer (ManagementFactory .getPlatformMBeanServer ());
249
240
interceptor .setObjectName ("java.lang:type=Threading" );
250
241
ThreadMXBean proxy = ProxyFactory .getProxy (ThreadMXBean .class , interceptor );
251
- assertTrue (proxy.getThreadInfo(Thread.currentThread().getId()).getStackTrace() != null );
242
+ assertThat (proxy .getThreadInfo (Thread .currentThread ().getId ()).getStackTrace ()). isNotNull ( );
252
243
}
253
244
254
- public void testMXBeanAttributeListAccess() throws Exception {
255
- MBeanClientInterceptor interceptor = new MBeanClientInterceptor();
256
- interceptor.setServer(ManagementFactory.getPlatformMBeanServer());
257
- interceptor.setObjectName("com.sun.management:type=HotSpotDiagnostic");
258
- HotSpotDiagnosticMXBean proxy = ProxyFactory.getProxy(HotSpotDiagnosticMXBean.class, interceptor);
259
- assertFalse(proxy.getDiagnosticOptions().isEmpty());
260
- }
261
- */
262
245
263
246
private static class ProxyTestAssembler extends AbstractReflectiveMBeanInfoAssembler {
264
247
0 commit comments