3939
4040import java .io .ByteArrayInputStream ;
4141import java .io .ByteArrayOutputStream ;
42- import java .io .InputStream ;
4342import java .io .IOException ;
43+ import java .io .InputStream ;
4444import java .nio .charset .StandardCharsets ;
4545import java .nio .file .Files ;
4646import java .nio .file .Path ;
@@ -877,7 +877,8 @@ void testRemoveUtf16LeBom() {
877877 void testNotShortBytesNoBom () {
878878 // Less than 2 bytes (not possible to have BOM)
879879 byte [] input = new byte [] {42 };
880- assertArrayEquals (input , U .removeBom (input ), "Short arrays with no BOM should be unchanged" );
880+ assertArrayEquals (
881+ input , U .removeBom (input ), "Short arrays with no BOM should be unchanged" );
881882 }
882883
883884 @ Test
@@ -891,26 +892,36 @@ void testNoBomPresent() {
891892 void testAlmostBomButNotEnoughBytes () {
892893 // only 2 bytes, not enough for UTF-8 BOM
893894 byte [] input = new byte [] {-17 , -69 };
894- assertArrayEquals (input , U .removeBom (input ), "Arrays with too few BOM bytes should be unchanged" );
895+ assertArrayEquals (
896+ input , U .removeBom (input ), "Arrays with too few BOM bytes should be unchanged" );
895897 }
896898
897899 @ Test
898900 void testPrefixSimilarButNotABom () {
899901 byte [] input = new byte [] {-1 , 0 , 1 };
900- assertArrayEquals (input , U .removeBom (input ), "Array starting with -1,0 is not a BOM, should be unchanged" );
902+ assertArrayEquals (
903+ input ,
904+ U .removeBom (input ),
905+ "Array starting with -1,0 is not a BOM, should be unchanged" );
901906 input = new byte [] {-2 , 0 , 1 };
902- assertArrayEquals (input , U .removeBom (input ), "Array starting with -2,0 is not a BOM, should be unchanged" );
907+ assertArrayEquals (
908+ input ,
909+ U .removeBom (input ),
910+ "Array starting with -2,0 is not a BOM, should be unchanged" );
903911 // 3 bytes but third is not -65
904912 input = new byte [] {-17 , -69 , 0 };
905913 assertArrayEquals (input , U .removeBom (input ), "Array with -17,-69,<not -65> is not a BOM" );
906- input = new byte [] { -17 , -69 };
914+ input = new byte [] {-17 , -69 };
907915 assertArrayEquals (input , U .removeBom (input ), "Should not remove BOM for length < 3" );
908- input = new byte [] { 0 , -69 , -65 , 33 };
909- assertArrayEquals (input , U .removeBom (input ), "Should not remove BOM if first byte is not -17" );
910- input = new byte [] { -17 , 0 , -65 , 13 };
911- assertArrayEquals (input , U .removeBom (input ), "Should not remove BOM if second byte is not -69" );
912- input = new byte [] { -17 , -69 , 0 , 14 };
913- assertArrayEquals (input , U .removeBom (input ), "Should not remove BOM if third byte is not -65" );
916+ input = new byte [] {0 , -69 , -65 , 33 };
917+ assertArrayEquals (
918+ input , U .removeBom (input ), "Should not remove BOM if first byte is not -17" );
919+ input = new byte [] {-17 , 0 , -65 , 13 };
920+ assertArrayEquals (
921+ input , U .removeBom (input ), "Should not remove BOM if second byte is not -69" );
922+ input = new byte [] {-17 , -69 , 0 , 14 };
923+ assertArrayEquals (
924+ input , U .removeBom (input ), "Should not remove BOM if third byte is not -65" );
914925 }
915926
916927 @ Test
@@ -926,90 +937,97 @@ void testLengthLessThan4() {
926937 @ Test
927938 void testCase_0x0000FEFF () {
928939 byte [] buf = {(byte ) 0x00 , (byte ) 0x00 , (byte ) 0xFE , (byte ) 0xFF };
929- assertEquals ("UTF_32BE" , U . detectEncoding ( buf ),
930- "Should return UTF_32BE for BOM 0x0000FEFF" );
940+ assertEquals (
941+ "UTF_32BE" , U . detectEncoding ( buf ), " Should return UTF_32BE for BOM 0x0000FEFF" );
931942 }
932943
933944 @ Test
934945 void testCase_0x0000003C () {
935946 byte [] buf = {(byte ) 0x00 , (byte ) 0x00 , (byte ) 0x00 , (byte ) 0x3C };
936- assertEquals ("UTF_32BE" , U .detectEncoding (buf ),
937- "Should return UTF_32BE for 0x0000003C" );
947+ assertEquals ("UTF_32BE" , U .detectEncoding (buf ), "Should return UTF_32BE for 0x0000003C" );
938948 }
939949
940950 @ Test
941951 void testCase_0x003C003F () {
942952 byte [] buf = {(byte ) 0x00 , (byte ) 0x3C , (byte ) 0x00 , (byte ) 0x3F };
943- assertEquals ("UnicodeBigUnmarked" , U .detectEncoding (buf ),
953+ assertEquals (
954+ "UnicodeBigUnmarked" ,
955+ U .detectEncoding (buf ),
944956 "Should return UnicodeBigUnmarked for 0x003C003F" );
945957 }
946958
947959 @ Test
948960 void testCase_0xFFFE0000 () {
949961 byte [] buf = {(byte ) 0xFF , (byte ) 0xFE , (byte ) 0x00 , (byte ) 0x00 };
950- assertEquals ("UTF_32LE" , U . detectEncoding ( buf ),
951- "Should return UTF_32LE for BOM 0xFFFE0000" );
962+ assertEquals (
963+ "UTF_32LE" , U . detectEncoding ( buf ), " Should return UTF_32LE for BOM 0xFFFE0000" );
952964 }
953965
954966 @ Test
955967 void testCase_0x3C000000 () {
956968 byte [] buf = {(byte ) 0x3C , (byte ) 0x00 , (byte ) 0x00 , (byte ) 0x00 };
957- assertEquals ("UTF_32LE" , U .detectEncoding (buf ),
958- "Should return UTF_32LE for 0x3C000000" );
969+ assertEquals ("UTF_32LE" , U .detectEncoding (buf ), "Should return UTF_32LE for 0x3C000000" );
959970 }
960971
961972 @ Test
962973 void testCase_0x3C003F00 () {
963974 byte [] buf = {(byte ) 0x3C , (byte ) 0x00 , (byte ) 0x3F , (byte ) 0x00 };
964- assertEquals ("UnicodeLittleUnmarked" , U .detectEncoding (buf ),
975+ assertEquals (
976+ "UnicodeLittleUnmarked" ,
977+ U .detectEncoding (buf ),
965978 "Should return UnicodeLittleUnmarked for 0x3C003F00" );
966979 }
967980
968981 @ Test
969982 void testCase_0x3C3F786D () {
970983 byte [] buf = {(byte ) 0x3C , (byte ) 0x3F , (byte ) 0x78 , (byte ) 0x6D };
971- assertEquals ("UTF8" , U .detectEncoding (buf ),
972- "Should return UTF8 for 0x3C3F786D" );
984+ assertEquals ("UTF8" , U .detectEncoding (buf ), "Should return UTF8 for 0x3C3F786D" );
973985 }
974986
975987 @ Test
976988 void testEfBbBf_UTF8 () {
977989 // 0xEFBBBF??, so n >>> 8 == 0xEFBBBF
978990 // Let's set: [0xEF, 0xBB, 0xBF, 0x42] (0x42 is arbitrary)
979991 byte [] buf = {(byte ) 0xEF , (byte ) 0xBB , (byte ) 0xBF , (byte ) 0x42 };
980- assertEquals ("UTF8" , U .detectEncoding (buf ),
981- "Should return UTF8 for buffer with UTF-8 BOM" );
992+ assertEquals ("UTF8" , U .detectEncoding (buf ), "Should return UTF8 for buffer with UTF-8 BOM" );
982993 }
983994
984995 @ Test
985996 void test_nShift24_0x3C () {
986997 // (n >>> 24) == 0x3C, but not matching any above case
987998 byte [] buf = {(byte ) 0x3C , 1 , 2 , 3 };
988- assertEquals ("UTF8" , U .detectEncoding (buf ),
999+ assertEquals (
1000+ "UTF8" ,
1001+ U .detectEncoding (buf ),
9891002 "Should return UTF8 when (n >>> 24) == 0x3C and no previous case matches" );
9901003 }
9911004
9921005 @ Test
9931006 void test_nShift16_0xFFFE () {
9941007 // (n >>> 16) == 0xFFFE (UnicodeLittleUnmarked branch)
9951008 byte [] buf = {(byte ) 0xFF , (byte ) 0xFE , (byte ) 0x21 , (byte ) 0x22 };
996- assertEquals ("UnicodeLittleUnmarked" , U .detectEncoding (buf ),
1009+ assertEquals (
1010+ "UnicodeLittleUnmarked" ,
1011+ U .detectEncoding (buf ),
9971012 "Should return UnicodeLittleUnmarked when (n >> 16) == 0xFFFE" );
9981013 }
9991014
10001015 @ Test
10011016 void test_nShift16_0xFEFF () {
10021017 // (n >>> 16) == 0xFEFF (UnicodeBigUnmarked branch)
10031018 byte [] buf = {(byte ) 0xFE , (byte ) 0xFF , (byte ) 0x99 , (byte ) 0x88 };
1004- assertEquals ("UnicodeBigUnmarked" , U .detectEncoding (buf ),
1019+ assertEquals (
1020+ "UnicodeBigUnmarked" ,
1021+ U .detectEncoding (buf ),
10051022 "Should return UnicodeBigUnmarked when (n >> 16) == 0xFEFF" );
10061023 }
10071024
10081025 @ Test
10091026 void testDefaultCase () {
10101027 // Random data, not matching any case nor any shift checks. Should default to UTF8
10111028 byte [] buf = {(byte ) 0x01 , (byte ) 0x23 , (byte ) 0x45 , (byte ) 0x67 };
1012- assertEquals ("UTF8" , U .detectEncoding (buf ), "Should default to UTF8 for unknown byte patterns" );
1029+ assertEquals (
1030+ "UTF8" , U .detectEncoding (buf ), "Should default to UTF8 for unknown byte patterns" );
10131031 }
10141032
10151033 @ Test
@@ -1019,31 +1037,25 @@ void testFormatString() {
10191037 assertEquals (
10201038 input1 ,
10211039 U .formatString (input1 , "\n " ),
1022- "Should not modify string when line separator is already \\ n"
1023- );
1040+ "Should not modify string when line separator is already \\ n" );
10241041
10251042 // Test with different line separator
10261043 String input2 = "line1\n line2\n line3" ;
10271044 String expected2 = "line1\r \n line2\r \n line3" ;
10281045 assertEquals (
10291046 expected2 ,
10301047 U .formatString (input2 , "\r \n " ),
1031- "Should replace \\ n with specified line separator"
1032- );
1048+ "Should replace \\ n with specified line separator" );
10331049
10341050 // Test with empty string
1035- assertTrue (
1036- U .formatString ("" , "\n " ).isEmpty (),
1037- "Should handle empty string correctly"
1038- );
1051+ assertTrue (U .formatString ("" , "\n " ).isEmpty (), "Should handle empty string correctly" );
10391052
10401053 // Test with no line breaks
10411054 String noBreaks = "text without breaks" ;
10421055 assertEquals (
10431056 noBreaks ,
10441057 U .formatString (noBreaks , "\r \n " ),
1045- "Should not modify string without line breaks"
1046- );
1058+ "Should not modify string without line breaks" );
10471059 }
10481060
10491061 @ Test
@@ -1059,21 +1071,19 @@ void testFileXmlToJson(@TempDir Path tempDir) throws IOException {
10591071 // Test file conversion
10601072 assertDoesNotThrow (
10611073 () -> U .fileXmlToJson (xmlPath .toString (), jsonPath .toString ()),
1062- "File conversion should not throw exceptions"
1063- );
1074+ "File conversion should not throw exceptions" );
10641075
10651076 // Verify the JSON file
1066- assertTrue (
1067- Files .exists (jsonPath ),
1068- "JSON file should be created"
1069- );
1077+ assertTrue (Files .exists (jsonPath ), "JSON file should be created" );
10701078
10711079 String jsonContent = Files .readString (jsonPath );
1072- assertAll ("JSON file content verification" ,
1080+ assertAll (
1081+ "JSON file content verification" ,
10731082 () -> assertNotNull (jsonContent , "JSON content should not be null" ),
1074- () -> assertTrue (jsonContent .contains ("\" item\" : \" value\" " ),
1075- "JSON should contain converted XML content" )
1076- );
1083+ () ->
1084+ assertTrue (
1085+ jsonContent .contains ("\" item\" : \" value\" " ),
1086+ "JSON should contain converted XML content" ));
10771087 }
10781088
10791089 @ Test
@@ -1084,8 +1094,7 @@ void testFileXmlToJsonWithInvalidInput(@TempDir Path tempDir) {
10841094 assertThrows (
10851095 IOException .class ,
10861096 () -> U .fileXmlToJson (nonExistentXml .toString (), outputJson .toString ()),
1087- "Should throw IOException when input file doesn't exist"
1088- );
1097+ "Should throw IOException when input file doesn't exist" );
10891098 }
10901099
10911100 @ Test
@@ -1105,9 +1114,14 @@ void testStreamXmlToJson_validXml_writesJson() throws IOException {
11051114 void testStreamXmlToJson_emptyInput_producesEmptyOrError () {
11061115 InputStream xmlStream = new ByteArrayInputStream (new byte [0 ]);
11071116 ByteArrayOutputStream jsonStream = new ByteArrayOutputStream ();
1108- Exception exception = assertThrows (Exception .class , () -> {
1109- U .streamXmlToJson (xmlStream , jsonStream , Json .JsonStringBuilder .Step .TWO_SPACES );
1110- }, "Should throw exception for empty input." );
1117+ Exception exception =
1118+ assertThrows (
1119+ Exception .class ,
1120+ () -> {
1121+ U .streamXmlToJson (
1122+ xmlStream , jsonStream , Json .JsonStringBuilder .Step .TWO_SPACES );
1123+ },
1124+ "Should throw exception for empty input." );
11111125 String msg = exception .getMessage ();
11121126 assertNotNull (msg , "Exception message should not be null." );
11131127 }
@@ -1118,9 +1132,14 @@ void testStreamXmlToJson_invalidXml_throwsException() {
11181132 String invalidXml = "<root><name>Test</name>" ;
11191133 InputStream xmlStream = new ByteArrayInputStream (invalidXml .getBytes ());
11201134 ByteArrayOutputStream jsonStream = new ByteArrayOutputStream ();
1121- Exception exception = assertThrows (Exception .class , () -> {
1122- U .streamXmlToJson (xmlStream , jsonStream , Json .JsonStringBuilder .Step .TWO_SPACES );
1123- }, "Should throw exception for invalid XML." );
1135+ Exception exception =
1136+ assertThrows (
1137+ Exception .class ,
1138+ () -> {
1139+ U .streamXmlToJson (
1140+ xmlStream , jsonStream , Json .JsonStringBuilder .Step .TWO_SPACES );
1141+ },
1142+ "Should throw exception for invalid XML." );
11241143 String msg = exception .getMessage ();
11251144 assertNotNull (msg , "Exception message for invalid XML should not be null." );
11261145 }
0 commit comments