Skip to content

Commit

Permalink
Replace anonymous classes with lambdas in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panic08 committed Oct 17, 2024
1 parent 5ec834f commit 26f9e35
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 387 deletions.
31 changes: 9 additions & 22 deletions gson/src/test/java/com/google/gson/GsonBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ public void testCreatingMoreThanOnce() {
assertThat(gson).isNotNull();
assertThat(builder.create()).isNotNull();

builder.setFieldNamingStrategy(
new FieldNamingStrategy() {
@Override
public String translateName(Field f) {
return "test";
}
});
builder.setFieldNamingStrategy(f -> "test");

Gson otherGson = builder.create();
assertThat(otherGson).isNotNull();
Expand Down Expand Up @@ -94,23 +88,16 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException {
out.value("custom-adapter");
}
});

gsonBuilder.registerTypeHierarchyAdapter(
CustomClass2.class,
new JsonSerializer<CustomClass2>() {
@Override
public JsonElement serialize(
CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("custom-hierarchy-adapter");
}
});
CustomClass2.class,
(JsonSerializer<CustomClass2>) (src, typeOfSrc, context) -> new JsonPrimitive("custom-hierarchy-adapter")
);

gsonBuilder.registerTypeAdapter(
CustomClass3.class,
new InstanceCreator<CustomClass3>() {
@Override
public CustomClass3 createInstance(Type type) {
return new CustomClass3("custom-instance");
}
});
CustomClass3.class,
(InstanceCreator<CustomClass3>) type -> new CustomClass3("custom-instance")
);

assertDefaultGson(gson);
// New GsonBuilder created from `gson` should not have been affected by changes
Expand Down
58 changes: 15 additions & 43 deletions gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,23 +486,16 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException {
out.value("custom-adapter");
}
});

gsonBuilder.registerTypeHierarchyAdapter(
CustomClass2.class,
new JsonSerializer<CustomClass2>() {
@Override
public JsonElement serialize(
CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("custom-hierarchy-adapter");
}
});
(JsonSerializer<CustomClass2>) (src, typeOfSrc, context) -> new JsonPrimitive("custom-hierarchy-adapter")
);

gsonBuilder.registerTypeAdapter(
CustomClass3.class,
new InstanceCreator<CustomClass3>() {
@Override
public CustomClass3 createInstance(Type type) {
return new CustomClass3("custom-instance");
}
});
CustomClass3.class,
(InstanceCreator<CustomClass3>) type -> new CustomClass3("custom-instance")
);

assertDefaultGson(gson);
// New GsonBuilder created from `gson` should not have been affected by changes either
Expand Down Expand Up @@ -548,22 +541,13 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException {
}
})
.registerTypeHierarchyAdapter(
CustomClass2.class,
new JsonSerializer<CustomClass2>() {
@Override
public JsonElement serialize(
CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("custom-hierarchy-adapter");
}
})
CustomClass2.class,
(JsonSerializer<CustomClass2>) (src, typeOfSrc, context) -> new JsonPrimitive("custom-hierarchy-adapter")
)
.registerTypeAdapter(
CustomClass3.class,
new InstanceCreator<CustomClass3>() {
@Override
public CustomClass3 createInstance(Type type) {
return new CustomClass3("custom-instance");
}
})
CustomClass3.class,
(InstanceCreator<CustomClass3>) type -> new CustomClass3("custom-instance")
)
.create();

assertCustomGson(gson);
Expand All @@ -585,21 +569,9 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException {
});
gsonBuilder.registerTypeHierarchyAdapter(
CustomClass2.class,
new JsonSerializer<CustomClass2>() {
@Override
public JsonElement serialize(
CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("overwritten custom-hierarchy-adapter");
}
});
(JsonSerializer<CustomClass2>) (src, typeOfSrc, context) -> new JsonPrimitive("overwritten custom-hierarchy-adapter"));
gsonBuilder.registerTypeAdapter(
CustomClass3.class,
new InstanceCreator<CustomClass3>() {
@Override
public CustomClass3 createInstance(Type type) {
return new CustomClass3("overwritten custom-instance");
}
});
CustomClass3.class, (InstanceCreator<CustomClass3>) type -> new CustomClass3("overwritten custom-instance"));

