package de.retest.recheck.persistence;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import de.retest.recheck.configuration.ProjectConfiguration;
import de.retest.recheck.util.junit.jupiter.SystemProperty;

class GoldenMasterProviderImplTest {

	private static final String NON_EXISTING_FILE = "blubber_schmu";
	private static final String EXISTING_FILE = "existing";

	GoldenMasterProviderImpl cut;

	@BeforeEach
	void setUp( @TempDir final Path temp ) {
		cut = new GoldenMasterProviderImpl( null );
	}

	@Test
	void null_should_result_in_error() {
		assertThatThrownBy( () -> cut.getGoldenMaster( null ) ).isInstanceOf( NoGoldenMasterFoundException.class );
	}

	@Test
	void non_existing_file_should_throw_error( @TempDir final Path temp ) {
		final File nonExistingFile = temp.resolve( NON_EXISTING_FILE ).toFile();

		assertThatThrownBy( () -> cut.getGoldenMaster( nonExistingFile.getPath() ) )
				.isInstanceOf( NoGoldenMasterFoundException.class );
	}

	@Test
	@SystemProperty( key = ProjectConfiguration.RETEST_PROJECT_ROOT )
	void existing_file_outside_project_root_should_throw_error( @TempDir final Path tempFolder ) throws IOException {
		final File existingFile = tempFolder.resolve( EXISTING_FILE ).toFile();
		existingFile.createNewFile();

		System.setProperty( ProjectConfiguration.RETEST_PROJECT_ROOT, tempFolder.toString() );

		assertThatThrownBy( () -> cut.getGoldenMaster( existingFile.getPath() ) )
				.isInstanceOf( NoGoldenMasterFoundException.class );
	}

	@Test
	@SystemProperty( key = ProjectConfiguration.RETEST_PROJECT_ROOT )
	void existing_file_in_project_root_should_be_ok( @TempDir final Path tempFolder ) throws Exception {
		final File existingFile = tempFolder.resolve( EXISTING_FILE ).toFile();
		existingFile.createNewFile();
		Files.createDirectories( tempFolder.resolve( "src/main/java" ) );
		Files.createDirectories( tempFolder.resolve( "src/test/java" ) );
		System.setProperty( ProjectConfiguration.RETEST_PROJECT_ROOT, tempFolder.toString() );

		final File gm = cut.getGoldenMaster( EXISTING_FILE );

		assertThat( gm ).isEqualTo( existingFile );
	}

}

@@ -964,24 +964,91 @@
 %0A%09%7D%0A%0A%09@Test%0A
+%09@SystemProperty( key = ProjectConfiguration.RETEST_PROJECT_ROOT )%0A
 %09void non_ex
@@ -1099,24 +1099,30 @@
 al Path temp
+Folder
  ) %7B%0A%09%09final
@@ -1149,16 +1149,22 @@
 e = temp
+Folder
 .resolve
@@ -1192,24 +1192,114 @@
 .toFile();%0A%0A
+%09%09System.setProperty( ProjectConfiguration.RETEST_PROJECT_ROOT, tempFolder.toString() );%0A%0A
 %09%09assertThat

Fix test for Windows