|
14 | 14 |
|
15 | 15 | package com.fasterxml.jackson.dataformat.ion; |
16 | 16 |
|
17 | | -import java.io.IOException; |
18 | 17 | import org.junit.Assert; |
19 | 18 | import org.junit.Test; |
20 | 19 | import com.amazon.ion.IonSystem; |
| 20 | +import com.amazon.ion.IonValue; |
21 | 21 | import com.amazon.ion.system.IonSystemBuilder; |
22 | 22 | import com.fasterxml.jackson.databind.SerializationFeature; |
23 | 23 |
|
24 | | -public class EnumAsIonSymbolSerializationTest { |
| 24 | +public class EnumAsIonSymbolSerializationTest |
| 25 | +{ |
| 26 | + private enum SomeEnum { |
| 27 | + SOME_VALUE; |
| 28 | + |
| 29 | + @Override |
| 30 | + public String toString() { |
| 31 | + return name().toLowerCase(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
25 | 35 | private static final IonSystem ION_SYSTEM = IonSystemBuilder.standard().build(); |
26 | 36 |
|
27 | 37 | @Test |
28 | | - public void testUsingName() throws IOException { |
29 | | - final IonObjectMapper mapper = newMapper(); |
30 | | - |
31 | | - Assert.assertEquals( |
32 | | - ION_SYSTEM.singleValue("SOME_VALUE"), |
33 | | - mapper.writeValueAsIonValue(SomeEnum.SOME_VALUE)); |
| 38 | + public void testUsingName() throws Exception |
| 39 | + { |
| 40 | + final IonValue EXP = ION_SYSTEM.singleValue("SOME_VALUE"); |
| 41 | + Assert.assertEquals(EXP, |
| 42 | + newMapper(false, false).writeValueAsIonValue(SomeEnum.SOME_VALUE)); |
| 43 | + Assert.assertEquals(EXP, |
| 44 | + newMapper(true, false).writeValueAsIonValue(SomeEnum.SOME_VALUE)); |
34 | 45 | } |
35 | 46 |
|
36 | 47 | @Test |
37 | | - public void testUsingToString() throws IOException { |
38 | | - final IonObjectMapper mapper = newMapper(); |
39 | | - |
40 | | - mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); |
41 | | - |
42 | | - Assert.assertEquals( |
43 | | - ION_SYSTEM.singleValue("some_value"), |
44 | | - mapper.writeValueAsIonValue(SomeEnum.SOME_VALUE)); |
| 48 | + public void testUsingToString() throws Exception |
| 49 | + { |
| 50 | + final IonValue EXP = ION_SYSTEM.singleValue("some_value"); |
| 51 | + Assert.assertEquals(EXP, |
| 52 | + newMapper(false, true).writeValueAsIonValue(SomeEnum.SOME_VALUE)); |
| 53 | + Assert.assertEquals(EXP, |
| 54 | + newMapper(true, true).writeValueAsIonValue(SomeEnum.SOME_VALUE)); |
45 | 55 | } |
46 | 56 |
|
47 | | - private static IonObjectMapper newMapper() { |
48 | | - final IonObjectMapper mapper = new IonObjectMapper(new IonFactory(null, ION_SYSTEM)); |
49 | | - mapper.registerModule(new EnumAsIonSymbolModule()); |
| 57 | + private static IonObjectMapper newMapper(boolean textual, boolean usingToString) { |
| 58 | + final IonFactory f = (textual |
| 59 | + ? IonFactory.builderForTextualWriters() |
| 60 | + : IonFactory.builderForBinaryWriters() |
| 61 | + ) |
| 62 | + .ionSystem(ION_SYSTEM) |
| 63 | + .build(); |
| 64 | + final IonObjectMapper mapper = IonObjectMapper.builder(f) |
| 65 | + .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, usingToString) |
| 66 | + .addModule(new EnumAsIonSymbolModule()) |
| 67 | + .build(); |
50 | 68 | return mapper; |
51 | 69 | } |
52 | | - |
53 | | - private enum SomeEnum { |
54 | | - SOME_VALUE; |
55 | | - |
56 | | - @Override |
57 | | - public String toString() { |
58 | | - return name().toLowerCase(); |
59 | | - } |
60 | | - } |
61 | 70 | } |
0 commit comments