|
2 | 2 |
|
3 | 3 | import org.junit.Test; |
4 | 4 |
|
| 5 | +import javax.annotation.Nullable; |
5 | 6 | import java.time.Instant; |
6 | 7 | import java.util.HashMap; |
7 | 8 | import java.util.LinkedList; |
8 | 9 | import java.util.List; |
9 | 10 | import java.util.Map; |
10 | 11 |
|
11 | | -import javax.annotation.Nullable; |
12 | | - |
13 | | - |
14 | 12 | import static org.junit.Assert.assertArrayEquals; |
15 | 13 | import static org.junit.Assert.assertEquals; |
16 | 14 | import static org.junit.Assert.assertThrows; |
@@ -44,15 +42,17 @@ public void keysString_valsSupportedTypes_works() { |
44 | 42 | map.put("string", "Grüezi"); |
45 | 43 | map.put("boolean", true); |
46 | 44 | map.put("long", 1L); |
47 | | -// map.put("float", 1.3f); |
| 45 | + map.put("float", 1.3f); |
48 | 46 | map.put("double", -1.4d); |
49 | | - convertAndBackThenAssert(map, converter); |
| 47 | + Map<String, Object> restoredMap = convertAndBack(map, converter); |
| 48 | + // Java float is returned as double, so expect double. |
| 49 | + map.put("float", (double) 1.3f); |
| 50 | + assertEquals(map, restoredMap); |
50 | 51 | } |
51 | 52 |
|
52 | 53 | /** |
53 | 54 | * If no item is wider than 32 bits, all integers are restored as Integer. |
54 | 55 | */ |
55 | | - @SuppressWarnings("unchecked") |
56 | 56 | @Test |
57 | 57 | public void keysString_valsIntegersBiggest32Bit_works() { |
58 | 58 | FlexMapConverter converter = new StringFlexMapConverter(); |
@@ -90,7 +90,6 @@ public void keysString_valsLongBiggest32Bit_works() { |
90 | 90 | /** |
91 | 91 | * If at least one item is 64 bit wide, all integers are restored as Long. |
92 | 92 | */ |
93 | | - @SuppressWarnings("unchecked") |
94 | 93 | @Test |
95 | 94 | public void keysString_valsIntegersBiggest64Bit_works() { |
96 | 95 | FlexMapConverter converter = new StringFlexMapConverter(); |
@@ -109,7 +108,6 @@ public void keysString_valsIntegersBiggest64Bit_works() { |
109 | 108 | } |
110 | 109 |
|
111 | 110 | // Note: can't use assertEquals(map, map) as byte[] does not implement equals(obj). |
112 | | - @SuppressWarnings("unchecked") |
113 | 111 | @Test |
114 | 112 | public void keysString_valsByteArray_works() { |
115 | 113 | FlexMapConverter converter = new StringFlexMapConverter(); |
@@ -184,21 +182,24 @@ public void nestedList_works() { |
184 | 182 | embeddedList1.add("Grüezi"); |
185 | 183 | embeddedList1.add(true); |
186 | 184 | embeddedList1.add(-2L); |
187 | | -// embeddedList1.add(1.3f); |
| 185 | + embeddedList1.add(1.3f); |
188 | 186 | embeddedList1.add(-1.4d); |
189 | 187 | map.put("Hello", embeddedList1); |
190 | 188 | List<Object> embeddedList2 = new LinkedList<>(); |
191 | 189 | embeddedList2.add("Grüezi"); |
192 | 190 | embeddedList2.add(true); |
193 | 191 | embeddedList2.add(-22L); |
194 | | -// embeddedList2.add(2.3f); |
| 192 | + embeddedList2.add(2.3f); |
195 | 193 | embeddedList2.add(-2.4d); |
196 | 194 | map.put("💡", embeddedList2); |
197 | | - convertAndBackThenAssert(map, converter); |
| 195 | + Map<String, List<Object>> restoredMap = convertAndBack(map, converter); |
| 196 | + // Java float is returned as double, so expect double. |
| 197 | + embeddedList1.set(3, (double) 1.3f); |
| 198 | + embeddedList2.set(3, (double) 2.3f); |
| 199 | + assertEquals(map, restoredMap); |
198 | 200 | } |
199 | 201 |
|
200 | 202 | // Note: can't use assertEquals(map, map) as byte[] does not implement equals(obj). |
201 | | - @SuppressWarnings("unchecked") |
202 | 203 | @Test |
203 | 204 | public void nestedListByteArray_works() { |
204 | 205 | FlexMapConverter converter = new StringFlexMapConverter(); |
@@ -237,15 +238,14 @@ public void unsupportedValue_throws() { |
237 | 238 | convertThenAssertThrows(map, converter); |
238 | 239 | } |
239 | 240 |
|
240 | | - @SuppressWarnings({"rawtypes", "unchecked"}) |
241 | | - private Map convertAndBack(@Nullable Map expected, FlexMapConverter converter) { |
242 | | - byte[] converted = converter.convertToDatabaseValue(expected); |
| 241 | + @SuppressWarnings("unchecked") |
| 242 | + private <K, V> Map<K, V> convertAndBack(@Nullable Map<K, V> expected, FlexMapConverter converter) { |
| 243 | + byte[] converted = converter.convertToDatabaseValue((Map<Object, Object>) expected); |
243 | 244 |
|
244 | | - return converter.convertToEntityProperty(converted); |
| 245 | + return (Map<K, V>) converter.convertToEntityProperty(converted); |
245 | 246 | } |
246 | 247 |
|
247 | | - @SuppressWarnings({"rawtypes"}) |
248 | | - private void convertAndBackThenAssert(@Nullable Map expected, FlexMapConverter converter) { |
| 248 | + private <K, V> void convertAndBackThenAssert(@Nullable Map<K, V> expected, FlexMapConverter converter) { |
249 | 249 | assertEquals(expected, convertAndBack(expected, converter)); |
250 | 250 | } |
251 | 251 |
|
|