package com.akiban.sql.aisddl;

import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.fail;
import org.junit.Test;
import org.junit.Before;

import com.akiban.ais.ddl.SchemaDef;
import com.akiban.ais.ddl.SchemaDefToAis;
import com.akiban.ais.model.AkibanInformationSchema;
import com.akiban.server.SchemaFactory;
import com.akiban.sql.parser.SQLParser;
import com.akiban.sql.parser.StatementNode;
import com.akiban.sql.parser.CreateSchemaNode;
import com.akiban.sql.parser.DropSchemaNode;
import com.akiban.sql.StandardException;


public class SchemaDDLIT {

    @Before
    public void before() throws Exception {
        parser = new SQLParser();
    }
    
    @Test
    public void createSchemaEmpty () throws Exception
    {
        String sql = "CREATE SCHEMA EMPTY";
        AkibanInformationSchema ais = new AkibanInformationSchema();
        
        StatementNode stmt = parser.parseStatement(sql);
        assertTrue (stmt instanceof CreateSchemaNode);
        
        SchemaDDL.createSchema(ais, null, (CreateSchemaNode)stmt);
    }
    
    @Test
    public void createSchemaUsed() throws Exception
    {
        String ddl = "create table s.t (c1 int primary key)";
        String sql = "CREATE SCHEMA S";
        AkibanInformationSchema ais = factory(ddl);
        
        assertNotNull(ais.getTable("s", "t"));
        
        StatementNode stmt = parser.parseStatement(sql);
        assertTrue (stmt instanceof CreateSchemaNode);
        
        try {
            SchemaDDL.createSchema(ais, null, (CreateSchemaNode)stmt);
            fail();
        } catch (StandardException ex) {
            ; // do nothing, exception expected. 
        }
    }
    
    @Test
    public void dropSchemaEmpty() throws Exception 
    {
        String sql = "DROP SCHEMA EMPTY RESTRICT";
        AkibanInformationSchema ais = new AkibanInformationSchema();
        
        StatementNode stmt = parser.parseStatement(sql);
        assertTrue (stmt instanceof DropSchemaNode);
        
        SchemaDDL.dropSchema(ais, null, (DropSchemaNode)stmt);
        
    }

    @Test
    public void dropSchemaUsed() throws Exception
    {
        String ddl = "create table s.t (c1 int primary key)";
        String sql = "DROP SCHEMA S RESTRICT";
        AkibanInformationSchema ais = factory(ddl);
        
        assertNotNull(ais.getTable("s", "t"));
        
        StatementNode stmt = parser.parseStatement(sql);
        assertTrue (stmt instanceof DropSchemaNode);
        
        try {
            SchemaDDL.dropSchema(ais, null, (DropSchemaNode)stmt);
            fail();
        } catch (StandardException ex) {
            ; // do nothing, exception expected. 
        }
        
    }
    protected SQLParser parser;

    private AkibanInformationSchema factory (String ddl) throws Exception
    {
        final SchemaDefToAis toAis = new SchemaDefToAis(
                SchemaDef.parseSchema(ddl), false);
        return toAis.getAis();
    }
}

@@ -326,38 +326,113 @@
 .model.A
-kibanInformationSchema
+ISBuilder;%0Aimport com.akiban.ais.model.AkibanInformationSchema;%0Aimport com.akiban.ais.model.Index
 ;%0Aimport
@@ -1276,70 +1276,8 @@
   %7B%0A
-        String ddl = %22create table s.t (c1 int primary key)%22;%0A
     
@@ -1350,35 +1350,32 @@
 a ais = factory(
-ddl
 );%0A        %0A    
@@ -2229,70 +2229,8 @@
   %7B%0A
-        String ddl = %22create table s.t (c1 int primary key)%22;%0A
     
@@ -2318,19 +2318,16 @@
 factory(
-ddl
 );%0A     
@@ -2804,18 +2804,8 @@
 ry (
-String ddl
 ) th
@@ -2837,136 +2837,408 @@
     
-final SchemaDefToAis toAis = new SchemaDefToAis(%0A                SchemaDef.parseSchema(ddl), false);%0A        return toAis.getAis
+AISBuilder builder = new AISBuilder();%0A        builder.userTable(%22s%22, %22t%22);%0A        builder.column (%22s%22, %22t%22, %22c1%22, 0, %22int%22, null, null, false, false, null, null);%0A        builder.index(%22s%22, %22t%22, %22PRIMARY%22, true, Index.PRIMARY_KEY_CONSTRAINT);%0A        builder.indexColumn(%22s%22, %22t%22, %22PRIMARY%22, %22c1%22, 0, true, 0);%0A        builder.basicSchemaIsComplete();%0A        return builder.akibanInformationSchema
 ();%0A

Replace use of SchemaDef with AISBuilder for building a test table.