Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions sdk-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ dependencies {
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 'org.mockito:mockito-core:4.11.0'
//testImplementation 'com.squareup.okhttp3:mockwebserver:3.7.0'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
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;
Expand Down
39 changes: 22 additions & 17 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ly.count.sdk.java.internal;

import java.lang.reflect.Field;
import java.net.URL;
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;

@RunWith(JUnit4.class)
public class RequestTests {
Expand All @@ -18,31 +18,36 @@ public void setupEveryTest() throws Exception {
url = new URL(urlString);
}

private static String getEOR() throws Exception {
Field field = Request.class.getDeclaredField("EOR");
field.setAccessible(true);
return (String) field.get(null);
}

@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((Object[]) new Object[] { paramsVals[0], paramsVals[1] });
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());
}
Expand All @@ -61,17 +66,17 @@ public void request_build() {
@Test
public void request_serialize() throws Exception {
String paramVals = "a=1&b=2";
Request request = Whitebox.invokeConstructor(Request.class, paramVals);
Request request = new Request(paramVals);

String manualSerialization = paramVals + Whitebox.<String>getInternalState(Request.class, "EOR");
String manualSerialization = paramVals + getEOR();
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();
Expand All @@ -92,13 +97,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++) {
Expand All @@ -109,8 +114,8 @@ 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));
}
}
}
20 changes: 13 additions & 7 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ly.count.sdk.java.internal;

import java.lang.reflect.Field;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import org.junit.After;
Expand All @@ -8,7 +9,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 {
Expand All @@ -24,14 +24,20 @@ public void tearDown() throws Exception {
tasks.shutdown();
}

private static Object getField(Object target, String fieldName) throws Exception {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(target);
}

@Test
public void testSetup() {
Assert.assertNotNull(Whitebox.getInternalState(tasks, "executor"));
Assert.assertNotNull(Whitebox.getInternalState(tasks, "pending"));
public void testSetup() throws Exception {
Assert.assertNotNull(getField(tasks, "executor"));
Assert.assertNotNull(getField(tasks, "pending"));
}

@Test
public void testShutdown() {
public void testShutdown() throws Exception {
Tasks other = new Tasks("test", null);
other.run(new Tasks.Task<Object>(0L) {
@Override
Expand All @@ -43,8 +49,8 @@ public Object call() throws Exception {
long now = System.nanoTime();
other.shutdown();
long timeToShutdown = TimeUtils.nsToMs(System.nanoTime() - now);
Assert.assertTrue(Whitebox.<ExecutorService>getInternalState(other, "executor").isShutdown());
Assert.assertTrue(Whitebox.<ExecutorService>getInternalState(other, "executor").isTerminated());
Assert.assertTrue(((ExecutorService) getField(other, "executor")).isShutdown());
Assert.assertTrue(((ExecutorService) getField(other, "executor")).isTerminated());
//Assert.assertTrue(timeToShutdown > 100);//todo, this line fails when trying to publish (AK, 12.12.18)
}

Expand Down
Loading