package eu.ocathain.jremotelog;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

import org.apache.commons.io.output.FileWriterWithEncoding;
import org.junit.Assert;
import org.junit.Test;

public class IvManagerTest {

	@Test
	public void testIvManager() {
		IvManager ivManager = new IvManager("target/test-classes/ivFile");

		Assert.assertArrayEquals(
				ivManager.toByteArrayForIv(BigInteger.valueOf(101000)),
				ivManager.next());

		Assert.assertArrayEquals(
				ivManager.toByteArrayForIv(BigInteger.valueOf(101001)),
				ivManager.next());
	}

	@Test
	public void testWithEmptyFile() {
		IvManager ivManager = new IvManager("target/test-classes/emptyIvFile");
		Assert.assertSame(IvManager.IV_SIZE_BYTES, ivManager.next().length);
	}

	@Test
	public void testPersist() throws IOException {
		BigInteger initial = BigInteger.valueOf(2000);

		File temp = File.createTempFile(getClass().getName() + "junitTest",
				".txt");

		temp.deleteOnExit();

		try (Writer fw = new FileWriterWithEncoding(temp,
				StandardCharsets.US_ASCII)) {
			fw.append(initial.toString());
		}

		IvManager ivManager = new IvManager(temp.getPath());

		appendLotsAndCheck(temp, ivManager, "4000");

		appendLotsAndCheck(temp, ivManager, "5000");
	}

	private void appendLotsAndCheck(File temp, IvManager ivManager,
			String expected) throws IOException {
		for (int i = 0; i < 1001; i++) {
			ivManager.next();
		}

		List<String> lines = Files.readAllLines(temp.toPath(),
				StandardCharsets.US_ASCII);

		Assert.assertEquals(expected, lines.get(lines.size() - 1));
	}

}

@@ -190,21 +190,95 @@
 io.file.
-Files
+CopyOption;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.StandardCopyOption
 ;%0Aimport
@@ -438,45 +438,372 @@
  %7B%0A%0A
-%09@Test%0A%09public void testIvManager() %7B
+    public static final String TEST_IV_FILE = %22target/test-classes/ivFile%22;%0A    public static final String TEST_IV_FILE_FORTEST = TEST_IV_FILE + %22forTest%22;%0A%0A    @Test%0A%09public void testIvManager() throws IOException %7B%0A        Files.copy(new File(TEST_IV_FILE).toPath(), new File(TEST_IV_FILE_FORTEST).toPath(),%0A                StandardCopyOption.REPLACE_EXISTING);%0A
 %0A%09%09I
@@ -841,36 +841,28 @@
 ger(
-%22target/test-classes/ivFile%22
+TEST_IV_FILE_FORTEST
 );%0A%0A

allow test to be rerun without mvn clean