/*
 * Copyright 2016 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.gradle.spotless;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

import com.diffplug.common.base.StringPrinter;
import com.diffplug.common.io.Files;

public class IdeHookTest extends GradleIntegrationTest {
	private String output, error;
	private File dirty, clean, diverge, outofbounds;

	@Before
	public void before() throws IOException {
		setFile("build.gradle").toLines(
				"plugins {",
				"  id 'com.diffplug.gradle.spotless'",
				"}",
				"spotless {",
				"  format 'misc', {",
				"    target 'DIRTY.md', 'CLEAN.md'",
				"    customLazyGroovy('lowercase') {",
				"       return { str -> str.toLowerCase(Locale.ROOT) }",
				"    }",
				"  }",
				"  format 'diverge', {",
				"    target 'DIVERGE.md'",
				"    customLazyGroovy('diverge') {",
				"       return { str -> str + ' ' }",
				"    }",
				"  }",
				"}");
		dirty = new File(rootFolder(), "DIRTY.md");
		Files.write("ABC".getBytes(StandardCharsets.UTF_8), dirty);
		clean = new File(rootFolder(), "CLEAN.md");
		Files.write("abc".getBytes(StandardCharsets.UTF_8), clean);
		diverge = new File(rootFolder(), "DIVERGE.md");
		Files.write("ABC".getBytes(StandardCharsets.UTF_8), diverge);
		outofbounds = new File(rootFolder(), "OUTOFBOUNDS.md");
		Files.write("ABC".getBytes(StandardCharsets.UTF_8), outofbounds);
	}

	private void runWith(String... arguments) throws IOException {
		StringBuilder output = new StringBuilder();
		StringBuilder error = new StringBuilder();
		try (Writer outputWriter = new StringPrinter(output::append).toWriter();
				Writer errorWriter = new StringPrinter(error::append).toWriter();) {
			gradleRunner()
					.withArguments(arguments)
					.forwardStdOutput(outputWriter)
					.forwardStdError(errorWriter)
					.build();
		}
		this.output = output.toString();
		this.error = error.toString();
	}

	@Test
	public void dirty() throws IOException {
		runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + dirty.getAbsolutePath(), "--stacktrace");
		Assertions.assertThat(output).isEqualTo("abc");
		Assertions.assertThat(error).startsWith("IS DIRTY");
	}

	@Test
	public void clean() throws IOException {
		runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + clean.getAbsolutePath());
		Assertions.assertThat(output).isEmpty();
		Assertions.assertThat(error).startsWith("IS CLEAN");
	}

	@Test
	public void diverge() throws IOException {
		runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + diverge.getAbsolutePath());
		Assertions.assertThat(output).isEmpty();
		Assertions.assertThat(error).startsWith("DID NOT CONVERGE");
	}

	@Test
	public void outofbounds() throws IOException {
		runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + outofbounds.getAbsolutePath());
		Assertions.assertThat(output).isEmpty();
		Assertions.assertThat(error).isEmpty();
	}

	@Test
	public void notAbsolute() throws IOException {
		runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=build.gradle");
		Assertions.assertThat(output).isEmpty();
		Assertions.assertThat(error).contains("Argument passed to spotlessIdeHook must be an absolute path");
	}
}

@@ -779,16 +779,63 @@
 rtions;%0A
+import org.gradle.testkit.runner.GradleRunner;%0A
 import o
@@ -2415,20 +2415,285 @@
 %0A%09%09%09
+// 
 gradle
-Runner
+ 2.14 -%3E 4.6 confirmed to work%0A%09%09%09// gradle 4.7 -%3E 5.1 don't work in tooling API because of https://github.com/gradle/gradle/issues/7617%0A%09%09%09// gradle 5.1 -%3E current confirmed to work%0A%09%09%09GradleRunner.create()%0A%09%09%09%09%09.withProjectDir(rootFolder())%0A%09%09%09%09%09.withPluginClasspath
 ()%0A%09

Fix IdeHookTest on JDK 11 (because it defaulted to testing with gradle 5, which has a stderr bug in the tooling API)