/*
 * 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.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;

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 dropUserIfExisted(final Connection connection) {
        String name = !Strings.isNullOrEmpty(assertion.getNewName()) ? assertion.getUser() : assertion.getNewName();
        String dropSQL = "drop " + assertion.getType() + " " + name;
        if (!Strings.isNullOrEmpty(assertion.getHost())) {
            dropSQL = dropSQL + ";";
        } else {
            dropSQL = dropSQL + "@" + assertion.getHost() + ";";
        }
        try (Statement statement = connection.createStatement()) {
            statement.execute(dropSQL);
            // CHECKSTYLE: OFF
        } catch (final SQLException ignored) {
            // CHECKSTYLE: ON
        }
    }
}

@@ -2255,25 +2255,24 @@
 oid 
-dropUserIfExisted
+cleanEnvironment
 (fin
@@ -2311,22 +2311,12 @@
     
-String name = 
+if (
 !Str
@@ -2351,324 +2351,441 @@
 .get
-NewName()) ? assertion.getUser() : assertion.getNewName();%0A        String dropSQL = %22drop %22 + assertion.getType() + %22 %22 + name;%0A        if (!Strings.isNullOrEmpty(assertion.getHost())) %7B%0A            dropSQL = dropSQL + %22;%22;%0A        %7D else %7B
+CleanSQL())) %7B%0A            try (Statement statement = connection.createStatement()) %7B%0A                statement.execute(assertion.getCleanSQL());%0A                // CHECKSTYLE: OFF%0A            %7D catch (final SQLException ignored) %7B%0A                // CHECKSTYLE: ON%0A            %7D%0A        %7D
 %0A    
+%7D%0A
     
+%0A
     
-dropSQL = dropSQL + %22@%22 + assertion.getHost() + %22;%22;%0A   
+protected void initEnvironment(final Connection connection) %7B%0A        if (!Strings.isNullOrEmpty(assertion.getInitSQL())) %7B%0A
     
- %7D%0A
     
@@ -2851,32 +2851,36 @@
 ) %7B%0A            
+    
 statement.execut
@@ -2885,15 +2885,30 @@
 ute(
-drop
+assertion.getInit
 SQL
+()
 );%0A 
@@ -2910,32 +2910,36 @@
 ));%0A            
+   
+ 
 // CHECKSTYLE: O
@@ -2933,32 +2933,36 @@
 CHECKSTYLE: OFF%0A
+    
         %7D catch 
@@ -2996,32 +2996,36 @@
 ) %7B%0A            
+   
+ 
 // CHECKSTYLE: O
@@ -3018,32 +3018,46 @@
  CHECKSTYLE: ON%0A
+            %7D%0A
         %7D%0A    %7D%0A

add cleanEnvironment initEnvironment