From 6d854e31185d737dd9bae35f0900a0fab850aa6e Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 5 Dec 2019 00:37:22 -0800 Subject: [PATCH] DATAGEODE-277 - Fix test failures in JSON Region auto-proxying support. --- .../serialization/json/JSONRegionAdvice.java | 63 +++++++++++++++++-- .../JSONRegionAdviceIntegrationTests.java | 7 +-- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdvice.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdvice.java index 2463fa8d..325c392e 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdvice.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdvice.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.serialization.json; import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; @@ -22,19 +21,26 @@ import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeList import static org.springframework.data.gemfire.util.RegionUtils.toRegionName; import static org.springframework.data.gemfire.util.RegionUtils.toRegionPath; +import java.util.AbstractCollection; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.geode.cache.Region; import org.apache.geode.cache.query.SelectResults; +import org.apache.geode.cache.query.types.CollectionType; +import org.apache.geode.cache.query.types.ObjectType; import org.apache.geode.pdx.JSONFormatter; import org.apache.geode.pdx.PdxInstance; @@ -263,13 +269,13 @@ public class JSONRegionAdvice { if (returnValue instanceof SelectResults && this.convertReturnedCollections) { - Collection results = new ArrayList<>(); + List results = new ArrayList<>(); for (Object obj : (SelectResults) returnValue) { results.add(convertToJson(obj)); } - returnValue = results; + returnValue = new ImmutableSelectResults<>(results); } else { returnValue = convertToJson(returnValue); @@ -282,10 +288,10 @@ public class JSONRegionAdvice { catch (Throwable cause) { handleThrowable(cause); } + return returnValue; } - private boolean isIncludedJsonRegion(Object target) { return target instanceof Region && isIncludedJsonRegion((Region) target); } @@ -369,4 +375,53 @@ public class JSONRegionAdvice { throw new RuntimeException(cause); } } + + private static final class ImmutableSelectResults extends AbstractCollection implements SelectResults { + + private final List results; + + private ImmutableSelectResults(List results) { + this.results = new ArrayList<>(results); + } + + @Override + public List asList() { + return Collections.unmodifiableList(this.results); + } + + @Override + public Set asSet() { + return new HashSet<>(this.results); + } + + @Override + public void setElementType(ObjectType elementType) { + throw new UnsupportedOperationException("Setting element type on an immutable SelectResults object is not supported"); + } + + @Override + public CollectionType getCollectionType() { + throw new UnsupportedOperationException("Not Implemented"); + } + + @Override + public boolean isModifiable() { + return false; + } + + @Override + public Iterator iterator() { + return asList().iterator(); + } + + @Override + public int occurrences(T element) { + return Long.valueOf(asList().stream().filter(item -> item.equals(element)).count()).intValue(); + } + + @Override + public int size() { + return this.results.size(); + } + } } diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdviceIntegrationTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdviceIntegrationTests.java index ba161914..0d2ac425 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdviceIntegrationTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/serialization/json/JSONRegionAdviceIntegrationTests.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.serialization.json; import static org.junit.Assert.assertEquals; @@ -29,13 +28,13 @@ import javax.annotation.Resource; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.geode.cache.Region; -import org.apache.geode.cache.query.SelectResults; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.query.SelectResults; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.gemfire.GemfireOperations; import org.springframework.data.gemfire.repository.sample.Person;