----初始化项目
This commit is contained in:
49
sys/src/test/java/com/alibaba/nacos/sys/env/EnvModuleStateBuilderTest.java
vendored
Normal file
49
sys/src/test/java/com/alibaba/nacos/sys/env/EnvModuleStateBuilderTest.java
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.sys.env;
|
||||
|
||||
import com.alibaba.nacos.common.utils.VersionUtils;
|
||||
import com.alibaba.nacos.sys.module.ModuleState;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class EnvModuleStateBuilderTest {
|
||||
|
||||
private static ConfigurableEnvironment environment;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
environment = new MockEnvironment();
|
||||
EnvUtil.setEnvironment(environment);
|
||||
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");
|
||||
EnvUtil.setIsStandalone(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuild() {
|
||||
ModuleState actual = new EnvModuleStateBuilder().build();
|
||||
assertEquals(Constants.SYS_MODULE, actual.getModuleName());
|
||||
assertEquals(EnvUtil.STANDALONE_MODE_ALONE, actual.getStates().get(Constants.STARTUP_MODE_STATE));
|
||||
assertNull(EnvUtil.FUNCTION_MODE_NAMING, actual.getStates().get(Constants.FUNCTION_MODE_STATE));
|
||||
assertEquals(VersionUtils.version, actual.getStates().get(Constants.NACOS_VERSION));
|
||||
}
|
||||
}
|
||||
63
sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithConfigTest.java
vendored
Normal file
63
sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithConfigTest.java
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 1999-2020 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.sys.env;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ActiveProfiles("test")
|
||||
@SpringBootTest(classes = EnvUtilWithConfigTest.class)
|
||||
public class EnvUtilWithConfigTest {
|
||||
|
||||
private static final int SETTING_PROCESSORS = 10;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
EnvUtil.setEnvironment((ConfigurableEnvironment) environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAvailableProcessors() {
|
||||
int actual = EnvUtil.getAvailableProcessors();
|
||||
assertEquals(SETTING_PROCESSORS, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAvailableProcessorsWithMultiple() {
|
||||
int actual = EnvUtil.getAvailableProcessors(2);
|
||||
assertEquals(SETTING_PROCESSORS * 2, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAvailableProcessorsWithScale() {
|
||||
int actual = EnvUtil.getAvailableProcessors(0.5);
|
||||
assertEquals((int) (SETTING_PROCESSORS * 0.5), actual);
|
||||
}
|
||||
}
|
||||
65
sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithoutConfigTest.java
vendored
Normal file
65
sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithoutConfigTest.java
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 1999-2020 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.sys.env;
|
||||
|
||||
import com.alibaba.nacos.common.utils.ThreadUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ActiveProfiles("empty")
|
||||
@SpringBootTest(classes = EnvUtilWithConfigTest.class)
|
||||
public class EnvUtilWithoutConfigTest {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
EnvUtil.setEnvironment((ConfigurableEnvironment) environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAvailableProcessors() {
|
||||
int expected = ThreadUtils.getSuitableThreadCount(1);
|
||||
int actual = EnvUtil.getAvailableProcessors();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAvailableProcessorsWithMultiple() {
|
||||
int expected = ThreadUtils.getSuitableThreadCount(2);
|
||||
int actual = EnvUtil.getAvailableProcessors(2);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAvailableProcessorsWithScale() {
|
||||
int expected = ThreadUtils.getSuitableThreadCount(1);
|
||||
int actual = EnvUtil.getAvailableProcessors(0.5);
|
||||
assertEquals((int) (expected * 0.5), actual);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.sys.module;
|
||||
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class ModuleStateHolderTest {
|
||||
|
||||
private Map<String, ModuleState> moduleStateMap;
|
||||
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
environment = new MockEnvironment();
|
||||
EnvUtil.setEnvironment(environment);
|
||||
moduleStateMap = (Map<String, ModuleState>) ReflectionTestUtils
|
||||
.getField(ModuleStateHolder.getInstance(), ModuleStateHolder.class, "moduleStates");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetModuleState() {
|
||||
assertNotNull(ModuleStateHolder.getInstance().getModuleState("mock"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllModuleStates() {
|
||||
assertEquals(2, ModuleStateHolder.getInstance().getAllModuleStates().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetStateValueByNameFound() {
|
||||
assertEquals("test", ModuleStateHolder.getInstance().getStateValueByName("mock", "test"));
|
||||
assertEquals("test", ModuleStateHolder.getInstance().getStateValueByName("mock", "test", "aaa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetStateValueByNameWithoutModuleState() {
|
||||
assertEquals("", ModuleStateHolder.getInstance().getStateValueByName("non-exist", "test"));
|
||||
assertEquals("aaa", ModuleStateHolder.getInstance().getStateValueByName("non-exist", "test", "aaa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetStateValueByNameWithoutStateName() {
|
||||
assertEquals("", ModuleStateHolder.getInstance().getStateValueByName("mock", "non-exist"));
|
||||
assertEquals("aaa", ModuleStateHolder.getInstance().getStateValueByName("mock", "non-exist", "aaa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchStateValue() {
|
||||
assertEquals("test", ModuleStateHolder.getInstance().searchStateValue("test", "aaa"));
|
||||
assertEquals("aaa", ModuleStateHolder.getInstance().searchStateValue("non-exist", "aaa"));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.sys.module.mock;
|
||||
|
||||
import com.alibaba.nacos.sys.module.ModuleState;
|
||||
import com.alibaba.nacos.sys.module.ModuleStateBuilder;
|
||||
|
||||
public class ExceptionMockModuleStateBuilder implements ModuleStateBuilder {
|
||||
|
||||
@Override
|
||||
public ModuleState build() {
|
||||
throw new RuntimeException("test");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.sys.module.mock;
|
||||
|
||||
import com.alibaba.nacos.sys.module.ModuleState;
|
||||
import com.alibaba.nacos.sys.module.ModuleStateBuilder;
|
||||
|
||||
public class MockModuleStateBuilder implements ModuleStateBuilder {
|
||||
|
||||
@Override
|
||||
public ModuleState build() {
|
||||
ModuleState result = new ModuleState("mock");
|
||||
result.newState("test", "test").newState("mock", true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
242
sys/src/test/java/com/alibaba/nacos/sys/utils/DiskUtilsTest.java
Normal file
242
sys/src/test/java/com/alibaba/nacos/sys/utils/DiskUtilsTest.java
Normal file
@ -0,0 +1,242 @@
|
||||
/*
|
||||
* 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.sys.utils;
|
||||
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DiskUtilsTest {
|
||||
|
||||
private static File testFile;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws IOException {
|
||||
testFile = DiskUtils.createTmpFile("nacostmp", ".ut");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() throws IOException {
|
||||
testFile.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTouch() throws IOException {
|
||||
File file = Paths.get(EnvUtil.getNacosTmpDir(), "touch.ut").toFile();
|
||||
Assert.assertFalse(file.exists());
|
||||
DiskUtils.touch(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
file.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTouchWithFileName() throws IOException {
|
||||
File file = Paths.get(EnvUtil.getNacosTmpDir(), UUID.randomUUID().toString()).toFile();
|
||||
Assert.assertFalse(file.exists());
|
||||
DiskUtils.touch(file.getParent(), file.getName());
|
||||
Assert.assertTrue(file.exists());
|
||||
file.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTmpFile() throws IOException {
|
||||
File tmpFile = null;
|
||||
try {
|
||||
tmpFile = DiskUtils.createTmpFile("nacos1", ".ut");
|
||||
Assert.assertTrue(tmpFile.getName().startsWith("nacos1"));
|
||||
Assert.assertTrue(tmpFile.getName().endsWith(".ut"));
|
||||
} finally {
|
||||
if (tmpFile != null) {
|
||||
tmpFile.deleteOnExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTmpFileWithPath() throws IOException {
|
||||
File tmpFile = null;
|
||||
try {
|
||||
tmpFile = DiskUtils.createTmpFile(EnvUtil.getNacosTmpDir(), "nacos1", ".ut");
|
||||
Assert.assertEquals(EnvUtil.getNacosTmpDir(), tmpFile.getParent());
|
||||
Assert.assertTrue(tmpFile.getName().startsWith("nacos1"));
|
||||
Assert.assertTrue(tmpFile.getName().endsWith(".ut"));
|
||||
} finally {
|
||||
if (tmpFile != null) {
|
||||
tmpFile.deleteOnExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFile() {
|
||||
Assert.assertNotNull(DiskUtils.readFile(testFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFileWithInputStream() throws FileNotFoundException {
|
||||
Assert.assertNotNull(DiskUtils.readFile(new FileInputStream(testFile)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFileWithPath() {
|
||||
Assert.assertNotNull(DiskUtils.readFile(testFile.getParent(), testFile.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFileBytes() {
|
||||
Assert.assertNotNull(DiskUtils.readFileBytes(testFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFileBytesWithPath() {
|
||||
Assert.assertNotNull(DiskUtils.readFileBytes(testFile.getParent(), testFile.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeFile() {
|
||||
Assert.assertTrue(DiskUtils.writeFile(testFile, "unit test".getBytes(StandardCharsets.UTF_8), false));
|
||||
Assert.assertEquals("unit test", DiskUtils.readFile(testFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteQuietly() throws IOException {
|
||||
File tmpFile = DiskUtils.createTmpFile(UUID.randomUUID().toString(), ".ut");
|
||||
DiskUtils.deleteQuietly(tmpFile);
|
||||
Assert.assertFalse(tmpFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteQuietlyWithPath() throws IOException {
|
||||
String dir = EnvUtil.getNacosTmpDir() + "/" + "diskutils";
|
||||
DiskUtils.forceMkdir(dir);
|
||||
DiskUtils.createTmpFile(dir, "nacos", ".ut");
|
||||
Path path = Paths.get(dir);
|
||||
DiskUtils.deleteQuietly(path);
|
||||
|
||||
Assert.assertFalse(path.toFile().exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFile() throws IOException {
|
||||
File tmpFile = DiskUtils.createTmpFile(UUID.randomUUID().toString(), ".ut");
|
||||
Assert.assertTrue(DiskUtils.deleteFile(tmpFile.getParent(), tmpFile.getName()));
|
||||
Assert.assertFalse(DiskUtils.deleteFile(tmpFile.getParent(), tmpFile.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteDirectory() throws IOException {
|
||||
Path diskutils = Paths.get(EnvUtil.getNacosTmpDir(), "diskutils");
|
||||
File file = diskutils.toFile();
|
||||
if (!file.exists()) {
|
||||
file.mkdir();
|
||||
}
|
||||
|
||||
Assert.assertTrue(file.exists());
|
||||
DiskUtils.deleteDirectory(diskutils.toString());
|
||||
Assert.assertFalse(file.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceMkdir() throws IOException {
|
||||
File dir = Paths.get(EnvUtil.getNacosTmpDir(), UUID.randomUUID().toString(), UUID.randomUUID().toString())
|
||||
.toFile();
|
||||
DiskUtils.forceMkdir(dir);
|
||||
Assert.assertTrue(dir.exists());
|
||||
dir.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceMkdirWithPath() throws IOException {
|
||||
Path path = Paths.get(EnvUtil.getNacosTmpDir(), UUID.randomUUID().toString(), UUID.randomUUID().toString());
|
||||
DiskUtils.forceMkdir(path.toString());
|
||||
File file = path.toFile();
|
||||
Assert.assertTrue(file.exists());
|
||||
file.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteDirThenMkdir() throws IOException {
|
||||
Path path = Paths.get(EnvUtil.getNacosTmpDir(), UUID.randomUUID().toString());
|
||||
DiskUtils.forceMkdir(path.toString());
|
||||
|
||||
DiskUtils.createTmpFile(path.toString(), UUID.randomUUID().toString(), ".ut");
|
||||
DiskUtils.createTmpFile(path.toString(), UUID.randomUUID().toString(), ".ut");
|
||||
|
||||
DiskUtils.deleteDirThenMkdir(path.toString());
|
||||
|
||||
File file = path.toFile();
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isDirectory());
|
||||
Assert.assertTrue(file.list() == null || file.list().length == 0);
|
||||
|
||||
file.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyDirectory() throws IOException {
|
||||
Path srcPath = Paths.get(EnvUtil.getNacosTmpDir(), UUID.randomUUID().toString());
|
||||
DiskUtils.forceMkdir(srcPath.toString());
|
||||
File nacos = DiskUtils.createTmpFile(srcPath.toString(), "nacos", ".ut");
|
||||
|
||||
Path destPath = Paths.get(EnvUtil.getNacosTmpDir(), UUID.randomUUID().toString());
|
||||
DiskUtils.copyDirectory(srcPath.toFile(), destPath.toFile());
|
||||
|
||||
File file = Paths.get(destPath.toString(), nacos.getName()).toFile();
|
||||
Assert.assertTrue(file.exists());
|
||||
|
||||
DiskUtils.deleteDirectory(srcPath.toString());
|
||||
DiskUtils.deleteDirectory(destPath.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyFile() throws IOException {
|
||||
File nacos = DiskUtils.createTmpFile("nacos", ".ut");
|
||||
DiskUtils.copyFile(testFile, nacos);
|
||||
|
||||
Assert.assertEquals(DiskUtils.readFile(testFile), DiskUtils.readFile(nacos));
|
||||
|
||||
nacos.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void openFile() {
|
||||
File file = DiskUtils.openFile(testFile.getParent(), testFile.getName());
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertEquals(testFile.getPath(), file.getPath());
|
||||
Assert.assertEquals(testFile.getName(), file.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenFileWithPath() {
|
||||
File file = DiskUtils.openFile(testFile.getParent(), testFile.getName(), false);
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertEquals(testFile.getPath(), file.getPath());
|
||||
Assert.assertEquals(testFile.getName(), file.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.sys.utils;
|
||||
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import com.alibaba.nacos.sys.env.Constants;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.nacos.sys.env.Constants.NACOS_SERVER_IP;
|
||||
|
||||
public class InetUtilsTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
EnvUtil.setEnvironment(new MockEnvironment());
|
||||
System.setProperty(NACOS_SERVER_IP, "1.1.1.1");
|
||||
System.setProperty(Constants.AUTO_REFRESH_TIME, "100");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshIp() throws InterruptedException {
|
||||
Assert.assertEquals("1.1.1.1", InetUtils.getSelfIP());
|
||||
|
||||
System.setProperty(NACOS_SERVER_IP, "1.1.1.2");
|
||||
TimeUnit.MILLISECONDS.sleep(300L);
|
||||
|
||||
Assert.assertTrue(StringUtils.equalsIgnoreCase(InetUtils.getSelfIP(), "1.1.1.2"));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.clearProperty(NACOS_SERVER_IP);
|
||||
System.clearProperty(Constants.AUTO_REFRESH_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSelfIP() {
|
||||
Assert.assertNotNull(InetUtils.getSelfIP());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findFirstNonLoopbackAddress() {
|
||||
InetAddress address = InetUtils.findFirstNonLoopbackAddress();
|
||||
|
||||
Assert.assertNotNull(address);
|
||||
Assert.assertFalse(address.isLoopbackAddress());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.sys.utils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class MethodUtilTest {
|
||||
|
||||
private static final Method DOUBLE_METHOD;
|
||||
|
||||
private static final Method LONG_METHOD;
|
||||
|
||||
static {
|
||||
try {
|
||||
DOUBLE_METHOD = InternalMethod.class.getMethod("getD");
|
||||
LONG_METHOD = InternalMethod.class.getMethod("getL");
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAndReturnDouble() {
|
||||
InternalMethod internalMethod = new InternalMethod();
|
||||
Assert.assertNotEquals(Double.NaN, MethodUtil.invokeAndReturnDouble(DOUBLE_METHOD, internalMethod), 0.000001d);
|
||||
|
||||
Assert.assertEquals(Double.NaN, MethodUtil.invokeAndReturnDouble(LONG_METHOD, internalMethod), 0.000001d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAndReturnLong() {
|
||||
InternalMethod internalMethod = new InternalMethod();
|
||||
Assert.assertEquals(100L, MethodUtil.invokeAndReturnLong(LONG_METHOD, internalMethod));
|
||||
Assert.assertNotEquals(100L, MethodUtil.invokeAndReturnLong(DOUBLE_METHOD, internalMethod));
|
||||
}
|
||||
|
||||
public static class InternalMethod {
|
||||
|
||||
private double d = 1.1d;
|
||||
|
||||
private long l = 100L;
|
||||
|
||||
public double getD() {
|
||||
return d;
|
||||
}
|
||||
|
||||
public long getL() {
|
||||
return l;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.sys.utils;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ActiveProfiles("prefix")
|
||||
@SpringBootTest(classes = PropertiesUtilTest.class)
|
||||
public class PropertiesUtilTest {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetPropertiesWithPrefixForMap() {
|
||||
Map<String, Object> actual = PropertiesUtil.getPropertiesWithPrefixForMap(environment, "nacos.prefix");
|
||||
assertEquals(3, actual.size());
|
||||
for (Map.Entry<String, Object> entry : actual.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Map<String, Object> subMap = (Map<String, Object>) entry.getValue();
|
||||
switch (key) {
|
||||
case "one":
|
||||
assertEquals("1", subMap.get("value"));
|
||||
break;
|
||||
case "two":
|
||||
assertEquals("2", subMap.get("value"));
|
||||
break;
|
||||
case "three":
|
||||
assertEquals("3", subMap.get("value"));
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPropertiesWithPrefix() {
|
||||
Properties actual = PropertiesUtil.getPropertiesWithPrefix(environment, "nacos.prefix");
|
||||
assertEquals(3, actual.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleSpringBinder() {
|
||||
Map properties = PropertiesUtil.handleSpringBinder(environment, "nacos.prefix", Map.class);
|
||||
assertEquals(3, properties.size());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
#
|
||||
# Copyright 1999-2023 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.
|
||||
#
|
||||
|
||||
com.alibaba.nacos.sys.module.mock.MockModuleStateBuilder
|
||||
com.alibaba.nacos.sys.module.mock.ExceptionMockModuleStateBuilder
|
||||
15
sys/src/test/resources/application-empty.properties
Normal file
15
sys/src/test/resources/application-empty.properties
Normal file
@ -0,0 +1,15 @@
|
||||
#
|
||||
# Copyright 1999-2018 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.
|
||||
#
|
||||
20
sys/src/test/resources/application-prefix.properties
Normal file
20
sys/src/test/resources/application-prefix.properties
Normal file
@ -0,0 +1,20 @@
|
||||
#
|
||||
# Copyright 1999-2018 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.
|
||||
#
|
||||
|
||||
nacos.prefix.one.value=1
|
||||
nacos.prefix.two.value=2
|
||||
nacos.prefix.three.value=3
|
||||
nacos.other.prefix.one.value=1
|
||||
17
sys/src/test/resources/application-test.properties
Normal file
17
sys/src/test/resources/application-test.properties
Normal file
@ -0,0 +1,17 @@
|
||||
#
|
||||
# Copyright 1999-2018 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.
|
||||
#
|
||||
|
||||
nacos.core.sys.basic.processors=10
|
||||
17
sys/src/test/resources/application.properties
Normal file
17
sys/src/test/resources/application.properties
Normal file
@ -0,0 +1,17 @@
|
||||
#
|
||||
# Copyright 1999-2018 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.
|
||||
#
|
||||
|
||||
name=test-1
|
||||
Reference in New Issue
Block a user