/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.shardingsphere.driver.jdbc.adapter;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.junit.Before;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class AbstractDataSourceAdapterTest {

    private ShardingSphereDataSource shardingSphereDataSource;

    @Before
    public void setUp() throws SQLException {
        shardingSphereDataSource = new ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, getDataSource(), getRuleConfigurations(), new Properties());
    }

    private Map<String, DataSource> getDataSource() throws SQLException {
        DataSource dataSource = mock(DataSource.class);
        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
        when(dataSource.getConnection()).thenReturn(connection);
        when(connection.getMetaData()).thenReturn(mock(DatabaseMetaData.class, RETURNS_DEEP_STUBS));
        when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/test");
        return Collections.singletonMap("ds", dataSource);
    }

    private List<RuleConfiguration> getRuleConfigurations() {
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        ShardingTableRuleConfiguration shardingTableRuleConfig = new ShardingTableRuleConfiguration("table", "ds" + "." + "table");
        shardingRuleConfig.setTables(Collections.singletonList(shardingTableRuleConfig));
        return Collections.singletonList(shardingRuleConfig);
    }

    @Test
    public void assertSetLogWriter() {
        assertThat(shardingSphereDataSource.getLogWriter(), instanceOf(PrintWriter.class));
        shardingSphereDataSource.setLogWriter(null);
        assertNull(shardingSphereDataSource.getLogWriter());
    }

    @Test
    public void assertGetParentLogger() {
        assertThat(shardingSphereDataSource.getParentLogger().getName(), is(Logger.GLOBAL_LOGGER_NAME));
    }

    @Test
    public void assertGetConnectionWithUsername() {
        assertThat(shardingSphereDataSource.getConnection("username", "password"), instanceOf(ShardingSphereConnection.class));
    }
}

@@ -855,95 +855,9 @@
 er;%0A
-import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
 %0A
+
 impo
@@ -1281,24 +1281,25 @@
 junit.Test;%0A
+%0A
 import javax
@@ -1347,36 +1347,8 @@
 er;%0A
-import java.sql.Connection;%0A
 impo
@@ -1403,24 +1403,53 @@
 LException;%0A
+import java.util.Collection;%0A
 import java.
@@ -1470,31 +1470,8 @@
 ns;%0A
-import java.util.List;%0A
 impo
@@ -1546,24 +1546,25 @@
 ing.Logger;%0A
+%0A
 import stati
@@ -1919,17 +1919,21 @@
 rTest %7B%0A
+    
 %0A
-
     priv
@@ -1987,17 +1987,21 @@
 Source;%0A
+    
 %0A
-
     @Bef
@@ -2205,33 +2205,37 @@
 rties());%0A    %7D%0A
+    
 %0A
-
     private Map%3C
@@ -2349,64 +2349,8 @@
 lass
-);%0A        Connection connection = mock(Connection.class
 , RE
@@ -2411,57 +2411,8 @@
 on()
-).thenReturn(connection);%0A        when(connection
 .get
@@ -2658,24 +2658,28 @@
 rce);%0A    %7D%0A
+    
 %0A    private
@@ -2679,20 +2679,26 @@
 private 
-List
+Collection
 %3CRuleCon
@@ -3073,36 +3073,32 @@
 ctions.singleton
-List
 (shardingRuleCon
@@ -3101,33 +3101,37 @@
 eConfig);%0A    %7D%0A
+    
 %0A
-
     @Test%0A    pu
@@ -3367,33 +3367,37 @@
 riter());%0A    %7D%0A
+    
 %0A
-
     @Test%0A    pu
@@ -3428,24 +3428,24 @@
 tLogger() %7B%0A
+
         asse
@@ -3546,204 +3546,7 @@
    %7D
-%0A%0A    @Test%0A    public void assertGetConnectionWithUsername() %7B%0A        assertThat(shardingSphereDataSource.getConnection(%22username%22, %22password%22), instanceOf(ShardingSphereConnection.class));%0A    %7D
 %0A%7D%0A

Revise #12914 (#12918)