diff --git a/sdk-java/build.gradle b/sdk-java/build.gradle index 5c80b57c..83c9d2f5 100644 --- a/sdk-java/build.gradle +++ b/sdk-java/build.gradle @@ -21,10 +21,10 @@ dependencies { implementation 'org.json:json:20250107' implementation 'com.google.code.findbugs:jsr305:3.0.2' - testImplementation 'junit:junit:4.13.1' - testImplementation 'org.mockito:mockito-core:2.8.9' - testImplementation "org.powermock:powermock-core:${POWERMOCK_VERSION}" - testImplementation "org.powermock:powermock-module-junit4:${POWERMOCK_VERSION}" + testImplementation(platform("org.mockito:mockito-bom:5.12.0")) + testImplementation("org.mockito:mockito-core") + testRuntimeOnly("net.bytebuddy:byte-buddy-agent") + testImplementation("junit:junit:4.13.2") //testImplementation 'com.squareup.okhttp3:mockwebserver:3.7.0' } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/EventImplTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/EventImplTests.java index 70df02df..89a0ab33 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/EventImplTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/EventImplTests.java @@ -15,7 +15,7 @@ @RunWith(JUnit4.class) public class EventImplTests { - Log L = mock(Log.class); + Log L = TestUtils.getLogger(); /** * Constructor of EventImpl class. diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/EventQueueTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/EventQueueTests.java index dd33ba11..723fb8ef 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/EventQueueTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/EventQueueTests.java @@ -20,7 +20,6 @@ import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_SEPARATOR; import static ly.count.sdk.java.internal.TestUtils.validateEvent; import static org.mockito.ArgumentMatchers.anyCollection; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -28,7 +27,7 @@ @RunWith(JUnit4.class) public class EventQueueTests { - Log L = mock(Log.class); + Log L = TestUtils.getLogger(); EventQueue eventQueue; @@ -96,7 +95,7 @@ public void writeEventQueueToStorage() { @Test public void writeEventQueueToStorage_emptyCache() { eventQueue = spy(EventQueue.class); - eventQueue.L = mock(Log.class); + eventQueue.L = TestUtils.getLogger(); eventQueue.eventQueueMemoryCache = new ArrayList<>(); eventQueue.writeEventQueueToStorage(); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java index c479c25a..f341db72 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ImmediateRequestTest.java @@ -14,7 +14,7 @@ @RunWith(JUnit4.class) public class ImmediateRequestTest { - Log L = mock(Log.class); + Log L = TestUtils.getLogger(); @After public void stop() { diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/JsonFileStorageTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/JsonFileStorageTests.java index 8111d5dc..ef0d4788 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/JsonFileStorageTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/JsonFileStorageTests.java @@ -16,7 +16,7 @@ @RunWith(JUnit4.class) public class JsonFileStorageTests { - private static final Log L = Mockito.mock(Log.class); + private static Log L = TestUtils.getLogger(); JsonFileStorage storage; static final String JSON_FILE_NAME = "test.json"; @@ -78,6 +78,7 @@ public void readJsonFile_emptyJsonFile() { @Test public void readJsonFile_IOException() throws IOException { File file = Mockito.mock(File.class); + L = Mockito.spy(L); Mockito.doThrow(new IOException("Simulated IOException")).when(file).createNewFile(); storage = new JsonFileStorage(file, L); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java index fd1f0b3d..805cd6b0 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java @@ -1,26 +1,10 @@ package ly.count.sdk.java.internal; +import ly.count.sdk.java.Config; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import org.powermock.reflect.Whitebox; - -import ly.count.sdk.java.Config; - -import static ly.count.sdk.java.Config.LoggingLevel.DEBUG; -import static ly.count.sdk.java.Config.LoggingLevel.ERROR; -import static ly.count.sdk.java.Config.LoggingLevel.INFO; -import static ly.count.sdk.java.Config.LoggingLevel.OFF; -import static ly.count.sdk.java.Config.LoggingLevel.WARN; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -@RunWith(JUnit4.class) public class LogTests { private static final String message = "message"; private static final Throwable exception = new IllegalStateException("IAS"); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/MigrationHelperTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/MigrationHelperTests.java index 419a6edf..ddba06b8 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/MigrationHelperTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/MigrationHelperTests.java @@ -71,7 +71,7 @@ public void beforeTest() { */ private void initStorage() { InternalConfig config = (new InternalConfig(TestUtils.getBaseConfig())); - config.setLogger(mock(Log.class)); + config.setLogger(TestUtils.getLogger()); storageProvider = new SDKStorage().init(config); } @@ -90,7 +90,7 @@ public void afterTest() { */ @Test public void migrationHelper_defaults() { - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); Assert.assertEquals(-1, migrationHelper.currentDataModelVersion);//validate the default version value Assert.assertNotNull(migrationHelper.logger); Assert.assertNull(migrationHelper.storageProvider); @@ -105,7 +105,7 @@ public void migrationHelper_defaults() { @Test public void setupMigrations_freshInstall() { initStorage(); // to initialize json storage - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(expectedLatestSchemaVersion, migrationHelper.currentDataModelVersion); } @@ -120,7 +120,7 @@ public void setupMigrations_legacyState() { TestUtils.createFile("test"); //mock a sdk file, to simulate storage is not empty initStorage(); // to initialize json storage after mock sdk file is created - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(0, migrationHelper.currentDataModelVersion); } @@ -135,7 +135,7 @@ public void setupMigrations_latestVersion() throws IOException { setDataVersionInConfigFile(expectedLatestSchemaVersion); initStorage(); // to initialize json storage after data version is set to expectedLatestVersion - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.logger = spy(migrationHelper.logger); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(expectedLatestSchemaVersion, migrationHelper.currentDataModelVersion); @@ -153,7 +153,7 @@ public void applyMigrations_legacyToLatest() { TestUtils.createFile("test"); //mock a sdk file, to simulate storage is not empty initStorage(); // to initialize json storage after mock sdk file is created - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(0, migrationHelper.currentDataModelVersion); //legacy state Map migrationParams = new ConcurrentHashMap<>(); @@ -175,7 +175,7 @@ public void applyMigrations_latestToLatest() throws IOException { setDataVersionInConfigFile(expectedLatestSchemaVersion); //set data version to latest initStorage(); // to initialize json storage after data version is set to 1 - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(expectedLatestSchemaVersion, migrationHelper.currentDataModelVersion); //latest state //run migration helper apply @@ -195,7 +195,7 @@ public void applyMigrations_0to1() throws IOException { Files.write(TestUtils.createFile("config_0").toPath(), MOCK_OLD_CONFIG_FILE_sdkGenId); //mock a sdk config file, to simulate storage is not empty initStorage(); // to initialize json storage after mock sdk file is created - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(0, migrationHelper.currentDataModelVersion); //legacy state @@ -294,7 +294,7 @@ public void applyMigrations_1to2_nothingToMigrate() throws IOException { Map migrationParams = new ConcurrentHashMap<>(); migrationParams.put("sdk_path", TestUtils.getTestSDirectory()); - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(1, migrationHelper.currentDataModelVersion); migrationHelper.logger = Mockito.spy(migrationHelper.logger); @@ -314,7 +314,7 @@ public void applyMigrations_1to2_nullMigrationParams() throws IOException { setDataVersionInConfigFile(1); // set previous data version initStorage(); - MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class)); + MigrationHelper migrationHelper = new MigrationHelper(TestUtils.getLogger()); migrationHelper.setupMigrations(storageProvider); Assert.assertEquals(1, migrationHelper.currentDataModelVersion); migrationHelper.logger = Mockito.spy(migrationHelper.logger); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java index 815dbbdd..95a760d9 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java @@ -25,7 +25,7 @@ @RunWith(JUnit4.class) public class ModuleFeedbackTests { - Log L = mock(Log.class); + Log L = TestUtils.getLogger(); @Before public void beforeTest() { diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/MultiThreadingTest.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/MultiThreadingTest.java index e40bfd55..12ed4483 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/MultiThreadingTest.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/MultiThreadingTest.java @@ -74,7 +74,7 @@ public void multiThread() throws BrokenBarrierException, InterruptedException { } gate.await(); - Storage.await(Mockito.mock(Log.class)); + Storage.await(TestUtils.getLogger()); for (Thread t : runs) { t.join(); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/RemoteConfigValueStoreTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/RemoteConfigValueStoreTests.java index d8f8e06d..63ad5920 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/RemoteConfigValueStoreTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/RemoteConfigValueStoreTests.java @@ -19,7 +19,7 @@ public class RemoteConfigValueStoreTests { */ @Test public void constructor_defaults() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(new JSONObject(), false, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(new JSONObject(), false, TestUtils.getLogger()); Assert.assertEquals(RemoteConfigValueStore.keyValue, "v"); Assert.assertEquals(RemoteConfigValueStore.keyCacheFlag, "c"); Assert.assertEquals(RemoteConfigValueStore.cacheValCached, 0); @@ -35,7 +35,7 @@ public void constructor_defaults() { */ @Test public void cacheClearValues_cachingDisabled() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), false, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), false, TestUtils.getLogger()); Assert.assertEquals(2, rcvs.getAllValues().size()); rcvs.cacheClearValues(); Assert.assertEquals(rcvs.values.length(), 0); @@ -48,7 +48,7 @@ public void cacheClearValues_cachingDisabled() { */ @Test(expected = NullPointerException.class) public void cacheClearValues_nullRcValues() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, false, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, false, TestUtils.getLogger()); rcvs.cacheClearValues(); } @@ -59,7 +59,7 @@ public void cacheClearValues_nullRcValues() { */ @Test public void cacheClearValues_cachingEnabled() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertEquals(2, rcvs.getAllValues().size()); rcvs.cacheClearValues(); Assert.assertEquals(2, rcvs.getAllValues().size()); @@ -74,7 +74,7 @@ public void cacheClearValues_cachingEnabled() { */ @Test public void cacheClearValues_garbageJson() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(garbageJson(), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(garbageJson(), true, TestUtils.getLogger()); rcvs.L = Mockito.spy(rcvs.L); rcvs.cacheClearValues(); Mockito.verify(rcvs.L, Mockito.times(1)).w("[RemoteConfigValueStore] cacheClearValues, stored entry was not a JSON object, key:[" + TestUtils.keysValues[0] + "] value:[garbage]"); @@ -89,7 +89,7 @@ public void cacheClearValues_garbageJson() { */ @Test public void mergeValues_fullUpdate() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertEquals(2, rcvs.getAllValues().size()); Assert.assertEquals(TestUtils.keysValues[1], rcvs.getValue(TestUtils.keysValues[0]).value); rcvs.mergeValues(newRCValues(), true); @@ -105,7 +105,7 @@ public void mergeValues_fullUpdate() { */ @Test public void mergeValues_notFullUpdate() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertEquals(2, rcvs.getAllValues().size()); Assert.assertEquals(TestUtils.keysValues[1], rcvs.getValue(TestUtils.keysValues[0]).value); rcvs.mergeValues(newRCValues(), false); @@ -121,7 +121,7 @@ public void mergeValues_notFullUpdate() { */ @Test(expected = NullPointerException.class) public void mergeValues_nullNewRcValues() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertEquals(2, rcvs.getAllValues().size()); rcvs.mergeValues(null, false); } @@ -133,7 +133,7 @@ public void mergeValues_nullNewRcValues() { */ @Test(expected = NullPointerException.class) public void mergeValues_nullRcValues() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, true, TestUtils.getLogger()); rcvs.mergeValues(newRCValues(), false); } @@ -144,7 +144,7 @@ public void mergeValues_nullRcValues() { */ @Test public void getValue_nullRcValues() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, true, TestUtils.getLogger()); rcvs.L = Mockito.spy(rcvs.L); Assert.assertNull(rcvs.getValue(TestUtils.keysValues[0]).value); Mockito.verify(rcvs.L, Mockito.times(1)).e(Mockito.anyString()); @@ -157,7 +157,7 @@ public void getValue_nullRcValues() { */ @Test public void getValue() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertEquals(TestUtils.keysValues[1], rcvs.getValue(TestUtils.keysValues[0]).value); } @@ -168,7 +168,7 @@ public void getValue() { */ @Test public void getValue_notExist() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertNull(rcvs.getValue(TestUtils.keysValues[5]).value); } @@ -179,7 +179,7 @@ public void getValue_notExist() { */ @Test public void getValue_garbageJson() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(garbageJson(), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(garbageJson(), true, TestUtils.getLogger()); Assert.assertEquals(1, rcvs.values.length()); Assert.assertNull(rcvs.getValue(TestUtils.keysValues[0]).value); } @@ -191,7 +191,7 @@ public void getValue_garbageJson() { */ @Test public void getAllValues() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson(RemoteConfigValueStore.keyCacheFlag), true, TestUtils.getLogger()); Assert.assertEquals(2, rcvs.getAllValues().size()); Assert.assertEquals(2, rcvs.values.length()); } @@ -203,7 +203,7 @@ public void getAllValues() { */ @Test(expected = NullPointerException.class) public void getAllValues_nullRcValues() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(null, true, TestUtils.getLogger()); rcvs.getAllValues(); } @@ -214,7 +214,7 @@ public void getAllValues_nullRcValues() { */ @Test public void getAllValues_garbageJson() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(garbageJson(), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(garbageJson(), true, TestUtils.getLogger()); Assert.assertEquals(0, rcvs.getAllValues().size()); Assert.assertEquals(1, rcvs.values.length()); } @@ -226,7 +226,7 @@ public void getAllValues_garbageJson() { */ @Test public void getAllValues_garbageCacheKey() { - RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson("garbage"), true, Mockito.mock(Log.class)); + RemoteConfigValueStore rcvs = new RemoteConfigValueStore(rcvsJson("garbage"), true, TestUtils.getLogger()); rcvs.L = Mockito.spy(rcvs.L); Assert.assertEquals(0, rcvs.getAllValues().size()); Mockito.verify(rcvs.L, Mockito.times(2)).e(Mockito.anyString()); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java index 4a7c8b26..1667eb30 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java @@ -6,7 +6,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.powermock.reflect.Whitebox; @RunWith(JUnit4.class) public class RequestTests { @@ -19,30 +18,30 @@ public void setupEveryTest() throws Exception { } @Test - public void request_constructorString() throws Exception { + public void request_constructorString() { String paramVals = "a=1&b=2"; Params params = new Params(paramVals); - Request request = Whitebox.invokeConstructor(Request.class, paramVals); + Request request = new Request(paramVals); Params requestParams = request.params; Assert.assertEquals(params.toString(), requestParams.toString()); } @Test - public void request_constructorObjectsNull() throws Exception { + public void request_constructorObjectsNull() { String[] paramsVals = new String[] { "asd", "123" }; Object[] vals = new Object[] { new Object[] { paramsVals[0], paramsVals[1] } }; - Request request = Whitebox.invokeConstructor(Request.class, vals); + Request request = new Request(vals); Assert.assertEquals(paramsVals[0] + "=" + paramsVals[1], request.params.toString()); } @Test - public void request_constructorObjects() throws Exception { + public void request_constructorObjects() { String[] paramsParts = new String[] { "abc", "123", "qwe", "456" }; String paramVals = paramsParts[0] + "=" + paramsParts[1] + "&" + paramsParts[2] + "=" + paramsParts[3]; Params params = new Params(paramVals); - Request request = Whitebox.invokeConstructor(Request.class, paramsParts[0], paramsParts[1], paramsParts[2], paramsParts[3]); + Request request = new Request(paramsParts[0], paramsParts[1], paramsParts[2], paramsParts[3]); Params requestParams = request.params; Assert.assertEquals(params.toString(), requestParams.toString()); } @@ -59,19 +58,19 @@ public void request_build() { } @Test - public void request_serialize() throws Exception { + public void request_serialize() { String paramVals = "a=1&b=2"; - Request request = Whitebox.invokeConstructor(Request.class, paramVals); + Request request = new Request(paramVals); - String manualSerialization = paramVals + Whitebox.getInternalState(Request.class, "EOR"); + String manualSerialization = paramVals + WhiteboxUtils.getInternalStaticState(Request.class, "EOR", String.class); String serializationRes = new String(request.store(null)); Assert.assertEquals(manualSerialization, serializationRes); } @Test - public void request_loadSimple() throws Exception { + public void request_loadSimple() { String paramVals = "a=1&b=2"; - Request request = Whitebox.invokeConstructor(Request.class, paramVals); + Request request = new Request(paramVals); byte[] serializationRes = request.store(null); Request requestNew = new Request(); @@ -92,13 +91,13 @@ public void request_loadNull() { } @Test - public void isGettable_ParamsEmptyUnderLimit() throws Exception { - Request request = Whitebox.invokeConstructor(Request.class, ""); + public void isGettable_ParamsEmptyUnderLimit() { + Request request = new Request(""); Assert.assertTrue(request.isGettable(url, 0)); } @Test - public void isGettable_ParamsFilledAboveLimitLarge() throws Exception { + public void isGettable_ParamsFilledAboveLimitLarge() { StringBuilder sbParams = new StringBuilder(); for (int a = 0; a < 1000; a++) { @@ -109,7 +108,7 @@ public void isGettable_ParamsFilledAboveLimitLarge() throws Exception { sbParams.append('=').append(a); } - Request request = Whitebox.invokeConstructor(Request.class, sbParams.toString()); + Request request = new Request(sbParams.toString()); Assert.assertFalse(request.isGettable(url, 0)); } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioDeviceIdInitTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioDeviceIdInitTests.java index b413555a..7cd28191 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioDeviceIdInitTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioDeviceIdInitTests.java @@ -14,7 +14,7 @@ public class ScenarioDeviceIdInitTests { final static String alternativeDeviceID = TestUtils.DEVICE_ID + "1"; - Log L = mock(Log.class); + Log L = TestUtils.getLogger(); @Before public void beforeTest() { diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/SessionImplTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/SessionImplTests.java index 859d50c3..a5aa5107 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/SessionImplTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/SessionImplTests.java @@ -41,7 +41,7 @@ public void stop() { @Test public void constructor() { Countly.instance().init(TestUtils.getConfigSessions()); - assertEquals(new Long(12_345L), createSessionImpl(12_345L).getId()); + assertEquals(Long.valueOf(12_345L), createSessionImpl(12_345L).getId()); } /** @@ -488,7 +488,7 @@ private void addCrashReport_base(Config config, int rqSize) { @Test public void hashCode_id() { Countly.instance().init(TestUtils.getConfigSessions()); - assertEquals(new Long(12_345L).hashCode(), createSessionImpl(12_345L).hashCode()); + assertEquals(Long.valueOf(12_345L).hashCode(), createSessionImpl(12_345L).hashCode()); } /** diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java index bcbce23f..134f5814 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java @@ -8,7 +8,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.powermock.reflect.Whitebox; @RunWith(JUnit4.class) public class TasksTests { @@ -26,8 +25,8 @@ public void tearDown() throws Exception { @Test public void testSetup() { - Assert.assertNotNull(Whitebox.getInternalState(tasks, "executor")); - Assert.assertNotNull(Whitebox.getInternalState(tasks, "pending")); + Assert.assertNotNull(WhiteboxUtils.getInternalState(tasks, "executor")); + Assert.assertNotNull(WhiteboxUtils.getInternalState(tasks, "pending")); } @Test @@ -43,8 +42,8 @@ public Object call() throws Exception { long now = System.nanoTime(); other.shutdown(); long timeToShutdown = TimeUtils.nsToMs(System.nanoTime() - now); - Assert.assertTrue(Whitebox.getInternalState(other, "executor").isShutdown()); - Assert.assertTrue(Whitebox.getInternalState(other, "executor").isTerminated()); + Assert.assertTrue(WhiteboxUtils.getInternalState(other, "executor", ExecutorService.class).isShutdown()); + Assert.assertTrue(WhiteboxUtils.getInternalState(other, "executor", ExecutorService.class).isTerminated()); //Assert.assertTrue(timeToShutdown > 100);//todo, this line fails when trying to publish (AK, 12.12.18) } 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 c959a5b9..d15a0299 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 @@ -139,7 +139,7 @@ static void checkSdkStorageRootDirectoryExist(File directory) { } protected static Map[] getCurrentRQ() { - return getCurrentRQ(getTestSDirectory(), mock(Log.class)); + return getCurrentRQ(getTestSDirectory(), TestUtils.getLogger()); } /** @@ -150,7 +150,7 @@ protected static Map[] getCurrentRQ() { * @return array of request params */ protected static Map[] getCurrentRQ(File targetFolder, Log logger) { - Storage.await(mock(Log.class)); // wait for request to be written to the disk + Storage.await(TestUtils.getLogger()); // wait for request to be written to the disk //check whether target folder is a directory or not if (!targetFolder.isDirectory()) { @@ -182,7 +182,7 @@ static void flushCurrentRQWithOldDeviceId(String oldDeviceId) { Arrays.stream(getRequestFiles(getTestSDirectory())).forEach(file -> { try { if (file.exists() && file.isFile()) { - String content = Utils.readFileContent(file, mock(Log.class)); + String content = Utils.readFileContent(file, TestUtils.getLogger()); if (content.contains(oldDeviceId)) { Files.delete(file.toPath()); } @@ -194,7 +194,7 @@ static void flushCurrentRQWithOldDeviceId(String oldDeviceId) { } protected static List getCurrentEQ() { - return getCurrentEQ(getTestSDirectory(), mock(Log.class)); + return getCurrentEQ(getTestSDirectory(), TestUtils.getLogger()); } /** @@ -375,7 +375,7 @@ static List readEventsFromRequest(int requestIndex, String deviceId) array.forEach(value -> { result.add(EventImpl.fromJSON(value.toString(), (ev) -> { - }, mock(Log.class))); + }, TestUtils.getLogger())); }); return result; @@ -387,7 +387,7 @@ static void validateEQSize(int expectedSize, List events, EventQueue } static void validateEQSize(int expectedSize, EventQueue eventQueue) { - validateEQSize(expectedSize, TestUtils.getCurrentEQ(getTestSDirectory(), mock(Log.class)), eventQueue); + validateEQSize(expectedSize, TestUtils.getCurrentEQ(getTestSDirectory(), TestUtils.getLogger()), eventQueue); } static void validateEQSize(int expectedSize) { @@ -492,7 +492,7 @@ static JSONObject readJsonFile(final String name) { */ static JSONObject readJsonFile(final File file) { try { - return new JSONObject(Utils.readFileContent(file, mock(Log.class))); + return new JSONObject(Utils.readFileContent(file, TestUtils.getLogger())); } catch (Exception e) { return new JSONObject(); } @@ -520,7 +520,7 @@ public static File createFile(final String fileName) { static InternalConfig getInternalConfigWithLogger(Config config) { InternalConfig ic = new InternalConfig(config); - ic.setLogger(mock(Log.class)); + ic.setLogger(TestUtils.getLogger()); return ic; } @@ -667,4 +667,8 @@ static InternalConfig getConfigViews(Map segmentation) { config.views.setGlobalViewSegmentation(segmentation); return config; } + + static Log getLogger() { + return new Log(Config.LoggingLevel.DEBUG, null); + } } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ViewImplTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ViewImplTests.java index 4dff4cba..79b0fa8e 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ViewImplTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ViewImplTests.java @@ -33,7 +33,7 @@ public void afterTest() { */ @Test public void constructor_defaults() { - ViewImpl view = new ViewImpl(null, TestUtils.keysValues[0], Mockito.mock(Log.class)); + ViewImpl view = new ViewImpl(null, TestUtils.keysValues[0], TestUtils.getLogger()); Assert.assertNull(view.session); Assert.assertEquals(TestUtils.keysValues[0], view.name); Assert.assertEquals("start", ModuleViews.KEY_START); @@ -207,7 +207,7 @@ public void stop_noConsentForEvents() { @Test public void stop_notStartedView() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Views, Config.Feature.Events)); - ViewImpl view = new ViewImpl(Countly.session(), TestUtils.keysValues[0], Mockito.mock(Log.class)); + ViewImpl view = new ViewImpl(Countly.session(), TestUtils.keysValues[0], TestUtils.getLogger()); TestUtils.validateEQSize(0); view.stop(false); TestUtils.validateEQSize(0); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/WhiteboxUtils.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/WhiteboxUtils.java new file mode 100644 index 00000000..f5e118e8 --- /dev/null +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/WhiteboxUtils.java @@ -0,0 +1,62 @@ +package ly.count.sdk.java.internal; + +import java.lang.reflect.Field; + +public class WhiteboxUtils { + + public static Object getInternalState(Object target, String fieldName) { + Class clazz = target.getClass(); + + while (clazz != null) { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(target); + } catch (NoSuchFieldException e) { + clazz = clazz.getSuperclass(); // search parent class + } catch (Exception e) { + throw new RuntimeException("Failed to get internal state for field: " + fieldName, e); + } + } + + throw new RuntimeException("Field not found: " + fieldName + " in class hierarchy of " + target.getClass()); + } + + public static T getInternalState(Object target, String fieldName, Class fieldType) { + Class clazz = target.getClass(); + + while (clazz != null) { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + + Object value = field.get(target); + return fieldType.cast(value); + } catch (NoSuchFieldException e) { + clazz = clazz.getSuperclass(); + } catch (Exception e) { + throw new RuntimeException("Failed to read field '" + fieldName + "'", e); + } + } + + throw new RuntimeException("Field '" + fieldName + "' not found in " + target.getClass()); + } + + public static T getInternalStaticState(Class clazz, String fieldName, Class fieldType) { + Class current = clazz; + + while (current != null) { + try { + Field field = current.getDeclaredField(fieldName); + field.setAccessible(true); + return fieldType.cast(field.get(null)); // static field -> null target + } catch (NoSuchFieldException e) { + current = current.getSuperclass(); + } catch (Exception e) { + throw new RuntimeException("Failed to read static field '" + fieldName + "'", e); + } + } + + throw new RuntimeException("Static field '" + fieldName + "' not found in " + clazz); + } +}