Skip to content

Commit

Permalink
Replace anonymous classes with lambdas in tests (#2766)
Browse files Browse the repository at this point in the history
Replace anonymous classes with lambdas in tests
  • Loading branch information
panic08 authored Oct 18, 2024
1 parent 8757b0c commit 73768be
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 397 deletions.
27 changes: 6 additions & 21 deletions gson/src/test/java/com/google/gson/GsonBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.text.DateFormat;
Expand Down Expand Up @@ -57,13 +56,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 +87,15 @@ 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");
}
});
(InstanceCreator<CustomClass3>) type -> new CustomClass3("custom-instance"));

assertDefaultGson(gson);
// New GsonBuilder created from `gson` should not have been affected by changes
Expand Down
51 changes: 11 additions & 40 deletions gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -486,23 +485,15 @@ 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");
}
});
(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 @@ -549,21 +540,11 @@ 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");
}
})
(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");
}
})
(InstanceCreator<CustomClass3>) type -> new CustomClass3("custom-instance"))
.create();

assertCustomGson(gson);
Expand All @@ -585,21 +566,11 @@ 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");
}
});
(InstanceCreator<CustomClass3>) type -> new CustomClass3("overwritten custom-instance"));

// `gson` object should not have been affected by changes to new GsonBuilder
assertCustomGson(gson);
Expand Down
9 changes: 2 additions & 7 deletions gson/src/test/java/com/google/gson/GsonTypeAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,8 @@ 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) {
Expand Down
29 changes: 13 additions & 16 deletions gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,20 @@ 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);
}
} catch (Throwable t) {
error.set(t);
} finally {
finishedLatch.countDown();
() -> {
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();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,12 @@ 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 @@ -170,15 +167,7 @@ public void testCustomDeserializerReturnsNullForTopLevelObject() {
Gson gson =
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "{baseName:'Base',subName:'SubRevised'}";
Base target = gson.fromJson(json, Base.class);
Expand All @@ -190,15 +179,7 @@ public void testCustomDeserializerReturnsNull() {
Gson gson =
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "{base:{baseName:'Base',subName:'SubRevised'}}";
ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class);
Expand All @@ -210,15 +191,7 @@ public void testCustomDeserializerReturnsNullForArrayElements() {
Gson gson =
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null)
.create();
String json = "[{baseName:'Base'},{baseName:'Base'}]";
Base[] target = gson.fromJson(json, Base[].class);
Expand All @@ -231,15 +204,7 @@ public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() {
Gson gson =
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonDeserializer<Base>() {
@Override
public Base deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
}
})
Base.class, (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 @@ -23,15 +23,13 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.Base;
import com.google.gson.common.TestTypes.BaseSerializer;
import com.google.gson.common.TestTypes.ClassWithBaseArrayField;
import com.google.gson.common.TestTypes.ClassWithBaseField;
import com.google.gson.common.TestTypes.Sub;
import com.google.gson.common.TestTypes.SubSerializer;
import java.lang.reflect.Type;
import org.junit.Test;

/**
Expand Down Expand Up @@ -98,14 +96,7 @@ public void testSerializerReturnsNull() {
Gson gson =
new GsonBuilder()
.registerTypeAdapter(
Base.class,
new JsonSerializer<Base>() {
@Override
public JsonElement serialize(
Base src, Type typeOfSrc, JsonSerializationContext context) {
return null;
}
})
Base.class, (JsonSerializer<Base>) (src, typeOfSrc, context) -> null)
.create();
JsonElement json = gson.toJsonTree(new Base());
assertThat(json.isJsonNull()).isTrue();
Expand Down
Loading

0 comments on commit 73768be

Please sign in to comment.