/*
 * #%L
 * SciJava Common shared library for SciJava software.
 * %%
 * Copyright (C) 2009 - 2017 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.module.process;

import static org.junit.Assert.assertTrue;

import java.util.concurrent.ExecutionException;

import org.junit.Test;
import org.scijava.Context;
import org.scijava.command.Command;
import org.scijava.command.CommandService;
import org.scijava.log.LogService;
import org.scijava.log.Logger;
import org.scijava.log.TestLogListener;
import org.scijava.plugin.Parameter;

import static org.junit.Assert.assertEquals;

/**
 * Tests {@link LoggerPreprocessor}.
 *
 * @author Matthias Arzt
 */
public class LoggerPreprocessorTest {

	@Test
	public void testInjection() throws InterruptedException, ExecutionException {
		final Context context = new Context(CommandService.class);
		final CommandService commandService = context.service(CommandService.class);
		final TestLogListener listener = new TestLogListener();
		context.service(LogService.class).addListener(listener);

		commandService.run(CommandWithLogger.class, true).get();
		assertTrue(listener.hasLogged(m -> m.source().path().contains(CommandWithLogger.class.getSimpleName())));
	}

	public static class CommandWithLogger implements Command {

		@Parameter
		public Logger log;

		@Override
		public void run() {
			log.info("log from the command.");
		}
	}

	@Test
	public void testLoggerNameByAnnotation() throws ExecutionException, InterruptedException {
		final Context context = new Context(CommandService.class);
		final CommandService commandService = context.service(CommandService.class);
		commandService.run(CommandWithNamedLogger.class, true).get();
	}

	public static class CommandWithNamedLogger implements Command {

		@Parameter(label = "MyLoggerName")
		public Logger log;

		@Override
		public void run() {
			assertEquals("MyLoggerName", log.getName());
		}
	}
}

@@ -1615,16 +1615,61 @@
 ocess;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -1703,16 +1703,16 @@
 rtTrue;%0A
-
 %0Aimport 
@@ -1875,32 +1875,103 @@
 CommandService;%0A
+import org.scijava.log.DefaultLogger;%0Aimport org.scijava.log.LogLevel;%0A
 import org.scija
@@ -1981,32 +1981,66 @@
 log.LogService;%0A
+import org.scijava.log.LogSource;%0A
 import org.scija
@@ -2136,54 +2136,8 @@
 r;%0A%0A
-import static org.junit.Assert.assertEquals;%0A%0A
 /**%0A
@@ -2647,16 +2647,16 @@
 .get();%0A
-
 %09%09assert
@@ -2755,24 +2755,696 @@
 e())));%0A%09%7D%0A%0A
+%09/** Tests redirection of a command's log output. */%0A%09@Test%0A%09public void testCustomLogger() throws ExecutionException,%0A%09%09InterruptedException%0A%09%7B%0A%09%09// setup%0A%09%09final Context context = new Context(CommandService.class);%0A%09%09final CommandService commandService = context.service(CommandService.class);%0A%09%09final TestLogListener listener = new TestLogListener();%0A%09%09final LogSource source = LogSource.newRoot();%0A%09%09final DefaultLogger customLogger = new DefaultLogger(listener, source,%0A%09%09%09LogLevel.TRACE);%0A%09%09// process%0A%09%09commandService.run(CommandWithLogger.class, true, %22log%22, customLogger)%0A%09%09%09.get();%0A%09%09// test%0A%09%09assertTrue(listener.hasLogged(m -%3E m.source().equals(source)));%0A%09%7D%0A%0A
 %09public stat

test log redirection