From d497011eec22dd71ca5ccac7585988b1a1ceedc4 Mon Sep 17 00:00:00 2001 From: JonasG Date: Mon, 4 Mar 2024 13:11:53 +0100 Subject: [PATCH] fix: allow deserializing tag with value and multiple attributes --- .../main/java/io/jonasg/xjx/serdes/Path.java | 6 +- .../TagAttributeDeserializationTest.java | 105 +++++++++++++----- 2 files changed, 83 insertions(+), 28 deletions(-) diff --git a/xjx-serdes/src/main/java/io/jonasg/xjx/serdes/Path.java b/xjx-serdes/src/main/java/io/jonasg/xjx/serdes/Path.java index 18f9169..e4adc7d 100644 --- a/xjx-serdes/src/main/java/io/jonasg/xjx/serdes/Path.java +++ b/xjx-serdes/src/main/java/io/jonasg/xjx/serdes/Path.java @@ -35,8 +35,10 @@ public static Path parse(String path) { } public Path appendAttribute(String attribute) { - this.attribute = attribute; - return this; + var copiedSections = new LinkedList<>(this.sections); + var path = new Path(copiedSections); + path.attribute = attribute; + return path; } public Path append(String section) { diff --git a/xjx-serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/TagAttributeDeserializationTest.java b/xjx-serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/TagAttributeDeserializationTest.java index a7a3dac..57f6062 100644 --- a/xjx-serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/TagAttributeDeserializationTest.java +++ b/xjx-serdes/src/test/java/io/jonasg/xjx/serdes/deserialize/TagAttributeDeserializationTest.java @@ -1,8 +1,9 @@ package io.jonasg.xjx.serdes.deserialize; +import static org.assertj.core.api.Assertions.assertThat; + import java.math.BigDecimal; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -25,25 +26,43 @@ void deserialize_StringField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.String).isEqualTo("11"); + assertThat(dataTypes.String).isEqualTo("11"); } - @Test - void deserialize_IntegerField() { - // given - String data = """ + @Test + void deserialize_IntegerField() { + // given + String data = """ """; - // when - DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); + // when + DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); - // then - Assertions.assertThat(dataTypes.Integer).isEqualTo(11); - } + // then + assertThat(dataTypes.Integer).isEqualTo(11); + } + + + @Test + void deserialize_primitiveIntField() { + // given + String data = """ + + + + + """; + + // when + DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); + + // then + assertThat(dataTypes.primitiveInt).isEqualTo(11); + } @Test void deserialize_LongField() { @@ -59,7 +78,7 @@ void deserialize_LongField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.Long).isEqualTo(11L); + assertThat(dataTypes.Long).isEqualTo(11L); } @Test @@ -76,7 +95,7 @@ void deserialize_primitiveLongField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.primitiveLong).isEqualTo(11L); + assertThat(dataTypes.primitiveLong).isEqualTo(11L); } @Test @@ -93,7 +112,7 @@ void deserialize_BigDecimalField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.BigDecimal).isEqualTo(BigDecimal.valueOf(11)); + assertThat(dataTypes.BigDecimal).isEqualTo(BigDecimal.valueOf(11)); } @Test @@ -110,7 +129,7 @@ void deserialize_DoubleField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.Double).isEqualTo(Double.valueOf(11)); + assertThat(dataTypes.Double).isEqualTo(Double.valueOf(11)); } @Test @@ -127,7 +146,7 @@ void deserialize_primitiveDoubleField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.primitiveDouble).isEqualTo(Double.valueOf(11)); + assertThat(dataTypes.primitiveDouble).isEqualTo(Double.valueOf(11)); } @Test @@ -144,7 +163,7 @@ void deserialize_multiCharString_toPrimitiveCharField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.multipleCharacters).isEqualTo('A'); + assertThat(dataTypes.multipleCharacters).isEqualTo('A'); } @Test @@ -161,7 +180,7 @@ void deserialize_primitiveCharField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.primitiveChar).isEqualTo('A'); + assertThat(dataTypes.primitiveChar).isEqualTo('A'); } @Test @@ -178,7 +197,7 @@ void deserialize_CharacterField() { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.Character).isEqualTo(Character.valueOf('A')); + assertThat(dataTypes.Character).isEqualTo(Character.valueOf('A')); } @ParameterizedTest @@ -195,8 +214,8 @@ void deserializeTrueValuesFor_booleanField(String value) { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.BooleanTrue).isTrue(); - Assertions.assertThat(dataTypes.booleanTrue).isTrue(); + assertThat(dataTypes.BooleanTrue).isTrue(); + assertThat(dataTypes.booleanTrue).isTrue(); } @ParameterizedTest @@ -213,8 +232,8 @@ void deserializeFalseValuesFor_booleanField(String value) { DataTypeHolder dataTypes = new XjxSerdes().read(data, DataTypeHolder.class); // then - Assertions.assertThat(dataTypes.BooleanFalse).isFalse(); - Assertions.assertThat(dataTypes.booleanFalse).isFalse(); + assertThat(dataTypes.BooleanFalse).isFalse(); + assertThat(dataTypes.booleanFalse).isFalse(); } @Test @@ -231,9 +250,40 @@ void deserialize_StringField_mappedUsingRelativePath() { ParentHolder parentHolder = new XjxSerdes().read(data, ParentHolder.class); // then - Assertions.assertThat(parentHolder.nestedField.String).isEqualTo("11"); + assertThat(parentHolder.nestedField.String).isEqualTo("11"); } + @Test + void deserialize_mapValueAndAtTheSameTimeAttributeValuesOfOneTag() { + // given + String data = """ + + + John + + """; + + // when + var person = new XjxSerdes().read(data, Person.class); + + // then + assertThat(person.name).isEqualTo("John"); + assertThat(person.sex).isEqualTo("MALE"); + assertThat(person.age).isEqualTo(18); + } + + public static class Person { + @Tag(path = "/Person/Name") + String name; + + @Tag(path = "/Person/Name", attribute = "sex") + String sex; + + @Tag(path = "/Person/Name", attribute = "age") + int age; + } + + public static class DataTypeHolder { public DataTypeHolder() { } @@ -241,8 +291,11 @@ public DataTypeHolder() { @Tag(path = "/DataTypes/String", attribute = "value") String String; - @Tag(path = "/DataTypes/Integer", attribute = "value") - Integer Integer; + @Tag(path = "/DataTypes/Integer", attribute = "value") + Integer Integer; + + @Tag(path = "/DataTypes/primitiveInt", attribute = "value") + int primitiveInt; @Tag(path = "/DataTypes/Long", attribute = "value") Long Long;