/*
 * #%L
 * SciJava Common shared library for SciJava software.
 * %%
 * Copyright (C) 2009 - 2014 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package org.scijava.event;

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

import java.lang.ref.WeakReference;

import org.junit.Test;
import org.scijava.Context;

/**
 * Verifies that the SciJava event service works as expected.
 * 
 * @author Johannes Schindelin
 */
public class EventServiceTest {
	@Test
	public void testWeakEventHandlers() {
		// verify that the garbage collector collects weak references
		final WeakReference<MyEventHandler> reference =
				new WeakReference<MyEventHandler>(new MyEventHandler());
		gc();
		assertNull(reference.get());

		// make a new context with an event service
		final Context context = new Context(EventService.class);
		final EventService eventService = context.getService(EventService.class);

		// register the custom event handler
		MyEventHandler handler = new MyEventHandler();
		eventService.subscribe(handler);

		// verify that the event handler is called even after garbage collecting
		gc();
		assertEquals(0, counter);
		eventService.publish(new MyEvent());
		assertEquals(1, counter);

		// verify that releasing the reference releases the event handler
		handler = null;
		gc();
		eventService.publish(new MyEvent());
		assertEquals(1, counter);
	}

	private static void gc() {
		System.gc();
		// for some reason, some systems need extra encouragement to collect their garbage
		System.gc();
	}

	private int counter = 0;

	private class MyEvent extends SciJavaEvent {
		public void inc() {
			counter++;
		}
	}

	public static class MyEventHandler {
		@EventHandler
		public void onEvent(final MyEvent e) {
			e.inc();
		}
	}
}

@@ -1695,99 +1695,356 @@
 ertN
+otN
 ull;%0A
-%0Aimport java.lang.ref.WeakReference;%0A%0Aimport org.junit.Test;%0Aimport org.scijava.Contex
+import static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.lang.ref.WeakReference;%0A%0Aimport org.junit.Test;%0Aimport org.scijava.Context;%0Aimport org.scijava.plugin.Plugin;%0Aimport org.scijava.service.AbstractService;%0Aimport org.scijava.service.Service;%0Aimport org.scijava.service.event.ServicesLoadedEven
 t;%0A%0A
@@ -3087,24 +3087,689 @@
 unter);%0A%09%7D%0A%0A
+%09/**%0A%09 * Tests that when a service has methods labeled with %7B@code @EventHandler%7D%0A%09 * annotations, the %7B@link EventService%7D will be brought in as a dependency.%0A%09 */%0A%09@Test%0A%09public void testEventHandlerDependencies() throws InterruptedException %7B%0A%09%09final Context context = new Context(ServiceNeedingAnEventService.class);%0A%09%09final EventService eventService = context.getService(EventService.class);%0A%09%09final ServiceNeedingAnEventService snaeService =%0A%09%09%09context.getService(ServiceNeedingAnEventService.class);%0A%09%09assertNotNull(eventService);%0A%09%09Thread.sleep(500); // NB: ServicesLoadedEvent is published asynchronously.%0A%09%09assertTrue(snaeService.isContextCreated());%0A%09%7D%0A%0A
 %09private sta
@@ -4134,10 +4134,331 @@
 %0A%09%09%7D%0A%09%7D%0A
+%0A%09public static class ServiceNeedingAnEventService extends AbstractService %7B%0A%0A%09%09private boolean contextCreated;%0A%0A%09%09public boolean isContextCreated() %7B%0A%09%09%09return contextCreated;%0A%09%09%7D%0A%0A%09%09@EventHandler%0A%09%09public void onEvent(%0A%09%09%09@SuppressWarnings(%22unused%22) final ServicesLoadedEvent evt)%0A%09%09%7B%0A%09%09%09contextCreated = true;%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

test support for @EventHandler