Skip to content

Commit fde646f

Browse files
committed
introduce OperationSystem.UNKNOWN to handle future operation systems
1 parent 0a91d05 commit fde646f

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

openwebstart/src/main/java/com/openwebstart/jvm/RemoteRuntimeManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Objects;
2525
import java.util.Optional;
2626
import java.util.concurrent.atomic.AtomicReference;
27+
import java.util.stream.Collectors;
2728

2829
import static com.openwebstart.jvm.runtimes.Vendor.ANY_VENDOR;
2930

openwebstart/src/main/java/com/openwebstart/jvm/json/JsonConstants.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ interface JsonConstants {
1616

1717
String HREF_PROPERTY = "href";
1818

19-
String HASH_PROPERTY = "hash";
20-
2119
String LAST_USAGE_PROPERTY = "lastUsage";
2220

2321
String RUNTIMES_PROPERTY = "runtimes";
2422

23+
String RUNTIMES_1_6_PROPERTY = "runtimes_1.6";
24+
2525
String CACHE_TIME_PROPERTY = "cacheTimeInMillis";
2626
}

openwebstart/src/main/java/com/openwebstart/jvm/json/RemoteRuntimeListSerializer.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import com.google.gson.JsonParseException;
99
import com.google.gson.JsonSerializationContext;
1010
import com.google.gson.JsonSerializer;
11+
import com.openwebstart.jvm.os.OperationSystem;
1112
import com.openwebstart.jvm.runtimes.RemoteJavaRuntime;
1213

1314
import java.lang.reflect.Type;
1415
import java.util.ArrayList;
1516
import java.util.List;
17+
import java.util.Optional;
1618

