5
5
*
6
6
* Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0
7
7
*/
8
- package de .rub .nds .modifiablevariable .length ;
8
+ package de .rub .nds .modifiablevariable .json ;
9
9
10
10
import static org .junit .jupiter .api .Assertions .*;
11
11
12
+ import com .fasterxml .jackson .databind .ObjectMapper ;
12
13
import de .rub .nds .modifiablevariable .bytearray .ModifiableByteArray ;
13
14
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 ;
20
16
import org .apache .logging .log4j .LogManager ;
21
17
import org .apache .logging .log4j .Logger ;
18
+ import org .junit .jupiter .api .BeforeAll ;
22
19
import org .junit .jupiter .api .BeforeEach ;
23
20
import org .junit .jupiter .api .Test ;
24
21
25
- public class ModifiableLengthFieldSerializationTest {
22
+ public class LengthFieldSerializationTest {
26
23
27
- private static final Logger LOGGER =
28
- LogManager . getLogger ( ModifiableLengthFieldSerializationTest . class ) ;
24
+ private static final Logger LOGGER = LogManager . getLogger ();
25
+ private static ObjectMapper mapper ;
29
26
30
27
private ModifiableLengthField lengthField ;
31
28
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
+ }
35
36
36
37
@ BeforeEach
37
- public void setUp () throws JAXBException {
38
+ public void setUp () {
38
39
byteArray = new ModifiableByteArray ();
39
40
byteArray .setOriginalValue (new byte [] {1 , 2 , 3 , 4 , 5 });
40
41
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 ();
50
42
}
51
43
52
44
@ Test
53
45
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 );
59
48
60
49
ModifiableLengthField deserialized =
61
- ( ModifiableLengthField ) unmarshaller . unmarshal ( new StringReader ( xmlString ) );
50
+ mapper . readValue ( jsonString , ModifiableLengthField . class );
62
51
63
52
assertNotNull (deserialized );
64
53
assertEquals (5 , (int ) deserialized .getOriginalValue ());
@@ -70,14 +59,11 @@ public void testSerializeDeserializeWithModification() throws Exception {
70
59
lengthField .setModifications (new IntegerAddModification (10 ));
71
60
assertEquals (15 , (int ) lengthField .getValue ());
72
61
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 );
78
64
79
65
ModifiableLengthField deserialized =
80
- ( ModifiableLengthField ) unmarshaller . unmarshal ( new StringReader ( xmlString ) );
66
+ mapper . readValue ( jsonString , ModifiableLengthField . class );
81
67
82
68
assertNotNull (deserialized );
83
69
assertEquals (5 , (int ) deserialized .getOriginalValue ());
@@ -90,14 +76,11 @@ public void testSerializeDeserializeWithNullByteArrayValue() throws Exception {
90
76
assertNull (lengthField .getOriginalValue ());
91
77
assertNull (lengthField .getValue ());
92
78
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 );
98
81
99
82
ModifiableLengthField deserialized =
100
- ( ModifiableLengthField ) unmarshaller . unmarshal ( new StringReader ( xmlString ) );
83
+ mapper . readValue ( jsonString , ModifiableLengthField . class );
101
84
102
85
assertNotNull (deserialized );
103
86
assertNull (deserialized .getOriginalValue ());
@@ -108,12 +91,9 @@ public void testSerializeDeserializeWithNullByteArrayValue() throws Exception {
108
91
public void testSerializedFieldReferencesArePreserved () throws Exception {
109
92
lengthField .setModifications (new IntegerAddModification (3 ));
110
93
111
- StringWriter writer = new StringWriter ();
112
- marshaller .marshal (lengthField , writer );
113
- String xmlString = writer .toString ();
114
-
94
+ String jsonString = mapper .writeValueAsString (lengthField );
115
95
ModifiableLengthField deserialized =
116
- ( ModifiableLengthField ) unmarshaller . unmarshal ( new StringReader ( xmlString ) );
96
+ mapper . readValue ( jsonString , ModifiableLengthField . class );
117
97
118
98
byteArray .setOriginalValue (new byte [] {1 , 2 });
119
99
assertEquals (2 , (int ) lengthField .getOriginalValue ());
0 commit comments