package Algorithms.Strings.Gemstones;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @className SolutionTest
 * @description Unit test
 * @author dair
 * @date Jun 14, 2016
 *
 */
public class SolutionTest {
    private final String path = this.getClass().getResource(".").getPath().toString().replaceFirst("/", "")
                    .replaceAll("\\w+/classes", "resource");
    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(outContent));
    }

    @After
    public void cleanUpStreams() {
        System.setOut(null);
    }

    @Test
    public void testMain() throws IOException {
        int index = 1;
        Path inputPath = Paths.get(path, "input.txt");
        Path outputPath = Paths.get(path, "output.txt");
        
        // for multiple test case
        do {
            System.setIn(Files.newInputStream(inputPath));
            Solution.main(null);
            assertEquals(new String(Files.readAllBytes(outputPath)), outContent.toString().replaceAll("\\s+$", "").replaceAll("\\s+\\n", "\r\n"));
            outContent.reset();
            index++;
        } while (Files.exists(inputPath = Paths.get(path, "input" + index + ".txt"))
                        && Files.exists(outputPath = Paths.get(path, "output" + index + ".txt")));
    }
}
