-
Notifications
You must be signed in to change notification settings - Fork 38k
/
NotificationPublisherTests.java
223 lines (180 loc) · 7.81 KB
/
NotificationPublisherTests.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jmx.export;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.ReflectionException;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jmx.AbstractMBeanServerTests;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;
import org.springframework.jmx.support.ObjectNameManager;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for the Spring JMX {@link NotificationPublisher} functionality.
*
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class NotificationPublisherTests extends AbstractMBeanServerTests {
private CountingNotificationListener listener = new CountingNotificationListener();
@Test
public void testSimpleBean() throws Exception {
// start the MBeanExporter
ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml");
this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null,
null);
MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher");
assertThat(publisher.getNotificationPublisher()).as("NotificationPublisher should not be null").isNotNull();
publisher.sendNotification();
assertThat(listener.count).as("Notification not sent").isEqualTo(1);
}
@Test
public void testSimpleBeanRegisteredManually() throws Exception {
// start the MBeanExporter
ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml");
MBeanExporter exporter = (MBeanExporter) ctx.getBean("exporter");
MyNotificationPublisher publisher = new MyNotificationPublisher();
exporter.registerManagedResource(publisher, ObjectNameManager.getInstance("spring:type=Publisher2"));
this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher2"), listener, null,
null);
assertThat(publisher.getNotificationPublisher()).as("NotificationPublisher should not be null").isNotNull();
publisher.sendNotification();
assertThat(listener.count).as("Notification not sent").isEqualTo(1);
}
@Test
public void testMBean() throws Exception {
// start the MBeanExporter
ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml");
this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=PublisherMBean"), listener,
null, null);
MyNotificationPublisherMBean publisher = (MyNotificationPublisherMBean) ctx.getBean("publisherMBean");
publisher.sendNotification();
assertThat(listener.count).as("Notification not sent").isEqualTo(1);
}
/*
@Test
public void testStandardMBean() throws Exception {
// start the MBeanExporter
ApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/notificationPublisherTests.xml");
this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=PublisherStandardMBean"), listener, null, null);
MyNotificationPublisherStandardMBean publisher = (MyNotificationPublisherStandardMBean) ctx.getBean("publisherStandardMBean");
publisher.sendNotification();
assertEquals("Notification not sent", 1, listener.count);
}
*/
@Test
public void testLazyInit() throws Exception {
// start the MBeanExporter
ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherLazyTests.xml");
assertThat(ctx.getBeanFactory().containsSingleton("publisher")).as("Should not have instantiated the bean yet").isFalse();
// need to touch the MBean proxy
server.getAttribute(ObjectNameManager.getInstance("spring:type=Publisher"), "Name");
this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null,
null);
MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher");
assertThat(publisher.getNotificationPublisher()).as("NotificationPublisher should not be null").isNotNull();
publisher.sendNotification();
assertThat(listener.count).as("Notification not sent").isEqualTo(1);
}
private static class CountingNotificationListener implements NotificationListener {
private int count;
private Notification lastNotification;
@Override
public void handleNotification(Notification notification, Object handback) {
this.lastNotification = notification;
this.count++;
}
@SuppressWarnings("unused")
public int getCount() {
return count;
}
@SuppressWarnings("unused")
public Notification getLastNotification() {
return lastNotification;
}
}
public static class MyNotificationPublisher implements NotificationPublisherAware {
private NotificationPublisher notificationPublisher;
@Override
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
this.notificationPublisher = notificationPublisher;
}
public NotificationPublisher getNotificationPublisher() {
return notificationPublisher;
}
public void sendNotification() {
this.notificationPublisher.sendNotification(new Notification("test", this, 1));
}
public String getName() {
return "Rob Harrop";
}
}
public static class MyNotificationPublisherMBean extends NotificationBroadcasterSupport implements DynamicMBean {
@Override
public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException,
ReflectionException {
return null;
}
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException, MBeanException, ReflectionException {
}
@Override
public AttributeList getAttributes(String[] attributes) {
return null;
}
@Override
public AttributeList setAttributes(AttributeList attributes) {
return null;
}
@Override
public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException,
ReflectionException {
return null;
}
@Override
public MBeanInfo getMBeanInfo() {
return new MBeanInfo(MyNotificationPublisherMBean.class.getName(), "", new MBeanAttributeInfo[0],
new MBeanConstructorInfo[0], new MBeanOperationInfo[0], new MBeanNotificationInfo[0]);
}
public void sendNotification() {
sendNotification(new Notification("test", this, 1));
}
}
public static class MyNotificationPublisherStandardMBean extends NotificationBroadcasterSupport implements MyMBean {
@Override
public void sendNotification() {
sendNotification(new Notification("test", this, 1));
}
}
public interface MyMBean {
void sendNotification();
}
}