1 package com.fasterxml.jackson.databind.struct;
2 
3 import java.util.Collections;
4 import java.util.EnumMap;
5 import java.util.Map;
6 
7 import com.fasterxml.jackson.core.type.TypeReference;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.exc.MismatchedInputException;
10 
11 public class UnwrapSingleArrayMiscTest extends BaseMapTest
12 {
13     private final ObjectMapper UNWRAPPING_MAPPER = jsonMapperBuilder()
14             .enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
15             .build();
16 
17     /*
18     /**********************************************************
19     /* Tests methods, POJOs
20     /**********************************************************
21      */
22 
testSimplePOJOUnwrapping()23     public void testSimplePOJOUnwrapping() throws Exception
24     {
25         ObjectReader r = UNWRAPPING_MAPPER.readerFor(IntWrapper.class);
26         IntWrapper w = r.readValue(aposToQuotes("[{'i':42}]"));
27         assertEquals(42, w.i);
28 
29         try {
30             r.readValue(aposToQuotes("[{'i':42},{'i':16}]"));
31             fail("Did not throw exception while reading a value from a multi value array");
32         } catch (MismatchedInputException e) {
33             verifyException(e, "more than one value");
34         }
35     }
36 
37     /*
38     /**********************************************************
39     /* Tests methods, Maps
40     /**********************************************************
41      */
42 
43     // [databind#2767]: should work for Maps, too
testSimpleMapUnwrapping()44     public void testSimpleMapUnwrapping() throws Exception
45     {
46         ObjectReader r = UNWRAPPING_MAPPER.readerFor(Map.class);
47         Map<String,Object> m = r.readValue(aposToQuotes("[{'stuff':42}]"));
48         assertEquals(Collections.<String,Object>singletonMap("stuff", Integer.valueOf(42)), m);
49 
50         try {
51             r.readValue(aposToQuotes("[{'i':42},{'i':16}]"));
52             fail("Did not throw exception while reading a value from a multi value array");
53         } catch (MismatchedInputException e) {
54             verifyException(e, "more than one value");
55         }
56     }
57 
testEnumMapUnwrapping()58     public void testEnumMapUnwrapping() throws Exception
59     {
60         ObjectReader r = UNWRAPPING_MAPPER.readerFor(new TypeReference<EnumMap<ABC,Integer>>() { });
61         EnumMap<ABC,Integer> m = r.readValue(aposToQuotes("[{'A':42}]"));
62         EnumMap<ABC,Integer> exp = new EnumMap<>(ABC.class);
63         exp.put(ABC.A, Integer.valueOf(42));
64         assertEquals(exp, m);
65 
66         try {
67             r.readValue(aposToQuotes("[{'A':42},{'B':13}]"));
68             fail("Did not throw exception while reading a value from a multi value array");
69         } catch (MismatchedInputException e) {
70             verifyException(e, "more than one value");
71         }
72     }
73 }
74