// `gson` object should not have been affected by changes to new GsonBuilder
assertCustomGson(gson);
Expand Down
11 changes: 3 additions & 8 deletions gson/src/test/java/com/google/gson/GsonTypeAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,9 @@ private static void assertSerialized(
boolean registerAbstractHierarchyDeserializer,
Object instance) {
JsonDeserializer<Abstract> deserializer =
new JsonDeserializer<>() {
@Override
public Abstract deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
throw new AssertionError();
}
};
(json, typeOfT, context) -> {
throw new AssertionError();
};
GsonBuilder builder = new GsonBuilder();
if (registerAbstractDeserializer) {
builder.registerTypeAdapter(Abstract.class, deserializer);
Expand Down
33 changes: 15 additions & 18 deletions gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,22 @@ public void testMultiThreadDeserialization() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(10);
for (int taskCount = 0; taskCount < 10; taskCount++) {
executor.execute(
new Runnable() {
@Override
public void run() {
try {
startLatch.await();
for (int i = 0; i < 10; i++) {
MyObject deserialized =
gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class);
assertThat(deserialized.a).isEqualTo("hello");
assertThat(deserialized.b).isEqualTo("world");
assertThat(deserialized.i).isEqualTo(1);
() -> {
try {
startLatch.await();
for (int i = 0; i < 10; i++) {
MyObject deserialized =
gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class);
assertThat(deserialized.a).isEqualTo("hello");
assertThat(deserialized.b).isEqualTo("world");
assertThat(deserialized.i).isEqualTo(1);
}
} catch (Throwable t) {
error.set(t);
} finally {
finishedLatch.countDown();
}
} catch (Throwable t) {
error.set(t);
} finally {
finishedLatch.countDown();
}
}
});
});
}
startLatch.countDown();
finishedLatch.await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,11 @@ public void testJsonTypeFieldBasedDeserialization() {
new GsonBuilder()
.registerTypeAdapter(
MyBase.class,
new JsonDeserializer<MyBase>() {
@Override
public MyBase deserialize(
JsonElement json, Type pojoType, JsonDeserializationContext context)
throws JsonParseException {
String type = json.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString();
return context.deserialize(json, SubTypes.valueOf(type).getSubclass());
}
})
(JsonDeserializer<MyBase>) (json1, pojoType, context) -> {
String type = json1.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString();

return context.deserialize(json1, SubTypes.valueOf(type).getSubclass());
})
.create();
SubType1 target = (SubType1) gson.fromJson(json, MyBase.class);
assertThat(target.field1).isEqualTo("abc");
Expand Down Expand Up @@ -171,14 +167,7 @@ public void testCustomDeserializerReturnsNullForTopLevelObject() {
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
(JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "{baseName:'Base',subName:'SubRevised'}";
Base target = gson.fromJson(json, Base.class);
Expand All @@ -191,14 +180,7 @@ public void testCustomDeserializerReturnsNull() {
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
(JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "{base:{baseName:'Base',subName:'SubRevised'}}";
ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class);
Expand All @@ -211,14 +193,7 @@ public void testCustomDeserializerReturnsNullForArrayElements() {
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
(JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "[{baseName:'Base'},{baseName:'Base'}]";
Base[] target = gson.fromJson(json, Base[].class);
Expand All @@ -232,14 +207,7 @@ public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() {
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
(JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "{bases:[{baseName:'Base'},{baseName:'Base'}]}";
ClassWithBaseArray target = gson.fromJson(json, ClassWithBaseArray.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ public void testSerializerReturnsNull() {
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonSerializer<Base>() {
@Override
public JsonElement serialize(
Base src, Type typeOfSrc, JsonSerializationContext context) {
return null;
}
})
(JsonSerializer<Base>) (src, typeOfSrc, context) -> null)
.create();
JsonElement json = gson.toJsonTree(new Base());
assertThat(json.isJsonNull()).isTrue();
Expand Down
Loading

0 comments on commit 26f9e35

Please sign in to comment.