diff --git a/sharding-proxy/sharding-proxy-backend/src/test/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java b/sharding-proxy/sharding-proxy-backend/src/test/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1d4b057289571ce73ad3821201fa7c8f9c5de244 --- /dev/null +++ b/sharding-proxy/sharding-proxy-backend/src/test/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java @@ -0,0 +1,49 @@ +/* + * 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.shardingproxy.backend.communication.jdbc; + +import org.apache.shardingsphere.shardingproxy.backend.communication.DatabaseCommunicationEngine; +import org.apache.shardingsphere.shardingproxy.backend.communication.DatabaseCommunicationEngineFactory; +import org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.connection.BackendConnection; +import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchema; +import org.junit.Test; + +import java.util.Collections; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public final class DatabaseCommunicationEngineFactoryTest { + + @Test + public void assertNewTextProtocolInstance() { + DatabaseCommunicationEngine engine = DatabaseCommunicationEngineFactory.getInstance().newTextProtocolInstance(mock(LogicSchema.class), "schemaName", mock(BackendConnection.class)); + assertNotNull(engine); + assertThat(engine, instanceOf(JDBCDatabaseCommunicationEngine.class)); + } + + @Test + public void assertNewBinaryProtocolInstance() { + DatabaseCommunicationEngine engine = DatabaseCommunicationEngineFactory.getInstance() + .newBinaryProtocolInstance(mock(LogicSchema.class), "schemaName", Collections.emptyList(), mock(BackendConnection.class)); + assertNotNull(engine); + assertThat(engine, instanceOf(JDBCDatabaseCommunicationEngine.class)); + } +} diff --git a/sharding-proxy/sharding-proxy-backend/src/test/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/callback/ProxyJDBCExecutePrepareCallbackTest.java b/sharding-proxy/sharding-proxy-backend/src/test/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/callback/ProxyJDBCExecutePrepareCallbackTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d17050bd0b5e8312b574f0ef6ab9b336596b7c87 --- /dev/null +++ b/sharding-proxy/sharding-proxy-backend/src/test/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/callback/ProxyJDBCExecutePrepareCallbackTest.java @@ -0,0 +1,74 @@ +/* + * 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.shardingproxy.backend.communication.jdbc.execute.callback; + +import org.apache.shardingsphere.core.constant.ConnectionMode; +import org.apache.shardingsphere.core.execute.StatementExecuteUnit; +import org.apache.shardingsphere.core.route.RouteUnit; +import org.apache.shardingsphere.core.route.SQLUnit; +import org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.connection.BackendConnection; +import org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.wrapper.JDBCExecutorWrapper; +import org.junit.Test; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class ProxyJDBCExecutePrepareCallbackTest { + + @Test + public void assertGetConnections() throws SQLException { + BackendConnection backendConnection = mock(BackendConnection.class); + List connections = Collections.emptyList(); + when(backendConnection.getConnections((ConnectionMode) any(), anyString(), anyInt())).thenReturn(connections); + ProxyJDBCExecutePrepareCallback proxyJDBCExecutePrepareCallback = new ProxyJDBCExecutePrepareCallback(backendConnection, mock(JDBCExecutorWrapper.class), false); + assertThat(proxyJDBCExecutePrepareCallback.getConnections(null, null, 1), is(connections)); + } + + @Test + public void assertCreateStatementExecuteUnitWhenNotMemoryStrictly() throws SQLException { + JDBCExecutorWrapper jdbcExecutorWrapper = mock(JDBCExecutorWrapper.class); + when(jdbcExecutorWrapper.createStatement((Connection) any(), (SQLUnit) any(), anyBoolean())).thenReturn(mock(Statement.class)); + ProxyJDBCExecutePrepareCallback proxyJDBCExecutePrepareCallback = new ProxyJDBCExecutePrepareCallback(mock(BackendConnection.class), jdbcExecutorWrapper, false); + RouteUnit routeUnit = mock(RouteUnit.class); + when(routeUnit.getSqlUnit()).thenReturn(mock(SQLUnit.class)); + assertThat(proxyJDBCExecutePrepareCallback.createStatementExecuteUnit(null, routeUnit, ConnectionMode.CONNECTION_STRICTLY), instanceOf(StatementExecuteUnit.class)); + } + + @Test + public void assertCreateStatementExecuteUnitWhenMemoryStrictly() throws SQLException { + JDBCExecutorWrapper jdbcExecutorWrapper = mock(JDBCExecutorWrapper.class); + when(jdbcExecutorWrapper.createStatement((Connection) any(), (SQLUnit) any(), anyBoolean())).thenReturn(mock(Statement.class)); + ProxyJDBCExecutePrepareCallback proxyJDBCExecutePrepareCallback = new ProxyJDBCExecutePrepareCallback(mock(BackendConnection.class), jdbcExecutorWrapper, false); + RouteUnit routeUnit = mock(RouteUnit.class); + when(routeUnit.getSqlUnit()).thenReturn(mock(SQLUnit.class)); + assertThat(proxyJDBCExecutePrepareCallback.createStatementExecuteUnit(null, routeUnit, ConnectionMode.MEMORY_STRICTLY), instanceOf(StatementExecuteUnit.class)); + } +}