Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.jsontype; |
| 2 | |
| 3 | import java.util.concurrent.atomic.AtomicReference; |
| 4 | |
| 5 | import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 6 | import com.fasterxml.jackson.annotation.JsonTypeInfo; |
Tatu Saloranta | 779411e | 2016-04-19 16:33:09 -0700 | [diff] [blame] | 7 | |
Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 8 | import com.fasterxml.jackson.databind.*; |
Tatu Saloranta | 2520c29 | 2016-04-19 19:53:48 -0700 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; |
Tatu Saloranta | 9da1f1b | 2019-05-01 21:51:41 -0700 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator; |
Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 11 | |
| 12 | public 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 Saloranta | 2520c29 | 2016-04-19 19:53:48 -0700 | [diff] [blame] | 33 | 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 Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 42 | /* |
| 43 | /********************************************************************** |
| 44 | /* Test methods |
| 45 | /********************************************************************** |
| 46 | */ |
| 47 | |
| 48 | private final ObjectMapper MAPPER = objectMapper(); |
| 49 | |
Tatu Saloranta | 779411e | 2016-04-19 16:33:09 -0700 | [diff] [blame] | 50 | public void testPolymorphicAtomicRefProperty() throws Exception |
Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 51 | { |
| 52 | TypeInfoAtomic data = new TypeInfoAtomic(); |
| 53 | data.value = new AtomicReference<BaseForAtomic>(new ImplForAtomic(42)); |
| 54 | String json = MAPPER.writeValueAsString(data); |
Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 55 | TypeInfoAtomic result = MAPPER.readValue(json, TypeInfoAtomic.class); |
| 56 | assertNotNull(result); |
Tatu Saloranta | 131a405 | 2016-04-19 20:04:09 -0700 | [diff] [blame] | 57 | BaseForAtomic value = result.value.get(); |
| 58 | assertNotNull(value); |
| 59 | assertEquals(ImplForAtomic.class, value.getClass()); |
| 60 | assertEquals(42, ((ImplForAtomic) value).x); |
Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 61 | } |
Tatu Saloranta | 2520c29 | 2016-04-19 19:53:48 -0700 | [diff] [blame] | 62 | |
| 63 | public void testAtomicRefViaDefaultTyping() throws Exception |
| 64 | { |
Tatu Saloranta | 9da1f1b | 2019-05-01 21:51:41 -0700 | [diff] [blame] | 65 | ObjectMapper mapper = jsonMapperBuilder() |
Tatu Saloranta | ac78790 | 2019-08-20 20:43:34 -0700 | [diff] [blame] | 66 | .activateDefaultTyping(NoCheckSubTypeValidator.instance, |
Tatu Saloranta | 9da1f1b | 2019-05-01 21:51:41 -0700 | [diff] [blame] | 67 | DefaultTyping.NON_FINAL) |
| 68 | .build(); |
Tatu Saloranta | 2520c29 | 2016-04-19 19:53:48 -0700 | [diff] [blame] | 69 | AtomicStringWrapper data = new AtomicStringWrapper("foo"); |
| 70 | String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data); |
| 71 | AtomicStringWrapper result = mapper.readValue(json, AtomicStringWrapper.class); |
| 72 | assertNotNull(result); |
Tatu Saloranta | 131a405 | 2016-04-19 20:04:09 -0700 | [diff] [blame] | 73 | assertNotNull(result.wrapper); |
| 74 | assertEquals(AtomicReference.class, result.wrapper.getClass()); |
| 75 | StringWrapper w = result.wrapper.get(); |
| 76 | assertEquals("foo", w.str); |
Tatu Saloranta | 2520c29 | 2016-04-19 19:53:48 -0700 | [diff] [blame] | 77 | } |
Tatu Saloranta | b254aff | 2016-04-17 20:47:48 -0700 | [diff] [blame] | 78 | } |