2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.listener;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
22 import java.util.ArrayList;
24 import javax.print.attribute.standard.Destination;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Transformation;
31 public class CompositeEventListenerTest {
33 private static final String DESTINATION_NAME = "dest";
34 private static final Id<Destination> DESTINATION_ID = new Id<Destination>(
36 private CompositeEventListener composite;
38 private EventInfo source;
42 composite = new CompositeEventListener();
43 source = mock(EventInfo.class);
47 public void testNoListeners() {
48 // verify no exceptions occur.
49 composite.delivered(mock(EventInfo.class), getTransformations(),
50 DESTINATION_ID, DESTINATION_NAME, true);
51 composite.notDelivered(mock(EventInfo.class));
55 public void testOneListener() {
56 EventListener listener = mock(EventListener.class);
57 composite.addListener(listener);
59 invokeAndVerifyListenerInvoked(listener);
62 private void invokeAndVerifyListenerInvoked(EventListener... listeners) {
65 for (EventListener listener : listeners) {
66 checkInvokeDelivered(listener);
67 verifyNoMoreInteractions(listener);
72 for (EventListener listener : listeners) {
73 verify(listener).notDelivered(same(source));
74 verifyNoMoreInteractions(listener);
79 private void invokeDelivered() {
80 composite.delivered(source, getTransformations(), DESTINATION_ID,
81 DESTINATION_NAME, true);
84 private void invokeNotDelivered() {
85 composite.notDelivered(source);
88 private void checkInvokeDelivered(EventListener listener) {
89 verify(listener).delivered(same(source), eq(getTransformations()),
90 eq(DESTINATION_ID), eq(DESTINATION_NAME), eq(true));
94 public void testRemoveListener() {
95 EventListener listener1 = mock(EventListener.class);
96 composite.addListener(listener1);
98 EventListener listener2 = mock(EventListener.class);
99 assertFalse(composite.removeListener(listener2));
101 invokeAndVerifyListenerInvoked(listener1);
103 assertTrue(composite.removeListener(listener1));
105 verifyNoMoreInteractions(listener1);
106 invokeNotDelivered();
107 verifyNoMoreInteractions(listener1);
110 private ArrayList<Transformation> getTransformations() {
111 return new ArrayList<Transformation>();
115 public void testMultipleListeners() {
116 EventListener listener1 = mock(EventListener.class);
117 EventListener listener2 = mock(EventListener.class);
118 composite.addListener(listener1);
119 composite.addListener(listener2);
121 invokeAndVerifyListenerInvoked(listener1, listener2);