package ru.matevosyan;

import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
 * Created SortFile for testing sort big file and write it to a new file.
 * Created on 19.02.2017.
 * @since 1.0
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class SortFileTest {

    private static String pathSource;
    private static String pathDist;
    private static File sourceFile;
    private static File distFile;
    private static SortFile sortFile;

    /**
     * execute all common variable before start testing class methods, that use anywhere in the class.
     * @throws IOException when path to a file is invalid.
     */

    @BeforeClass
    public static void executeSameVariable() throws IOException {

        pathSource = "C:\\Users\\Admin\\Desktop\\Abbat.txt";
        pathDist = "C:\\Users\\Admin\\Desktop\\AbbatResults.txt";

        sourceFile = new File(pathSource);
        distFile = new File(pathDist);
        sortFile = new SortFile();
    }

    /**
     * testing files size.
     */

    @Test
    public void whenAddFileAndSortToAnotherFileThanCheckFileSize() {

        try {
            sortFile.sort(sourceFile, distFile);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }

        long l = sourceFile.length();

        assertThat(distFile.length(), is(l));
    }

    /**
     * testing {@link SortFile#sort(File, File)} that sorting the file.
     */

    @Test
    public void whenAddFileAndSortToAnotherFileThanCheckIfItSort() {

        try {

            File sourceFile = new File(pathSource);
            File distFile = new File(pathDist);
            sortFile.sort(sourceFile, distFile);

        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }

        String[] expectedArray = {
                "1",
                "22",
                "22",
                "333",
                "4444",
                "0000",
                "55555",
                "55555",
                "666666",
                "666666",
                "666666",
                "666666",
                "8888888",
                "77777777",
                "999999999",
                "999999999"
        };

       String[] sActualArray = sortFile.getStringsThirdSortedToRead();

        for (int i = 0; i < expectedArray.length; i++) {
            assertThat(expectedArray[i], is(sActualArray[i]));
        }
    }

}
@@ -119,16 +119,49 @@
 ception;
+%0Aimport java.io.RandomAccessFile;
 %0A%0Aimport
@@ -615,16 +615,94 @@
 ortFile;
+%0A    private static RandomAccessFile out;%0A    private static String%5B%5D forSort;
 %0A%0A    /*
@@ -1211,148 +1211,861 @@
     
-%7D%0A%0A    /**%0A     * testing files size.%0A     */%0A%0A    @Test%0A    public void whenAddFileAndSortToAnotherFileThanCheckFileSize() %7B%0A%0A        try %7B
+    out = new RandomAccessFile(sourceFile, %22rw%22);%0A        forSort = new String%5B%5D%7B%0A                %221%22,%0A                %2222%22,%0A                %22333%22,%0A                %224444%22,%0A                %2255555%22,%0A                %22666666%22,%0A                %2277777777%22,%0A                %228888888%22,%0A                %22999999999%22,%0A                %220000%22,%0A                %22666666%22,%0A                %22666666%22,%0A                %2222%22,%0A                %2255555%22,%0A                %22999999999%22,%0A                %22666666%22%0A        %7D;%0A    %7D%0A%0A    /**%0A     * testing files size.%0A     */%0A%0A    @Test%0A    public void whenAddFileAndSortToAnotherFileThanCheckFileSize() %7B%0A%0A        try %7B%0A            for (String aForSort : forSort) %7B%0A                out.write(aForSort.getBytes());%0A                String s = System.getProperty(%22line.separator%22);%0A                out.write(s.getBytes());%0A            %7D
 %0A   
@@ -2291,16 +2291,133 @@
  is(l));
+%0A%0A        try %7B%0A            out.close();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A    %7D%0A%0A
@@ -2598,16 +2598,230 @@
    try %7B
+%0A            for (String aForSort : forSort) %7B%0A                out.write(aForSort.getBytes());%0A                String s = System.getProperty(%22line.separator%22);%0A                out.write(s.getBytes());%0A            %7D
 %0A%0A      
@@ -3703,25 +3703,142 @@
 %5Bi%5D));%0A        %7D
+%0A%0A        try %7B%0A            out.close();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A    %7D%0A%0A%7D

add creation file in test method