/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.dbtest.engine.dcl;

import com.google.common.base.Strings;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.dbtest.cases.assertion.dcl.DCLIntegrateTestCaseAssertion;
import io.shardingsphere.dbtest.engine.SingleIntegrateTest;
import io.shardingsphere.dbtest.env.DatabaseTypeEnvironment;
import io.shardingsphere.dbtest.env.EnvironmentPath;
import io.shardingsphere.dbtest.env.dataset.DataSetEnvironmentManager;
import io.shardingsphere.test.sql.SQLCaseType;
import org.junit.Before;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Collection;
import java.util.LinkedList;

public abstract class BaseDCLIntegrateTest extends SingleIntegrateTest {
    
    private final DCLIntegrateTestCaseAssertion assertion;
    
    public BaseDCLIntegrateTest(final String sqlCaseId, final String path, final DCLIntegrateTestCaseAssertion assertion, final String shardingRuleType,
                                final DatabaseTypeEnvironment databaseTypeEnvironment, final SQLCaseType caseType) throws IOException, JAXBException, SQLException, ParseException {
        super(sqlCaseId, path, assertion, shardingRuleType, databaseTypeEnvironment, caseType);
        this.assertion = assertion;
    }
    
    @Before
    public void insertData() throws SQLException, ParseException, IOException, JAXBException {
        if (getDatabaseTypeEnvironment().isEnabled()) {
            new DataSetEnvironmentManager(EnvironmentPath.getDataInitializeResourceFile(getShardingRuleType()), getDataSourceMap()).initialize();
        }
    }
    
    protected void cleanEnvironment(final Connection connection) {
        if (!Strings.isNullOrEmpty(assertion.getCleanSQLs())) {
            try (Statement statement = connection.createStatement()) {
                for (String each : assertion.getCleanSQLs().split(";")) {
                    statement.execute(each);
                }
                // CHECKSTYLE: OFF
            } catch (final SQLException ignored) {
                // CHECKSTYLE: ON
            }
        }
    }
    
    protected void initEnvironment(final Connection connection) {
        if (!Strings.isNullOrEmpty(assertion.getInitSQLs())) {
            try (Statement statement = connection.createStatement()) {
                for (String each : assertion.getInitSQLs().split(";")) {
                    statement.execute(each);
                }
                // CHECKSTYLE: OFF
            } catch (final SQLException ignored) {
                // CHECKSTYLE: ON
            }
        }
    }
    
    protected boolean isExecuted() {
        if (Strings.isNullOrEmpty(assertion.getDbTypes())) {
            return true;
        }
        Collection<DatabaseType> databaseTypeList = new LinkedList<>();
        for (String each : assertion.getDbTypes().split(",")) {
            databaseTypeList.add(DatabaseType.valueOf(each));
        }
        return databaseTypeList.contains(getDatabaseTypeEnvironment().getDatabaseType());
    }
}

@@ -663,100 +663,8 @@
 l;%0A%0A
-import com.google.common.base.Strings;%0Aimport io.shardingsphere.core.constant.DatabaseType;%0A
 impo
@@ -1034,16 +1034,40 @@
 seType;%0A
+import org.junit.After;%0A
 import o
@@ -1153,36 +1153,8 @@
 on;%0A
-import java.sql.Connection;%0A
 impo
@@ -1183,35 +1183,8 @@
 on;%0A
-import java.sql.Statement;%0A
 impo
@@ -1215,66 +1215,8 @@
 ion;
-%0Aimport java.util.Collection;%0Aimport java.util.LinkedList;
 %0A%0Apu
@@ -1295,72 +1295,8 @@
    %0A
-    private final DCLIntegrateTestCaseAssertion assertion;%0A    %0A
     
@@ -1725,44 +1725,8 @@
 e);%0A
-        this.assertion = assertion;%0A
     
@@ -2059,532 +2059,24 @@
     
-%7D%0A
     
-%0A    protected void cleanEnvironment(final Connection connection) %7B%0A        if (!Strings.isNullOrEmpty(assertion.getCleanSQLs())) %7B%0A            try (Statement statement = connection.createStatement()) %7B%0A                for (String each : assertion.getCleanSQLs().split(%22;%22)) %7B%0A                    statement.execute(each);%0A                %7D%0A                // CHECKSTYLE: OFF%0A            %7D catch (final SQLException ignored) %7B%0A                // CHECKSTYLE: ON%0A            %7D%0A        %7D%0A    %7D%0A    %0A    protected void in
+getAuthor
 it
+y
 Envi
@@ -2086,805 +2086,83 @@
 ment
-(final Connection connection) %7B%0A        if (!Strings.isNullOrEmpty(assertion.getInitSQLs())) %7B%0A            try (Statement statement = connection.createStatement()) %7B%0A                for (String each : assertion.getInitSQLs().split(%22;%22)) %7B%0A                    statement.execute(each);%0A                %7D%0A                // CHECKSTYLE: OFF%0A            %7D catch (final SQLException ignored) %7B%0A                // CHECKSTYLE: ON%0A            %7D%0A        %7D%0A    %7D%0A    %0A    protected boolean isExecuted() %7B%0A        if (Strings.isNullOrEmpty(assertion.getDbTypes())) %7B%0A            return true;%0A        %7D%0A        Collection%3CDatabaseType%3E databaseTypeList = new LinkedList%3C%3E();%0A        for (String each : assertion.getDbTypes().split(%22,%22)) %7B%0A            databaseTypeList.add(DatabaseType.valueOf(each));%0A        %7D
+Manager().initialize();%0A    %7D%0A    %0A    @After%0A    public void cleanData() %7B
 %0A   
@@ -2170,87 +2170,47 @@
     
-return databaseTypeList.contains(getDatabaseTypeEnvironment().getDatabaseType()
+getAuthorityEnvironmentManager().clean(
 );%0A 

USE getAuthorityEnvironmentManager