diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 4b0a7a706..3a9cad6dd 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,5 +1,9 @@ name: Java Gradle Build and Test +permissions: + checks: write + pull-requests: write + on: push: branches: @@ -16,7 +20,8 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - name: Set up JDK 8 uses: actions/setup-java@v3 @@ -24,7 +29,15 @@ jobs: java-version: '8' distribution: 'corretto' - - name: Build with Gradle - uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 + - name: Build and Test with Gradle + run: ./gradlew build + + - name: Publish Test Results + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() with: - arguments: build \ No newline at end of file + check_name: "Unit Test Results :rocket:" + comment_title: "Unit Test Results :rocket:" + files: | + sdk-java/build/test-results/**/*.xml + diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java index c4905acbb..fb4724f56 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java @@ -302,7 +302,7 @@ protected String readEventQueue() { String eventQueue = ""; try { - eventQueue = Utils.readFileContent(file); + eventQueue = Utils.readFileContent(file, L); } catch (IOException e) { // Handle the error if reading fails L.e("[SDKStorage] Failed to read EQ from json file: " + e); diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java index 4bbc0a20d..8d565317f 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java @@ -364,6 +364,8 @@ public Boolean call() throws Exception { } public static void stop() { - tasks.shutdown(); + if (tasks != null) { + tasks.shutdown(); + } } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java index 0a9ebfcdd..cfcc092b2 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java @@ -304,13 +304,20 @@ public static boolean isValidDataType(Object value) { * If file doesn't exist, return empty string * * @param file to read + * @param logger to log errors * @return file contents or empty string * @throws IOException if file exists but couldn't be read */ - public static String readFileContent(File file) throws IOException { + public static String readFileContent(File file, Log logger) throws IOException { StringBuilder fileContent = new StringBuilder(); if (!file.exists()) { + logger.v("[Utils] readFileContent : File doesn't exist: " + file.getAbsolutePath() + ". returning empty string"); + return fileContent.toString(); + } + + if (!file.canRead()) { + logger.v("[Utils] readFileContent : File exists but can't be read: " + file.getAbsolutePath() + ". returning empty string"); return fileContent.toString(); } 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 22dab3932..684cd309f 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 @@ -17,6 +17,8 @@ public class TestUtils { + private static String DELIMETER = ":::"; + private TestUtils() { } @@ -73,7 +75,7 @@ protected static List getCurrentEventQueue(File targetFolder, Log log File file = new File(targetFolder, FILE_NAME_PREFIX + FILE_NAME_SEPARATOR + EVENT_QUEUE_FILE_NAME); String fileContent = ""; try { - fileContent = Utils.readFileContent(file); + fileContent = Utils.readFileContent(file, logger); } catch (IOException e) { //do nothing } @@ -148,4 +150,4 @@ private static Map parseRequestParams(File file) throws IOExcept return paramMap; } } -} +} \ No newline at end of file diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java index 5a293fd7b..717bf2e31 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java @@ -255,7 +255,7 @@ public void readFileContent() throws IOException { writer.write(fileContent); writer.close(); - String result = Utils.readFileContent(file); + String result = Utils.readFileContent(file, logger); //delete file file.delete(); Assert.assertEquals(fileContent, result); @@ -272,7 +272,7 @@ public void readFileContent_fileNotExist() throws IOException { File file = new File(fileName); - String result = Utils.readFileContent(file); + String result = Utils.readFileContent(file, logger); Assert.assertNotEquals(fileContent, result); Assert.assertEquals("", result); @@ -280,9 +280,9 @@ public void readFileContent_fileNotExist() throws IOException { /** * If the file is not readable for some reason, - * the method should throw exception. + * the method should return empty string. */ - @Test(expected = IOException.class) + @Test public void readFileContent_fileNotReadable() throws IOException { try { String fileContent = "testContent"; @@ -294,7 +294,8 @@ public void readFileContent_fileNotReadable() throws IOException { writer.close(); file.setReadable(false); - Utils.readFileContent(file); + String content = Utils.readFileContent(file, logger); + Assert.assertEquals("", content); } finally { File file = new File(TEST_FILE_NAME); if (file.exists()) {