1719
public class RemoteRuntimeListSerializer implements JsonSerializer<RemoteRuntimeList>, JsonDeserializer<RemoteRuntimeList> {
1820

@@ -37,9 +39,22 @@ public RemoteRuntimeList deserialize(final JsonElement jsonElement, final Type t
3739
final long cacheTime = jsonObject.getAsJsonPrimitive(JsonConstants.CACHE_TIME_PROPERTY).getAsLong();
3840

3941
final JsonArray jsonArray = jsonObject.getAsJsonArray(JsonConstants.RUNTIMES_PROPERTY);
42+
final JsonArray jsonArray_1_6 = jsonObject.getAsJsonArray(JsonConstants.RUNTIMES_1_6_PROPERTY);
4043

4144
final List<RemoteJavaRuntime> runtimes = new ArrayList<>();
42-
jsonArray.forEach(e -> runtimes.add(jsonDeserializationContext.deserialize(e, RemoteJavaRuntime.class)));
45+
Optional.ofNullable(jsonArray).ifPresent(a -> a.forEach(e -> {
46+
final RemoteJavaRuntime deserialize = jsonDeserializationContext.deserialize(e, RemoteJavaRuntime.class);
47+
if (deserialize.getOperationSystem() != OperationSystem.UNKNOWN) {
48+
runtimes.add(deserialize);
49+
}
50+
}));
51+
52+
Optional.ofNullable(jsonArray_1_6).ifPresent(a -> a.forEach(e -> {
53+
final RemoteJavaRuntime deserialize = jsonDeserializationContext.deserialize(e, RemoteJavaRuntime.class);
54+
if (deserialize.getOperationSystem() != OperationSystem.UNKNOWN) {
55+
runtimes.add(deserialize);
56+
}
57+
}));
4358

4459
return new RemoteRuntimeList(runtimes, cacheTime);
4560
}

openwebstart/src/main/java/com/openwebstart/jvm/json/RemoteRuntimeSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public RemoteJavaRuntime deserialize(final JsonElement jsonElement, final Type t
3434

3535
final String version = jsonObject.get(JsonConstants.VERSION_PROPERTY).getAsString();
3636
final String vendor = jsonObject.get(JsonConstants.VENDOR_PROPERTY).getAsString();
37-
final OperationSystem os = OperationSystem.valueOf(jsonObject.get(JsonConstants.OS_PROPERTY).getAsString());
37+
final OperationSystem os = OperationSystem.parse(jsonObject.get(JsonConstants.OS_PROPERTY).getAsString());
3838
final String href = jsonObject.get(JsonConstants.HREF_PROPERTY).getAsString();
3939

4040

openwebstart/src/main/java/com/openwebstart/jvm/os/OperationSystem.java

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public enum OperationSystem {
3333
MACARM64("Mac OS X aarch64", AARCH64),
3434
WIN32("Windows x86", X86),
3535
WIN64("Windows x64", X64, WIN32),
36+
37+
UNKNOWN("Unknown", null)
3638
;
3739

3840
/**
@@ -118,4 +120,12 @@ public static Optional<OperationSystem> getOperationSystem(String osName, String
118120
}
119121
return Optional.empty();
120122
}
123+
124+
public static OperationSystem parse(String os) {
125+
try {
126+
return valueOf(os);
127+
} catch (Exception e) {
128+
return UNKNOWN;
129+
}
130+
}
121131
}

openwebstart/src/test/java/com/openwebstart/jvm/RemoteRuntimeManagerTest.java

+57
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,61 @@ public void testParseRemoteRuntimeJson_AdoptIsConvertedToEclipse() {
303303
assertEquals(1, result.getRuntimes().size());
304304
assertEquals(result.getRuntimes().get(0).getVendor(), ECLIPSE);
305305
}
306+
307+
@Test
308+
public void testParseRemoteRuntimeJson_BrokenVendor() {
309+
// given
310+
final String json = "{\n" +
311+
" \"cacheTimeInMillis\":5000,\n" +
312+
" \"runtimes\":\n" +
313+
" [\n" +
314+
" {\n" +
315+
" \"version\":\"8.0.282\",\n" +
316+
" \"vendor\":\"AdoptOpenJDK\",\n" +
317+
" \"os\":\"BROKEN\",\n" +
318+
" \"href\":\"" + THE_ONE_AND_ONLY_JDK_ZIP + "\"\n" +
319+
" }\n" +
320+
" ]\n" +
321+
"}\n" +
322+
"\n";
323+
324+
// when
325+
RemoteRuntimeList result = RemoteRuntimeManager.getInstance().parseRemoteRuntimeJson(json);
326+
327+
// then
328+
assertEquals(0, result.getRuntimes().size());
329+
}
330+
331+
@Test
332+
public void testParseRemoteRuntimeJson_1_6() {
333+
// given
334+
final String json = "{\n" +
335+
" \"cacheTimeInMillis\":5000,\n" +
336+
" \"runtimes\":\n" +
337+
" [\n" +
338+
" {\n" +
339+
" \"version\":\"8.0.282\",\n" +
340+
" \"vendor\":\"AdoptOpenJDK\",\n" +
341+
" \"os\":\"WIN64\",\n" +
342+
" \"href\":\"" + THE_ONE_AND_ONLY_JDK_ZIP + "\"\n" +
343+
" }\n" +
344+
" ],\n" +
345+
" \"runtimes_1.6\":\n" +
346+
" [\n" +
347+
" {\n" +
348+
" \"version\":\"8.0.282\",\n" +
349+
" \"vendor\":\"AdoptOpenJDK\",\n" +
350+
" \"os\":\"MACARM64\",\n" +
351+
" \"href\":\"" + THE_ONE_AND_ONLY_JDK_ZIP + "\"\n" +
352+
" }\n" +
353+
" ]\n" +
354+
"}\n" +
355+
"\n";
356+
357+
// when
358+
RemoteRuntimeList result = RemoteRuntimeManager.getInstance().parseRemoteRuntimeJson(json);
359+
360+
// then
361+
assertEquals(2, result.getRuntimes().size());
362+
}
306363
}

0 commit comments

Comments
 (0)