Skip to content

Commit 31dbd8d

Browse files
committed
Added tests
1 parent a1d34bb commit 31dbd8d

File tree

1 file changed

+176
-8
lines changed

1 file changed

+176
-8
lines changed

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

+176-8
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@
2525

2626
import static java.util.Arrays.asList;
2727
import static java.util.Collections.singletonList;
28-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertFalse;
31-
import static org.junit.jupiter.api.Assertions.assertNotSame;
32-
import static org.junit.jupiter.api.Assertions.assertNull;
33-
import static org.junit.jupiter.api.Assertions.assertThrows;
34-
import static org.junit.jupiter.api.Assertions.assertTrue;
35-
import static org.junit.jupiter.api.Assertions.fail;
28+
import static org.junit.jupiter.api.Assertions.*;
3629

30+
import java.io.IOException;
31+
import java.nio.charset.StandardCharsets;
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
3734
import java.util.Iterator;
3835
import java.util.LinkedHashMap;
3936
import java.util.List;
@@ -45,6 +42,7 @@
4542
import java.util.function.Function;
4643
import java.util.function.Predicate;
4744
import org.junit.jupiter.api.Test;
45+
import org.junit.jupiter.api.io.TempDir;
4846

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

0 commit comments

Comments
 (0)