You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/02/22 15:54:52 UTC
[shardingsphere] branch master updated: Remove ProxyContextRestorer on front-core module (#24309)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 84da991aed4 Remove ProxyContextRestorer on front-core module (#24309)
84da991aed4 is described below
commit 84da991aed4f09766f16caf0fa35b6996ed20600
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Feb 22 23:54:44 2023 +0800
Remove ProxyContextRestorer on front-core module (#24309)
* Remove ProxyContextRestorer on CommandExecutorTaskTest
* Remove ProxyContextRestorer on ConnectionLimitContextTest
* Remove ProxyContextRestorer on FrontDatabaseProtocolTypeFactoryTest
* Remove ProxyContextRestorer on OKProxyStateTest
* Remove ProxyContextRestorer on front-core module
---
.../proxy/frontend/ProxyContextRestorer.java | 38 ------------
.../frontend/command/CommandExecutorTaskTest.java | 71 ++++++++++++++--------
.../connection/ConnectionLimitContextTest.java | 47 ++++++++------
.../FrontDatabaseProtocolTypeFactoryTest.java | 63 +++++++++----------
.../frontend/state/impl/OKProxyStateTest.java | 58 +++++++++++-------
5 files changed, 138 insertions(+), 139 deletions(-)
diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/ProxyContextRestorer.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/ProxyContextRestorer.java
deleted file mode 100644
index 4d3edb2a728..00000000000
--- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/ProxyContextRestorer.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.proxy.frontend;
-
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.junit.After;
-import org.junit.Before;
-
-public abstract class ProxyContextRestorer {
-
- private ContextManager currentContextManager;
-
- @Before
- public void recordCurrentContextManager() {
- currentContextManager = ProxyContext.getInstance().getContextManager();
- }
-
- @After
- public void restorePreviousContextManager() {
- ProxyContext.init(currentContextManager);
- }
-}
diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java
index 508fe3c975d..45c35bd27bb 100644
--- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java
+++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java
@@ -33,7 +33,6 @@ import org.apache.shardingsphere.proxy.backend.connector.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.BackendConnectionException;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import org.apache.shardingsphere.proxy.frontend.ProxyContextRestorer;
import org.apache.shardingsphere.proxy.frontend.command.executor.CommandExecutor;
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.context.FrontendContext;
@@ -43,6 +42,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
+import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnitRunner;
import java.nio.charset.StandardCharsets;
@@ -51,14 +51,16 @@ import java.util.Collections;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
-public final class CommandExecutorTaskTest extends ProxyContextRestorer {
+public final class CommandExecutorTaskTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private DatabaseProtocolFrontendEngine engine;
@@ -99,7 +101,6 @@ public final class CommandExecutorTaskTest extends ProxyContextRestorer {
@Before
public void setup() {
- ProxyContext.init(new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()), mock(InstanceContext.class)));
when(connectionSession.getBackendConnection()).thenReturn(backendConnection);
when(handlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get()).thenReturn(StandardCharsets.UTF_8);
}
@@ -112,9 +113,13 @@ public final class CommandExecutorTaskTest extends ProxyContextRestorer {
when(engine.getCommandExecuteEngine().getCommandPacketType(payload)).thenReturn(commandPacketType);
when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
CommandExecutorTask actual = new CommandExecutorTask(engine, connectionSession, handlerContext, message);
- actual.run();
- verify(queryCommandExecutor).close();
- verify(backendConnection).closeExecutionResources();
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(
+ new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()), mock(InstanceContext.class)));
+ actual.run();
+ verify(queryCommandExecutor).close();
+ verify(backendConnection).closeExecutionResources();
+ }
}
@SuppressWarnings("unchecked")
@@ -126,12 +131,16 @@ public final class CommandExecutorTaskTest extends ProxyContextRestorer {
when(engine.getCommandExecuteEngine().getCommandPacketType(payload)).thenReturn(commandPacketType);
when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
CommandExecutorTask actual = new CommandExecutorTask(engine, connectionSession, handlerContext, message);
- actual.run();
- verify(handlerContext).write(databasePacket);
- verify(handlerContext).flush();
- verify(engine.getCommandExecuteEngine()).writeQueryData(handlerContext, backendConnection, queryCommandExecutor, 1);
- verify(queryCommandExecutor).close();
- verify(backendConnection).closeExecutionResources();
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(
+ new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()), mock(InstanceContext.class)));
+ actual.run();
+ verify(handlerContext).write(databasePacket);
+ verify(handlerContext).flush();
+ verify(engine.getCommandExecuteEngine()).writeQueryData(handlerContext, backendConnection, queryCommandExecutor, 1);
+ verify(queryCommandExecutor).close();
+ verify(backendConnection).closeExecutionResources();
+ }
}
@SuppressWarnings("unchecked")
@@ -144,11 +153,15 @@ public final class CommandExecutorTaskTest extends ProxyContextRestorer {
when(engine.getCommandExecuteEngine().getCommandPacketType(payload)).thenReturn(commandPacketType);
when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
CommandExecutorTask actual = new CommandExecutorTask(engine, connectionSession, handlerContext, message);
- actual.run();
- verify(handlerContext).write(databasePacket);
- verify(handlerContext).flush();
- verify(commandExecutor).close();
- verify(backendConnection).closeExecutionResources();
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(
+ new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()), mock(InstanceContext.class)));
+ actual.run();
+ verify(handlerContext).write(databasePacket);
+ verify(handlerContext).flush();
+ verify(commandExecutor).close();
+ verify(backendConnection).closeExecutionResources();
+ }
}
@SuppressWarnings("unchecked")
@@ -163,10 +176,14 @@ public final class CommandExecutorTaskTest extends ProxyContextRestorer {
when(engine.getCommandExecuteEngine().getErrorPacket(mockException)).thenReturn(databasePacket);
when(engine.getCommandExecuteEngine().getOtherPacket(connectionSession)).thenReturn(Optional.of(databasePacket));
CommandExecutorTask actual = new CommandExecutorTask(engine, connectionSession, handlerContext, message);
- actual.run();
- verify(handlerContext, times(2)).write(databasePacket);
- verify(handlerContext).flush();
- verify(backendConnection).closeExecutionResources();
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(
+ new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()), mock(InstanceContext.class)));
+ actual.run();
+ verify(handlerContext, times(2)).write(databasePacket);
+ verify(handlerContext).flush();
+ verify(backendConnection).closeExecutionResources();
+ }
}
@Test
@@ -179,9 +196,13 @@ public final class CommandExecutorTaskTest extends ProxyContextRestorer {
when(engine.getCommandExecuteEngine().getErrorPacket(any(RuntimeException.class))).thenReturn(databasePacket);
when(engine.getCommandExecuteEngine().getOtherPacket(connectionSession)).thenReturn(Optional.of(databasePacket));
CommandExecutorTask actual = new CommandExecutorTask(engine, connectionSession, handlerContext, message);
- actual.run();
- verify(handlerContext, times(2)).write(databasePacket);
- verify(handlerContext).flush();
- verify(backendConnection).closeExecutionResources();
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(
+ new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()), mock(InstanceContext.class)));
+ actual.run();
+ verify(handlerContext, times(2)).write(databasePacket);
+ verify(handlerContext).flush();
+ verify(backendConnection).closeExecutionResources();
+ }
}
}
diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
index 8375e04da3a..9adb7ea7598 100644
--- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
+++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/connection/ConnectionLimitContextTest.java
@@ -20,40 +20,47 @@ package org.apache.shardingsphere.proxy.frontend.connection;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.frontend.ProxyContextRestorer;
-import org.junit.Before;
import org.junit.Test;
+import org.mockito.MockedStatic;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
-public final class ConnectionLimitContextTest extends ProxyContextRestorer {
-
- @Before
- public void setup() {
- ProxyContext.init(mock(ContextManager.class, RETURNS_DEEP_STUBS));
- }
+public final class ConnectionLimitContextTest {
@Test
public void assertConnectionsLimited() {
- when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_MAX_CONNECTIONS)).thenReturn(2);
- assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
- assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
- assertFalse(ConnectionLimitContext.getInstance().connectionAllowed());
- ConnectionLimitContext.getInstance().connectionInactive();
- ConnectionLimitContext.getInstance().connectionInactive();
- assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
- ConnectionLimitContext.getInstance().connectionInactive();
- ConnectionLimitContext.getInstance().connectionInactive();
- ConnectionLimitContext.getInstance().connectionInactive();
+ ContextManager contextManager = mockContextManager(2);
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
+ assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
+ assertFalse(ConnectionLimitContext.getInstance().connectionAllowed());
+ ConnectionLimitContext.getInstance().connectionInactive();
+ ConnectionLimitContext.getInstance().connectionInactive();
+ assertTrue(ConnectionLimitContext.getInstance().connectionAllowed());
+ ConnectionLimitContext.getInstance().connectionInactive();
+ ConnectionLimitContext.getInstance().connectionInactive();
+ ConnectionLimitContext.getInstance().connectionInactive();
+ }
}
@Test
public void assertConnectionsUnlimited() {
- when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_MAX_CONNECTIONS)).thenReturn(0);
- assertFalse(ConnectionLimitContext.getInstance().limitsMaxConnections());
+ ContextManager contextManager = mockContextManager(0);
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ assertFalse(ConnectionLimitContext.getInstance().limitsMaxConnections());
+ }
+ }
+
+ private static ContextManager mockContextManager(final int maxConnections) {
+ ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_MAX_CONNECTIONS)).thenReturn(maxConnections);
+ return result;
}
}
diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java
index 3de8c4f59e9..60dce9e224b 100644
--- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java
+++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java
@@ -30,71 +30,66 @@ import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.frontend.ProxyContextRestorer;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.Test;
+import org.mockito.MockedStatic;
import java.util.Collections;
-import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
-public final class FrontDatabaseProtocolTypeFactoryTest extends ProxyContextRestorer {
+public final class FrontDatabaseProtocolTypeFactoryTest {
@Test
public void assertGetDatabaseTypeWhenThrowShardingSphereConfigurationException() {
- MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class),
- new ShardingSphereMetaData(Collections.emptyMap(), mock(ShardingSphereRuleMetaData.class), new ConfigurationProperties(new Properties())));
- ProxyContext.init(new ContextManager(metaDataContexts, mock(InstanceContext.class)));
- assertTrue(metaDataContexts.getMetaData().getDatabases().isEmpty());
- assertThat(FrontDatabaseProtocolTypeFactory.getDatabaseType().getType(), is("MySQL"));
+ ContextManager contextManager = mockContextManager(Collections.emptyMap(), new Properties());
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ assertThat(FrontDatabaseProtocolTypeFactory.getDatabaseType().getType(), is("MySQL"));
+ }
}
@Test
public void assertGetDatabaseTypeInstanceOfMySQLDatabaseTypeFromMetaDataContextsSchemaName() {
- MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class),
- new ShardingSphereMetaData(mockDatabases(), mock(ShardingSphereRuleMetaData.class), new ConfigurationProperties(new Properties())));
- ProxyContext.init(new ContextManager(metaDataContexts, mock(InstanceContext.class)));
- assertFalse(metaDataContexts.getMetaData().getDatabases().isEmpty());
- String configuredDatabaseType = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
- assertTrue(configuredDatabaseType.isEmpty());
- assertTrue(metaDataContexts.getMetaData().containsDatabase(DefaultDatabase.LOGIC_NAME));
- DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
- assertThat(databaseType, instanceOf(DatabaseType.class));
- assertThat(databaseType.getType(), is("MySQL"));
+ ContextManager contextManager = mockContextManager(mockDatabases(), new Properties());
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ assertThat(databaseType, instanceOf(DatabaseType.class));
+ assertThat(databaseType.getType(), is("MySQL"));
+ }
}
@Test
public void assertGetDatabaseTypeOfPostgreSQLDatabaseTypeFromMetaDataContextsProps() {
Properties props = PropertiesBuilder.build(new Property(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "PostgreSQL"));
+ ContextManager contextManager = mockContextManager(mockDatabases(), props);
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ assertThat(databaseType, instanceOf(DatabaseType.class));
+ assertThat(databaseType.getType(), is("PostgreSQL"));
+ }
+ }
+
+ private static ContextManager mockContextManager(final Map<String, ShardingSphereDatabase> databases, final Properties props) {
MetaDataContexts metaDataContexts = new MetaDataContexts(
- mock(MetaDataPersistService.class), new ShardingSphereMetaData(mockDatabases(), mock(ShardingSphereRuleMetaData.class), new ConfigurationProperties(props)));
- ProxyContext.init(new ContextManager(metaDataContexts, mock(InstanceContext.class)));
- assertFalse(metaDataContexts.getMetaData().getDatabases().isEmpty());
- String configuredDatabaseType = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
- assertThat(configuredDatabaseType, is("PostgreSQL"));
- assertTrue(metaDataContexts.getMetaData().containsDatabase(DefaultDatabase.LOGIC_NAME));
- DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
- assertThat(databaseType, instanceOf(DatabaseType.class));
- assertThat(databaseType.getType(), is("PostgreSQL"));
- assertThat(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME).getProtocolType(), instanceOf(MySQLDatabaseType.class));
+ mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ShardingSphereRuleMetaData.class), new ConfigurationProperties(props)));
+ return new ContextManager(metaDataContexts, mock(InstanceContext.class));
}
private Map<String, ShardingSphereDatabase> mockDatabases() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(database.getProtocolType()).thenReturn(new MySQLDatabaseType());
- Map<String, ShardingSphereDatabase> result = new LinkedHashMap<>(1, 1);
- result.put(DefaultDatabase.LOGIC_NAME, database);
- return result;
+ return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database);
}
}
diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
index 53ca7f30f5a..7bee61f5927 100644
--- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
+++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/OKProxyStateTest.java
@@ -26,7 +26,6 @@ import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.connector.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import org.apache.shardingsphere.proxy.frontend.ProxyContextRestorer;
import org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask;
import org.apache.shardingsphere.proxy.frontend.executor.ConnectionThreadExecutorGroup;
import org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
@@ -36,6 +35,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
+import org.mockito.MockedStatic;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.MockitoJUnitRunner;
@@ -45,11 +45,12 @@ import java.util.concurrent.ExecutorService;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
-public final class OKProxyStateTest extends ProxyContextRestorer {
+public final class OKProxyStateTest {
@Mock
private ChannelHandlerContext context;
@@ -64,49 +65,62 @@ public final class OKProxyStateTest extends ProxyContextRestorer {
public void setup() {
when(connectionSession.getConnectionId()).thenReturn(1);
when(connectionSession.getBackendConnection()).thenReturn(mock(BackendConnection.class));
- ProxyContext.init(mock(ContextManager.class, RETURNS_DEEP_STUBS));
}
@Test
public void assertExecuteWithProxyHintEnabled() {
- when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(true);
+ ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(contextManager.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(true);
ExecutorService executorService = registerMockExecutorService(1);
- new OKProxyState().execute(context, null, frontendEngine, connectionSession);
- verify(executorService).execute(any(CommandExecutorTask.class));
- ConnectionThreadExecutorGroup.getInstance().unregisterAndAwaitTermination(1);
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ new OKProxyState().execute(context, null, frontendEngine, connectionSession);
+ verify(executorService).execute(any(CommandExecutorTask.class));
+ ConnectionThreadExecutorGroup.getInstance().unregisterAndAwaitTermination(1);
+ }
}
@Test
public void assertExecuteWithDistributedTransaction() {
- when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
+ ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(contextManager.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
when(connectionSession.getTransactionStatus().getTransactionType()).thenReturn(TransactionType.XA);
ExecutorService executorService = registerMockExecutorService(1);
- new OKProxyState().execute(context, null, frontendEngine, connectionSession);
- verify(executorService).execute(any(CommandExecutorTask.class));
- ConnectionThreadExecutorGroup.getInstance().unregisterAndAwaitTermination(1);
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ new OKProxyState().execute(context, null, frontendEngine, connectionSession);
+ verify(executorService).execute(any(CommandExecutorTask.class));
+ ConnectionThreadExecutorGroup.getInstance().unregisterAndAwaitTermination(1);
+ }
}
@Test
public void assertExecuteWithProxyBackendExecutorSuitableForOLTP() {
- when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
- when(ProxyContext.getInstance().getContextManager()
- .getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLTP);
+ ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(contextManager.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
+ when(contextManager.getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLTP);
EventExecutor eventExecutor = mock(EventExecutor.class);
when(context.executor()).thenReturn(eventExecutor);
- new OKProxyState().execute(context, null, frontendEngine, connectionSession);
- verify(eventExecutor).execute(any(CommandExecutorTask.class));
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ new OKProxyState().execute(context, null, frontendEngine, connectionSession);
+ verify(eventExecutor).execute(any(CommandExecutorTask.class));
+ }
}
@Test
public void assertExecuteWithProxyBackendExecutorSuitableForOLAPAndRequiredSameThreadForConnection() {
- when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
- when(ProxyContext.getInstance().getContextManager()
- .getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLAP);
+ ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(contextManager.getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
+ when(contextManager.getMetaDataContexts().getMetaData().getProps().<BackendExecutorType>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn(BackendExecutorType.OLAP);
when(frontendEngine.getFrontendContext().isRequiredSameThreadForConnection(null)).thenReturn(true);
ExecutorService executorService = registerMockExecutorService(1);
- new OKProxyState().execute(context, null, frontendEngine, connectionSession);
- verify(executorService).execute(any(CommandExecutorTask.class));
- ConnectionThreadExecutorGroup.getInstance().unregisterAndAwaitTermination(1);
+ try (MockedStatic<ProxyContext> proxyContext = mockStatic(ProxyContext.class, RETURNS_DEEP_STUBS)) {
+ proxyContext.when(() -> ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+ new OKProxyState().execute(context, null, frontendEngine, connectionSession);
+ verify(executorService).execute(any(CommandExecutorTask.class));
+ ConnectionThreadExecutorGroup.getInstance().unregisterAndAwaitTermination(1);
+ }
}
@SuppressWarnings({"unchecked", "SameParameterValue"})