• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.ser;
2 
3 import java.util.concurrent.atomic.AtomicReference;
4 
5 import com.fasterxml.jackson.databind.*;
6 import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
7 
8 public class TestSerializerProvider
9     extends com.fasterxml.jackson.databind.BaseMapTest
10 {
11     static class MyBean {
getX()12         public int getX() { return 3; }
13     }
14 
15     static class NoPropsBean {
16     }
17 
testFindExplicit()18     public void testFindExplicit() throws JsonMappingException
19     {
20         ObjectMapper mapper = new ObjectMapper();
21         SerializationConfig config = mapper.getSerializationConfig();
22         SerializerFactory f = new BeanSerializerFactory(null);
23         DefaultSerializerProvider prov = new DefaultSerializerProvider.Impl().createInstance(config, f);
24 
25         // Should have working default key and null key serializers
26         assertNotNull(prov.findKeySerializer(mapper.constructType(String.class), null));
27         assertNotNull(prov.getDefaultNullKeySerializer());
28         assertNotNull(prov.getDefaultNullValueSerializer());
29         // as well as 'unknown type' one (throws exception)
30         assertNotNull(prov.getUnknownTypeSerializer(getClass()));
31 
32         assertTrue(prov.createInstance(config, f).hasSerializerFor(String.class, null));
33         // call twice to verify it'll be cached (second code path)
34         assertTrue(prov.createInstance(config, f).hasSerializerFor(String.class, null));
35 
36         assertTrue(prov.createInstance(config, f).hasSerializerFor(MyBean.class, null));
37         assertTrue(prov.createInstance(config, f).hasSerializerFor(MyBean.class, null));
38 
39         // And then some negative testing
40         AtomicReference<Throwable> cause = new AtomicReference<Throwable>();
41         assertFalse(prov.createInstance(config, f).hasSerializerFor(NoPropsBean.class, cause));
42         Throwable t = cause.get();
43         // no actual exception: just fails since there are no properties
44         assertNull(t);
45     }
46 }
47