From f660a9fe2d3b51dc4e738288bb8e746a341f5d6f Mon Sep 17 00:00:00 2001 From: Anton Pinsky Date: Wed, 1 Nov 2023 14:40:53 +0100 Subject: [PATCH] [#620]Small Javadoc, type, import corrections. Rewrite one of CollectionsTest's test with stream. Signed-off-by: Anton Pinsky --- .../VariableTypeInheritanceSearch.java | 4 +- .../internal/components/AdapterBinding.java | 2 +- .../BeanManagerInstanceCreator.java | 4 +- .../yasson/internal/model/PropertyModel.java | 2 +- .../yasson/adapters/JsonbTypeAdapterTest.java | 4 +- .../collections/CollectionsTest.java | 47 +++++++++---------- .../defaultmapping/specific/OptionalTest.java | 3 -- .../MapToObjectSerializerTest.java | 14 +++--- 8 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/eclipse/yasson/internal/VariableTypeInheritanceSearch.java b/src/main/java/org/eclipse/yasson/internal/VariableTypeInheritanceSearch.java index 1a144f10..bfc2060a 100644 --- a/src/main/java/org/eclipse/yasson/internal/VariableTypeInheritanceSearch.java +++ b/src/main/java/org/eclipse/yasson/internal/VariableTypeInheritanceSearch.java @@ -83,7 +83,7 @@ Type searchParametrizedType(Type typeToSearch, TypeVariable typeVar) { return matchedGenericType; } parameterizedSubclasses.push(parameterizedType); - return searchParametrizedType(((Class) parameterizedType.getRawType()).getGenericSuperclass(), typeVar); + return searchParametrizedType(((Class) parameterizedType.getRawType()).getGenericSuperclass(), typeVar); } private Type checkSubclassRuntimeInfo(TypeVariable typeVar) { @@ -120,6 +120,6 @@ private static ParameterizedType findParameterizedSuperclass(Type type) { if (!(type instanceof Class)) { throw new JsonbException(Messages.getMessage(MessageKeys.RESOLVE_PARAMETRIZED_TYPE, type)); } - return findParameterizedSuperclass(((Class) type).getGenericSuperclass()); + return findParameterizedSuperclass(((Class) type).getGenericSuperclass()); } } diff --git a/src/main/java/org/eclipse/yasson/internal/components/AdapterBinding.java b/src/main/java/org/eclipse/yasson/internal/components/AdapterBinding.java index d9dffc0c..8a377299 100644 --- a/src/main/java/org/eclipse/yasson/internal/components/AdapterBinding.java +++ b/src/main/java/org/eclipse/yasson/internal/components/AdapterBinding.java @@ -46,7 +46,7 @@ public AdapterBinding(Type fromType, Type toType, JsonbAdapter * During marshalling object property is adapted to this type and result is marshalled. * During unmarshalling object is unmarshalled into this type first, than converted to field type and set. * diff --git a/src/main/java/org/eclipse/yasson/internal/components/BeanManagerInstanceCreator.java b/src/main/java/org/eclipse/yasson/internal/components/BeanManagerInstanceCreator.java index 29e7f024..0e2f60b2 100644 --- a/src/main/java/org/eclipse/yasson/internal/components/BeanManagerInstanceCreator.java +++ b/src/main/java/org/eclipse/yasson/internal/components/BeanManagerInstanceCreator.java @@ -31,7 +31,7 @@ * CDI instance manager. * Instances are created and stored per instance of {@link JsonBinding}. * Calling close on JsonBinding, cleans up Jsonb CDI instances and in case of "dependant" scope its dependencies. - * + *

* CDI API dependency is optional, this class is never referenced / loaded if CDI API is not resolvable. */ public class BeanManagerInstanceCreator implements JsonbComponentInstanceCreator { @@ -71,7 +71,7 @@ public T getOrCreateComponent(Class componentClass) { final T beanInstance = injectionTarget.produce(creationalContext); injectionTarget.inject(beanInstance, creationalContext); injectionTarget.postConstruct(beanInstance); - return new CDIManagedBean(beanInstance, injectionTarget, creationalContext); + return new CDIManagedBean<>(beanInstance, injectionTarget, creationalContext); }).getInstance(); return instance; } diff --git a/src/main/java/org/eclipse/yasson/internal/model/PropertyModel.java b/src/main/java/org/eclipse/yasson/internal/model/PropertyModel.java index 0a243e90..8f074017 100644 --- a/src/main/java/org/eclipse/yasson/internal/model/PropertyModel.java +++ b/src/main/java/org/eclipse/yasson/internal/model/PropertyModel.java @@ -356,7 +356,7 @@ public Object getValue(Object object) { /** * Sets a property. - * + *

* If not writable (final, transient, static), ignores property. * * @param object Object to set value in. diff --git a/src/test/java/org/eclipse/yasson/adapters/JsonbTypeAdapterTest.java b/src/test/java/org/eclipse/yasson/adapters/JsonbTypeAdapterTest.java index 8d918688..266bd96e 100644 --- a/src/test/java/org/eclipse/yasson/adapters/JsonbTypeAdapterTest.java +++ b/src/test/java/org/eclipse/yasson/adapters/JsonbTypeAdapterTest.java @@ -60,7 +60,7 @@ public static class AnnotatedPojo { } @Test - public void testIncompatibleAdapter() throws Exception { + public void testIncompatibleAdapter() { IncompatibleAdapterPojo incompatibleAdapterFieldPojo = new IncompatibleAdapterPojo<>(); incompatibleAdapterFieldPojo.str = "STR"; try { @@ -85,7 +85,7 @@ public void testGenericFieldsMatch() { } @Test - public void testAnnotatedTbox() throws Exception { + public void testAnnotatedTbox() { AnnotatedPojo pojo = new AnnotatedPojo<>(); pojo.box = new Box("STR", 101); String marshalledJson = defaultJsonb.toJson(pojo); diff --git a/src/test/java/org/eclipse/yasson/defaultmapping/collections/CollectionsTest.java b/src/test/java/org/eclipse/yasson/defaultmapping/collections/CollectionsTest.java index d930d3d3..e7ceb987 100644 --- a/src/test/java/org/eclipse/yasson/defaultmapping/collections/CollectionsTest.java +++ b/src/test/java/org/eclipse/yasson/defaultmapping/collections/CollectionsTest.java @@ -24,7 +24,6 @@ import java.util.Deque; import java.util.EnumMap; import java.util.EnumSet; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -32,13 +31,15 @@ import java.util.List; import java.util.Map; import java.util.NavigableMap; -import java.util.Optional; import java.util.SortedMap; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.eclipse.yasson.TestTypeToken; import org.eclipse.yasson.defaultmapping.generics.model.Circle; @@ -87,6 +88,7 @@ public void testListOfNumbers() { numberList.add(10); String result = nullableJsonb.toJson(numberList, new TestTypeToken>(){}.getType()); + assertEquals("[1,2.0,10]", result); } @Test @@ -108,26 +110,19 @@ public void testListOfListsOfStrings() { @Test public void listOfMapsOfListsOfMaps() { - List>>> listOfMapsOfListsOfMaps = new ArrayList<>(); - - for(int i = 0; i < 3; i++) { - Map>> mapOfListsOfMap = new HashMap<>(); - - for(int j = 0; j < 3; j++) { - List> listOfMaps = new ArrayList<>(); - - for(int k = 0; k < 3; k++) { - Map stringIntegerMap = new HashMap<>(); - stringIntegerMap.put("first", 1); - stringIntegerMap.put("second", 2); - stringIntegerMap.put("third", 3); - listOfMaps.add(stringIntegerMap); - } - mapOfListsOfMap.put(String.valueOf(j), listOfMaps); - } - listOfMapsOfListsOfMaps.add(mapOfListsOfMap); - } - + List>>> listOfMapsOfListsOfMaps = IntStream.range(0, 3).mapToObj(i -> + IntStream.range(0, 3).boxed().collect(Collectors.toMap(String::valueOf, j -> + IntStream.range(0, 3).mapToObj(k -> + IntStream.range(1, 4).boxed().collect(Collectors.toMap(l -> switch (l) { + case 1 -> "first"; + case 2 -> "second"; + case 3 -> "third"; + default -> throw new IllegalStateException("Unexpected value: " + l); + }, Function.identity())) + ).toList() + )) + ).toList(); + String expected = "[{\"0\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}],\"1\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}],\"2\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}]},{\"0\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}],\"1\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}],\"2\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}]},{\"0\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}],\"1\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}],\"2\":[{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2},{\"third\":3,\"first\":1,\"second\":2}]}]"; assertEquals(expected, nullableJsonb.toJson(listOfMapsOfListsOfMaps)); ArrayList>>> result = nullableJsonb.fromJson(expected, new TestTypeToken>>>>(){}.getType()); @@ -290,7 +285,7 @@ public static class ConcurrentMapContainer { public void testConcurrentMaps() { // ConcurrentMap ConcurrentMapContainer c = new ConcurrentMapContainer(); - c.concurrentMap = new ConcurrentHashMap(); + c.concurrentMap = new ConcurrentHashMap<>(); c.concurrentMap.put("foo", "fooVal"); c.concurrentMap.put("bar", "barVal"); String expectedJson = "{\"concurrentMap\":{\"bar\":\"barVal\",\"foo\":\"fooVal\"}}"; @@ -299,7 +294,7 @@ public void testConcurrentMaps() { // ConcurrentHashMap c = new ConcurrentMapContainer(); - c.concurrentHashMap = new ConcurrentHashMap(); + c.concurrentHashMap = new ConcurrentHashMap<>(); c.concurrentHashMap.put("foo", "fooVal2"); c.concurrentHashMap.put("bar", "barVal2"); expectedJson = "{\"concurrentHashMap\":{\"bar\":\"barVal2\",\"foo\":\"fooVal2\"}}"; @@ -308,7 +303,7 @@ public void testConcurrentMaps() { // ConcurrentNavigableMap c = new ConcurrentMapContainer(); - c.concurrentNavigableMap = new ConcurrentSkipListMap(); + c.concurrentNavigableMap = new ConcurrentSkipListMap<>(); c.concurrentNavigableMap.put("foo", "fooVal3"); c.concurrentNavigableMap.put("bar", "barVal3"); expectedJson = "{\"concurrentNavigableMap\":{\"bar\":\"barVal3\",\"foo\":\"fooVal3\"}}"; @@ -317,7 +312,7 @@ public void testConcurrentMaps() { // ConcurrentSkipListMap c = new ConcurrentMapContainer(); - c.concurrentSkipListMap = new ConcurrentSkipListMap(); + c.concurrentSkipListMap = new ConcurrentSkipListMap<>(); c.concurrentSkipListMap.put("foo", "fooVal4"); c.concurrentSkipListMap.put("bar", "barVal4"); expectedJson = "{\"concurrentSkipListMap\":{\"bar\":\"barVal4\",\"foo\":\"fooVal4\"}}"; diff --git a/src/test/java/org/eclipse/yasson/defaultmapping/specific/OptionalTest.java b/src/test/java/org/eclipse/yasson/defaultmapping/specific/OptionalTest.java index e1468250..291df09a 100644 --- a/src/test/java/org/eclipse/yasson/defaultmapping/specific/OptionalTest.java +++ b/src/test/java/org/eclipse/yasson/defaultmapping/specific/OptionalTest.java @@ -26,10 +26,7 @@ import org.eclipse.yasson.defaultmapping.specific.model.OptionalWrapper; import org.eclipse.yasson.defaultmapping.specific.model.NotMatchingGettersAndSetters; import org.eclipse.yasson.defaultmapping.specific.model.Street; -import org.eclipse.yasson.internal.JsonBindingBuilder; -import jakarta.json.bind.Jsonb; -import jakarta.json.bind.JsonbBuilder; import java.util.*; /** diff --git a/src/test/java/org/eclipse/yasson/serializers/MapToObjectSerializerTest.java b/src/test/java/org/eclipse/yasson/serializers/MapToObjectSerializerTest.java index 9e6a3f9e..db8c615b 100644 --- a/src/test/java/org/eclipse/yasson/serializers/MapToObjectSerializerTest.java +++ b/src/test/java/org/eclipse/yasson/serializers/MapToObjectSerializerTest.java @@ -40,10 +40,10 @@ public class MapToObjectSerializerTest { enum TestEnum { ONE, TWO; - @Override /** * Force to lower case to check toString is not used during serialization of maps */ + @Override public String toString() { return this.name().toLowerCase(); } @@ -90,17 +90,17 @@ public String toString() { } } - public static class MapObjectIntegerString extends MapObject {}; + public static class MapObjectIntegerString extends MapObject {} - public static class MapObjectBigIntegerString extends MapObject {}; + public static class MapObjectBigIntegerString extends MapObject {} - public static class MapObjectEnumString extends MapObject {}; + public static class MapObjectEnumString extends MapObject {} - public static class MapObjectStringString extends MapObject {}; + public static class MapObjectStringString extends MapObject {} - public static class MapObjectBooleanString extends MapObject {}; + public static class MapObjectBooleanString extends MapObject {} - /** + /** * Test serialization of Map with Number keys and String values. */ @Test