Skip to content

Commit 47acd9d

Browse files
committed
test: Adapt LengthFieldSerializationTest to JSON serialization
1 parent ee202e7 commit 47acd9d

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed
Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,49 @@
55
*
66
* Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0
77
*/
8-
package de.rub.nds.modifiablevariable.length;
8+
package de.rub.nds.modifiablevariable.json;
99

1010
import static org.junit.jupiter.api.Assertions.*;
1111

12+
import com.fasterxml.jackson.databind.ObjectMapper;
1213
import de.rub.nds.modifiablevariable.bytearray.ModifiableByteArray;
1314
import de.rub.nds.modifiablevariable.integer.IntegerAddModification;
14-
import jakarta.xml.bind.JAXBContext;
15-
import jakarta.xml.bind.JAXBException;
16-
import jakarta.xml.bind.Marshaller;
17-
import jakarta.xml.bind.Unmarshaller;
18-
import java.io.StringReader;
19-
import java.io.StringWriter;
15+
import de.rub.nds.modifiablevariable.length.ModifiableLengthField;
2016
import org.apache.logging.log4j.LogManager;
2117
import org.apache.logging.log4j.Logger;
18+
import org.junit.jupiter.api.BeforeAll;
2219
import org.junit.jupiter.api.BeforeEach;
2320
import org.junit.jupiter.api.Test;
2421

25-
public class ModifiableLengthFieldSerializationTest {
22+
public class LengthFieldSerializationTest {
2623

27-
private static final Logger LOGGER =
28-
LogManager.getLogger(ModifiableLengthFieldSerializationTest.class);
24+
private static final Logger LOGGER = LogManager.getLogger();
25+
private static ObjectMapper mapper;
2926

3027
private ModifiableLengthField lengthField;
3128
private ModifiableByteArray byteArray;
32-
private JAXBContext context;
33-
private Marshaller marshaller;
34-
private Unmarshaller unmarshaller;
29+
30+
@BeforeAll
31+
public static void setUpClass() {
32+
mapper = new ObjectMapper();
33+
mapper.registerModule(new ModifiableVariableModule());
34+
mapper.setVisibility(ModifiableVariableModule.getFieldVisibilityChecker());
35+
}
3536

3637
@BeforeEach
37-
public void setUp() throws JAXBException {
38+
public void setUp() {
3839
byteArray = new ModifiableByteArray();
3940
byteArray.setOriginalValue(new byte[] {1, 2, 3, 4, 5});
4041
lengthField = new ModifiableLengthField(byteArray);
41-
42-
context =
43-
JAXBContext.newInstance(
44-
ModifiableLengthField.class,
45-
ModifiableByteArray.class,
46-
IntegerAddModification.class);
47-
marshaller = context.createMarshaller();
48-
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
49-
unmarshaller = context.createUnmarshaller();
5042
}
5143

5244
@Test
5345
public void testSerializeDeserializeSimple() throws Exception {
54-
StringWriter writer = new StringWriter();
55-
marshaller.marshal(lengthField, writer);
56-
57-
String xmlString = writer.toString();
58-
LOGGER.debug("Serialized XML: {}", xmlString);
46+
String jsonString = mapper.writeValueAsString(lengthField);
47+
LOGGER.debug("Serialized JSON: {}", jsonString);
5948

6049
ModifiableLengthField deserialized =
61-
(ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString));
50+
mapper.readValue(jsonString, ModifiableLengthField.class);
6251

6352
assertNotNull(deserialized);
6453
assertEquals(5, (int) deserialized.getOriginalValue());
@@ -70,14 +59,11 @@ public void testSerializeDeserializeWithModification() throws Exception {
7059
lengthField.setModifications(new IntegerAddModification(10));
7160
assertEquals(15, (int) lengthField.getValue());
7261

73-
StringWriter writer = new StringWriter();
74-
marshaller.marshal(lengthField, writer);
75-
76-
String xmlString = writer.toString();
77-
LOGGER.debug("Serialized XML with modification: {}", xmlString);
62+
String jsonString = mapper.writeValueAsString(lengthField);
63+
LOGGER.debug("Serialized JSON with modification: {}", jsonString);
7864

7965
ModifiableLengthField deserialized =
80-
(ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString));
66+
mapper.readValue(jsonString, ModifiableLengthField.class);
8167

8268
assertNotNull(deserialized);
8369
assertEquals(5, (int) deserialized.getOriginalValue());
@@ -90,14 +76,11 @@ public void testSerializeDeserializeWithNullByteArrayValue() throws Exception {
9076
assertNull(lengthField.getOriginalValue());
9177
assertNull(lengthField.getValue());
9278

93-
StringWriter writer = new StringWriter();
94-
marshaller.marshal(lengthField, writer);
95-
96-
String xmlString = writer.toString();
97-
LOGGER.debug("Serialized XML with null byte array: {}", xmlString);
79+
String jsonString = mapper.writeValueAsString(lengthField);
80+
LOGGER.debug("Serialized JSON with null byte array: {}", jsonString);
9881

9982
ModifiableLengthField deserialized =
100-
(ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString));
83+
mapper.readValue(jsonString, ModifiableLengthField.class);
10184

10285
assertNotNull(deserialized);
10386
assertNull(deserialized.getOriginalValue());
@@ -108,12 +91,9 @@ public void testSerializeDeserializeWithNullByteArrayValue() throws Exception {
10891
public void testSerializedFieldReferencesArePreserved() throws Exception {
10992
lengthField.setModifications(new IntegerAddModification(3));
11093

111-
StringWriter writer = new StringWriter();
112-
marshaller.marshal(lengthField, writer);
113-
String xmlString = writer.toString();
114-
94+
String jsonString = mapper.writeValueAsString(lengthField);
11595
ModifiableLengthField deserialized =
116-
(ModifiableLengthField) unmarshaller.unmarshal(new StringReader(xmlString));
96+
mapper.readValue(jsonString, ModifiableLengthField.class);
11797

11898
byteArray.setOriginalValue(new byte[] {1, 2});
11999
assertEquals(2, (int) lengthField.getOriginalValue());

0 commit comments

Comments
 (0)