/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * 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 imagej.legacy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import ij.IJ;

import org.junit.Test;

/**
 * Tests that the legacy Java agent identifies the correct spot where ImageJ 1.x classes were loaded.
 * 
 * @author Johannes Schindelin
 */
public class LegacyJavaAgentIT {

	private StackTraceElement[] trace;

	@Test
	public void testAgentInit() {
		assumeTrue("init".equals(System.getProperty("legacy.agent.mode")));
		IJ.log("Now ij.IJ is loaded.");
		final LegacyService legacyService =
			(LegacyService)IJ.runPlugIn(LegacyService.class.getName(), null);
		assertTrue(legacyService != null);
	}

	@Test
	public void testAgentDebug() {
		assumeTrue("debug".equals(System.getProperty("legacy.agent.mode")));
		try {
			trace = Thread.currentThread().getStackTrace();
			IJ.log("Now ij.IJ would be loaded.");
			assertTrue("This code should not be reached", false);
		} catch (final ExceptionInInitializerError e) {
			final Throwable e2 = e.getCause();
			assertTrue(e2 != null);
			final String message = e2.getMessage();
			assertTrue("Message should begin with 'Loading ij/IJ': " + message,
				message.startsWith("Loading ij/IJ "));
			final StackTraceElement[] stackTrace = e2.getStackTrace();
			assertEquals(getFileName(stackTrace, 0), getFileName(trace, 1));
			assertEquals(getLineNumber(stackTrace, 0), getLineNumber(trace, 1) + 1);
			System.err.println("All is fine, we got the exception:");
			e2.printStackTrace();
		}
	}

	private int getLineNumber(final StackTraceElement[] trace, int no) {
		if (trace == null || trace.length <= no) return -1;
		return trace[no].getLineNumber();
	}

	private String getFileName(final StackTraceElement[] trace, int no) {
		if (trace == null || trace.length <= no) return null;
		return trace[no].getFileName();
	}

}


@@ -1665,16 +1665,62 @@
 Equals;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import s
@@ -2088,19 +2088,36 @@
 ntInit()
+ throws Exception
  %7B%0A
-
 %09%09assume
@@ -2218,147 +2218,49 @@
 ;%0A%09%09
-final LegacyService legacyService =%0A%09%09%09(LegacyService)IJ.runPlugIn(LegacyService.class.getName(), null);%0A%09%09assertTrue(legacyService != null
+assertNotNull(IJ.class.getField(%22_hooks%22)
 );%0A%09

Make LegacyJavaAgentIT less implementation-dependent