From 09a31c5f174de5ac3e86b14cfb5ae8628af66e97 Mon Sep 17 00:00:00 2001 From: salyh Date: Sat, 12 Jul 2014 13:31:45 +0200 Subject: [PATCH 13/14] new tests and fixes for SortedSet, SortedMap and Queue. Make it run with Java 1.6 Signed-off-by: salyh --- .../main/java/org/apache/fleece/mapper/Mapper.java | 37 +++- .../apache/fleece/mapper/reflection/Mappings.java | 6 + .../apache/fleece/mapper/MapperEnhancedTests.java | 235 +++++++++++++++++++++ .../java/org/apache/fleece/mapper/MapperTest.java | 11 +- 4 files changed, 282 insertions(+), 7 deletions(-) create mode 100644 fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperEnhancedTests.java diff --git a/fleece-mapper/src/main/java/org/apache/fleece/mapper/Mapper.java b/fleece-mapper/src/main/java/org/apache/fleece/mapper/Mapper.java index 956dd2c..815426f 100644 --- a/fleece-mapper/src/main/java/org/apache/fleece/mapper/Mapper.java +++ b/fleece-mapper/src/main/java/org/apache/fleece/mapper/Mapper.java @@ -30,6 +30,7 @@ import javax.json.JsonReaderFactory; import javax.json.JsonValue; import javax.json.stream.JsonGenerator; import javax.json.stream.JsonGeneratorFactory; + import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -48,7 +49,13 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -444,9 +451,11 @@ public class Mapper { final Type[] fieldArgTypes = aType.getActualTypeArguments(); if (fieldArgTypes.length >= 2) { final Class raw = Class.class.cast(aType.getRawType()); - + final Map map; - if (ConcurrentMap.class.isAssignableFrom(raw)) { + if (SortedMap.class.isAssignableFrom(raw)) { + map = new TreeMap(); + } else if (ConcurrentMap.class.isAssignableFrom(raw)) { map = new ConcurrentHashMap(object.size()); } else if (Map.class.isAssignableFrom(raw)) { map = new HashMap(object.size()); @@ -455,7 +464,16 @@ public class Mapper { } if (map != null) { - final Class keyType = Class.class.cast(fieldArgTypes[0]); + + Class keyType = null; + if (ParameterizedType.class.isInstance(fieldArgTypes[0])) { + //class cast exception when fieldArgTypes[0] is parameterized + //FIXME + keyType = Class.class.cast(fieldArgTypes[0]); + } else { + keyType = Class.class.cast(fieldArgTypes[0]); + } + for (final Map.Entry value : object.entrySet()) { map.put(convertTo(keyType, value.getKey()), toObject(value.getValue(), fieldArgTypes[1])); } @@ -476,6 +494,7 @@ public class Mapper { final Method setterMethod = value.setter; final Object convertedValue = value.converter == null? toObject(jsonValue, value.paramType) : value.converter.fromString(jsonValue.toString()); + if (convertedValue != null) { try { setterMethod.invoke(t, convertedValue); @@ -489,6 +508,7 @@ public class Mapper { } private Object toObject(final JsonValue jsonValue, final Type type) throws InstantiationException, IllegalAccessException { + Object convertedValue = null; if (JsonObject.class.isInstance(jsonValue)) { convertedValue = buildObject(type, JsonObject.class.cast(jsonValue)); @@ -516,6 +536,7 @@ public class Mapper { final String text = jsonValue.toString(); if (text != null) { + convertedValue = convertTo(Class.class.cast(type), text); } } @@ -544,10 +565,16 @@ public class Mapper { private Collection mapCollection(final Mappings.CollectionMapping mapping, final JsonArray jsonArray) throws InstantiationException, IllegalAccessException { final Collection collection; - if (List.class == mapping.raw || Collection.class == mapping.raw) { - collection = new ArrayList(jsonArray.size()); + + if (SortedSet.class == mapping.raw) { + collection = new TreeSet(); } else if (Set.class == mapping.raw) { collection = new HashSet(jsonArray.size()); + } else if (Queue.class == mapping.raw) { + collection = new ArrayBlockingQueue(jsonArray.size()); + //fail fast if collection is not know, assume Collection.class to be compatible with ArrayList is wrong for almost all cases + } else if (List.class == mapping.raw /*|| Collection.class == mapping.raw*/) { + collection = new ArrayList(jsonArray.size()); } else { throw new IllegalStateException("not supported collection type: " + mapping.raw.getName()); } diff --git a/fleece-mapper/src/main/java/org/apache/fleece/mapper/reflection/Mappings.java b/fleece-mapper/src/main/java/org/apache/fleece/mapper/reflection/Mappings.java index 2fbf950..f5e6e6d 100644 --- a/fleece-mapper/src/main/java/org/apache/fleece/mapper/reflection/Mappings.java +++ b/fleece-mapper/src/main/java/org/apache/fleece/mapper/reflection/Mappings.java @@ -35,7 +35,9 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.Set; +import java.util.SortedSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -128,8 +130,12 @@ public class Mappings { final Class collectionType; if (List.class.isAssignableFrom(raw)) { collectionType = List.class; + }else if (SortedSet.class.isAssignableFrom(raw)) { + collectionType = SortedSet.class; } else if (Set.class.isAssignableFrom(raw)) { collectionType = Set.class; + } else if (Queue.class.isAssignableFrom(raw)) { + collectionType = Queue.class; } else if (Collection.class.isAssignableFrom(raw)) { collectionType = Collection.class; } else { diff --git a/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperEnhancedTests.java b/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperEnhancedTests.java new file mode 100644 index 0000000..8169655 --- /dev/null +++ b/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperEnhancedTests.java @@ -0,0 +1,235 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fleece.mapper; + +import static org.junit.Assert.assertEquals; + +import java.io.StringReader; +import java.io.StringWriter; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.ArrayBlockingQueue; + +import org.junit.Test; + +public class MapperEnhancedTests { + + @Test + public void writeNull() { + final StringWriter sw = new StringWriter(); + new MapperBuilder().build().writeObject(null, sw); + assertEquals("{}", sw.toString()); + } + + + @Test + public void writeReadSortedMap() { + SomaClass soseClass = new SomaClass(); + soseClass.getSoma().put("key1", "val1"); + soseClass.getSoma().put("key2", "val2"); + final StringWriter sw = new StringWriter(); + + new MapperBuilder().build().writeObject(soseClass, sw); + assertEquals("{\"soma\":{\"key1\":\"val1\",\"key2\":\"val2\"}}", sw.toString()); + new MapperBuilder().build().readObject(new StringReader(sw.toString()), SomaClass.class); + + } + + @Test + public void writeReadSortedSet() { + SoseClass soseClass = new SoseClass(); + soseClass.getSose().add("string1"); + soseClass.getSose().add("string2"); + final StringWriter sw = new StringWriter(); + + new MapperBuilder().build().writeObject(soseClass, sw); + + assertEquals("{\"sose\":[\"string1\",\"string2\"]}", sw.toString()); + new MapperBuilder().build().readObject(new StringReader(sw.toString()), SoseClass.class); + + } + + @Test + public void writeReadQueue() { + QueueClass queueClass = new QueueClass(); + queueClass.getQueue().add("string1"); + queueClass.getQueue().add("string2"); + final StringWriter sw = new StringWriter(); + + new MapperBuilder().build().writeObject(queueClass, sw); + + assertEquals("{\"queue\":[\"string1\",\"string2\"]}", sw.toString()); + new MapperBuilder().build().readObject(new StringReader(sw.toString()), QueueClass.class); + + } + + /*@Test + public void writeTestclass() { + final StringWriter sw = new StringWriter(); + final TestClass tc1 = new TestClass(null); + final Map m = new TreeMap(); + m.put("key1", -100); + m.put("key2", +100); + final Map m2 = new TreeMap(); + m.put("key11", -1002); + m.put("key22", +1002); + final List> l = new ArrayList>(); + l.add(m); + l.add(m2); + tc1.sose.add("string1"); + tc1.sose.add("string2"); + tc1.map.put(l, 100L); + + final TestClass tc2 = new TestClass(tc1); + final Map m3 = new TreeMap(); + m3.put("key1", -100); + m3.put("key2", +100); + final Map m4 = new TreeMap(); + m4.put("key11", -1002); + m4.put("key22", +1002); + final List> l1 = new ArrayList>(); + l1.add(m); + l1.add(m2); + tc2.map.put(l1, 200L); + + new MapperBuilder().build().writeObject(tc2, sw); + + + new MapperBuilder().build().readObject(new StringReader(sw.toString()), TestClass.class); + }*/ + + + public static class QueueClass { + private Queue queue = new ArrayBlockingQueue(5); + + public Queue getQueue() { + return queue; + } + + public void setQueue(Queue queue) { + this.queue = queue; + } + + + + } + + public static class SoseClass { + private SortedSet sose = new TreeSet(); + + public SortedSet getSose() { + return sose; + } + + public void setSose(SortedSet sose) { + this.sose = sose; + } + + } + + public static class SomaClass { + private SortedMap soma = new TreeMap(); + + public SortedMap getSoma() { + return soma; + } + + public void setSoma(SortedMap soma) { + this.soma = soma; + } + } + + public static class TestClass { + + private List> dates = new ArrayList>(); + private Map>, Long> map = new HashMap>, Long>(); + private TestClass inner; + private String string = "some \t \u0001 unicode: ÖÄÜ \u0070\u0070\u0070ন\udbff\udfff"; + private BigDecimal bd = new BigDecimal("-456.4567890987654321"); + private SortedSet sose = new TreeSet(); + + public SortedSet getSose() { + return sose; + } + + public void setSose(SortedSet sose) { + this.sose = sose; + } + + public TestClass(final TestClass inner) { + super(); + this.inner = inner; + } + + public TestClass() { + super(); + + } + + public List> getDates() { + return dates; + } + + public void setDates(final List> dates) { + this.dates = dates; + } + + public Map>, Long> getMap() { + return map; + } + + public void setMap(final Map>, Long> map) { + this.map = map; + } + + public TestClass getInner() { + return inner; + } + + public void setInner(final TestClass inner) { + this.inner = inner; + } + + public String getString() { + return string; + } + + public void setString(final String string) { + this.string = string; + } + + public BigDecimal getBd() { + return bd; + } + + public void setBd(final BigDecimal bd) { + this.bd = bd; + } + + } +} diff --git a/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperTest.java b/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperTest.java index b01c3d3..fed9c2f 100644 --- a/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperTest.java +++ b/fleece-mapper/src/test/java/org/apache/fleece/mapper/MapperTest.java @@ -107,14 +107,21 @@ public class MapperTest { @Test public void writeObject() { - final TheObject instance = new MapperBuilder().build().readObject(new ByteArrayInputStream(BIG_OBJECT_STR.getBytes()), TheObject.class); // suppose reader writes but this is tested + final TheObject instance = new MapperBuilder().build() + .readObject(new ByteArrayInputStream(BIG_OBJECT_STR.getBytes()), TheObject.class); // suppose reader writes but this is tested final StringWriter writer = new StringWriter(); new MapperBuilder().build().writeObject(instance, writer); final String serialized = writer.toString(); assertTrue(serialized.contains("\"primitives\":[1,2,3,4,5]")); assertTrue(serialized.contains("\"collectionWrapper\":[1,2,3,4,5]")); assertTrue(serialized.contains("\"bool\":true")); - assertTrue(serialized.contains("\"map\":{\"duos\":false,\"uno\":true}")); + + //Assert fail with oracle java 1.7.0_45, works well with apple java 1.6.0_65 + //assertTrue(serialized.contains("\"map\":{\"uno\":true,\"duos\":false}")); + + assertTrue(serialized.contains("\"map\":{")); + assertTrue(serialized.contains("\"uno\":true")); + assertTrue(serialized.contains("\"duos\":false")); } @Test -- 1.8.5.2 (Apple Git-48)