From 3b3dd8e664194a35d0a4f2b0c1a04ec77e5cc0f9 Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-jy Date: Wed, 27 Mar 2024 13:53:20 -0700 Subject: [PATCH] added testing cases and divided converting window file paths --- .../client/config/SFClientConfigParser.java | 40 ++++++++++--------- .../config/SFClientConfigParserTest.java | 30 +++++++------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/snowflake/client/config/SFClientConfigParser.java b/src/main/java/net/snowflake/client/config/SFClientConfigParser.java index 540e571fa..7a7360860 100644 --- a/src/main/java/net/snowflake/client/config/SFClientConfigParser.java +++ b/src/main/java/net/snowflake/client/config/SFClientConfigParser.java @@ -87,24 +87,7 @@ public static String getConfigFilePathFromJDBCJarLocation() { if (systemGetProperty("os.name") != null && systemGetProperty("os.name").toLowerCase().startsWith("windows")) { - - // Find the Windows file path pattern: ex) C:\ or D:\ - Pattern windowsFilePattern = Pattern.compile("[C-Z]:[\\\\/]"); - Matcher matcher = windowsFilePattern.matcher(updatedPath); - String prefix = ""; - - // Path translation for windows - if (updatedPath.startsWith("/")) { - updatedPath = updatedPath.substring(1); - } else if (updatedPath.startsWith("file:\\")) { - updatedPath = updatedPath.substring(6); - } else if (updatedPath.startsWith("\\")) { - updatedPath = updatedPath.substring(2); - } else if (matcher.find() && matcher.start() != 0) { - prefix = updatedPath.substring(0, matcher.start()); - updatedPath = updatedPath.substring(matcher.start()); - } - updatedPath = prefix + updatedPath.replace("/", "\\"); + updatedPath = convertToWindowsPath(updatedPath); } return updatedPath; } @@ -115,4 +98,25 @@ && systemGetProperty("os.name").toLowerCase().startsWith("windows")) { return ""; } } + + public static String convertToWindowsPath(String filePath) { + // Find the Windows file path pattern: ex) C:\ or D:\ + Pattern windowsFilePattern = Pattern.compile("[C-Z]:[\\\\/]"); + Matcher matcher = windowsFilePattern.matcher(filePath); + String prefix = ""; + + // Path translation for windows + if (filePath.startsWith("/")) { + filePath = filePath.substring(1); + } else if (filePath.startsWith("file:\\")) { + filePath = filePath.substring(6); + } else if (filePath.startsWith("\\")) { + filePath = filePath.substring(2); + } else if (matcher.find() && matcher.start() != 0) { + prefix = filePath.substring(0, matcher.start()); + filePath = filePath.substring(matcher.start()); + } + filePath = prefix + filePath.replace("/", "\\"); + return filePath; + } } diff --git a/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java b/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java index a09c3036e..a0ea1041c 100644 --- a/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java +++ b/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java @@ -1,15 +1,8 @@ package net.snowflake.client.config; -import static net.snowflake.client.config.SFClientConfigParser.SF_CLIENT_CONFIG_ENV_NAME; -import static net.snowflake.client.config.SFClientConfigParser.SF_CLIENT_CONFIG_FILE_NAME; -import static net.snowflake.client.config.SFClientConfigParser.getConfigFilePathFromJDBCJarLocation; -import static net.snowflake.client.jdbc.SnowflakeUtil.systemGetProperty; -import static net.snowflake.client.jdbc.SnowflakeUtil.systemSetEnv; -import static net.snowflake.client.jdbc.SnowflakeUtil.systemUnsetEnv; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static net.snowflake.client.config.SFClientConfigParser.*; +import static net.snowflake.client.jdbc.SnowflakeUtil.*; +import static org.junit.Assert.*; import static org.mockito.Mockito.mockStatic; import java.io.IOException; @@ -135,11 +128,18 @@ public void testgetConfigFileNameFromJDBCJarLocation() { } @Test - public void testgetConfigFileNameFromJDBCJarLocationForWindows() { - try (MockedStatic mockedSnowflakeUtil = mockStatic(SnowflakeUtil.class)) { - mockedSnowflakeUtil.when(() -> systemGetProperty("os.name")).thenReturn("windows"); - String jdbcDirectoryPath = getConfigFilePathFromJDBCJarLocation(); - assertFalse(jdbcDirectoryPath.contains("/")); // windows use \ in paths + public void testconvertToWindowsPath() { + String mockWindowsPath = "C:/Program Files/example.txt"; + String resultWindowsPath = "C:\\Program Files\\example.txt"; + String[] testCases = new String[] {"", "file:\\", "\\\\", "/"}; + String mockCloudPrefix = "cloud://"; + + for (String testcase : testCases) { + assertEquals(resultWindowsPath, convertToWindowsPath(testcase + mockWindowsPath)); } + + assertEquals( + mockCloudPrefix + resultWindowsPath, + convertToWindowsPath(mockCloudPrefix + mockWindowsPath)); } }