import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import junit.framework.TestCase;

/**
 * This program tests the application that copies a file into another file.
 *
 * @author fpereira
 */
public class TestCopy extends TestCase {
  /**
   * Class constructor.
   *
   * @param name
   *        the name of the test.
   */
  public TestCopy(final String name) {
    super(name);
  }

  /**
   * Test if we can correctly copy a file into another using FileCopier0.
   *
   * @throws Exception
   *         in case something goes wrong.
   */
  public final void testCopy1() throws Exception {
    byte[] inData = {70,101,114,110,97,110,100,111};
    String inputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test0.data";
    String outputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test1.data";
    // Create the file:
    FileOutputStream srcFile = new FileOutputStream(new File(inputFileName));
    srcFile.write(inData);
    // Do the copy:
    FileCopier0 fc = new FileCopier0();
    fc.cp(inputFileName, outputFileName);
    // Check if the output file has the same contents of the input file.
    FileInputStream dstFile = new FileInputStream(new File(outputFileName));
    byte[] outData = new byte[inData.length];
    dstFile.read(outData);
    for (int i = 0; i < inData.length; i++) {
      assertEquals(outData[i], inData[i]);
    }
  }

  /**
   * Test if we can correctly copy a file into another using FileCopier1.
   *
   * @throws Exception
   *         in case something goes wrong.
   */
  public final void testCopy2() throws Exception {
    byte[] inData = {70,101,114,110,97,110,100,111};
    String inputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test0.data";
    String outputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test1.data";
    // Create the file:
    FileOutputStream srcFile = new FileOutputStream(new File(inputFileName));
    srcFile.write(inData);
    // Do the copy:
    FileCopier1 fc = new FileCopier1();
    fc.cp(inputFileName, new FileWriter(outputFileName));
    // Check if the output file has the same contents of the input file.
    FileInputStream dstFile = new FileInputStream(new File(outputFileName));
    byte[] outData = new byte[inData.length];
    dstFile.read(outData);
    for (int i = 0; i < inData.length; i++) {
      assertEquals(outData[i], inData[i]);
    }
  }

  /**
   * Test if we can correctly count the number of characters in an input
   * file.
   *
   * @throws Exception
   *         in case something goes wrong.
   */
  public final void testCharacterCounter() throws Exception {
    byte[] inData = {70,101,114,110,97,110,100,111};
    String inputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test0.data";
    String outputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test1.data";
    // Create the file:
    FileOutputStream srcFile = new FileOutputStream(new File(inputFileName));
    srcFile.write(inData);
    // Do the copy:
    FileCopier1 fc = new FileCopier1();
    CharacterCounter cc = new CharacterCounter();
    fc.cp(inputFileName, cc);
    // Check if the output file has the same contents of the input file.
    assertEquals(inData.length, cc.getNumCharacters());
  }

  /**
   * Test if we can correctly count the number of words in an input file.
   *
   * @throws Exception
   *         in case something goes wrong.
   */
  public final void testWordCounter() throws Exception {
    byte[] inData = {97,32,32,98,32,9,10,32,99,9,9,100};
    String inputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test0.data";
    String outputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test1.data";
    // Create the file:
    FileOutputStream srcFile = new FileOutputStream(new File(inputFileName));
    srcFile.write(inData);
    // Do the copy:
    FileCopier1 fc = new FileCopier1();
    WordCounter cc = new WordCounter();
    fc.cp(inputFileName, cc);
    // Check if the output file has the same contents of the input file.
    assertEquals(4, cc.getNumWords());
  }

  /**
   * Test if we can correctly count the number of lines in an input file.
   *
   * @throws Exception
   *         in case something goes wrong.
   */
  public final void testLineCounter() throws Exception {
    byte[] inData = {97,10,98,10,10,99};
    String inputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test0.data";
    String outputFileName = "/Users/ata/Documents/Classes/52/lessons_02_10/lesson11/src/test1.data";
    // Create the file:
    FileOutputStream srcFile = new FileOutputStream(new File(inputFileName));
    srcFile.write(inData);
    // Do the copy:
    FileCopier1 fc = new FileCopier1();
    LineCounter lc = new LineCounter();
    fc.cp(inputFileName, lc);
    // Check if the output file has the same contents of the input file.
    assertEquals(3, lc.getNumLines());
  }

}
