Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 402b8fe

Browse files
authored
Merge pull request #266 from launchdarkly/gw/ch90182/fix-java-7-compat
Fix compatibility with Java 7.
2 parents 89954b5 + 67dea95 commit 402b8fe

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ ext.versions = [
5858
"gson": "2.7",
5959
"guava": "19.0",
6060
"jodaTime": "2.9.3",
61-
"okhttp": "3.14.9", // specify this for the SDK build instead of relying on the transitive dependency from okhttp-eventsource
61+
"okhttp": "3.12.2", // specify this for the SDK build instead of relying on the transitive dependency from okhttp-eventsource
6262
"okhttpEventsource": "1.11.0",
6363
"slf4j": "1.7.21",
6464
"snakeyaml": "1.26",
@@ -445,3 +445,4 @@ gitPublish {
445445
}
446446
commitMessage = 'publishing javadocs'
447447
}
448+

src/main/java/com/launchdarkly/client/LDClientInterface.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public interface LDClientInterface extends Closeable {
5050

5151
/**
5252
* Tracks that a user performed an event, and provides an additional numeric value for custom metrics.
53-
* <p>
54-
* As of this version’s release date, the LaunchDarkly service does not support the {@code metricValue}
55-
* parameter. As a result, calling this overload of {@code track} will not yet produce any different
56-
* behavior from calling {@link #track(String, LDUser, JsonElement)} without a {@code metricValue}.
57-
* Refer to the <a href="https://docs.launchdarkly.com/docs/java-sdk-reference#section-track">SDK reference guide</a> for the latest status.
5853
*
5954
* @param eventName the name of the event
6055
* @param user the user that performed the event
@@ -70,11 +65,6 @@ public interface LDClientInterface extends Closeable {
7065

7166
/**
7267
* Tracks that a user performed an event, and provides an additional numeric value for custom metrics.
73-
* <p>
74-
* As of this version’s release date, the LaunchDarkly service does not support the {@code metricValue}
75-
* parameter. As a result, calling this overload of {@code track} will not yet produce any different
76-
* behavior from calling {@link #trackData(String, LDUser, LDValue)} without a {@code metricValue}.
77-
* Refer to the <a href="https://docs.launchdarkly.com/docs/java-sdk-reference#section-track">SDK reference guide</a> for the latest status.
7868
*
7969
* @param eventName the name of the event
8070
* @param user the user that performed the event

src/main/java/com/launchdarkly/client/value/LDValue.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.base.Function;
44
import com.google.common.collect.ImmutableList;
55
import com.google.common.collect.Iterables;
6+
import com.google.common.collect.Ordering;
67
import com.google.gson.Gson;
78
import com.google.gson.JsonElement;
89
import com.google.gson.annotations.JsonAdapter;
@@ -468,7 +469,9 @@ public int hashCode() {
468469
return ah;
469470
case OBJECT:
470471
int oh = 0;
471-
for (String name: keys()) {
472+
// We sort the keys here to guarantee ordering equivalence with LDValueJsonElement
473+
// wrapping JsonObjects.
474+
for (String name: Ordering.natural().immutableSortedCopy(keys())) {
472475
oh = (oh * 31 + name.hashCode()) * 31 + get(name).hashCode();
473476
}
474477
return oh;

src/test/java/com/launchdarkly/client/LDUserTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.gson.JsonArray;
88
import com.google.gson.JsonElement;
99
import com.google.gson.JsonObject;
10+
import com.google.gson.JsonPrimitive;
1011
import com.google.gson.reflect.TypeToken;
1112
import com.launchdarkly.client.value.LDValue;
1213

@@ -29,6 +30,7 @@
2930
import static org.junit.Assert.assertEquals;
3031
import static org.junit.Assert.assertNotNull;
3132
import static org.junit.Assert.assertNull;
33+
import static org.junit.Assert.assertTrue;
3234

3335
@SuppressWarnings("javadoc")
3436
public class LDUserTest {
@@ -344,8 +346,14 @@ private Map<LDUser, String> getUserPropertiesJsonMap() {
344346
@Test
345347
public void defaultJsonEncodingHasPrivateAttributeNames() {
346348
LDUser user = new LDUser.Builder("userkey").privateName("x").privateEmail("y").build();
347-
String expected = "{\"key\":\"userkey\",\"name\":\"x\",\"email\":\"y\",\"privateAttributeNames\":[\"name\",\"email\"]}";
348-
assertEquals(defaultGson.fromJson(expected, JsonElement.class), defaultGson.toJsonTree(user));
349+
JsonObject serialized = defaultGson.toJsonTree(user).getAsJsonObject();
350+
assertEquals(serialized.get("key").getAsJsonPrimitive().getAsString(), "userkey");
351+
assertEquals(serialized.get("name").getAsJsonPrimitive().getAsString(), "x");
352+
assertEquals(serialized.get("email").getAsJsonPrimitive().getAsString(), "y");
353+
JsonArray privateAttrs = serialized.get("privateAttributeNames").getAsJsonArray();
354+
assertEquals(privateAttrs.size(), 2);
355+
assertTrue(privateAttrs.contains(new JsonPrimitive("name")));
356+
assertTrue(privateAttrs.contains(new JsonPrimitive("email")));
349357
}
350358

351359
@Test

src/test/java/com/launchdarkly/client/TestHttpUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import okhttp3.mockwebserver.MockWebServer;
1818
import okhttp3.tls.HandshakeCertificates;
1919
import okhttp3.tls.HeldCertificate;
20-
import okhttp3.tls.internal.TlsUtil;
2120

2221
class TestHttpUtil {
2322
static MockWebServer makeStartedServer(MockResponse... responses) throws IOException {
@@ -72,9 +71,14 @@ public ServerWithCert() throws IOException, GeneralSecurityException {
7271
.certificateAuthority(1)
7372
.commonName(hostname)
7473
.addSubjectAlternativeName(hostname)
74+
.rsa2048()
7575
.build();
7676

77-
HandshakeCertificates hc = TlsUtil.localhost();
77+
HandshakeCertificates hc = new HandshakeCertificates.Builder()
78+
.addPlatformTrustedCertificates()
79+
.heldCertificate(cert)
80+
.addTrustedCertificate(cert.certificate())
81+
.build();
7882
socketFactory = hc.sslSocketFactory();
7983
trustManager = hc.trustManager();
8084

src/test/java/com/launchdarkly/client/value/LDValueTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.junit.Test;
1111

1212
import java.util.ArrayList;
13+
import java.util.Collections;
1314
import java.util.List;
1415

1516
import static org.junit.Assert.assertEquals;
@@ -277,7 +278,7 @@ public void objectKeysCanBeEnumerated() {
277278
for (String key: LDValue.buildObject().put("1", LDValue.of("x")).put("2", LDValue.of("y")).build().keys()) {
278279
keys.add(key);
279280
}
280-
keys.sort(null);
281+
Collections.sort(keys);
281282
assertEquals(ImmutableList.of("1", "2"), keys);
282283
}
283284

@@ -287,7 +288,7 @@ public void objectValuesCanBeEnumerated() {
287288
for (LDValue value: LDValue.buildObject().put("1", LDValue.of("x")).put("2", LDValue.of("y")).build().values()) {
288289
values.add(value.stringValue());
289290
}
290-
values.sort(null);
291+
Collections.sort(values);
291292
assertEquals(ImmutableList.of("x", "y"), values);
292293
}
293294

0 commit comments

Comments
 (0)