/* * JBoss, Home of Professional Open Source * Copyright 2006, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.soa.esb.actions.event; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.net.URI; import junit.framework.Assert; import junit.framework.JUnit4TestAdapter; import org.apache.log4j.Logger; import org.jboss.internal.soa.esb.couriers.MockCourier; import org.jboss.internal.soa.esb.couriers.MockCourierFactory; import org.jboss.internal.soa.esb.services.registry.MockRegistry; import org.jboss.soa.esb.actions.ActionProcessingException; import org.jboss.soa.esb.actions.EventManager; import org.jboss.soa.esb.addressing.EPR; import org.jboss.soa.esb.message.Message; import org.jboss.soa.esb.message.format.MessageFactory; import org.jboss.soa.esb.message.format.MessageType; import org.junit.BeforeClass; import org.junit.Test; /** * Test the Event action. */ public class EventManagerUnitTest { static Logger logger = Logger.getLogger(EventManagerUnitTest.class); static Message subscribeMessage = null; static Message unsubscribeMessage = null; static Message triggerMessage = null; static Message nullTriggerMessage = null; static Message invalidTriggerMessage = null; static Message wildcardSubscribeMessage = null; static String eventType1 = "foobar"; static String eventType2 = "barfoo"; private static EPR epr1; private static EPR epr2; private static MockCourier courier1; private static MockCourier courier2; public static junit.framework.Test suite () { return new JUnit4TestAdapter(EventManagerUnitTest.class); } @BeforeClass public static void before () throws Exception { MockCourierFactory.install(); MockRegistry.install(); epr1 = new EPR(new URI("test1")); epr2 = new EPR(new URI("test2")); courier1 = new MockCourier(true); courier2 = new MockCourier(true); MockRegistry.register("test", "hello", epr1, courier1); MockRegistry.register("test", "world", epr2, courier2); subscribeMessage = MessageFactory.getInstance().getMessage( MessageType.JAVA_SERIALIZED); subscribeMessage.getBody().add(EventManager.OPCODE, EventManager.SUBSCRIBE_MESSAGE); subscribeMessage.getBody().add(EventManager.EVENT_TYPE, eventType1); wildcardSubscribeMessage = MessageFactory.getInstance().getMessage( MessageType.JAVA_SERIALIZED); wildcardSubscribeMessage.getBody().add(EventManager.OPCODE, EventManager.SUBSCRIBE_MESSAGE); wildcardSubscribeMessage.getBody().add(EventManager.EVENT_TYPE, EventManager.WILD_CARD); unsubscribeMessage = MessageFactory.getInstance().getMessage( MessageType.JAVA_SERIALIZED); unsubscribeMessage.getBody().add(EventManager.OPCODE, EventManager.UNSUBSCRIBE_MESSAGE); triggerMessage = MessageFactory.getInstance().getMessage( MessageType.JAVA_SERIALIZED); triggerMessage.getBody().add(EventManager.OPCODE, EventManager.EVENT_MESSAGE); triggerMessage.getBody().add(EventManager.EVENT_TYPE, eventType1); nullTriggerMessage = MessageFactory.getInstance().getMessage( MessageType.JAVA_SERIALIZED); nullTriggerMessage.getBody().add(EventManager.OPCODE, EventManager.EVENT_MESSAGE); nullTriggerMessage.getBody().add(EventManager.EVENT_TYPE, eventType2); invalidTriggerMessage = MessageFactory.getInstance().getMessage( MessageType.JAVA_SERIALIZED); invalidTriggerMessage.getBody().add(EventManager.OPCODE, EventManager.EVENT_MESSAGE); invalidTriggerMessage.getBody().add(EventManager.EVENT_TYPE, ""); } @Test public void noSubscribers () { try { EventManager eventManager = new EventManager(); subscribeMessage.getHeader().getCall().setReplyTo(epr1); eventManager.process(subscribeMessage); subscribeMessage.getHeader().getCall().setReplyTo(epr2); eventManager.process(subscribeMessage); subscribeMessage.getHeader().getCall().setReplyTo(null); // to be on the safe side. eventManager.process(nullTriggerMessage); Message msg = courier1.message; assertTrue(msg == null); msg = courier2.message; assertTrue(msg == null); } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } @Test public void invalidSubscribe () { try { EventManager eventManager = new EventManager(); subscribeMessage.getHeader().getCall().setReplyTo(null); eventManager.process(subscribeMessage); Assert.fail(); } catch (ActionProcessingException ex) { } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } @Test public void invalidTrigger () { try { EventManager eventManager = new EventManager(); eventManager.process(invalidTriggerMessage); Assert.fail(); } catch (ActionProcessingException ex) { } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } @Test public void invalidUnsubscribe () { try { EventManager eventManager = new EventManager(); unsubscribeMessage.getHeader().getCall().setReplyTo(null); eventManager.process(unsubscribeMessage); Assert.fail(); } catch (ActionProcessingException ex) { } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } @Test public void triggerEvents () { try { EventManager eventManager = new EventManager(); subscribeMessage.getHeader().getCall().setReplyTo(epr1); eventManager.process(subscribeMessage); subscribeMessage.getHeader().getCall().setReplyTo(epr2); eventManager.process(subscribeMessage); courier1.message = null; courier2.message = null; eventManager.process(triggerMessage); Message msg = courier1.message; assertNotNull(msg); assertTrue(eventType1.equals((String) msg.getBody().get(EventManager.EVENT_TYPE))); msg = courier2.message; assertNotNull(msg); assertTrue(eventType1.equals((String) msg.getBody().get(EventManager.EVENT_TYPE))); } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } @Test public void triggerWildcardEvents () { try { EventManager eventManager = new EventManager(); wildcardSubscribeMessage.getHeader().getCall().setReplyTo(epr1); eventManager.process(wildcardSubscribeMessage); wildcardSubscribeMessage.getHeader().getCall().setReplyTo(null); subscribeMessage.getHeader().getCall().setReplyTo(epr2); courier1.message = null; courier2.message = null; eventManager.process(subscribeMessage); subscribeMessage.getHeader().getCall().setReplyTo(null); // to be on the safe side. eventManager.process(nullTriggerMessage); // should trigger wildcards Message msg = courier1.message; assertNotNull(msg); assertTrue(eventType2.equals((String) msg.getBody().get(EventManager.EVENT_TYPE))); msg = courier2.message; assertTrue(msg == null); } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } }