Skip to content

Commit

Permalink
Merge pull request #2802 from SCADA-LTS/fix/#2801_Corrected_Serializa…
Browse files Browse the repository at this point in the history
…tionHelperTest

#2801 Corrected SerializationHelperTest -
  • Loading branch information
Limraj authored Jan 15, 2024
2 parents ea322c5 + 90ecdc7 commit d0c10f9
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 39 deletions.
1 change: 0 additions & 1 deletion test-resources/encoding/3_5mb_5242880_bytes_iso_latin1.txt

This file was deleted.

1 change: 0 additions & 1 deletion test-resources/encoding/3_5mb_5242880_bytes_us_ascii.txt

This file was deleted.

1 change: 0 additions & 1 deletion test-resources/encoding/65535_bytes_iso_latin1.txt

This file was deleted.

1 change: 0 additions & 1 deletion test-resources/encoding/65535_bytes_us_ascii.txt

This file was deleted.

Binary file removed test-resources/encoding/65535_bytes_utf16.txt
Binary file not shown.
1 change: 0 additions & 1 deletion test-resources/encoding/65536_bytes_iso_latin1.txt

This file was deleted.

1 change: 0 additions & 1 deletion test-resources/encoding/65536_bytes_us_ascii.txt

This file was deleted.

Binary file removed test-resources/encoding/65536_bytes_utf16.txt
Binary file not shown.
1 change: 0 additions & 1 deletion test-resources/encoding/662_bytes_iso_latin1.txt

This file was deleted.

1 change: 0 additions & 1 deletion test-resources/encoding/662_bytes_us_ascii.txt

This file was deleted.

Binary file removed test-resources/encoding/662_bytes_utf16.txt
Binary file not shown.
Binary file removed test-resources/encoding/7mb_5242880_bytes_utf16.txt
Binary file not shown.
59 changes: 28 additions & 31 deletions test/com/serotonin/util/SerializationHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,9 @@ public class SerializationHelperTest {
public static String[][] data() {
return new String[][] {
{"662_bytes_utf8.txt", "UTF-8"},
{"662_bytes_iso_latin1.txt", "ISO_8859_1"},
{"662_bytes_us_ascii.txt", "ASCII"},
{"662_bytes_utf16.txt", "UTF-16"},

{"65535_bytes_utf8.txt", "UTF-8"},
{"65535_bytes_iso_latin1.txt", "ISO_8859_1"},
{"65535_bytes_us_ascii.txt", "ASCII"},
{"65535_bytes_utf16.txt", "UTF-16"},

{"65536_bytes_utf8.txt", "UTF-8"},
{"65536_bytes_iso_latin1.txt", "ISO_8859_1"},
{"65536_bytes_us_ascii.txt", "ASCII"},
{"65536_bytes_utf16.txt", "UTF-16"},

{"5mb_5242880_bytes_utf8.txt", "UTF-8"},
{"3_5mb_5242880_bytes_iso_latin1.txt", "ISO_8859_1"},
{"3_5mb_5242880_bytes_us_ascii.txt", "ASCII"},
{"7mb_5242880_bytes_utf16.txt", "UTF-16"}
};
}

Expand All @@ -52,41 +37,53 @@ public SerializationHelperTest(String file, String encoding) throws IOException

@Test
public void when_writeSafeUTF_and_readSafeUTF_then_equals_bytes() throws IOException {

//given:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream outputStream = new ObjectOutputStream(baos)) {

//when:
//when:
try(ObjectOutputStream outputStream = new ObjectOutputStream(baos)) {
SerializationHelper.writeSafeUTF(outputStream, toWrite);
}

//then
try(ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
String result = SerializationHelper.readSafeUTF(inputStream);

//then
Assert.assertEquals(toList(utf8Expected.getBytes()), toList(result.getBytes()));
byte[] bytesExpected = utf8Expected.getBytes();
byte[] bytesResult = result.getBytes();

Assert.assertEquals(bytesExpected.length, bytesResult.length);

for(int i = 0; i < bytesExpected.length ; i ++) {
Assert.assertEquals(bytesExpected[i], bytesResult[i]);
}
}
}

@Test
public void when_writeSafeUTF_and_readSafeUTF_then_equals() throws IOException {
public void when_writeSafeUTF_and_readSafeUTF_then_equals_chars() throws IOException {

//given:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream outputStream = new ObjectOutputStream(baos)) {

//when:
//when:
try(ObjectOutputStream outputStream = new ObjectOutputStream(baos)) {
SerializationHelper.writeSafeUTF(outputStream, toWrite);
}

//then
try(ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
String result = SerializationHelper.readSafeUTF(inputStream);

//then
Assert.assertEquals(utf8Expected, result);
}
}
char[] charsExpected = utf8Expected.toCharArray();
char[] charsResult = result.toCharArray();

Assert.assertEquals(charsExpected.length, charsResult.length);

private static List<Byte> toList(byte[] bytes) {
List<Byte> byteList = new ArrayList<>();
for(byte by: bytes) {
byteList.add(by);
for(int i = 0; i < charsExpected.length ; i ++) {
Assert.assertEquals(charsExpected[i], charsResult[i]);
}
}
return byteList;
}
}

0 comments on commit d0c10f9

Please sign in to comment.