Skip to content

Commit 8f1e9a5

Browse files
chore(ci): update to jdk 17 (#34)
* update to jdk 17 without using any features * re ignore test it fails in CI but not locally * use some more modern java * add missing imports --------- Co-authored-by: Harry Waye <[email protected]>
1 parent cc38e4d commit 8f1e9a5

File tree

8 files changed

+59
-60
lines changed

8 files changed

+59
-60
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
- name: Checkout the repository
1616
uses: actions/checkout@v3
1717

18-
- name: Set up JDK 8
18+
- name: Set up JDK
1919
uses: actions/setup-java@v3
2020
with:
21-
java-version: 8
21+
java-version: 17
2222
distribution: 'adopt'
2323
server-id: ossrh
2424
server-username: MAVEN_USERNAME

posthog/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
</scm>
3333
<properties>
3434
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35-
<maven.compiler.source>1.8</maven.compiler.source>
36-
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>17</maven.compiler.source>
36+
<maven.compiler.target>17</maven.compiler.target>
3737
</properties>
3838

3939
<dependencies>

posthog/src/main/java/com/posthog/java/HttpSender.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import okhttp3.Response;
1616

1717
public class HttpSender implements Sender {
18-
private String apiKey;
19-
private String host;
20-
private OkHttpClient client;
18+
private final String apiKey;
19+
private final String host;
20+
private final OkHttpClient client;
2121
private int maxRetries;
2222
private Duration initialRetryInterval;
2323

@@ -140,7 +140,7 @@ public Boolean send(List<JSONObject> events) {
140140
}
141141

142142
private String getRequestBody(List<JSONObject> events) {
143-
JSONObject jsonObject = new JSONObject();
143+
var jsonObject = new JSONObject();
144144
try {
145145
jsonObject.put("api_key", apiKey);
146146
jsonObject.put("batch", events);

posthog/src/main/java/com/posthog/java/PostHog.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.json.JSONObject;
1010

1111
public class PostHog {
12-
private QueueManager queueManager;
12+
private final QueueManager queueManager;
1313
private Thread queueManagerThread;
1414

1515
private static abstract class BuilderBase {
@@ -107,7 +107,7 @@ public void capture(String distinctId, String event) {
107107
* set without overwriting previous values.
108108
*/
109109
public void identify(String distinctId, Map<String, Object> properties, Map<String, Object> propertiesSetOnce) {
110-
Map<String, Object> props = new HashMap<String, Object>();
110+
var props = new HashMap<String, Object>();
111111
if (properties != null) {
112112
props.put("$set", properties);
113113
}
@@ -137,7 +137,7 @@ public void identify(String distinctId, Map<String, Object> properties) {
137137
* overriden.
138138
*/
139139
public void alias(String distinctId, String alias) {
140-
Map<String, Object> props = new HashMap<String, Object>() {
140+
var props = new HashMap<String, Object>() {
141141
{
142142
put("distinct_id", distinctId);
143143
put("alias", alias);
@@ -153,7 +153,7 @@ public void alias(String distinctId, String alias) {
153153
* @param properties an array with any person properties you'd like to set.
154154
*/
155155
public void set(String distinctId, Map<String, Object> properties) {
156-
Map<String, Object> props = new HashMap<String, Object>() {
156+
var props = new HashMap<String, Object>() {
157157
{
158158
put("$set", properties);
159159
}
@@ -169,7 +169,7 @@ public void set(String distinctId, Map<String, Object> properties) {
169169
* Previous values will not be overwritten.
170170
*/
171171
public void setOnce(String distinctId, Map<String, Object> properties) {
172-
Map<String, Object> props = new HashMap<String, Object>() {
172+
var props = new HashMap<String, Object>() {
173173
{
174174
put("$set_once", properties);
175175
}
@@ -178,7 +178,7 @@ public void setOnce(String distinctId, Map<String, Object> properties) {
178178
}
179179

180180
private JSONObject getEventJson(String event, String distinctId, Map<String, Object> properties) {
181-
JSONObject eventJson = new JSONObject();
181+
var eventJson = new JSONObject();
182182
try {
183183
// Ensure that we generate an identifier for this event such that we can e.g.
184184
// deduplicate server-side any duplicates we may receive.

posthog/src/main/java/com/posthog/java/QueueManager.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import org.json.JSONObject;
1010

1111
public class QueueManager implements Runnable {
12-
class QueuePtr {
12+
static class QueuePtr {
1313
// TODO: not sure if there's a datastructure in Java that gives me these
1414
// operations efficiently, specifically retrieveAndReset
1515
// Not sure if LinkedList and JSONObject are good choices here
16-
public List<JSONObject> ptr = new LinkedList<JSONObject>();
16+
public List<JSONObject> ptr = new LinkedList<>();
1717

1818
public QueuePtr() {
19-
};
19+
}
2020

2121
public synchronized int size() {
2222
return ptr.size();
@@ -35,19 +35,19 @@ public synchronized List<JSONObject> retrieveAndReset() {
3535
return Collections.emptyList();
3636
}
3737
List<JSONObject> cur = ptr;
38-
ptr = new LinkedList<JSONObject>();
38+
ptr = new LinkedList<>();
3939
return cur;
4040
}
4141
}
4242

43-
private QueuePtr queue = new QueuePtr();
43+
private final QueuePtr queue = new QueuePtr();
4444
private volatile boolean stop = false;
4545
private Instant sendAfter;
4646
// builder inputs
47-
private Sender sender;
48-
private int maxQueueSize;
49-
private Duration maxTimeInQueue;
50-
private int sleepMs;
47+
private final Sender sender;
48+
private final int maxQueueSize;
49+
private final Duration maxTimeInQueue;
50+
private final int sleepMs;
5151

5252
public static class Builder {
5353
// required
@@ -102,7 +102,7 @@ public int queueSize() {
102102
}
103103

104104
public void sendAll() {
105-
List<JSONObject> toSend = queue.retrieveAndReset();
105+
var toSend = queue.retrieveAndReset();
106106
updateSendAfter(); // after queue reset but before sending as the latter could take a long time
107107
if (!toSend.isEmpty()) {
108108
sender.send(toSend);

posthog/src/test/java/com/posthog/java/HttpSenderTest.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616

1717
import okhttp3.mockwebserver.MockResponse;
1818
import okhttp3.mockwebserver.MockWebServer;
19-
import okhttp3.mockwebserver.RecordedRequest;
2019

2120
public class HttpSenderTest {
2221

2322
public MockWebServer mockWebServer;
2423
private HttpSender sender;
25-
private String apiKey = "UNIT_TESTING_API_KEY";
2624

2725
@Before
2826
public void setUp() throws IOException {
2927
mockWebServer = new MockWebServer();
3028
mockWebServer.start();
3129

32-
String httpUrl = mockWebServer.url("").toString();
33-
String host = httpUrl.substring(0, httpUrl.length() - 1); // strip trailing /
30+
var httpUrl = mockWebServer.url("").toString();
31+
var host = httpUrl.substring(0, httpUrl.length() - 1); // strip trailing /
32+
var apiKey = "UNIT_TESTING_API_KEY";
3433
sender = new HttpSender.Builder(apiKey).host(host).maxRetries(1).build();
3534
}
3635

@@ -47,11 +46,11 @@ public void testEmpty() {
4746
@Test
4847
public void testOneItem() throws InterruptedException {
4948
mockWebServer.enqueue(new MockResponse());
50-
JSONObject json = new JSONObject("{'key': 'value'}");
51-
List<JSONObject> input = new ArrayList<JSONObject>();
49+
var json = new JSONObject("{'key': 'value'}");
50+
var input = new ArrayList<JSONObject>();
5251
input.add(json);
5352
sender.send(input);
54-
RecordedRequest request = mockWebServer.takeRequest();
53+
var request = mockWebServer.takeRequest();
5554
assertEquals("/batch", request.getPath());
5655
assertThatJson("{\"api_key\":\"UNIT_TESTING_API_KEY\",\"batch\":[{\"key\":\"value\"}]}")
5756
.isEqualTo(request.getBody().readUtf8());
@@ -60,15 +59,15 @@ public void testOneItem() throws InterruptedException {
6059
@Test
6160
public void testMultipleItems() throws InterruptedException {
6261
mockWebServer.enqueue(new MockResponse());
63-
JSONObject json = new JSONObject("{'key': 'value'}");
64-
JSONObject json2 = new JSONObject("{'key2': 'value2'}");
65-
JSONObject json3 = new JSONObject("{'key3': 'value3'}");
62+
var json = new JSONObject("{'key': 'value'}");
63+
var json2 = new JSONObject("{'key2': 'value2'}");
64+
var json3 = new JSONObject("{'key3': 'value3'}");
6665
List<JSONObject> input = new ArrayList<JSONObject>();
6766
input.add(json);
6867
input.add(json2);
6968
input.add(json3);
7069
sender.send(input);
71-
RecordedRequest request = mockWebServer.takeRequest();
70+
var request = mockWebServer.takeRequest();
7271
assertEquals("/batch", request.getPath());
7372
assertThatJson("{\"api_key\":\"UNIT_TESTING_API_KEY\",\"batch\":"
7473
+ "[{\"key\":\"value\"},{\"key2\":\"value2\"},{\"key3\":\"value3\"}]}")
@@ -98,9 +97,9 @@ public void testOnlyMakesOneRequestOnSuccess() throws InterruptedException {
9897
assertEquals(success, true);
9998

10099
// Now verify that we only
101-
RecordedRequest firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
100+
var firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
102101
assertEquals(firstRequest.getPath(), "/batch");
103-
RecordedRequest secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
102+
var secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
104103
assertEquals(secondRequest, null);
105104
}
106105

@@ -116,9 +115,9 @@ public void testDoesNotRetryOnClientErrors() throws InterruptedException {
116115
assertEquals(success, false);
117116

118117
// Now verify that we only
119-
RecordedRequest firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
118+
var firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
120119
assertEquals(firstRequest.getPath(), "/batch");
121-
RecordedRequest secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
120+
var secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
122121
assertEquals(secondRequest, null);
123122
}
124123

@@ -134,9 +133,9 @@ public void testReturnFalseOnRetriesExhausted() throws InterruptedException {
134133
assertEquals(success, false);
135134

136135
// Verify we made two requests
137-
RecordedRequest firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
136+
var firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
138137
assertEquals(firstRequest.getPath(), "/batch");
139-
RecordedRequest secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
138+
var secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
140139
assertEquals(secondRequest.getPath(), "/batch");
141140
}
142141
}

posthog/src/test/java/com/posthog/java/PostHogTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void testCaptureSimple() {
5353
ph.shutdown();
5454
assertEquals(1, sender.calls.size());
5555
assertEquals(1, sender.calls.get(0).size());
56-
JSONObject json = sender.calls.get(0).get(0);
56+
var json = sender.calls.get(0).get(0);
5757
// Assert JSON includes the expected distinct_id, event, and timestamp, ignoring
5858
// any extraneus properties.
5959
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"test event\",\"timestamp\":\"" + instantExpected
@@ -85,7 +85,7 @@ public void testEnsureEventHasGeneratedUuid() {
8585

8686
@Test
8787
public void testCaptureWithProperties() {
88-
ph.capture("test id", "test event", new HashMap<String, Object>() {
88+
ph.capture("test id", "test event", new HashMap<>() {
8989
{
9090
put("movie_id", 123);
9191
put("category", "romcom");
@@ -94,15 +94,15 @@ public void testCaptureWithProperties() {
9494
ph.shutdown();
9595
assertEquals(1, sender.calls.size());
9696
assertEquals(1, sender.calls.get(0).size());
97-
JSONObject json = sender.calls.get(0).get(0);
97+
var json = sender.calls.get(0).get(0);
9898
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"test event\""
9999
+ ",\"properties\":{\"movie_id\":123,\"category\":\"romcom\"},\"timestamp\":\"" + instantExpected
100100
+ "\"}").isEqualTo(new JSONObject(json, "distinct_id", "event", "properties", "timestamp").toString());
101101
}
102102

103103
@Test
104104
public void testIdentifySimple() {
105-
ph.identify("test id", new HashMap<String, Object>() {
105+
ph.identify("test id", new HashMap<>() {
106106
{
107107
put("email", "[email protected]");
108108
put("proUser", false);
@@ -111,7 +111,7 @@ public void testIdentifySimple() {
111111
ph.shutdown();
112112
assertEquals(1, sender.calls.size());
113113
assertEquals(1, sender.calls.get(0).size());
114-
JSONObject json = sender.calls.get(0).get(0);
114+
var json = sender.calls.get(0).get(0);
115115
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$identify\""
116116
+ ",\"properties\":{\"$set\":{\"email\":\"[email protected]\",\"proUser\":false}},\"timestamp\":\""
117117
+ instantExpected + "\"}")
@@ -120,12 +120,12 @@ public void testIdentifySimple() {
120120

121121
@Test
122122
public void testIdentifyWithSetOnce() {
123-
ph.identify("test id", new HashMap<String, Object>() {
123+
ph.identify("test id", new HashMap<>() {
124124
{
125125
put("email", "[email protected]");
126126
put("proUser", false);
127127
}
128-
}, new HashMap<String, Object>() {
128+
}, new HashMap<>() {
129129
{
130130
put("first_location", "colorado");
131131
put("first_number", 5);
@@ -134,7 +134,7 @@ public void testIdentifyWithSetOnce() {
134134
ph.shutdown();
135135
assertEquals(1, sender.calls.size());
136136
assertEquals(1, sender.calls.get(0).size());
137-
JSONObject json = sender.calls.get(0).get(0);
137+
var json = sender.calls.get(0).get(0);
138138
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$identify\""
139139
+ ",\"properties\":{\"$set\":{\"email\":\"[email protected]\",\"proUser\":false}"
140140
+ ",\"$set_once\":{\"first_location\":\"colorado\",\"first_number\":5}" + "},\"timestamp\":\""
@@ -145,7 +145,7 @@ public void testIdentifyWithSetOnce() {
145145

146146
@Test
147147
public void testSet() {
148-
ph.set("test id", new HashMap<String, Object>() {
148+
ph.set("test id", new HashMap<>() {
149149
{
150150
put("email", "[email protected]");
151151
put("proUser", false);
@@ -154,7 +154,7 @@ public void testSet() {
154154
ph.shutdown();
155155
assertEquals(1, sender.calls.size());
156156
assertEquals(1, sender.calls.get(0).size());
157-
JSONObject json = sender.calls.get(0).get(0);
157+
var json = sender.calls.get(0).get(0);
158158
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$set\""
159159
+ ",\"properties\":{\"$set\":{\"email\":\"[email protected]\",\"proUser\":false}},\"timestamp\":\""
160160
+ instantExpected + "\"}")
@@ -163,7 +163,7 @@ public void testSet() {
163163

164164
@Test
165165
public void testSetOnce() {
166-
ph.setOnce("test id", new HashMap<String, Object>() {
166+
ph.setOnce("test id", new HashMap<>() {
167167
{
168168
put("first_location", "colorado");
169169
put("first_number", 5);
@@ -172,7 +172,7 @@ public void testSetOnce() {
172172
ph.shutdown();
173173
assertEquals(1, sender.calls.size());
174174
assertEquals(1, sender.calls.get(0).size());
175-
JSONObject json = sender.calls.get(0).get(0);
175+
var json = sender.calls.get(0).get(0);
176176
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$set_once\""
177177
+ ",\"properties\":{\"$set_once\":{\"first_location\":\"colorado\",\"first_number\":5}"
178178
+ "},\"timestamp\":\"" + instantExpected + "\"}")
@@ -185,7 +185,7 @@ public void testAlias() {
185185
ph.shutdown();
186186
assertEquals(1, sender.calls.size());
187187
assertEquals(1, sender.calls.get(0).size());
188-
JSONObject json = sender.calls.get(0).get(0);
188+
var json = sender.calls.get(0).get(0);
189189
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$create_alias\""
190190
+ ",\"properties\":{\"distinct_id\":\"test id\",\"alias\":\"second id\"}" + ",\"timestamp\":\""
191191
+ instantExpected + "\"}")
@@ -219,7 +219,7 @@ public void testQueueSize3() throws InterruptedException {
219219
assertEquals(2, sender.calls.size());
220220
assertEquals(3, sender.calls.get(0).size());
221221
assertEquals(1, sender.calls.get(1).size());
222-
JSONObject json = sender.calls.get(0).get(0);
222+
var json = sender.calls.get(0).get(0);
223223
assertThatJson(
224224
"{\"distinct_id\":\"id1\",\"event\":\"first batch event\",\"timestamp\":\"" + instantExpected + "\"}")
225225
.isEqualTo(new JSONObject(json, "distinct_id", "event", "timestamp").toString());
@@ -246,9 +246,9 @@ public void testMaxTimeInQueue() throws InterruptedException {
246246
queueManager = new QueueManager.Builder(sender).sleepMs(0).maxTimeInQueue(Duration.ofDays(3))
247247
.maxQueueSize(10000).build();
248248
ph = new PostHog.BuilderWithCustomQueueManager(queueManager).build();
249-
String originalInstant = "2020-02-02T02:02:02Z";
250-
String secondInstant = "2020-02-03T02:02:02Z";
251-
String thirdInstant = "2020-02-09T02:02:02Z";
249+
var originalInstant = "2020-02-02T02:02:02Z";
250+
var secondInstant = "2020-02-03T02:02:02Z";
251+
var thirdInstant = "2020-02-09T02:02:02Z";
252252
updateInstantNow(originalInstant);
253253
ph.capture("id1", "first batch event");
254254
updateInstantNow(secondInstant);
@@ -260,7 +260,7 @@ public void testMaxTimeInQueue() throws InterruptedException {
260260
assertEquals(2, sender.calls.size());
261261
assertEquals(2, sender.calls.get(0).size());
262262
assertEquals(1, sender.calls.get(1).size());
263-
JSONObject json = sender.calls.get(0).get(0);
263+
var json = sender.calls.get(0).get(0);
264264
assertThatJson(
265265
"{\"distinct_id\":\"id1\",\"event\":\"first batch event\",\"timestamp\":\"" + originalInstant + "\"}")
266266
.isEqualTo(new JSONObject(json, "distinct_id", "event", "timestamp").toString());

0 commit comments

Comments
 (0)