diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java index ab2c8f71bdf..e3b64a9e066 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.config.server.service.dump.disk; import junit.framework.TestCase; -import org.junit.After; import org.junit.Before; import java.io.File; @@ -33,12 +32,10 @@ public class ConfigRawDiskServiceTest extends TestCase { @Before public void setUp() throws Exception { cachedOsName = System.getProperty("os.name"); - System.setProperty("os.name", "window"); } - @After - public void tearDown() throws Exception { - System.setProperty("os.name", cachedOsName); + private boolean isWindows() { + return cachedOsName.toLowerCase().startsWith("win"); } /** @@ -47,7 +44,7 @@ public void tearDown() throws Exception { public void testTargetFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = ConfigRawDiskService.class.getDeclaredMethod("targetFile", String.class, String.class, String.class); method.setAccessible(true); - File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf"); + File result = (File) method.invoke(null, "aaaa?dsaknkf", "aaaa*dsaknkf", "aaaa:dsaknkf"); // 分解路径 Path path = Paths.get(result.getPath()); Path parent = path.getParent(); @@ -56,9 +53,9 @@ public void testTargetFile() throws NoSuchMethodException, IllegalAccessExceptio String lastSegment = path.getFileName().toString(); String secondLastSegment = parent.getFileName().toString(); String thirdLastSegment = grandParent.getFileName().toString(); - assertEquals("aaaa%A3%dsaknkf", thirdLastSegment); - assertEquals("aaaa%A2%dsaknkf", secondLastSegment); - assertEquals("aaaa%A1%dsaknkf", lastSegment); + assertEquals(isWindows() ? "aaaa%A3%dsaknkf" : thirdLastSegment, thirdLastSegment); + assertEquals(isWindows() ? "aaaa%A4%dsaknkf" : secondLastSegment, secondLastSegment); + assertEquals(isWindows() ? "aaaa%A5%dsaknkf" : lastSegment, lastSegment); } /** @@ -67,7 +64,7 @@ public void testTargetFile() throws NoSuchMethodException, IllegalAccessExceptio public void testTargetBetaFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = ConfigRawDiskService.class.getDeclaredMethod("targetBetaFile", String.class, String.class, String.class); method.setAccessible(true); - File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf"); + File result = (File) method.invoke(null, "aaaa?dsaknkf", "aaaa*dsaknkf", "aaaa:dsaknkf"); // 分解路径 Path path = Paths.get(result.getPath()); Path parent = path.getParent(); @@ -76,9 +73,9 @@ public void testTargetBetaFile() throws NoSuchMethodException, IllegalAccessExce String lastSegment = path.getFileName().toString(); String secondLastSegment = parent.getFileName().toString(); String thirdLastSegment = grandParent.getFileName().toString(); - assertEquals("aaaa%A3%dsaknkf", thirdLastSegment); - assertEquals("aaaa%A2%dsaknkf", secondLastSegment); - assertEquals("aaaa%A1%dsaknkf", lastSegment); + assertEquals(isWindows() ? "aaaa%A3%dsaknkf" : thirdLastSegment, thirdLastSegment); + assertEquals(isWindows() ? "aaaa%A4%dsaknkf" : secondLastSegment, secondLastSegment); + assertEquals(isWindows() ? "aaaa%A5%dsaknkf" : lastSegment, lastSegment); } @@ -91,7 +88,7 @@ public void testTargetBetaFile() throws NoSuchMethodException, IllegalAccessExce public void testTargetTagFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = ConfigRawDiskService.class.getDeclaredMethod("targetTagFile", String.class, String.class, String.class, String.class); method.setAccessible(true); - File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf", "aaaadsaknkf"); + File result = (File) method.invoke(null, "aaaa?dsaknkf", "aaaa*dsaknkf", "aaaa:dsaknkf", "aaaadsaknkf"); // 分解路径 Path path = Paths.get(result.getPath()); Path parent = path.getParent(); @@ -101,9 +98,9 @@ public void testTargetTagFile() throws NoSuchMethodException, IllegalAccessExcep String secondLastSegment = parent.getFileName().toString(); String thirdLastSegment = grandParent.getFileName().toString(); String fourthLastSegment = greatGrandParent.getFileName().toString(); - assertEquals("aaaa%A3%dsaknkf", fourthLastSegment); - assertEquals("aaaa%A2%dsaknkf", thirdLastSegment); - assertEquals("aaaa%A1%dsaknkf", secondLastSegment); + assertEquals(isWindows() ? "aaaa%A3%dsaknkf" : fourthLastSegment, fourthLastSegment); + assertEquals(isWindows() ? "aaaa%A4%dsaknkf" : thirdLastSegment, thirdLastSegment); + assertEquals(isWindows() ? "aaaa%A5%dsaknkf" : secondLastSegment, secondLastSegment); String lastSegment = path.getFileName().toString(); assertEquals("aaaadsaknkf", lastSegment); }