/* This file is part of VoltDB.
 * Copyright (C) 2008-2014 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.voltdb.canonicalddl;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLDecoder;

import org.junit.Test;
import org.voltdb.AdhocDDLTestBase;
import org.voltdb.VoltDB;
import org.voltdb.VoltDB.Configuration;
import org.voltdb.compiler.VoltCompiler;
import org.voltdb.compiler.VoltProjectBuilder;
import org.voltdb.utils.MiscUtils;

public class TestCanonicalDDLThroughSQLcmd extends AdhocDDLTestBase
{
    String firstCanonicalDDL = null;
    String secondCanonicalDDL = null;

    public String getFirstCanonicalDDL() throws Exception
    {
        String pathToCatalog = Configuration.getPathToCatalogForTest("fullDDL.jar");

        VoltCompiler compiler = new VoltCompiler();
        final URL url = TestCanonicalDDLThroughSQLcmd.class.getResource("fullDDL.sql");
        String pathToSchema = URLDecoder.decode(url.getPath(), "UTF-8");
        boolean success = compiler.compileFromDDL(pathToCatalog, pathToSchema);
        assertTrue(success);
        return compiler.getCanonicalDDL();
    }

    public String getSecondCanonicalDDL() throws Exception
    {
        String pathToCatalog = Configuration.getPathToCatalogForTest("emptyDDL.jar");
        String pathToDeployment = Configuration.getPathToCatalogForTest("emptyDDL.xml");

        VoltCompiler compiler = new VoltCompiler();
        VoltProjectBuilder builder = new VoltProjectBuilder();

        final URL url = TestCanonicalDDLThroughSQLcmd.class.getResource("emptyDDL.sql");
        String pathToSchema = URLDecoder.decode(url.getPath(), "UTF-8");
        builder.setUseAdhocSchema(true);
        boolean success = compiler.compileFromDDL(pathToCatalog, pathToSchema);
        assertTrue(success);
System.out.println(pathToCatalog);
System.out.println(pathToSchema);
System.out.println(success);
        success = builder.compile(pathToCatalog);
        assertTrue(success);
        MiscUtils.copyFile(builder.getPathToDeployment(), pathToDeployment);

        VoltDB.Configuration config = new VoltDB.Configuration();
        config.m_pathToCatalog = pathToCatalog;
        config.m_pathToDeployment = pathToDeployment;

        startSystem(config);

        m_client.callProcedure("@AdHoc", firstCanonicalDDL);

        teardownSystem();
        return compiler.getCanonicalDDL();
    }

    @Test
    public void testCanonicalDDLRoundtrip() throws Exception {

        FileOutputStream fos1 = new FileOutputStream(new File("/home/yhe/1.txt"));
        firstCanonicalDDL = getFirstCanonicalDDL();
        fos1.write(firstCanonicalDDL.getBytes());

        FileOutputStream fos2 = new FileOutputStream(new File("/home/yhe/2.txt"));
        secondCanonicalDDL = getSecondCanonicalDDL();
        fos2.write(secondCanonicalDDL.getBytes());

        assertEquals(firstCanonicalDDL, secondCanonicalDDL);
    }

}

@@ -2691,32 +2691,60 @@
 ath(), %22UTF-8%22);
+%0A%0A        boolean success;%0A%0A
 %0A        builder
@@ -2770,38 +2770,77 @@
 true);%0A        b
-oolean
+uilder.addLiteralSchema(%22--nothing%22);%0A       
  success = compi
@@ -2826,38 +2826,37 @@
       success = 
-comp
+bu
 il
+d
 er.compileFromDD
@@ -2841,39 +2841,32 @@
  builder.compile
-FromDDL
 (pathToCatalog, 
@@ -2859,38 +2859,24 @@
 athToCatalog
-, pathToSchema
 );%0A        a
@@ -2899,105 +2899,88 @@
 s);%0A
-System.out.println(pathToCatalog);%0ASystem.out.println(pathToSchema);%0ASystem.out.println(success);
+        MiscUtils.copyFile(builder.getPathToDeployment(), pathToDeployment);%0A%0A%0A%0A
 %0A   
@@ -2986,37 +2986,38 @@
       success = 
-bu
+comp
 il
-d
 er.compile(pathT
@@ -3002,32 +3002,39 @@
 compiler.compile
+FromDDL
 (pathToCatalog);
@@ -3023,32 +3023,46 @@
 DL(pathToCatalog
+, pathToSchema
 );%0A        asser
@@ -3081,84 +3081,11 @@
 s);%0A
-        MiscUtils.copyFile(builder.getPathToDeployment(), pathToDeployment);
+%0A%0A%0A
 %0A%0A  

fix VoltProjectBuilder and VoltCompiler comflict