/*
 * Copyright 2012-2014 the original author or authors.
 *
 * 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 org.springframework.boot.logging;

import java.io.File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.util.StringUtils;

import static org.junit.Assert.assertTrue;

/**
 * Base for {@link LoggingSystem} tests.
 *
 * @author Phillip Webb
 * @author Andy Wilkinson
 */
public abstract class AbstractLoggingSystemTests {

	private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";

	private static String tempDir;

	@BeforeClass
	public static void configureTempdir() {
		tempDir = System.getProperty(JAVA_IO_TMPDIR);
		File newTempDir = new File("target/tmp");
		newTempDir.mkdirs();
		System.setProperty(JAVA_IO_TMPDIR, newTempDir.getAbsolutePath());
	}

	@AfterClass
	public static void reinstateTempDir() {
		System.setProperty(JAVA_IO_TMPDIR, tempDir);
	}

	@Before
	public void deleteTempLog() {
		deleteFile(new File(tmpDir() + "/spring.log"));
		deleteFile(new File(tmpDir() + "/tmp.log"));
	}

	private void deleteFile(File file) {
		if (file.exists()) {
			assertTrue("Unable to delete file", file.delete());
		}
	}

	@After
	public void clear() {
		System.clearProperty("LOG_FILE");
		System.clearProperty("PID");
	}

	protected final String[] getSpringConfigLocations(AbstractLoggingSystem system) {
		return system.getSpringConfigLocations();
	}

	protected final LogFile getLogFile(String file, String path) {
		return new LogFile(file, path);
	}

	protected final String tmpDir() {
		String path = StringUtils.cleanPath(System.getProperty(JAVA_IO_TMPDIR));
		if (path.endsWith("/")) {
			path = path.substring(0, path.length() - 1);
		}
		return path;
	}

}

@@ -673,20 +673,27 @@
 java.io.
-File
+IOException
 ;%0A%0Aimpor
@@ -728,26 +728,22 @@
 g.junit.
-AfterClass
+Before
 ;%0Aimport
@@ -745,37 +745,35 @@
 mport org.junit.
-Befor
+Rul
 e;%0Aimport org.ju
@@ -776,27 +776,37 @@
 g.junit.
-BeforeClass
+rules.TemporaryFolder
 ;%0Aimport
@@ -849,52 +849,8 @@
 s;%0A%0A
-import static org.junit.Assert.assertTrue;%0A%0A
 /**%0A
@@ -1069,36 +1069,102 @@
 ;%0A%0A%09
-private static String tempDi
+@Rule%0A%09public TemporaryFolder temp = new TemporaryFolder();%0A%0A%09private String originalTempFolde
 r;%0A%0A
@@ -1171,21 +1171,16 @@
 %09@Before
-Class
 %0A%09public
@@ -1176,31 +1176,24 @@
 ore%0A%09public 
-static 
 void configu
@@ -1208,122 +1208,90 @@
 r() 
-%7B%0A%09%09tempDir = System.getProperty(JAVA_IO_TMPDIR);%0A%09%09File newTempDir = new File(%22target/tmp%22);%0A%09%09newTempDir.mkdirs(
+throws IOException %7B%0A%09%09this.originalTempFolder = System.getProperty(JAVA_IO_TMPDIR
 );%0A%09
@@ -1330,18 +1330,29 @@
 IR, 
-newTempDir
+this.temp.newFolder()
 .get
@@ -1383,13 +1383,8 @@
 fter
-Class
 %0A%09pu
@@ -1388,23 +1388,16 @@
 %09public 
-static 
 void rei
@@ -1456,281 +1456,32 @@
 R, t
-empDir);%0A%09%7D%0A%0A%09@Before%0A%09public void deleteTempLog() %7B%0A%09%09deleteFile(new File(tmpDir() + %22/spring.log%22));%0A%09%09deleteFile(new File(tmpDir() + %22/tmp.log%22));%0A%09%7D%0A%0A%09private void deleteFile(File file) %7B%0A%09%09if (file.exists()) %7B%0A%09%09%09assertTrue(%22Unable to delete file%22, file.delete());%0A%09%09%7D
+his.originalTempFolder);
 %0A%09%7D%0A

Change LoggingSystemTests to use real temp folder