/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2013 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.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.legacy;

import static imagej.legacy.LegacyTestUtils.ijRun;
import static imagej.legacy.LegacyTestUtils.makeJar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;

import org.junit.After;
import org.junit.Test;
import org.scijava.util.FileUtils;

/**
 * Tests the support for <i>ij1.plugin.dirs</i> (falling back to the <i>.plugins/</i> subdirectory of <i>user.home</i>).
 * 
 * @author Johannes Schindelin
 */
public class ExtraPluginDirsTest {

	static {
		DefaultLegacyService.preinit();
	}

	private File tmpDir;

	@After
	public void rmRFTmpDir() {
		if (tmpDir != null && tmpDir.isDirectory()) {
			FileUtils.deleteRecursively(tmpDir);
		}
	}

	@Test
	public void findsExtraPluginDir() throws Exception {
		tmpDir = FileUtils.createTemporaryDirectory("legacy-", "");
		final File jarFile = new File(tmpDir, "Set_Property.jar");
		makeJar(jarFile, Set_Property.class.getName());
		assertTrue(jarFile.getAbsolutePath() + " exists", jarFile.exists());
		System.setProperty("ij1.plugin.dirs", tmpDir.getAbsolutePath());

		final String key = "random-" + Math.random();
		System.setProperty(key, "321");
		final ClassLoader loader = LegacyTestUtils.getFreshIJClassLoader(true, true);
		ijRun(loader, "Set Property", "key=" + key + " value=123");
		assertEquals("123", System.getProperty(key));
	}
}

@@ -2033,59 +2033,201 @@
 sert
-True;%0A%0Aimport java.io.File;%0A%0Aimport org.junit.After
+NotNull;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before
 ;%0Aim
@@ -2688,20 +2688,22 @@
 %7D%0A%09%7D%0A%0A%09@
-Test
+Before
 %0A%09public
@@ -2712,24 +2712,15 @@
 oid 
-findsExtraPlugin
+makeTmp
 Dir(
@@ -2728,16 +2728,18 @@
  throws 
+IO
 Exceptio
@@ -2800,24 +2800,89 @@
 acy-%22, %22%22);%0A
+%09%7D%0A%0A%09@Test%0A%09public void findsExtraPluginDir() throws Exception %7B%0A
 %09%09final File
@@ -3393,11 +3393,854 @@
 ey));%0A%09%7D
+%0A%0A%09@Test%0A%09public void knowsAboutJarsDirectory() throws Exception %7B%0A%09%09final File pluginsDir = new File(tmpDir, %22plugins%22);%0A%09%09assertTrue(pluginsDir.mkdirs());%0A%09%09final File jarsDir = new File(tmpDir, %22jars%22);%0A%09%09assertTrue(jarsDir.mkdirs());%0A%09%09ClassLoader loader = LegacyTestUtils.getFreshIJClassLoader(true, true);%0A%09%09final String helperClassName = Test.class.getName();%0A%09%09try %7B%0A%09%09%09assertNull(loader.loadClass(helperClassName));%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09/* all okay, we did not find the class */%0A%09%09%7D%0A%09%09final File jarFile = new File(jarsDir, %22helper.jar%22);%0A%09%09LegacyTestUtils.makeJar(jarFile, helperClassName);%0A%09%09System.setProperty(%22plugins.dir%22, pluginsDir.getAbsolutePath());%0A%09%09try %7B%0A%09%09%09assertNotNull(loader.loadClass(helperClassName));%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09assertNull(%22Should have found %22 + helperClassName + %22 in %22 + jarFile);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

make sure that jars/ is in IJ1's plugin class path