@@ -1226,4 +1226,86 @@ void testListResult(@TempDir Path tempDir) throws IOException {
1226
1226
xmlStr ,
1227
1227
"Should write XML using UTF-8 when result is a List" );
1228
1228
}
1229
+
1230
+ @ Test
1231
+ void testStreamJsonToXml_ObjectMap_DefaultEncoding () throws Exception {
1232
+ String inputJson = "{\" root\" :\" value\" }" ;
1233
+ ByteArrayInputStream inputStream =
1234
+ new ByteArrayInputStream (inputJson .getBytes (StandardCharsets .UTF_8 ));
1235
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
1236
+ U .streamJsonToXml (inputStream , outputStream );
1237
+ String expectedXml =
1238
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>"
1239
+ + System .lineSeparator ()
1240
+ + "<root>value</root>" ;
1241
+ String actualXml = outputStream .toString (StandardCharsets .UTF_8 );
1242
+ assertEquals (
1243
+ expectedXml ,
1244
+ actualXml ,
1245
+ "XML output should match expected XML for simple JSON object with default encoding" );
1246
+ }
1247
+
1248
+ @ Test
1249
+ void testStreamJsonToXml_ObjectMap_CustomEncoding () throws Exception {
1250
+ String inputJson = "{\" root\" :\" value\" ,\" #encoding\" :\" UTF-16\" }" ;
1251
+ ByteArrayInputStream inputStream =
1252
+ new ByteArrayInputStream (inputJson .getBytes (StandardCharsets .UTF_8 ));
1253
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
1254
+ U .streamJsonToXml (inputStream , outputStream , Xml .XmlStringBuilder .Step .TWO_SPACES );
1255
+ String expectedXml =
1256
+ "<?xml version=\" 1.0\" encoding=\" UTF-16\" ?>"
1257
+ + System .lineSeparator ()
1258
+ + "<root>value</root>" ;
1259
+ String actualXml = outputStream .toString (StandardCharsets .UTF_16 );
1260
+ assertEquals (
1261
+ expectedXml ,
1262
+ actualXml ,
1263
+ "XML output should match expected XML for simple JSON object with default encoding" );
1264
+ }
1265
+
1266
+ @ Test
1267
+ void testStreamJsonToXml_List () throws Exception {
1268
+ String inputJson = "[{\" item\" :42}]" ;
1269
+ ByteArrayInputStream inputStream =
1270
+ new ByteArrayInputStream (inputJson .getBytes (StandardCharsets .UTF_8 ));
1271
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
1272
+ U .streamJsonToXml (inputStream , outputStream , Xml .XmlStringBuilder .Step .TWO_SPACES );
1273
+ String expectedXml =
1274
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>"
1275
+ + System .lineSeparator ()
1276
+ + "<root>"
1277
+ + System .lineSeparator ()
1278
+ + " <element array=\" true\" >"
1279
+ + System .lineSeparator ()
1280
+ + " <item number=\" true\" >42</item>"
1281
+ + System .lineSeparator ()
1282
+ + " </element>"
1283
+ + System .lineSeparator ()
1284
+ + "</root>" ;
1285
+ String actualXml = outputStream .toString (StandardCharsets .UTF_8 );
1286
+ assertEquals (
1287
+ expectedXml ,
1288
+ actualXml ,
1289
+ "XML output should match expected XML for JSON array root with default encoding" );
1290
+ }
1291
+
1292
+ @ Test
1293
+ void testStreamJsonToXml_InvalidJson_ThrowsException () {
1294
+ String inputJson = "invalid" ;
1295
+ ByteArrayInputStream inputStream =
1296
+ new ByteArrayInputStream (inputJson .getBytes (StandardCharsets .UTF_8 ));
1297
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
1298
+ Exception exception =
1299
+ assertThrows (
1300
+ Json .ParseException .class ,
1301
+ () ->
1302
+ U .streamJsonToXml (
1303
+ inputStream ,
1304
+ outputStream ,
1305
+ Xml .XmlStringBuilder .Step .TWO_SPACES ));
1306
+ String actualMessage = exception .getMessage ();
1307
+ assertTrue (
1308
+ actualMessage .contains ("Expected value" ),
1309
+ "Should throw exception if JSON is invalid" );
1310
+ }
1229
1311
}
0 commit comments