-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE #9415]update setExternalStorage logic,support more db check,ad…
…d 8 count unit test (#9470) * [impr]update setExternalStorage method logic,support more db check,add 8 count unit test method * update comment desc
- Loading branch information
1 parent
898cbb1
commit 2168395
Showing
5 changed files
with
278 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
config/src/test/java/com/alibaba/nacos/config/server/utils/ClusterExternalStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.config.server.utils; | ||
|
||
import com.alibaba.nacos.config.server.constant.PropertiesConstant; | ||
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; | ||
import com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceServiceImpl; | ||
import com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl; | ||
import com.alibaba.nacos.sys.env.Constants; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
/** | ||
* ClusterExternalStorage unit test. | ||
* | ||
* @author Long Yu | ||
* @since 2.2.0 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class ClusterExternalStorageTest { | ||
|
||
@InjectMocks | ||
private DynamicDataSource dataSource; | ||
|
||
private MockEnvironment environment; | ||
|
||
@Mock | ||
private LocalDataSourceServiceImpl localDataSourceService; | ||
|
||
@Mock | ||
private ExternalDataSourceServiceImpl basicDataSourceService; | ||
|
||
PropertyUtil propertyUtil = new PropertyUtil(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
environment = new MockEnvironment(); | ||
EnvUtil.setEnvironment(environment); | ||
dataSource = DynamicDataSource.getInstance(); | ||
ReflectionTestUtils.setField(dataSource, "localDataSourceService", localDataSourceService); | ||
ReflectionTestUtils.setField(dataSource, "basicDataSourceService", basicDataSourceService); | ||
|
||
} | ||
|
||
@Test | ||
public void test005WithClusterAndNullDatabase() { | ||
// 模拟设置环境05:指定集群,未指定数据库,UseExternalDB是true,数据库类型是mysql | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, ""); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
PropertyUtil.setEmbeddedStorage(EnvUtil.getStandaloneMode()); | ||
|
||
// 模拟初始化 | ||
propertyUtil.initialize(null); | ||
|
||
Assert.assertFalse(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(PropertyUtil.isUseExternalDB()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl); | ||
} | ||
|
||
@Test | ||
public void test006WithClusterAndMysqlDatabase() { | ||
// 模拟设置环境06:指定集群,指定数据库mysql,UseExternalDB是true,数据库类型是mysql | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "mysql"); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
PropertyUtil.setEmbeddedStorage(EnvUtil.getStandaloneMode()); | ||
|
||
// 模拟初始化 | ||
propertyUtil.initialize(null); | ||
|
||
Assert.assertFalse(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(PropertyUtil.isUseExternalDB()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl); | ||
} | ||
|
||
@Test | ||
public void test007WithClusterAndDerbyDatabase() { | ||
// 模拟设置环境07:指定集群,指定数据库derby,UseExternalDB是false,数据库类型是derby | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "derby"); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
PropertyUtil.setEmbeddedStorage(true); | ||
|
||
// 模拟初始化 | ||
propertyUtil.initialize(null); | ||
|
||
Assert.assertFalse(EnvUtil.getStandaloneMode()); | ||
Assert.assertFalse(PropertyUtil.isUseExternalDB()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl); | ||
} | ||
|
||
@Test | ||
public void test008WithClusterAndOtherDatabase() { | ||
// 模拟设置环境08: 指定集群,指定数据库其他,UseExternalDB是true,数据库类型是其他 | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "postgresql"); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
PropertyUtil.setEmbeddedStorage(EnvUtil.getStandaloneMode()); | ||
|
||
// 模拟初始化 | ||
propertyUtil.initialize(null); | ||
|
||
Assert.assertFalse(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(PropertyUtil.isUseExternalDB()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl); | ||
} | ||
|
||
} |
129 changes: 129 additions & 0 deletions
129
...ig/src/test/java/com/alibaba/nacos/config/server/utils/StandaloneExternalStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.config.server.utils; | ||
|
||
import com.alibaba.nacos.config.server.constant.PropertiesConstant; | ||
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; | ||
import com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceServiceImpl; | ||
import com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl; | ||
import com.alibaba.nacos.sys.env.Constants; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
/** | ||
* StandaloneExternalStorage unit test. | ||
* | ||
* @author Long Yu | ||
* @since 2.2.0 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class StandaloneExternalStorageTest { | ||
|
||
@InjectMocks | ||
private DynamicDataSource dataSource; | ||
|
||
private MockEnvironment environment; | ||
|
||
@Mock | ||
private LocalDataSourceServiceImpl localDataSourceService; | ||
|
||
@Mock | ||
private ExternalDataSourceServiceImpl basicDataSourceService; | ||
|
||
PropertyUtil propertyUtil = new PropertyUtil(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
environment = new MockEnvironment(); | ||
EnvUtil.setEnvironment(environment); | ||
dataSource = DynamicDataSource.getInstance(); | ||
ReflectionTestUtils.setField(dataSource, "localDataSourceService", localDataSourceService); | ||
ReflectionTestUtils.setField(dataSource, "basicDataSourceService", basicDataSourceService); | ||
} | ||
|
||
@Test | ||
public void test001WithStandaloneAndNullDatabase() { | ||
// 模拟设置环境01:指定单例,未指定数据库,UseExternalDB是false | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, ""); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
|
||
// 模拟初始化 | ||
propertyUtil.initialize(null); | ||
|
||
Assert.assertTrue(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl); | ||
Assert.assertFalse(PropertyUtil.isUseExternalDB()); | ||
} | ||
|
||
@Test | ||
public void test002WithStandaloneAndDerbyDatabase() { | ||
// 模拟设置环境02:指定单例,指定数据库derby,UseExternalDB是false | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "derby"); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
// 模拟初始化 | ||
|
||
propertyUtil.initialize(null); | ||
|
||
Assert.assertTrue(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl); | ||
Assert.assertFalse(PropertyUtil.isUseExternalDB()); | ||
} | ||
|
||
@Test | ||
public void test003WithStandaloneAndMysqlDatabase() { | ||
// 模拟设置环境03:指定单例,指定数据库为mysql, UseExternalDB是true | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "mysql"); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
// 模拟初始化 | ||
|
||
propertyUtil.initialize(null); | ||
|
||
Assert.assertTrue(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl); | ||
Assert.assertTrue(PropertyUtil.isUseExternalDB()); | ||
} | ||
|
||
@Test | ||
public void test004WithStandaloneAndOtherDatabase() { | ||
// 模拟设置环境04:指定单例,指定数据库为其他, UseExternalDB是true | ||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true"); | ||
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "postgresql"); | ||
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME)); | ||
// 模拟初始化 | ||
|
||
propertyUtil.initialize(null); | ||
|
||
Assert.assertTrue(EnvUtil.getStandaloneMode()); | ||
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl); | ||
Assert.assertTrue(PropertyUtil.isUseExternalDB()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters