Skip to content

Commit 9bb151f

Browse files
committed
test: 新增 MBean 注册注销异常处理及 TestMBean 测试用例
1 parent 4b81079 commit 9bb151f

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 Tencent.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.core.management.support;
13+
14+
import java.lang.management.ManagementFactory;
15+
import javax.management.MBeanServer;
16+
import javax.management.ObjectName;
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
20+
/**
21+
* Test class for MBeanRegistryHelper to cover exception handling branches
22+
*/
23+
public class MBeanRegistryHelperTest {
24+
25+
/**
26+
* Test registerMBean method with normal operation - should not throw exception
27+
*/
28+
@Test
29+
public void testRegisterMBeanNormal() throws Exception {
30+
// Create test objects
31+
Object testObject = new TestMBeanImpl();
32+
ObjectName objectName = new ObjectName("test:type=TestMBean");
33+
34+
// This should not throw exception
35+
MBeanRegistryHelper.registerMBean(testObject, objectName);
36+
37+
// Clean up - unregister the MBean
38+
MBeanRegistryHelper.unregisterMBean(objectName);
39+
}
40+
41+
/**
42+
* Test registerMBean method with invalid ObjectName - should trigger exception handling
43+
*/
44+
@Test
45+
public void testRegisterMBeanWithInvalidObjectName() throws Exception {
46+
// Create test objects
47+
Object testObject = new Object(); // Not a valid MBean
48+
ObjectName objectName = new ObjectName("test:type=TestMBean");
49+
50+
// This should trigger exception handling (NotCompliantMBeanException) but not throw
51+
MBeanRegistryHelper.registerMBean(testObject, objectName);
52+
}
53+
54+
/**
55+
* Test registerMBean method with duplicate registration - should trigger exception handling
56+
*/
57+
@Test
58+
public void testRegisterMBeanDuplicate() throws Exception {
59+
// Create test objects
60+
Object testObject = new TestMBeanImpl();
61+
ObjectName objectName = new ObjectName("test:type=DuplicateTestMBean");
62+
63+
try {
64+
// Register first time - should succeed
65+
MBeanRegistryHelper.registerMBean(testObject, objectName);
66+
67+
// Register second time - should trigger exception handling (InstanceAlreadyExistsException) but not throw
68+
MBeanRegistryHelper.registerMBean(testObject, objectName);
69+
} finally {
70+
// Clean up
71+
MBeanRegistryHelper.unregisterMBean(objectName);
72+
}
73+
}
74+
75+
/**
76+
* Test unregisterMBean method with valid ObjectName - should work normally
77+
*/
78+
@Test
79+
public void testUnregisterMBeanNormal() throws Exception {
80+
// Create ObjectName for a simple test
81+
ObjectName objectName = new ObjectName("test:type=UnregisterTestMBean");
82+
83+
// This should not throw exception even if MBean doesn't exist
84+
MBeanRegistryHelper.unregisterMBean(objectName);
85+
}
86+
87+
/**
88+
* Test unregisterMBean method with non-existent ObjectName - should not throw exception
89+
*/
90+
@Test
91+
public void testUnregisterMBeanNonExistent() throws Exception {
92+
// Create ObjectName for non-existent MBean
93+
ObjectName objectName = new ObjectName("test:type=NonExistentMBean");
94+
95+
// This should not throw exception even though MBean doesn't exist
96+
MBeanRegistryHelper.unregisterMBean(objectName);
97+
}
98+
99+
/**
100+
* Test unregisterMBean method with invalid ObjectName pattern - should trigger exception handling
101+
*/
102+
@Test
103+
public void testUnregisterMBeanWithInvalidPattern() throws Exception {
104+
// Create ObjectName with pattern (which cannot be used for unregistering)
105+
ObjectName objectName = new ObjectName("test:type=*");
106+
107+
// This should trigger exception handling but not throw
108+
MBeanRegistryHelper.unregisterMBean(objectName);
109+
}
110+
111+
/**
112+
* Test MBean interface for testing purposes
113+
*/
114+
public interface TestMBean {
115+
String getName();
116+
}
117+
118+
/**
119+
* Test MBean implementation for testing purposes
120+
*/
121+
public static class TestMBeanImpl implements TestMBean {
122+
@Override
123+
public String getName() {
124+
return "TestMBean";
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)