package at.jku.pervasive.ecg.wfdb;

import java.io.File;
import java.net.URISyntaxException;
import java.util.List;

import org.joda.time.LocalTime;

public class HRVOptionsTest extends PhysioToolkitTestCase {

  public void testEndTime() {
    HRVOptions opts = new HRVOptions("/rrfile.rr");
    opts.setStart(5, 42);
    opts.setEnd(10, 11);

    List<String> cmd = opts.getCommand();
    assertEquals(5, cmd.size());
    assertEquals("00:05:42", cmd.get(3));
    assertEquals("00:10:11", cmd.get(4));
  }

  public void testEndTimeWithoutStartTime() {
    HRVOptions opts = new HRVOptions("/rrfile.rr");
    opts.setEnd(10, 11);

    List<String> cmd = opts.getCommand();
    assertEquals(5, cmd.size());
    assertEquals("00:00:00", cmd.get(3));
    assertEquals("00:10:11", cmd.get(4));
  }

  public void testInstantation() throws Exception {
    File testFile = getFile("/test.dat");
    testFile = new File(testFile.getParentFile(), "test");

    HRVOptions options = new HRVOptions(testFile, "qrs");
    options.setStart(10, 11);
    options.setEnd(new LocalTime(1, 10, 11));
    List<String> cmd = options.getCommand();

    assertNotNull(cmd);
    assertEquals(5, cmd.size());
    assertTrue(cmd.contains(testFile.getAbsolutePath()));
    assertTrue(cmd.contains("qrs"));

    int fileIndex = cmd.indexOf(testFile.getAbsolutePath());
    int annotationIndex = cmd.indexOf("qrs");

    assertEquals("get_hrv", cmd.get(0));
    assertTrue(fileIndex == annotationIndex - 1);
    assertEquals("00:10:11", cmd.get(annotationIndex + 1));
    assertEquals("01:10:11", cmd.get(annotationIndex + 2));
  }

  public void testInvalidAnnotation() throws URISyntaxException {
    File testFile = getFile("/test.dat");
    testFile = new File(testFile.getParentFile(), "test");
    try {
      new HRVOptions(testFile, "");
    } catch (Exception e) {
      fail("should not throw an exception");
    }
  }

  public void testInvalidFile() throws Exception {
    File temporaryFile = File.createTempFile("tmpFile", "tmp");
    try {
      new HRVOptions(temporaryFile, "");
    } catch (Exception e) {
      fail("should not throw an exception");
    } finally {
      temporaryFile.delete();
    }
  }

  public void testNullAnnotation() throws URISyntaxException {
    File testFile = getFile("/test.dat");
    testFile = new File(testFile.getParentFile(), "test");
    try {
      new HRVOptions(testFile, null);
    } catch (Exception e) {
      fail("should not throw an exception");
    }
  }

  public void testNullFile() throws Exception {
    try {
      new HRVOptions((File) null, "");
    } catch (Exception e) {
      fail("should not throw an exception");
    }
  }

  public void testNullFileString() throws Exception {
    try {
      new HRVOptions((String) null, "");
    } catch (Exception e) {
      fail("should not throw an exception");
    }
  }

  public void testRRFile() {
    HRVOptions opts = new HRVOptions("rrfile.rr");

    List<String> cmd = opts.getCommand();
    assertEquals(3, cmd.size());
    assertEquals("-R", cmd.get(1));
    assertEquals("rrfile.rr", cmd.get(2));
  }

  public void testStartTime() {
    HRVOptions opts = new HRVOptions("/rrfile.rr");
    opts.setStart(10, 11);

    List<String> cmd = opts.getCommand();
    assertEquals(4, cmd.size());
    assertEquals("00:10:11", cmd.get(3));
  }

}

@@ -794,819 +794,8 @@
  %7D%0A%0A
-  public void testInstantation() throws Exception %7B%0A    File testFile = getFile(%22/test.dat%22);%0A    testFile = new File(testFile.getParentFile(), %22test%22);%0A%0A    HRVOptions options = new HRVOptions(testFile, %22qrs%22);%0A    options.setStart(10, 11);%0A    options.setEnd(new LocalTime(1, 10, 11));%0A    List%3CString%3E cmd = options.getCommand();%0A%0A    assertNotNull(cmd);%0A    assertEquals(5, cmd.size());%0A    assertTrue(cmd.contains(testFile.getAbsolutePath()));%0A    assertTrue(cmd.contains(%22qrs%22));%0A%0A    int fileIndex = cmd.indexOf(testFile.getAbsolutePath());%0A    int annotationIndex = cmd.indexOf(%22qrs%22);%0A%0A    assertEquals(%22get_hrv%22, cmd.get(0));%0A    assertTrue(fileIndex == annotationIndex - 1);%0A    assertEquals(%2200:10:11%22, cmd.get(annotationIndex + 1));%0A    assertEquals(%2201:10:11%22, cmd.get(annotationIndex + 2));%0A  %7D%0A%0A
   pu
@@ -2041,32 +2041,757 @@
 n%22);%0A    %7D%0A  %7D%0A%0A
+  public void testRecordFile() throws Exception %7B%0A    HRVOptions options = new HRVOptions(%22testFile%22, %22qrs%22);%0A%0A    List%3CString%3E cmd = options.getCommand();%0A    assertEquals(3, cmd.size());%0A    assertEquals(%22testFile%22, cmd.get(1));%0A    assertEquals(%22qrs%22, cmd.get(2));%0A  %7D%0A%0A  public void testRecordFileWithStartAndEndTime() throws Exception %7B%0A    HRVOptions options = new HRVOptions(%22testFile%22, %22qrs%22);%0A    options.setStart(10, 11);%0A    options.setEnd(new LocalTime(1, 10, 11));%0A%0A    List%3CString%3E cmd = options.getCommand();%0A    assertEquals(5, cmd.size());%0A    assertEquals(%22testFile%22, cmd.get(1));%0A    assertEquals(%22qrs%22, cmd.get(2));%0A    assertEquals(%2200:10:11%22, cmd.get(3));%0A    assertEquals(%2201:10:11%22, cmd.get(4));%0A  %7D%0A%0A
   public void te

add tests for record + annotator