From 618ce2ed349b4964ac1d7c1689efbfb4b4d651eb Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 11 Mar 2010 15:27:12 +0000 Subject: [PATCH] INT-1013, INT-1014 Added tests for notification-publishing-channel-adapter and notification-listening-channel-adapter parsers. --- ...eningChannelAdapterParserTests-context.xml | 29 +++++++++ ...ionListeningChannelAdapterParserTests.java | 59 ++++++++++++++++++ ...shingChannelAdapterParserTests-context.xml | 27 ++++++++ ...onPublishingChannelAdapterParserTests.java | 62 +++++++++++++++++++ .../integration/jmx/config/TestListener.java | 50 +++++++++++++++ .../integration/jmx/config/TestPublisher.java | 44 +++++++++++++ 6 files changed, 271 insertions(+) create mode 100644 org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml create mode 100644 org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests.java create mode 100644 org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml create mode 100644 org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests.java create mode 100644 org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestListener.java create mode 100644 org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestPublisher.java diff --git a/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml new file mode 100644 index 0000000000..27a9a1d92c --- /dev/null +++ b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + diff --git a/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests.java b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests.java new file mode 100644 index 0000000000..9710dde34c --- /dev/null +++ b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2010 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 + * + * http://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.integration.jmx.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import javax.management.Notification; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.channel.PollableChannel; +import org.springframework.integration.core.Message; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class NotificationListeningChannelAdapterParserTests { + + @Autowired + private PollableChannel channel; + + @Autowired + private TestPublisher testPublisher; + + + @Test + public void receiveNotification() throws Exception { + assertNull(channel.receive(0)); + testPublisher.send("ABC"); + Message message = channel.receive(1000); + assertNotNull(message); + assertEquals(Notification.class, message.getPayload().getClass()); + assertEquals("ABC", ((Notification) message.getPayload()).getMessage()); + } + +} diff --git a/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml new file mode 100644 index 0000000000..7d42ff811b --- /dev/null +++ b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + diff --git a/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests.java b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests.java new file mode 100644 index 0000000000..631b4dcb11 --- /dev/null +++ b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2002-2010 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 + * + * http://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.integration.jmx.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import javax.management.Notification; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.core.Message; +import org.springframework.integration.core.MessageChannel; +import org.springframework.integration.jmx.JmxHeaders; +import org.springframework.integration.message.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class NotificationPublishingChannelAdapterParserTests { + + @Autowired + private MessageChannel channel; + + @Autowired + private TestListener listener; + + @Test + public void publishAndVerify() throws Exception { + assertNull(listener.lastNotification); + Message message = MessageBuilder.withPayload("XYZ") + .setHeader(JmxHeaders.NOTIFICATION_TYPE, "test.type").build(); + channel.send(message); + assertNotNull(listener.lastNotification); + Notification notification = listener.lastNotification; + assertEquals("XYZ", (notification).getMessage()); + assertEquals("test.type", notification.getType()); + } + +} diff --git a/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestListener.java b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestListener.java new file mode 100644 index 0000000000..d02745a6cd --- /dev/null +++ b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestListener.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2010 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 + * + * http://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.integration.jmx.config; + +import javax.management.MBeanServer; +import javax.management.Notification; +import javax.management.NotificationListener; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.jmx.support.ObjectNameManager; + +/** + * @author Mark Fisher + * @since 2.0 + */ +public class TestListener implements NotificationListener, BeanFactoryAware { + + Notification lastNotification; + + public void setBeanFactory(BeanFactory beanFactory) { + MBeanServer server = beanFactory.getBean("mbeanServer", MBeanServer.class); + try { + server.addNotificationListener( + ObjectNameManager.getInstance("test.publisher:name=publisher"), this, null, null); + } + catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + + public void handleNotification(Notification notification, Object handback) { + this.lastNotification = notification; + } + +} diff --git a/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestPublisher.java b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestPublisher.java new file mode 100644 index 0000000000..0f95cc179c --- /dev/null +++ b/org.springframework.integration.jmx/src/test/java/org/springframework/integration/jmx/config/TestPublisher.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2010 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 + * + * http://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.integration.jmx.config; + +import javax.management.Notification; + +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.export.notification.NotificationPublisher; +import org.springframework.jmx.export.notification.NotificationPublisherAware; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@ManagedResource +public class TestPublisher implements NotificationPublisherAware { + + private volatile NotificationPublisher notificationPublisher; + + public void setNotificationPublisher(NotificationPublisher notificationPublisher) { + this.notificationPublisher = notificationPublisher; + } + + public void send(String s) { + Notification notification = new Notification("test.type", + "org.springframework.integration.jmx.config:type=TestPublisher,name=testPublisher", 1, s); + this.notificationPublisher.sendNotification(notification); + } + +}