/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.querydsl.sql.codegen;

import java.sql.SQLException;
import java.util.Set;

import javax.tools.JavaCompiler;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.mysema.codegen.SimpleCompiler;
import com.querydsl.codegen.BeanSerializer;
import com.querydsl.sql.AbstractJDBCTest;

public class MetaDataSerializerTest extends AbstractJDBCTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Override
    @Before
    public void setUp() throws SQLException, ClassNotFoundException {
        super.setUp();
        statement.execute("drop table employee if exists");
        statement.execute("drop table survey if exists");
        statement.execute("drop table date_test if exists");
        statement.execute("drop table date_time_test if exists");

        // survey
        statement.execute("create table survey (id int, name varchar(30), "
                + "CONSTRAINT PK_survey PRIMARY KEY (id, name))");

        // date_test
        statement.execute("create table date_test (d date)");

        // date_time
        statement.execute("create table date_time_test (dt datetime)");

        // spaces
        statement.execute("create table spaces (\"spaces  \n 1\" date)");

        // employee
        statement.execute("create table employee("
                + "id INT, "
                + "firstname VARCHAR(50), "
                + "lastname VARCHAR(50), "
                + "salary DECIMAL(10, 2), "
                + "datefield DATE, "
                + "timefield TIME, "
                + "superior_id int, "
                + "survey_id int, "
                + "\"123abc\" int,"
                + "survey_name varchar(30), "
                + "CONSTRAINT PK_employee PRIMARY KEY (id), "
                + "CONSTRAINT FK_survey FOREIGN KEY (survey_id, survey_name) REFERENCES survey(id,name), "
                + "CONSTRAINT FK_superior FOREIGN KEY (superior_id) REFERENCES employee(id))");
    }

    @Test
    public void normal_serialization() throws SQLException {
        String namePrefix = "Q";
        NamingStrategy namingStrategy = new DefaultNamingStrategy();
        // customization of serialization
        MetaDataExporter exporter = new MetaDataExporter();
        exporter.setBeanSerializerClass(BeanSerializer.class);
        exporter.setNamePrefix(namePrefix);
        exporter.setPackageName("test");
        exporter.setTargetFolder(folder.getRoot());
        exporter.setNamingStrategy(namingStrategy);
        exporter.export(connection.getMetaData());

        compile(exporter);
    }

    private void compile(MetaDataExporter exporter) {
        JavaCompiler compiler = new SimpleCompiler();
        Set<String> classes = exporter.getClasses();
        int compilationResult = compiler.run(null, null, null, classes.toArray(new String[classes.size()]));
        if (compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            Assert.fail("Compilation Failed");
        }
    }

}

@@ -651,16 +651,78 @@
 degen;%0A%0A
+import java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0A
 import j
@@ -743,16 +743,16 @@
 eption;%0A
-
 import j
@@ -1064,16 +1064,137 @@
 DBCTest;
+%0Aimport com.querydsl.sql.Configuration;%0Aimport com.querydsl.sql.SQLTemplates;%0Aimport com.querydsl.sql.types.AbstractType;
 %0A%0Apublic
@@ -1249,16 +1249,57 @@
 BCTest %7B
+%0A    public static class CustomNumber %7B %7D
 %0A%0A    @R
@@ -1725,16 +1725,74 @@
 xists%22);
+%0A        statement.execute(%22drop table spaces if exists%22);
 %0A%0A      
@@ -3422,24 +3422,24 @@
 getRoot());%0A
-
         expo
@@ -3560,24 +3560,1377 @@
 er);%0A    %7D%0A%0A
+    @Test%0A    public void customized_serialization() throws SQLException %7B%0A        String namePrefix = %22Q%22;%0A        Configuration conf = new Configuration(SQLTemplates.DEFAULT);%0A        conf.register(%22EMPLOYEE%22, %22ID%22, new AbstractType%3CCustomNumber%3E(0) %7B%0A            @Override%0A            public Class%3CCustomNumber%3E getReturnedClass() %7B%0A                return CustomNumber.class;%0A            %7D%0A%0A            @Override%0A            public CustomNumber getValue(ResultSet rs, int startIndex) throws SQLException %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A%0A            @Override%0A            public void setValue(PreparedStatement st, int startIndex, CustomNumber value) throws SQLException %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A        %7D);%0A        NamingStrategy namingStrategy = new DefaultNamingStrategy();%0A        // customization of serialization%0A        MetaDataExporter exporter = new MetaDataExporter();%0A        exporter.setBeanSerializerClass(BeanSerializer.class);%0A        exporter.setNamePrefix(namePrefix);%0A        exporter.setPackageName(%22test%22);%0A        exporter.setTargetFolder(folder.getRoot());%0A        exporter.setNamingStrategy(namingStrategy);%0A        exporter.setConfiguration(conf);%0A        exporter.export(connection.getMetaData());%0A%0A        compile(exporter);%0A    %7D%0A%0A
     private 

Add test