@@ -1153,4 +1153,61 @@ void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOExcepti
11531153 String jsonOutput = jsonStream .toString ("UTF-8" );
11541154 assertTrue (jsonOutput .contains (" " ), "JSON output should be indented with four spaces." );
11551155 }
1156+
1157+ @ Test
1158+ void testMapWithEncodingKey (@ TempDir Path tempDir ) throws IOException {
1159+ // Arrange
1160+ Path jsonFile = tempDir .resolve ("in.json" );
1161+ Path xmlFile = tempDir .resolve ("out.xml" );
1162+ String encoding = "UTF-16" ;
1163+ Map <String , Object > map = new LinkedHashMap <>();
1164+ map .put ("#encoding" , encoding );
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 ("<?xml version=\" 1.0\" encoding=\" UTF-16\" ?>" + System .lineSeparator ()
1174+ + "<root></root>" , xmlStr , "Should write XML with provided encoding when #encoding key present" );
1175+ }
1176+
1177+ @ Test
1178+ void testMapWithoutEncodingKey (@ TempDir Path tempDir ) throws IOException {
1179+ // Arrange
1180+ Path jsonFile = tempDir .resolve ("in.json" );
1181+ Path xmlFile = tempDir .resolve ("out.xml" );
1182+ Map <String , Object > map = new LinkedHashMap <>();
1183+ String jsonText = "{}" ;
1184+ Files .write (jsonFile , jsonText .getBytes (StandardCharsets .UTF_8 ));
1185+ // Act
1186+ U .fileJsonToXml (jsonFile .toString (), xmlFile .toString (), Xml .XmlStringBuilder .Step .TWO_SPACES );
1187+ // Assert
1188+ byte [] xmlBytes = Files .readAllBytes (xmlFile );
1189+ String xmlStr = new String (xmlBytes , StandardCharsets .UTF_8 );
1190+ assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" + System .lineSeparator ()
1191+ + "<root></root>" , xmlStr , "Should write XML using UTF-8 when #encoding key not present" );
1192+ }
1193+
1194+ @ Test
1195+ void testListResult (@ TempDir Path tempDir ) throws IOException {
1196+ // Arrange
1197+ Path jsonFile = tempDir .resolve ("in.json" );
1198+ Path xmlFile = tempDir .resolve ("out.xml" );
1199+ Files .write (jsonFile , "[1,2,3]" .getBytes (StandardCharsets .UTF_8 ));
1200+ // Act
1201+ U .fileJsonToXml (jsonFile .toString (), xmlFile .toString (), Xml .XmlStringBuilder .Step .TWO_SPACES );
1202+ // Assert
1203+ byte [] xmlBytes = Files .readAllBytes (xmlFile );
1204+ String xmlStr = new String (xmlBytes , StandardCharsets .UTF_8 );
1205+ assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" + System .lineSeparator ()
1206+ + "<root>" + System .lineSeparator ()
1207+ + " <element number=\" true\" >1</element>" + System .lineSeparator ()
1208+ + " <element number=\" true\" >2</element>" + System .lineSeparator ()
1209+ + " <element number=\" true\" >3</element>" + System .lineSeparator ()
1210+ + "</root>" , xmlStr ,
1211+ "Should write XML using UTF-8 when result is a List" );
1212+ }
11561213}
0 commit comments