/**
 * Copyright (C) 2011 Akiban Technologies Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

package com.akiban.sql.parser;

import com.akiban.sql.TestBase;

import com.akiban.sql.parser.SQLParser;
import com.akiban.sql.parser.StatementNode;

import org.junit.Before;
import org.junit.Test;
import static junit.framework.Assert.*;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;

@RunWith(Parameterized.class)
public class SQLParserTest extends TestBase implements TestBase.GenerateAndCheckResult
{
    public static final File RESOURCE_DIR = 
        new File("src/test/resources/"
                 + SQLParserTest.class.getPackage().getName().replace('.', '/'));

    protected SQLParser parser;

    @Before
    public void before() throws Exception {
        parser = new SQLParser();
    }

    protected String getTree(StatementNode stmt) throws IOException {
        StringWriter str = new StringWriter();
        stmt.treePrint(str);
        return str.toString().trim();
    }

    @Parameters
    public static Collection<Object[]> queries() throws Exception {
        return sqlAndExpected(RESOURCE_DIR);
    }

    public SQLParserTest(String caseName, String sql, String expected, String error) {
        super(caseName, sql, expected, error);
    }

    @Test
    public void testParser() throws Exception {
        generateAndCheckResult();
    }

    @Override
    public String generateResult() throws Exception {
        StatementNode stmt = parser.parseStatement(sql);
        return getTree(stmt);
    }

    @Override
    public void checkResult(String result) throws IOException {
        assertEqualsWithoutHashes(caseName, expected, result);
    }

}

@@ -1032,69 +1032,9 @@
 .io.
-File;%0Aimport java.io.IOException;%0Aimport java.io.StringWriter
+*
 ;%0Aim
@@ -1048,26 +1048,17 @@
 va.util.
-Collection
+*
 ;%0A%0A@RunW
@@ -1369,16 +1369,49 @@
  parser;
+%0A    protected File featuresFile;
 %0A%0A    @B
@@ -1502,329 +1502,1930 @@
     
-%7D%0A%0A    protected String getTree(StatementNode stmt) throws IOException %7B%0A        StringWriter str = new StringWriter();%0A        stmt.treePrint(str);%0A        return str.toString().trim();%0A    %7D%0A%0A    @Parameters%0A    public static Collection%3CObject%5B%5D%3E queries() throws Exception %7B%0A        return sqlAndExpected(RESOURCE_DIR)
+    if (featuresFile != null)%0A            parseFeatures(featuresFile, parser.getFeatures());%0A    %7D%0A%0A    protected void parseFeatures(File file, Set%3CSQLParserFeature%3E features) %0A            throws IOException %7B%0A        BufferedReader reader = new BufferedReader(new FileReader(file));%0A        try %7B%0A            while (true) %7B%0A                String line = reader.readLine();%0A                if (line == null) break;%0A                boolean add;%0A                switch (line.charAt(0)) %7B%0A                case '+':%0A                    add = true;%0A                    break;%0A                case '-':%0A                    add = false;%0A                    break;%0A                default:%0A                    throw new IOException(%22Malformed features line: should start with + or - %22 + line);%0A                %7D%0A                SQLParserFeature feature = SQLParserFeature.valueOf(line.substring(1));%0A                if (add)%0A                    features.add(feature);%0A                else%0A                    features.remove(feature);%0A            %7D%0A        %7D%0A        finally %7B%0A            reader.close();%0A        %7D%0A    %7D%0A%0A    protected String getTree(StatementNode stmt) throws IOException %7B%0A        StringWriter str = new StringWriter();%0A        stmt.treePrint(str);%0A        return str.toString().trim();%0A    %7D%0A%0A    @Parameters%0A    public static Collection%3CObject%5B%5D%3E queries() throws Exception %7B%0A        Collection%3CObject%5B%5D%3E result = new ArrayList%3CObject%5B%5D%3E();%0A        for (Object%5B%5D args : sqlAndExpected(RESOURCE_DIR)) %7B%0A            File featuresFile = new File(RESOURCE_DIR, args%5B0%5D + %22.features%22);%0A            if (!featuresFile.exists())%0A                featuresFile = null;%0A            Object%5B%5D nargs = new Object%5Bargs.length+1%5D;%0A            nargs%5B0%5D = args%5B0%5D;%0A            nargs%5B1%5D = featuresFile;%0A            System.arraycopy(args, 1, nargs, 2, args.length-1);%0A            result.add(nargs);%0A        %7D%0A        return result
 ;%0A  
@@ -3470,16 +3470,60 @@
 aseName,
+ File featuresFile,%0A                        
  String 
@@ -3603,24 +3603,66 @@
 ed, error);%0A
+        this.featuresFile = featuresFile;%0A
     %7D%0A%0A    @

Allow a parser test to have a set of parser feature adjustments.