Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f4c7dadaffc213a4ec655181982295a82646d4c (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2010 IBM Corporation and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.event.tests;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Dictionary;
import java.util.Hashtable;
import org.eclipse.equinox.compendium.tests.Activator;
import org.junit.*;
import org.osgi.framework.*;
import org.osgi.service.event.*;

public class EventAdminTest {
	private EventAdmin eventAdmin;
	private ServiceReference<EventAdmin> eventAdminReference;

	@Before
	public void setUp() throws Exception {
		Activator.getBundle(Activator.BUNDLE_EVENT).start();
		eventAdminReference = Activator.getBundleContext().getServiceReference(EventAdmin.class);
		eventAdmin = Activator.getBundleContext().getService(eventAdminReference);
	}

	@After
	public void tearDown() throws Exception {
		Activator.getBundleContext().ungetService(eventAdminReference);
		Activator.getBundle(Activator.BUNDLE_EVENT).stop();
	}

	/*
	 * Ensures EventAdmin does not deliver an event published on topic "a/b/c" 
	 * to an EventHandler listening to topic a/b/c/*.
	 * 
	 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=325064.
	 */
	@Test
	public void testEventDeliveryForWildcardTopic1() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, "a/b/c/*"); //$NON-NLS-1$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a/b/c", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNull("Received event published to topic 'a/b/c' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}

	/*
	 * Ensures EventAdmin does not deliver an event published on topic "a/b" to 
	 * an EventHandler listening to topic a/b/c/*.
	 */
	@Test
	public void testEventDeliveryForWildcardTopic2() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, "a/b/c/*"); //$NON-NLS-1$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a/b", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNull("Received event published to topic 'a/b' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}

	/*
	 * Ensures EventAdmin does not deliver an event published on topic "a" to 
	 * an EventHandler listening to topic a/b/c/*.
	 */
	@Test
	public void testEventDeliveryForWildcardTopic3() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, "a/b/c/*"); //$NON-NLS-1$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNull("Received event published to topic 'a' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}

	/*
	 * Ensures EventAdmin delivers an event published on topic "a/b/c/d" to an 
	 * EventHandler listening to topic "a/b/c/*".
	 */
	@Test
	public void testEventDeliveryForWildcardTopic4() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, "a/b/c/*"); //$NON-NLS-1$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a/b/c/d", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNotNull("Did not receive event published to topic 'a/b/c/d' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}

	/*
	 * Ensures EventAdmin delivers an event published on topic "a/b/c/d/e" to 
	 * an EventHandler listening to topic "a/b/c/*".
	 */
	@Test
	public void testEventDeliveryForWildcardTopic5() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, "a/b/c/*"); //$NON-NLS-1$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a/b/c/d/e", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNotNull("Did not receive event published to topic 'a/b/c/d/e' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}

	/*
	 * Ensures EventAdmin delivers an event published on topic "a/b/c/d/e/f" to 
	 * an EventHandler listening to topic "a/b/c/*".
	 */
	@Test
	public void testEventDeliveryForWildcardTopic6() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, "a/b/c/*"); //$NON-NLS-1$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a/b/c/d/e/f", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNotNull("Did not receive event published to topic 'a/b/c/d/e/f' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}

	/*
	 * Ensures EventAdmin delivers an event published to topics "a/b/c" and 
	 * "a/b/c/d" to an EventHandler listening to topics "a/b/c" and "a/b/c/*".
	 * 
	 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=325064.
	 */
	@Test
	public void testEventDeliveryForWildcardTopic7() {
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, new String[] {"a/b/c", "a/b/c/*"}); //$NON-NLS-1$ //$NON-NLS-2$
		BundleContext bundleContext = Activator.getBundleContext();
		EventHandlerHelper handler = new EventHandlerHelper();
		ServiceRegistration<EventHandler> handlerRegistration = bundleContext.registerService(EventHandler.class, handler, properties);
		Event event = new Event("a/b/c", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNotNull("Did not receive event published to topic 'a/b/c' while listening to 'a/b/c'", handler.clearLastEvent()); //$NON-NLS-1$
		event = new Event("a/b/c/d", (Dictionary<String, Object>) null); //$NON-NLS-1$
		eventAdmin.sendEvent(event);
		assertNotNull("Did not receive event published to topic 'a/b/c/d' while listening to 'a/b/c/*'", handler.lastEvent()); //$NON-NLS-1$
		handlerRegistration.unregister();
	}
}

Back to the top