• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.module;
2 
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.Map;
6 
7 import com.fasterxml.jackson.core.Version;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.jsontype.NamedType;
10 
11 public class SimpleModuleArgCheckTest extends BaseMapTest
12 {
13     /*
14     /**********************************************************
15     /* Unit tests for invalid deserializers
16     /**********************************************************
17      */
18 
testInvalidForDeserializers()19     public void testInvalidForDeserializers() throws Exception
20     {
21         SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
22                 (Map<Class<?>,JsonDeserializer<?>>) null);
23 
24         try {
25             mod.addDeserializer(String.class, null);
26             fail("Should not pass");
27         } catch (IllegalArgumentException e) {
28             verifyException(e, "Cannot pass `null` as deserializer");
29         }
30 
31         try {
32             mod.addKeyDeserializer(String.class, null);
33             fail("Should not pass");
34         } catch (IllegalArgumentException e) {
35             verifyException(e, "Cannot pass `null` as key deserializer");
36         }
37     }
38 
39     /*
40     /**********************************************************
41     /* Unit tests for invalid serializers
42     /**********************************************************
43      */
44 
testInvalidForSerializers()45     public void testInvalidForSerializers() throws Exception
46     {
47         SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
48                 (List<JsonSerializer<?>>) null);
49 
50         try {
51             mod.addSerializer(String.class, null);
52             fail("Should not pass");
53         } catch (IllegalArgumentException e) {
54             verifyException(e, "Cannot pass `null` as serializer");
55         }
56 
57         try {
58             mod.addSerializer((JsonSerializer<?>) null);
59             fail("Should not pass");
60         } catch (IllegalArgumentException e) {
61             verifyException(e, "Cannot pass `null` as serializer");
62         }
63 
64         try {
65             mod.addKeySerializer(String.class, null);
66             fail("Should not pass");
67         } catch (IllegalArgumentException e) {
68             verifyException(e, "Cannot pass `null` as key serializer");
69         }
70     }
71 
72     /*
73     /**********************************************************
74     /* Unit tests for invalid misc other
75     /**********************************************************
76      */
77 
testInvalidAbstractTypeMapping()78     public void testInvalidAbstractTypeMapping() throws Exception
79     {
80         // just for funsies let's use more esoteric constructor
81         Map<Class<?>,JsonDeserializer<?>>  desers = Collections.emptyMap();
82         List<JsonSerializer<?>> sers = Collections.emptyList();
83         SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
84                 desers, sers);
85 
86         try {
87             mod.addAbstractTypeMapping(null, String.class);
88             fail("Should not pass");
89         } catch (IllegalArgumentException e) {
90             verifyException(e, "Cannot pass `null` as abstract type to map");
91         }
92         try {
93             mod.addAbstractTypeMapping(String.class, null);
94             fail("Should not pass");
95         } catch (IllegalArgumentException e) {
96             verifyException(e, "Cannot pass `null` as concrete type to map to");
97         }
98     }
99 
testInvalidSubtypeMappings()100     public void testInvalidSubtypeMappings() throws Exception
101     {
102         SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
103                 null, null);
104         try {
105             mod.registerSubtypes(String.class, null);
106             fail("Should not pass");
107         } catch (IllegalArgumentException e) {
108             verifyException(e, "Cannot pass `null` as subtype to register");
109         }
110 
111         try {
112             mod.registerSubtypes(new NamedType(Integer.class), (NamedType) null);
113             fail("Should not pass");
114         } catch (IllegalArgumentException e) {
115             verifyException(e, "Cannot pass `null` as subtype to register");
116         }
117     }
118 
testInvalidValueInstantiator()119     public void testInvalidValueInstantiator() throws Exception
120     {
121         SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
122 
123         try {
124             mod.addValueInstantiator(null, null);
125             fail("Should not pass");
126         } catch (IllegalArgumentException e) {
127             verifyException(e, "Cannot pass `null` as class to register value instantiator for");
128         }
129         try {
130             mod.addValueInstantiator(CharSequence.class, null);
131             fail("Should not pass");
132         } catch (IllegalArgumentException e) {
133             verifyException(e, "Cannot pass `null` as value instantiator");
134         }
135     }
136 
testInvalidMixIn()137     public void testInvalidMixIn() throws Exception
138     {
139         SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
140 
141         try {
142             mod.setMixInAnnotation(null, String.class);
143             fail("Should not pass");
144         } catch (IllegalArgumentException e) {
145             verifyException(e, "Cannot pass `null` as target type");
146         }
147         try {
148             mod.setMixInAnnotation(String.class, null);
149             fail("Should not pass");
150         } catch (IllegalArgumentException e) {
151             verifyException(e, "Cannot pass `null` as mixin class");
152         }
153     }
154 }
155