Skip to content

Commit c3d40ad

Browse files
committed
Added tests
1 parent a1d34bb commit c3d40ad

1 file changed

Lines changed: 178 additions & 0 deletions

File tree

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@
2525

2626
import static java.util.Arrays.asList;
2727
import static java.util.Collections.singletonList;
28+
import static org.junit.jupiter.api.Assertions.assertAll;
2829
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
30+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2931
import static org.junit.jupiter.api.Assertions.assertEquals;
3032
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3134
import static org.junit.jupiter.api.Assertions.assertNotSame;
3235
import static org.junit.jupiter.api.Assertions.assertNull;
3336
import static org.junit.jupiter.api.Assertions.assertThrows;
3437
import static org.junit.jupiter.api.Assertions.assertTrue;
3538
import static org.junit.jupiter.api.Assertions.fail;
3639

40+
import java.io.IOException;
41+
import java.nio.charset.StandardCharsets;
42+
import java.nio.file.Files;
43+
import java.nio.file.Path;
3744
import java.util.Iterator;
3845
import java.util.LinkedHashMap;
3946
import java.util.List;
@@ -45,6 +52,7 @@
4552
import java.util.function.Function;
4653
import java.util.function.Predicate;
4754
import org.junit.jupiter.api.Test;
55+
import org.junit.jupiter.api.io.TempDir;
4856

