aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleArgCheckTest.java
blob: 8de27cd85b48c7048a75f94d1021fcb8dab1a50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.fasterxml.jackson.databind.module;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.NamedType;

public class SimpleModuleArgCheckTest extends BaseMapTest
{
    /*
    /**********************************************************
    /* Unit tests for invalid deserializers
    /**********************************************************
     */

    public void testInvalidForDeserializers() throws Exception
    {
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
                (Map<Class<?>,JsonDeserializer<?>>) null);

        try {
            mod.addDeserializer(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as deserializer");
        }

        try {
            mod.addKeyDeserializer(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as key deserializer");
        }
    }

    /*
    /**********************************************************
    /* Unit tests for invalid serializers
    /**********************************************************
     */

    public void testInvalidForSerializers() throws Exception
    {
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
                (List<JsonSerializer<?>>) null);

        try {
            mod.addSerializer(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as serializer");
        }

        try {
            mod.addSerializer((JsonSerializer<?>) null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as serializer");
        }
        
        try {
            mod.addKeySerializer(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as key serializer");
        }
    }

    /*
    /**********************************************************
    /* Unit tests for invalid misc other
    /**********************************************************
     */

    public void testInvalidAbstractTypeMapping() throws Exception
    {
        // just for funsies let's use more esoteric constructor
        Map<Class<?>,JsonDeserializer<?>>  desers = Collections.emptyMap();
        List<JsonSerializer<?>> sers = Collections.emptyList();
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
                desers, sers);

        try {
            mod.addAbstractTypeMapping(null, String.class);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as abstract type to map");
        }
        try {
            mod.addAbstractTypeMapping(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as concrete type to map to");
        }
    }

    public void testInvalidSubtypeMappings() throws Exception
    {
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
                null, null);
        try {
            mod.registerSubtypes(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as subtype to register");
        }

        try {
            mod.registerSubtypes(new NamedType(Integer.class), (NamedType) null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as subtype to register");
        }
    }

    public void testInvalidValueInstantiator() throws Exception
    {
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion());

        try {
            mod.addValueInstantiator(null, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as class to register value instantiator for");
        }
        try {
            mod.addValueInstantiator(CharSequence.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as value instantiator");
        }
    }

    public void testInvalidMixIn() throws Exception
    {
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion());

        try {
            mod.setMixInAnnotation(null, String.class);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as target type");
        }
        try {
            mod.setMixInAnnotation(String.class, null);
            fail("Should not pass");
        } catch (IllegalArgumentException e) {
            verifyException(e, "Cannot pass `null` as mixin class");
        }
    }
}