package ee.shy.core.merge;

import ee.shy.TemporaryDirectory;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class InputStreamMergerTest {
    @Parameters(name = "{0}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"numbers"}
        });
    }

    @Rule
    public TemporaryDirectory temporaryDirectory = new TemporaryDirectory();

    private final String subpackage;

    private InputStream patchable;
    private InputStream original;
    private InputStream revised;
    private InputStream patched;
    private InputStreamMerger merger;

    public InputStreamMergerTest(String subpackage) {
        this.subpackage = subpackage;
    }

    @Before
    public void setUp() throws Exception {
        patchable = getClass().getResourceAsStream(subpackage + "/patchable");
        original = getClass().getResourceAsStream(subpackage + "/original");
        revised = getClass().getResourceAsStream(subpackage + "/revised");
        patched = getClass().getResourceAsStream(subpackage + "/patched");
        merger = new InputStreamMerger();
    }

    @Test
    public void testForwardMerge() throws Exception {
        Path path = temporaryDirectory.newFile();
        Files.delete(path);
        Files.copy(patchable, path);

        merger.merge(path, original, revised);

        List<String> actualLines = Files.readAllLines(path);
        List<String> expectedLines = IOUtils.readLines(patched);
        assertEquals(expectedLines, actualLines);
    }

    @Test
    public void testBackwardMerge() throws Exception {
        Path path = temporaryDirectory.newFile();
        Files.delete(path);
        Files.copy(patched, path);

        merger.merge(path, revised, original);

        List<String> actualLines = Files.readAllLines(path);
        List<String> expectedLines = IOUtils.readLines(patchable);
        assertEquals(expectedLines, actualLines);
    }
}
@@ -289,16 +289,44 @@
 eters;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1593,24 +1593,28 @@
 testForwardM
+ainM
 erge() throw
@@ -1639,391 +1639,578 @@
     
-Path path = temporaryDirectory.newFile(
+testMerge(patchable, original, revised, patched
 );%0A    
+%7D%0A%0A
     
-Files.delete(path);%0A        Files.copy(patchable, path);%0A%0A        merger.merge(path, original, revised);%0A%0A        List%3CString%3E actualLines = Files.readAllLines(path
+@Test%0A    public void testForwardSideMerge() throws Exception %7B%0A        testMerge(revised, original, patchable, patched);%0A    %7D%0A%0A    @Test%0A    public void testBackwardMainMerge() throws Exception %7B%0A        testMerge(patched, revised, original, patchable
 );%0A    
+%7D%0A%0A
     
-List%3CString%3E expectedLines = IOUtils.readLines(patched);%0A        assertEquals(expectedLines, actualLines);%0A    %7D%0A%0A    @Test%0A    public void testBackwardMerge(
+@Test%0A    public void testBackwardSideMerge() throws Exception %7B%0A        testMerge(original, revised, patched, patchable);%0A    %7D%0A%0A    private void testMerge(InputStream patchable, InputStream original, InputStream revised, InputStream patched
 ) th
@@ -2206,32 +2206,34 @@
 patched) throws 
+IO
 Exception %7B%0A    
@@ -2326,26 +2326,28 @@
 s.copy(patch
+abl
 e
-d
 , path);%0A%0A  
@@ -2371,33 +2371,33 @@
 e(path, 
-revised, original
+original, revised
 );%0A%0A    
@@ -2509,28 +2509,26 @@
 dLines(patch
-abl
 e
+d
 );%0A        a

Extract 4-way test methods