blob: ffd2fe68c6f6e3a3f71f3da3e94b0bef0b722800 [file] [log] [blame]
Tatu Salorantab254aff2016-04-17 20:47:48 -07001package com.fasterxml.jackson.databind.jsontype;
2
3import java.util.concurrent.atomic.AtomicReference;
4
5import com.fasterxml.jackson.annotation.JsonSubTypes;
6import com.fasterxml.jackson.annotation.JsonTypeInfo;
Tatu Saloranta779411e2016-04-19 16:33:09 -07007
Tatu Salorantab254aff2016-04-17 20:47:48 -07008import com.fasterxml.jackson.databind.*;
Tatu Saloranta2520c292016-04-19 19:53:48 -07009import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
Tatu Saloranta9da1f1b2019-05-01 21:51:41 -070010import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator;
Tatu Salorantab254aff2016-04-17 20:47:48 -070011
12public class PolymorphicViaRefTypeTest extends BaseMapTest
13{
14
15 @JsonSubTypes({
16 @JsonSubTypes.Type(name = "impl5", value = ImplForAtomic.class)
17 })
18 static class BaseForAtomic {
19 }
20
21 static class ImplForAtomic extends BaseForAtomic {
22 public int x;
23
24 protected ImplForAtomic() { }
25 public ImplForAtomic(int x) { this.x = x; }
26 }
27
28 static class TypeInfoAtomic {
29 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "$type")
30 public AtomicReference<BaseForAtomic> value;
31 }
32
Tatu Saloranta2520c292016-04-19 19:53:48 -070033 static class AtomicStringWrapper {
34 public AtomicReference<StringWrapper> wrapper;
35
36 protected AtomicStringWrapper() { }
37 public AtomicStringWrapper(String str) {
38 wrapper = new AtomicReference<StringWrapper>(new StringWrapper(str));
39 }
40 }
41
Tatu Salorantab254aff2016-04-17 20:47:48 -070042 /*
43 /**********************************************************************
44 /* Test methods
45 /**********************************************************************
46 */
47
48 private final ObjectMapper MAPPER = objectMapper();
49
Tatu Saloranta779411e2016-04-19 16:33:09 -070050 public void testPolymorphicAtomicRefProperty() throws Exception
Tatu Salorantab254aff2016-04-17 20:47:48 -070051 {
52 TypeInfoAtomic data = new TypeInfoAtomic();
53 data.value = new AtomicReference<BaseForAtomic>(new ImplForAtomic(42));
54 String json = MAPPER.writeValueAsString(data);
Tatu Salorantab254aff2016-04-17 20:47:48 -070055 TypeInfoAtomic result = MAPPER.readValue(json, TypeInfoAtomic.class);
56 assertNotNull(result);
Tatu Saloranta131a4052016-04-19 20:04:09 -070057 BaseForAtomic value = result.value.get();
58 assertNotNull(value);
59 assertEquals(ImplForAtomic.class, value.getClass());
60 assertEquals(42, ((ImplForAtomic) value).x);
Tatu Salorantab254aff2016-04-17 20:47:48 -070061 }
Tatu Saloranta2520c292016-04-19 19:53:48 -070062
63 public void testAtomicRefViaDefaultTyping() throws Exception
64 {
Tatu Saloranta9da1f1b2019-05-01 21:51:41 -070065 ObjectMapper mapper = jsonMapperBuilder()
Tatu Salorantaac787902019-08-20 20:43:34 -070066 .activateDefaultTyping(NoCheckSubTypeValidator.instance,
Tatu Saloranta9da1f1b2019-05-01 21:51:41 -070067 DefaultTyping.NON_FINAL)
68 .build();
Tatu Saloranta2520c292016-04-19 19:53:48 -070069 AtomicStringWrapper data = new AtomicStringWrapper("foo");
70 String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data);
71 AtomicStringWrapper result = mapper.readValue(json, AtomicStringWrapper.class);
72 assertNotNull(result);
Tatu Saloranta131a4052016-04-19 20:04:09 -070073 assertNotNull(result.wrapper);
74 assertEquals(AtomicReference.class, result.wrapper.getClass());
75 StringWrapper w = result.wrapper.get();
76 assertEquals("foo", w.str);
Tatu Saloranta2520c292016-04-19 19:53:48 -070077 }
Tatu Salorantab254aff2016-04-17 20:47:48 -070078}