|
| 1 | +/* |
| 2 | + * Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at: |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0/ |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific |
| 12 | + * language governing permissions and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.fasterxml.jackson.dataformat.ion; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | +import com.amazon.ion.IonSystem; |
| 21 | +import com.amazon.ion.system.IonSystemBuilder; |
| 22 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 23 | + |
| 24 | +public class EnumAsIonSymbolSerializationTest { |
| 25 | + private static final IonSystem ION_SYSTEM = IonSystemBuilder.standard().build(); |
| 26 | + |
| 27 | + @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)); |
| 34 | + } |
| 35 | + |
| 36 | + @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)); |
| 45 | + } |
| 46 | + |
| 47 | + private static IonObjectMapper newMapper() { |
| 48 | + final IonObjectMapper mapper = new IonObjectMapper(new IonFactory(null, ION_SYSTEM)); |
| 49 | + mapper.registerModule(new EnumAsIonSymbolModule()); |
| 50 | + return mapper; |
| 51 | + } |
| 52 | + |
| 53 | + private enum SomeEnum { |
| 54 | + SOME_VALUE; |
| 55 | + |
| 56 | + @Override |
| 57 | + public String toString() { |
| 58 | + return name().toLowerCase(); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments