From e01ae5e2e42f5919a94aabc4f40c12f1ef7ec036 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray <57103426+arifBurakDemiray@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:11:40 +0300 Subject: [PATCH] feat: sdk storage check fix (#79) --- .../count/java/demo/BackendModeExample.java | 11 ++++---- .../demo/BackendModePerformanceTests.java | 13 +++++---- .../main/java/ly/count/java/demo/Example.java | 7 +++-- .../sdk/java/internal/BackendModeTests.java | 28 ++++++++++--------- .../ly/count/sdk/java/internal/TestUtils.java | 14 ++++++++++ 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/app-java/src/main/java/ly/count/java/demo/BackendModeExample.java b/app-java/src/main/java/ly/count/java/demo/BackendModeExample.java index 91ee51d4c..0e1697827 100644 --- a/app-java/src/main/java/ly/count/java/demo/BackendModeExample.java +++ b/app-java/src/main/java/ly/count/java/demo/BackendModeExample.java @@ -1,13 +1,12 @@ package ly.count.java.demo; -import ly.count.sdk.java.Config; -import ly.count.sdk.java.Countly; - import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.concurrent.CountDownLatch; +import ly.count.sdk.java.Config; +import ly.count.sdk.java.Countly; public class BackendModeExample { final static String DEVICE_ID = "device-id"; @@ -314,8 +313,10 @@ public static void main(String[] args) throws Exception { String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" }; File sdkStorageRootDirectory = new File(String.join(File.separator, sdkStorageRootPath)); - if(sdkStorageRootDirectory.mkdirs()){ - System.out.println("Directory creation failed"); + if (!(sdkStorageRootDirectory.exists() && sdkStorageRootDirectory.isDirectory())) { + if (!sdkStorageRootDirectory.mkdirs()) { + System.out.println("Directory creation failed"); + } } // Main initialization call, SDK can be used after this one is done diff --git a/app-java/src/main/java/ly/count/java/demo/BackendModePerformanceTests.java b/app-java/src/main/java/ly/count/java/demo/BackendModePerformanceTests.java index 7645fe756..1736a21b1 100644 --- a/app-java/src/main/java/ly/count/java/demo/BackendModePerformanceTests.java +++ b/app-java/src/main/java/ly/count/java/demo/BackendModePerformanceTests.java @@ -1,13 +1,12 @@ package ly.count.java.demo; -import ly.count.sdk.java.Config; -import ly.count.sdk.java.Countly; -import ly.count.sdk.java.internal.Device; - import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Scanner; +import ly.count.sdk.java.Config; +import ly.count.sdk.java.Countly; +import ly.count.sdk.java.internal.Device; public class BackendModePerformanceTests { final static String DEVICE_ID = "device-id"; @@ -27,8 +26,10 @@ private static void initSDK(int eventQueueSize, int requestQueueSize) { String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" }; File sdkStorageRootDirectory = new File(String.join(File.separator, sdkStorageRootPath)); - if(sdkStorageRootDirectory.mkdirs()){ - System.out.println("Directory creation failed"); + if (!(sdkStorageRootDirectory.exists() && sdkStorageRootDirectory.isDirectory())) { + if (!sdkStorageRootDirectory.mkdirs()) { + System.out.println("Directory creation failed"); + } } // Main initialization call, SDK can be used after this one is done diff --git a/app-java/src/main/java/ly/count/java/demo/Example.java b/app-java/src/main/java/ly/count/java/demo/Example.java index 4367ab115..8278e9d55 100755 --- a/app-java/src/main/java/ly/count/java/demo/Example.java +++ b/app-java/src/main/java/ly/count/java/demo/Example.java @@ -4,7 +4,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Scanner; - import ly.count.sdk.java.Config; import ly.count.sdk.java.Countly; import ly.count.sdk.java.internal.LogCallback; @@ -125,8 +124,10 @@ public static void main(String[] args) throws Exception { String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" }; File sdkStorageRootDirectory = new File(String.join(File.separator, sdkStorageRootPath)); - if(sdkStorageRootDirectory.mkdirs()){ - System.out.println("Directory creation failed"); + if (!(sdkStorageRootDirectory.exists() && sdkStorageRootDirectory.isDirectory())) { + if (!sdkStorageRootDirectory.mkdirs()) { + System.out.println("Directory creation failed"); + } } Config config = new Config(COUNTLY_SERVER_URL, COUNTLY_APP_KEY, sdkStorageRootDirectory) diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java index 7ff3ff78c..289771702 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/BackendModeTests.java @@ -1,19 +1,25 @@ package ly.count.sdk.java.internal; +import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.math.BigDecimal; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; import ly.count.sdk.java.Config; import ly.count.sdk.java.Countly; import org.json.JSONArray; import org.json.JSONObject; -import org.junit.*; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.File; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.math.BigDecimal; -import java.util.*; - @RunWith(JUnit4.class) public class BackendModeTests { private ModuleBackendMode moduleBackendMode; @@ -25,12 +31,8 @@ public static void init() { cc.setEventQueueSizeToSend(4).enableBackendMode(); // System specific folder structure - String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" }; - File sdkStorageRootDirectory = new File(String.join(File.separator, sdkStorageRootPath)); - - if(sdkStorageRootDirectory.mkdirs()){ - System.out.println("Directory creation failed"); - } + File sdkStorageRootDirectory = TestUtils.getSdkStorageRootDirectory(); + TestUtils.checkSdkStorageRootDirectoryExist(sdkStorageRootDirectory); Countly.init(sdkStorageRootDirectory, cc); } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java index e393c1ce3..502466a48 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java @@ -151,4 +151,18 @@ private static Map parseRequestParams(File file) throws IOExcept return paramMap; } } + + static File getSdkStorageRootDirectory() { + // System specific folder structure + String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" }; + return new File(String.join(File.separator, sdkStorageRootPath)); + } + + static void checkSdkStorageRootDirectoryExist(File directory) { + if (!(directory.exists() && directory.isDirectory())) { + if (!directory.mkdirs()) { + throw new RuntimeException("Directory creation failed"); + } + } + } } \ No newline at end of file