package de.itemis.mosig.racecar.textconv;

import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

public class TextFileReaderTest {

    @Test
    public void shouldReadContentsOfExistingFile() throws IOException {
        Path tmpFilePath = Files.createTempFile(this.getClass().getSimpleName(),"txt");
        String expectedContent = new String(new byte[]{(byte) (char) (new Random().nextInt(40) + 20)}, StandardCharsets.UTF_8);
        Files.write(tmpFilePath, expectedContent.getBytes(StandardCharsets.UTF_8));

        TextFileReader underTest = new TextFileReader(tmpFilePath);

        String result = underTest.contents();

        Assert.assertEquals(expectedContent, result);
    }
}

@@ -54,21 +54,70 @@
 .junit.A
-ssert
+fter;%0Aimport org.junit.Assert;%0Aimport org.junit.Before
 ;%0Aimport
@@ -356,83 +356,84 @@
     
-@Test%0A    public void shouldReadContentsOfExistingFile() throws IOException
+private Path tmpFilePath;%0A%0A    @Before%0A    public void setUp() %7B%0A        try
  %7B%0A 
@@ -439,20 +439,19 @@
         
-Path
+   
  tmpFile
@@ -530,125 +530,587 @@
     
-String expectedContent = new String(new byte%5B%5D%7B(byte) (char) (new Random().nextInt(40) + 20)%7D, StandardCharsets.UTF_8
+%7D catch (IOException e) %7B%0A            Assert.fail(%22Could not create temporary test file: %22 + e.getMessage());%0A        %7D%0A    %7D%0A%0A    @After%0A    public void tearDown() %7B%0A        if (tmpFilePath != null) %7B%0A            try %7B%0A                Files.deleteIfExists(tmpFilePath);%0A            %7D catch (IOException e) %7B%0A                System.err.println(%22WARNING: Could not delete temporary test file: %22 + e.getMessage());%0A            %7D%0A        %7D%0A    %7D%0A    @Test%0A    public void shouldReadContentsOfExistingFile() throws IOException %7B%0A        String expectedContent = generateRandomString(
 );%0A 
@@ -1368,11 +1368,227 @@
 );%0A    %7D
+%0A%0A    /*%0A     * ##### start private helper code%0A     */%0A%0A    private String generateRandomString() %7B%0A        return new String(new byte%5B%5D%7B(byte) (char) (new Random().nextInt(40) + 20)%7D, StandardCharsets.UTF_8);%0A    %7D
 %0A%7D%0A

Test does now proper tmpFile creation and deletion.