package com.conveyal.gtfs;

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

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class GTFSTest {
    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

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

    @Test
    public void canPrintHelp() throws Exception {
        String[] args = {"-help"};
        GTFS.main(args);
        assertThat(outContent.toString(), containsString("usage: java"));
    }

    @Test
    public void handlesUnknownArgs() throws Exception {
        String[] args = {"-blah"};
        GTFS.main(args);
        assertThat(outContent.toString(), containsString("usage: java"));
    }

    @Test
    public void requiresActionCommand() throws Exception {
        String[] args = {"-u", "blah"};
        GTFS.main(args);
        assertThat(outContent.toString(), containsString("usage: java"));
    }

    /**
     * Tests whether or not a super simple 2-stop, 1-route, 1-trip, valid gtfs can be loaded
     */
    @Test
    public void canLoadSimpleAgency() {
        runSimpleIntegrationTest("fake-agency.zip");
    }

    /**
     * Run GTFS.main with a certain zip file.
     *
     * @param zipFileName
     */
    private void runSimpleIntegrationTest(String zipFileName) {
        String newDBName = TestUtils.generateNewDB();
        try {
            String[] args = {
                "-load",
                TestUtils.getResourceFileName(zipFileName),
                "-d", "jdbc:postgresql://localhost/" + newDBName,
                "-u", "gtfs_test",
                "-p", "gtfs_test",
                "-validate"
            };
            GTFS.main(args);
        } finally {
            TestUtils.dropDB(newDBName);
        }
    }
}

@@ -243,16 +243,59 @@
 tring;%0A%0A
+/**%0A * A test suite for the GTFS Class%0A */%0A
 public c
@@ -393,16 +393,77 @@
 eam();%0A%0A
+    // setup a stream to capture the output from the program%0A
     @Bef
@@ -550,32 +550,123 @@
 ntent));%0A    %7D%0A%0A
+    /**%0A     * Make sure that help can be printed.%0A     *%0A     * @throws Exception%0A     */%0A
     @Test%0A    pu
@@ -842,32 +842,161 @@
 java%22));%0A    %7D%0A%0A
+    /**%0A     * Make sure that help is printed if no recognizable arguments are provided.%0A     *%0A     * @throws Exception%0A     */%0A
     @Test%0A    pu
@@ -1182,24 +1182,152 @@
 %22));%0A    %7D%0A%0A
+    /**%0A     * Make sure that help is printed if not enough key arguments are provided.%0A     *%0A     * @throws Exception%0A     */%0A
     @Test%0A  
@@ -1763,17 +1763,43 @@
 %0A     * 
-R
+A helper method that will r
 un GTFS.
@@ -1828,16 +1828,92 @@
 p file.%0A
+     * This tests whether a GTFS zip file can be loaded without any errors.%0A
      *%0A 

Add more comments to the GTFSTest class