|
| 1 | +package com.fasterxml.jackson.databind.jdk15; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | + |
| 6 | +// for [databind#3305] |
| 7 | +public class CharSequenceDeser3305Test extends BaseMapTest |
| 8 | +{ |
| 9 | + static final class AppId implements CharSequence { |
| 10 | + private final long value; |
| 11 | + |
| 12 | + public AppId(long value) throws IllegalArgumentException { |
| 13 | + this.value = value; |
| 14 | + } |
| 15 | + |
| 16 | + public static AppId valueOf(String value) throws IllegalArgumentException { |
| 17 | + if (value == null) { |
| 18 | + throw new IllegalArgumentException("value is null"); |
| 19 | + } |
| 20 | + return new AppId(Long.parseLong(value)); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public int length() { |
| 25 | + return toString().length(); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public char charAt(int index) { |
| 30 | + return toString().charAt(index); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public CharSequence subSequence(int start, int end) { |
| 35 | + return toString().subSequence(start, end); |
| 36 | + } |
| 37 | + |
| 38 | + // pay attention: no @JsonValue here |
| 39 | + @Override |
| 40 | + public String toString() { |
| 41 | + return Long.toString(value); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static final String APP_ID = "3074457345618296002"; |
| 46 | + |
| 47 | + private final static ObjectMapper MAPPER = newJsonMapper(); |
| 48 | + |
| 49 | + public void testCharSequenceSerialization() throws Exception { |
| 50 | + AppId appId = AppId.valueOf(APP_ID); |
| 51 | + |
| 52 | + String serialized = MAPPER.writeValueAsString(appId); |
| 53 | + |
| 54 | + //Without a fix fails on JDK17 with |
| 55 | + //org.junit.ComparisonFailure: |
| 56 | + //Expected :{"empty":false} |
| 57 | + //Actual :"3074457345618296002" |
| 58 | + assertEquals("\"" + APP_ID + "\"", serialized); |
| 59 | + } |
| 60 | +} |
0 commit comments