package novoda.lib.sqliteprovider.util;

import android.test.AndroidTestCase;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RoboRunner.class)
public class SQLFileTest extends AndroidTestCase {

    @Test
    public void testReadMultiLineCommand() throws IOException {
        String firstLine = "CREATE TABLE 'testTable'";
        String secondLine = "_id INTEGER PRIMARY KEY AUTOINCREMENT;";
        String singleLineCommand = firstLine + " " + secondLine;
        String multiLineCommand = firstLine + "\n" + secondLine;
        List<String> statementsFromMultiline = SQLFile.statementsFrom(new StringReader(multiLineCommand));
        assertEquals(singleLineCommand, statementsFromMultiline.get(0));
    }

    @Test
    public void testSkippingTrailingComments() throws IOException {
        String rawLine = "CREATE TABLE 'testTable'";
        String comment = "-- adding some comment that should get stripped off";
        String nextLine = "_id INTEGER PRIMARY KEY AUTOINCREMENT;";
        String lineCommand = rawLine + comment + "\n" + nextLine;
        List<String> statements = SQLFile.statementsFrom(new StringReader(lineCommand));
        assertEquals(rawLine+ " "+nextLine, statements.get(0));
    }

    @Test
    public void testMultiLineEqualsSingleLineStatements() throws IOException {
        List<String> oneLiners = readStatements("one_line_statements.sql");
        List<String> multiLiners = readStatements("multi_line_statements.sql");
        assertStatementsAreEqual(oneLiners, multiLiners);
    }

    @Test
    public void testMultiLineEqualsSingleLineCount() throws IOException {
        List<String> oneLiners = readStatements("one_line_statements.sql");
        List<String> multiLiners = readStatements("multi_line_statements.sql");
        assertEquals(oneLiners.size(), multiLiners.size());
    }

    @Test
    public void testIncompleteLastStatementDetection() throws IOException {
        try {
            readStatements("missing_last_semicolon.sql");
            fail("incomplete last statement should throw an IOException.");
        } catch (IOException e) {
            assertNotNull(e);
        }
    }

    private void assertStatementsAreEqual(List<String> oneLiners, List<String> multiLiners) {
        for (int index = 0; index < oneLiners.size(); index++) {
            assertEquals(oneLiners.get(index), multiLiners.get(index));
        }
    }

    private List<String> readStatements(String fileName) throws IOException {
        return SQLFile.statementsFrom(getSqlFileReader(fileName));
    }

    private InputStreamReader getSqlFileReader(String fileName) {
        return new InputStreamReader(getClass().getResourceAsStream("/assets/sql/" + fileName));
    }
}

@@ -1305,14 +1305,17 @@
 Line
+ 
 + %22 %22
-+
+ + 
 next
@@ -1965,33 +1965,63 @@
     %7D%0A%0A    @Test
+(expected = IOException.class)
 %0A
-
     public void 
@@ -2084,26 +2084,8 @@
 n %7B%0A
-        try %7B%0A    
     
@@ -2138,158 +2138,8 @@
 %22);%0A
-            fail(%22incomplete last statement should throw an IOException.%22);%0A        %7D catch (IOException e) %7B%0A            assertNotNull(e);%0A        %7D%0A
     

use annotation to declared expected exception