sandynz commented on code in PR #25920: URL: https://github.com/apache/shardingsphere/pull/25920#discussion_r1209661694
########## jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.context; + +import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ExtendWith(MockitoExtension.class) +class JDBCContextTest { + + public static final String TEST_DB = "test_db"; + + @Mock + private DataSourceChangedEvent event; Review Comment: If `event` is just used one time, could we embed in method? ########## jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.context; + +import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ExtendWith(MockitoExtension.class) +class JDBCContextTest { + + public static final String TEST_DB = "test_db"; + + @Mock + private DataSourceChangedEvent event; + + @Test + void assertNullCachedDbMetadataWithEmptyDatasource() throws Exception { + JDBCContext actual = new JDBCContext(new HashMap<>()); + assertNull(actual.getCachedDatabaseMetaData()); + } + + @Test + void assertNotNullCashedDbMetadataWith() throws SQLException { + Map<String, DataSource> dataSourceMap = getStringDataSourceMap(); + JDBCContext jdbcContext = new JDBCContext(dataSourceMap); + assertThat(jdbcContext.getCachedDatabaseMetaData(), notNullValue()); + } + + @Test + void assetNullMetadataAfterRefreshingExisting() throws SQLException { + Map<String, DataSource> stringDataSourceMap = getStringDataSourceMap(); + JDBCContext jdbcContext = new JDBCContext(stringDataSourceMap); + jdbcContext.refreshCachedDatabaseMetaData(event); + assertThat(jdbcContext.getCachedDatabaseMetaData(), nullValue()); Review Comment: assertThat and nullValue could be replaced with assertNull ########## jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.context; + +import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ExtendWith(MockitoExtension.class) +class JDBCContextTest { + + public static final String TEST_DB = "test_db"; + + @Mock + private DataSourceChangedEvent event; + + @Test + void assertNullCachedDbMetadataWithEmptyDatasource() throws Exception { + JDBCContext actual = new JDBCContext(new HashMap<>()); + assertNull(actual.getCachedDatabaseMetaData()); + } + + @Test + void assertNotNullCashedDbMetadataWith() throws SQLException { + Map<String, DataSource> dataSourceMap = getStringDataSourceMap(); + JDBCContext jdbcContext = new JDBCContext(dataSourceMap); + assertThat(jdbcContext.getCachedDatabaseMetaData(), notNullValue()); Review Comment: assertThat and notNullValue() could be replaced with assertNotNull ########## jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.context; + +import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ExtendWith(MockitoExtension.class) +class JDBCContextTest { + + public static final String TEST_DB = "test_db"; + + @Mock + private DataSourceChangedEvent event; + + @Test + void assertNullCachedDbMetadataWithEmptyDatasource() throws Exception { + JDBCContext actual = new JDBCContext(new HashMap<>()); + assertNull(actual.getCachedDatabaseMetaData()); + } + + @Test + void assertNotNullCashedDbMetadataWith() throws SQLException { + Map<String, DataSource> dataSourceMap = getStringDataSourceMap(); + JDBCContext jdbcContext = new JDBCContext(dataSourceMap); + assertThat(jdbcContext.getCachedDatabaseMetaData(), notNullValue()); + } + + @Test + void assetNullMetadataAfterRefreshingExisting() throws SQLException { + Map<String, DataSource> stringDataSourceMap = getStringDataSourceMap(); + JDBCContext jdbcContext = new JDBCContext(stringDataSourceMap); + jdbcContext.refreshCachedDatabaseMetaData(event); + assertThat(jdbcContext.getCachedDatabaseMetaData(), nullValue()); + } + + private static Map<String, DataSource> getStringDataSourceMap() { + CircuitBreakerDataSource dataSource = new CircuitBreakerDataSource(); + Map<String, DataSource> result = new HashMap<>(); + result.put(TEST_DB, dataSource); Review Comment: Could we embed `new CircuitBreakerDataSource()` in `put(key, value)` to reduce code lines? ########## jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.context; + +import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ExtendWith(MockitoExtension.class) +class JDBCContextTest { + + public static final String TEST_DB = "test_db"; Review Comment: Looks it's used one time, could we embed it in method? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
