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

import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.dbtest.cases.authority.Authority;

import javax.sql.DataSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * Authority environment manager.
 *
 * @author panjuan
 */
public final class AuthorityEnvironmentManager {
    
    private final Authority authority;
    
    private final DataSource dataSource;
    
    private final DatabaseType databaseType;
    
    public AuthorityEnvironmentManager(final String path, final DataSource dataSource, final DatabaseType databaseType) throws IOException, JAXBException {
        try (FileReader reader = new FileReader(path)) {
            authority = (Authority) JAXBContext.newInstance(Authority.class).createUnmarshaller().unmarshal(reader);
        }
        this.dataSource = dataSource;
        this.databaseType = databaseType;
    }
    
    /**
     * Initialize data.
     *
     */
    public void initialize() {
        Collection<String> initSQLs = authority.getInitSQLs(databaseType);
        if (initSQLs.isEmpty()) {
            return;
        }
        try (Connection connection = dataSource.getConnection()) {
            for (String each : initSQLs) {
                connection.createStatement().execute(each);
            }
        } catch (final SQLException ignored) {
        }
    }
    
    /**
     * Clean data.
     *
     */
    public void clean() {
        Collection<String> cleanSQLs = authority.getCleanSQLs(databaseType);
        if (cleanSQLs.isEmpty()) {
            return;
        }
        try (Connection connection = dataSource.getConnection()) {
            for (String each : cleanSQLs) {
                connection.createStatement().execute(each);
            }
        } catch (final SQLException ignored) {
        }
    }
}

@@ -658,24 +658,69 @@
 authority;%0A%0A
+import com.google.common.base.Preconditions;%0A
 import io.sh
@@ -810,32 +810,66 @@
 ority.Authority;
+%0Aimport lombok.extern.slf4j.Slf4j;
 %0A%0Aimport javax.s
@@ -1162,16 +1162,23 @@
 uan%0A */%0A
+@Slf4j%0A
 public f
@@ -1869,16 +1869,36 @@
 ialize()
+ throws SQLException
  %7B%0A     
@@ -2031,37 +2031,32 @@
       %7D%0A        
-try (
 Connection conne
@@ -2085,32 +2085,108 @@
 Connection()
-) %7B
+;
 %0A    
+    Preconditions.checkState(null != connection, %22No available connection.%22);%0A
         for 
@@ -2204,32 +2204,50 @@
 h : initSQLs) %7B%0A
+            try %7B%0A
                 
@@ -2299,34 +2299,24 @@
            %7D
-%0A        %7D
  catch (fina
@@ -2330,36 +2330,129 @@
 ception 
-ignored) %7B%0A        %7D
+ex) %7B%0A                log.warn(%22Init SQL: %22+ex.getMessage());%0A            %7D%0A        %7D%0A        connection.close();
 %0A    %7D%0A 
@@ -2520,16 +2520,36 @@
  clean()
+ throws SQLException
  %7B%0A     
@@ -2693,21 +2693,16 @@
         
-try (
 Connecti
@@ -2743,24 +2743,100 @@
 ection()
-) %7B
+;
 %0A    
+    Preconditions.checkState(null != connection, %22No available connection.%22);%0A
         
@@ -2859,32 +2859,50 @@
  : cleanSQLs) %7B%0A
+            try %7B%0A
                 
@@ -2958,26 +2958,16 @@
        %7D
-%0A        %7D
  catch (
@@ -2989,28 +2989,122 @@
 ion 
-ignored) %7B%0A        %7D
+ex) %7B%0A                log.warn(%22Clean SQL: %22+ex.getMessage());%0A            %7D%0A        %7D%0A        connection.close();
 %0A   

rewrite initialize() and clean() for not auto closing connection.