@@ -1103,7 +1103,7 @@ void testStreamXmlToJson_validXml_writesJson() throws IOException {
11031103 InputStream xmlStream = new ByteArrayInputStream (xml .getBytes ());
11041104 ByteArrayOutputStream jsonStream = new ByteArrayOutputStream ();
11051105 U .streamXmlToJson (xmlStream , jsonStream );
1106- String jsonOutput = jsonStream .toString ("UTF-8" );
1106+ String jsonOutput = jsonStream .toString (StandardCharsets . UTF_8 );
11071107 assertTrue (jsonOutput .contains ("name" ), "JSON output should contain 'name' field." );
11081108 assertTrue (jsonOutput .contains ("Test" ), "JSON output should contain 'Test' value." );
11091109 assertTrue (jsonOutput .startsWith ("{" ), "JSON output should start with '{'." );
@@ -1117,10 +1117,11 @@ void testStreamXmlToJson_emptyInput_producesEmptyOrError() {
11171117 Exception exception =
11181118 assertThrows (
11191119 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 ),
11241125 "Should throw exception for empty input." );
11251126 String msg = exception .getMessage ();
11261127 assertNotNull (msg , "Exception message should not be null." );
@@ -1135,10 +1136,11 @@ void testStreamXmlToJson_invalidXml_throwsException() {
11351136 Exception exception =
11361137 assertThrows (
11371138 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 ),
11421144 "Should throw exception for invalid XML." );
11431145 String msg = exception .getMessage ();
11441146 assertNotNull (msg , "Exception message for invalid XML should not be null." );
@@ -1150,7 +1152,78 @@ void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOExcepti
11501152 InputStream xmlStream = new ByteArrayInputStream (xml .getBytes ());
11511153 ByteArrayOutputStream jsonStream = new ByteArrayOutputStream ();
11521154 U .streamXmlToJson (xmlStream , jsonStream , Json .JsonStringBuilder .Step .FOUR_SPACES );
1153- String jsonOutput = jsonStream .toString ("UTF-8" );
1155+ String jsonOutput = jsonStream .toString (StandardCharsets . UTF_8 );
11541156 assertTrue (jsonOutput .contains (" " ), "JSON output should be indented with four spaces." );
11551157 }
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+ }
11561229}
0 commit comments