diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java index 7c879509f..1abaaafbb 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java @@ -49,7 +49,7 @@ public class CacheDirUtil { */ public static String initCacheDir(String namespace, NacosClientProperties properties) { - String jmSnapshotPath = System.getProperty(JM_SNAPSHOT_PATH_PROPERTY); + String jmSnapshotPath = properties.getProperty(JM_SNAPSHOT_PATH_PROPERTY); String namingCacheRegistryDir = ""; if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) { @@ -62,7 +62,7 @@ public class CacheDirUtil { + FILE_PATH_NAMING + File.separator + namespace; } else { cacheDir = - System.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + properties.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + File.separator + FILE_PATH_NAMING + File.separator + namespace; } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/utils/CacheDirUtilTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/utils/CacheDirUtilTest.java index 5abbc54a5..76eb36854 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/utils/CacheDirUtilTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/utils/CacheDirUtilTest.java @@ -38,6 +38,14 @@ class CacheDirUtilTest { assertEquals("/home/admin/nacos/naming/test", actual); } + @Test + void testInitCacheDirWithDefaultRootAndWithoutCache2() { + NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); + properties.setProperty("user.home", "/home/test"); + String actual = CacheDirUtil.initCacheDir("test", properties); + assertEquals("/home/test/nacos/naming/test", actual); + } + @Test void testInitCacheDirWithDefaultRootAndWithCache() { System.setProperty("user.home", "/home/admin"); @@ -54,6 +62,14 @@ class CacheDirUtilTest { assertEquals("/home/snapshot/nacos/naming/test", actual); } + @Test + void testInitCacheDirWithJmSnapshotPathRootAndWithoutCache2() { + NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); + properties.setProperty("JM.SNAPSHOT.PATH", "/home/custom/snapshot"); + String actual = CacheDirUtil.initCacheDir("test", properties); + assertEquals("/home/custom/snapshot/nacos/naming/test", actual); + } + @Test void testInitCacheDirWithJmSnapshotPathRootAndWithCache() { System.setProperty("user.home", "/home/snapshot"); @@ -62,4 +78,4 @@ class CacheDirUtilTest { String actual = CacheDirUtil.initCacheDir("test", properties); assertEquals("/home/snapshot/nacos/custom/naming/test", actual); } -} \ No newline at end of file +}