-
Notifications
You must be signed in to change notification settings - Fork 47
/
TestExports.java
87 lines (71 loc) · 2.21 KB
/
TestExports.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.weakref.jmx;
import java.lang.management.ManagementFactory;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.weakref.jmx.JmxException.JmxCause;
public class TestExports
{
private MBeanServer server = null;
private MBeanExporter exporter = null;
private ObjectName objectName = null;
private String name = null;
@BeforeMethod
public void setUp()
{
Assert.assertNull(name);
objectName = Util.getUniqueObjectName();
name = objectName.getCanonicalName();
server = ManagementFactory.getPlatformMBeanServer();
exporter = new MBeanExporter(server);
Assert.assertNotNull(server);
Assert.assertNotNull(exporter);
Assert.assertNotNull(objectName);
Assert.assertNotNull(name);
}
@AfterMethod
public void tearDown()
{
Assert.assertNotNull(name);
Assert.assertNotNull(server);
Assert.assertNotNull(exporter);
Assert.assertNotNull(objectName);
exporter.unexport(name);
name = null;
server = null;
exporter = null;
objectName = null;
}
@Test
public void testExportOk() throws Exception
{
exporter.export(name, new TestBean());
Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
}
@Test(expectedExceptions = JmxException.class)
public void testExportDouble() throws Throwable
{
exporter.export(name, new TestBean());
Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
try {
exporter.export(name, new TestBean());
}
catch (Throwable t) {
Assert.assertTrue(t instanceof JmxException);
Assert.assertEquals(((JmxException) t).getJmxCause(), JmxCause.INSTANCE_ALREADY_EXISTS);
throw t;
}
}
public static class TestBean
{
@Managed
public String getHello()
{
return "Hello!";
}
}
}