@@ -1103,7 +1103,7 @@ void testStreamXmlToJson_validXml_writesJson() throws IOException {
1103
1103
InputStream xmlStream = new ByteArrayInputStream (xml .getBytes ());
1104
1104
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream ();
1105
1105
U .streamXmlToJson (xmlStream , jsonStream );
1106
- String jsonOutput = jsonStream .toString ("UTF-8" );
1106
+ String jsonOutput = jsonStream .toString (StandardCharsets . UTF_8 );
1107
1107
assertTrue (jsonOutput .contains ("name" ), "JSON output should contain 'name' field." );
1108
1108
assertTrue (jsonOutput .contains ("Test" ), "JSON output should contain 'Test' value." );
1109
1109
assertTrue (jsonOutput .startsWith ("{" ), "JSON output should start with '{'." );
@@ -1117,10 +1117,11 @@ void testStreamXmlToJson_emptyInput_producesEmptyOrError() {
1117
1117
Exception exception =
1118
1118
assertThrows (
1119
1119
Exception .class ,
1120
- () -> {
1121
- U .streamXmlToJson (
1122
- xmlStream , jsonStream , Json .JsonStringBuilder .Step .TWO_SPACES );
1123
- },
1120
+ () ->
1121
+ U .streamXmlToJson (
1122
+ xmlStream ,
1123
+ jsonStream ,
1124
+ Json .JsonStringBuilder .Step .TWO_SPACES ),
1124
1125
"Should throw exception for empty input." );
1125
1126
String msg = exception .getMessage ();
1126
1127
assertNotNull (msg , "Exception message should not be null." );
@@ -1135,10 +1136,11 @@ void testStreamXmlToJson_invalidXml_throwsException() {
1135
1136
Exception exception =
1136
1137
assertThrows (
1137
1138
Exception .class ,
1138
- () -> {
1139
- U .streamXmlToJson (
1140
- xmlStream , jsonStream , Json .JsonStringBuilder .Step .TWO_SPACES );
1141
- },
1139
+ () ->
1140
+ U .streamXmlToJson (
1141
+ xmlStream ,
1142
+ jsonStream ,
1143
+ Json .JsonStringBuilder .Step .TWO_SPACES ),
1142
1144
"Should throw exception for invalid XML." );
1143
1145
String msg = exception .getMessage ();
1144
1146
assertNotNull (msg , "Exception message for invalid XML should not be null." );
@@ -1150,7 +1152,78 @@ void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOExcepti
1150
1152
InputStream xmlStream = new ByteArrayInputStream (xml .getBytes ());
1151
1153
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream ();
1152
1154
U .streamXmlToJson (xmlStream , jsonStream , Json .JsonStringBuilder .Step .FOUR_SPACES );
1153
- String jsonOutput = jsonStream .toString ("UTF-8" );
1155
+ String jsonOutput = jsonStream .toString (StandardCharsets . UTF_8 );
1154
1156
assertTrue (jsonOutput .contains (" " ), "JSON output should be indented with four spaces." );
1155
1157
}
1158
+
1159
+ @ Test
1160
+ void testMapWithEncodingKey (@ TempDir Path tempDir ) throws IOException {
1161
+ // Arrange
1162
+ Path jsonFile = tempDir .resolve ("in.json" );
1163
+ Path xmlFile = tempDir .resolve ("out.xml" );
1164
+ String encoding = "UTF-16" ;
1165
+ // Write json
1166
+ String jsonText = "{\" #encoding\" :\" " + encoding + "\" }" ;
1167
+ Files .write (jsonFile , jsonText .getBytes (StandardCharsets .UTF_8 ));
1168
+ // Act
1169
+ U .fileJsonToXml (jsonFile .toString (), xmlFile .toString ());
1170
+ // Assert
1171
+ byte [] xmlBytes = Files .readAllBytes (xmlFile );
1172
+ String xmlStr = new String (xmlBytes , encoding );
1173
+ assertEquals (
1174
+ "<?xml version=\" 1.0\" encoding=\" UTF-16\" ?>"
1175
+ + System .lineSeparator ()
1176
+ + "<root></root>" ,
1177
+ xmlStr ,
1178
+ "Should write XML with provided encoding when #encoding key present" );
1179
+ }
1180
+
1181
+ @ Test
1182
+ void testMapWithoutEncodingKey (@ TempDir Path tempDir ) throws IOException {
1183
+ // Arrange
1184
+ Path jsonFile = tempDir .resolve ("in.json" );
1185
+ Path xmlFile = tempDir .resolve ("out.xml" );
1186
+ String jsonText = "{}" ;
1187
+ Files .write (jsonFile , jsonText .getBytes (StandardCharsets .UTF_8 ));
1188
+ // Act
1189
+ U .fileJsonToXml (
1190
+ jsonFile .toString (), xmlFile .toString (), Xml .XmlStringBuilder .Step .TWO_SPACES );
1191
+ // Assert
1192
+ byte [] xmlBytes = Files .readAllBytes (xmlFile );
1193
+ String xmlStr = new String (xmlBytes , StandardCharsets .UTF_8 );
1194
+ assertEquals (
1195
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>"
1196
+ + System .lineSeparator ()
1197
+ + "<root></root>" ,
1198
+ xmlStr ,
1199
+ "Should write XML using UTF-8 when #encoding key not present" );
1200
+ }
1201
+
1202
+ @ Test
1203
+ void testListResult (@ TempDir Path tempDir ) throws IOException {
1204
+ // Arrange
1205
+ Path jsonFile = tempDir .resolve ("in.json" );
1206
+ Path xmlFile = tempDir .resolve ("out.xml" );
1207
+ Files .write (jsonFile , "[1,2,3]" .getBytes (StandardCharsets .UTF_8 ));
1208
+ // Act
1209
+ U .fileJsonToXml (
1210
+ jsonFile .toString (), xmlFile .toString (), Xml .XmlStringBuilder .Step .TWO_SPACES );
1211
+ // Assert
1212
+ byte [] xmlBytes = Files .readAllBytes (xmlFile );
1213
+ String xmlStr = new String (xmlBytes , StandardCharsets .UTF_8 );
1214
+ assertEquals (
1215
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>"
1216
+ + System .lineSeparator ()
1217
+ + "<root>"
1218
+ + System .lineSeparator ()
1219
+ + " <element number=\" true\" >1</element>"
1220
+ + System .lineSeparator ()
1221
+ + " <element number=\" true\" >2</element>"
1222
+ + System .lineSeparator ()
1223
+ + " <element number=\" true\" >3</element>"
1224
+ + System .lineSeparator ()
1225
+ + "</root>" ,
1226
+ xmlStr ,
1227
+ "Should write XML using UTF-8 when result is a List" );
1228
+ }
1156
1229
}
0 commit comments