4957
/**
5058
* Underscore library unit test.
@@ -837,4 +845,174 @@ void testMapToPropertiesWithNullValues() {
837845
Properties properties2 = U.mapToProperties(null);
838846
assertEquals(0, properties2.size());
839847
}
848+
849+
@Test
850+
void testRemoveBom() {
851+
// Test UTF-8 BOM
852+
byte[] utf8Bom = new byte[]{(byte)-17, (byte)-69, (byte)-65, 'a', 'b', 'c'};
853+
assertArrayEquals(
854+
new byte[]{'a', 'b', 'c'},
855+
U.removeBom(utf8Bom),
856+
"Should remove UTF-8 BOM (EF BB BF) and keep content"
857+
);
858+
859+
// Test UTF-16 LE BOM
860+
byte[] utf16LeBom = new byte[]{(byte)-1, (byte)-2, 'a', 'b'};
861+
assertArrayEquals(
862+
new byte[]{'a', 'b'},
863+
U.removeBom(utf16LeBom),
864+
"Should remove UTF-16 LE BOM (FF FE) and keep content"
865+
);
866+
867+
// Test UTF-16 BE BOM
868+
byte[] utf16BeBom = new byte[]{(byte)-2, (byte)-1, 'a', 'b'};
869+
assertArrayEquals(
870+
new byte[]{'a', 'b'},
871+
U.removeBom(utf16BeBom),
872+
"Should remove UTF-16 BE BOM (FE FF) and keep content"
873+
);
874+
875+
// Test no BOM
876+
byte[] noBom = new byte[]{'a', 'b', 'c'};
877+
assertArrayEquals(
878+
noBom,
879+
U.removeBom(noBom),
880+
"Should return original array when no BOM is present"
881+
);
882+
883+
// Test empty array
884+
byte[] empty = new byte[]{};
885+
assertArrayEquals(
886+
empty,
887+
U.removeBom(empty),
888+
"Should handle empty byte array correctly"
889+
);
890+
}
891+
892+
@Test
893+
void testDetectEncoding() {
894+
// Test UTF-32BE
895+
byte[] utf32be = new byte[]{0x00, 0x00, (byte)0xFE, (byte)0xFF, 'a'};
896+
assertEquals(
897+
"UTF_32BE",
898+
U.detectEncoding(utf32be),
899+
"Should detect UTF-32BE encoding from BOM"
900+
);
901+
902+
// Test UTF-32LE
903+
byte[] utf32le = new byte[]{(byte)0xFF, (byte)0xFE, 0x00, 0x00, 'a'};
904+
assertEquals(
905+
"UTF_32LE",
906+
U.detectEncoding(utf32le),
907+
"Should detect UTF-32LE encoding from BOM"
908+
);
909+
910+
// Test Unicode Big Unmarked
911+
byte[] unicodeBig = new byte[]{0x00, 0x3C, 0x00, 0x3F};
912+
assertEquals(
913+
"UnicodeBigUnmarked",
914+
U.detectEncoding(unicodeBig),
915+
"Should detect Unicode Big Unmarked encoding"
916+
);
917+
918+
// Test UTF-8 XML declaration
919+
byte[] utf8Xml = new byte[]{0x3C, 0x3F, 0x78, 0x6D};
920+
assertEquals(
921+
"UTF8",
922+
U.detectEncoding(utf8Xml),
923+
"Should detect UTF-8 encoding from XML declaration"
924+
);
925+
926+
// Test UTF-8 with BOM
927+
byte[] utf8Bom = new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF, 'a'};
928+
assertEquals(
929+
"UTF8",
930+
U.detectEncoding(utf8Bom),
931+
"Should detect UTF-8 encoding from BOM"
932+
);
933+
934+
// Test small buffer
935+
byte[] small = new byte[]{0x3C, 0x3F};
936+
assertEquals(
937+
"UTF8",
938+
U.detectEncoding(small),
939+
"Should default to UTF-8 for buffers smaller than 4 bytes"
940+
);
941+
}
942+
943+
@Test
944+
void testFormatString() {
945+
// Test with \n line separator
946+
String input1 = "line1\nline2\nline3";
947+
assertEquals(
948+
input1,
949+
U.formatString(input1, "\n"),
950+
"Should not modify string when line separator is already \\n"
951+
);
952+
953+
// Test with different line separator
954+
String input2 = "line1\nline2\nline3";
955+
String expected2 = "line1\r\nline2\r\nline3";
956+
assertEquals(
957+
expected2,
958+
U.formatString(input2, "\r\n"),
959+
"Should replace \\n with specified line separator"
960+
);
961+
962+
// Test with empty string
963+
assertTrue(
964+
U.formatString("", "\n").isEmpty(),
965+
"Should handle empty string correctly"
966+
);
967+
968+
// Test with no line breaks
969+
String noBreaks = "text without breaks";
970+
assertEquals(
971+
noBreaks,
972+
U.formatString(noBreaks, "\r\n"),
973+
"Should not modify string without line breaks"
974+
);
975+
}
976+
977+
@Test
978+
void testFileXmlToJson(@TempDir Path tempDir) throws IOException {
979+
// Create temporary files
980+
Path xmlPath = tempDir.resolve("test.xml");
981+
Path jsonPath = tempDir.resolve("test.json");
982+
983+
// Write test XML content
984+
String xml = "<?xml version=\"1.0\"?><root><item>value</item></root>";
985+
Files.write(xmlPath, xml.getBytes(StandardCharsets.UTF_8));
986+
987+
// Test file conversion
988+
assertDoesNotThrow(
989+
() -> U.fileXmlToJson(xmlPath.toString(), jsonPath.toString()),
990+
"File conversion should not throw exceptions"
991+
);
992+
993+
// Verify the JSON file
994+
assertTrue(
995+
Files.exists(jsonPath),
996+
"JSON file should be created"
997+
);
998+
999+
String jsonContent = Files.readString(jsonPath);
1000+
assertAll("JSON file content verification",
1001+
() -> assertNotNull(jsonContent, "JSON content should not be null"),
1002+
() -> assertTrue(jsonContent.contains("\"item\": \"value\""),
1003+
"JSON should contain converted XML content")
1004+
);
1005+
}
1006+
1007+
@Test
1008+
void testFileXmlToJsonWithInvalidInput(@TempDir Path tempDir) {
1009+
Path nonExistentXml = tempDir.resolve("nonexistent.xml");
1010+
Path outputJson = tempDir.resolve("output.json");
1011+
1012+
assertThrows(
1013+
IOException.class,
1014+
() -> U.fileXmlToJson(nonExistentXml.toString(), outputJson.toString()),
1015+
"Should throw IOException when input file doesn't exist"
1016+
);
1017+
}
8401018
}

0 commit comments

Comments
 (0)