--- /dev/null
+/*
+ * Copyright 2005-2011 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.wamblee.xmlrouter.listener;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
+import java.util.ArrayList;
+
+import javax.print.attribute.standard.Destination;
+import javax.xml.transform.dom.DOMSource;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Transformation;
+
+public class CompositeEventListenerTest {
+
+ private static final String DESTINATION_NAME = "dest";
+ private static final Id<Destination> DESTINATION_ID = new Id<Destination>(
+ 12);
+ private static final Id<DOMSource> EVENT_ID = new Id<DOMSource>(1);
+ private static final String DOCTYPE = "doc";
+ private CompositeEventListener composite;
+
+ private DOMSource source;
+
+ @Before
+ public void setUp() {
+ composite = new CompositeEventListener();
+ source = mock(DOMSource.class);
+ }
+
+ @Test
+ public void testNoListeners() {
+ // verify no exceptions occur.
+ composite.delivered(DOCTYPE, EVENT_ID, mock(DOMSource.class),
+ getTransformations(), DESTINATION_ID, DESTINATION_NAME, true);
+ composite.notDelivered(DOCTYPE, EVENT_ID, mock(DOMSource.class));
+ }
+
+ @Test
+ public void testOneListener() {
+ EventListener listener = mock(EventListener.class);
+ composite.addListener(listener);
+
+ invokeAndVerifyListenerInvoked(listener);
+ }
+
+ private void invokeAndVerifyListenerInvoked(EventListener... listeners) {
+ invokeDelivered();
+
+ for (EventListener listener : listeners) {
+ checkInvokeDelivered(listener);
+ verifyNoMoreInteractions(listener);
+ reset(listener);
+ }
+
+ invokeNotDelivered();
+ for (EventListener listener : listeners) {
+ verify(listener).notDelivered(eq(DOCTYPE), eq(EVENT_ID),
+ same(source));
+ verifyNoMoreInteractions(listener);
+ reset(listener);
+ }
+ }
+
+ private void invokeDelivered() {
+ composite.delivered(DOCTYPE, EVENT_ID, source, getTransformations(),
+ DESTINATION_ID, DESTINATION_NAME, true);
+ }
+
+ private void invokeNotDelivered() {
+ composite.notDelivered(DOCTYPE, EVENT_ID, source);
+ }
+
+ private void checkInvokeDelivered(EventListener listener) {
+ verify(listener).delivered(eq(DOCTYPE), eq(EVENT_ID), same(source),
+ eq(getTransformations()), eq(DESTINATION_ID), eq(DESTINATION_NAME),
+ eq(true));
+ }
+
+ @Test
+ public void testRemoveListener() {
+ EventListener listener1 = mock(EventListener.class);
+ composite.addListener(listener1);
+
+ EventListener listener2 = mock(EventListener.class);
+ assertFalse(composite.removeListener(listener2));
+
+ invokeAndVerifyListenerInvoked(listener1);
+
+ assertTrue(composite.removeListener(listener1));
+ invokeDelivered();
+ verifyNoMoreInteractions(listener1);
+ invokeNotDelivered();
+ verifyNoMoreInteractions(listener1);
+ }
+
+ private ArrayList<Transformation> getTransformations() {
+ return new ArrayList<Transformation>();
+ }
+
+ @Test
+ public void testMultipleListeners() {
+ EventListener listener1 = mock(EventListener.class);
+ EventListener listener2 = mock(EventListener.class);
+ composite.addListener(listener1);
+ composite.addListener(listener2);
+
+ invokeAndVerifyListenerInvoked(listener1, listener2);
+ }
+}