-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX/345 : Serializing enums as property or as Map key has different b…
…ehavior in case of toString overriding Signed-off-by: Sébastien RIUS <sebastien.rius@capgemini.com>
- Loading branch information
Showing
2 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/org/eclipse/yasson/serializers/MapToObjectSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 | ||
* which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* Sebastien Rius | ||
******************************************************************************/ | ||
package org.eclipse.yasson.serializers; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
import javax.json.bind.JsonbConfig; | ||
import java.util.EnumMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Test various use-cases with {@code Map} serializer which | ||
* stores Map as JSON object. | ||
*/ | ||
public class MapToObjectSerializerTest { | ||
|
||
/** | ||
* Enum used as key in maps during tests | ||
*/ | ||
enum TestEnum { | ||
ONE, TWO; | ||
|
||
@Override | ||
/** | ||
* Force to lower case to check toString is not used during serialization of maps | ||
*/ | ||
public String toString() { | ||
return this.name().toLowerCase(); | ||
} | ||
} | ||
|
||
/** | ||
* Test serialization of Map with Number keys and String values. | ||
*/ | ||
@Test | ||
public void testSerializeEnumMapToObject() { | ||
Map<TestEnum, Object> map = new EnumMap<>(TestEnum.class); | ||
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withFormatting(true)); | ||
map.put(TestEnum.ONE, "value1"); | ||
map.put(TestEnum.TWO, "value2"); | ||
String json = jsonb.toJson(map); | ||
for (TestEnum e : TestEnum.values()) { | ||
Assert.assertTrue("Enumeration not well serialized", json.contains(e.name())); | ||
} | ||
} | ||
} |