Skip to content

Commit

Permalink
Add tests in ArjArchiveInputStreamTest
Browse files Browse the repository at this point in the history
- Specifically test ArjArchiveInputStream.read(byte[])
- Specifically test ArjArchiveInputStream.read(byte[],int,int)
  • Loading branch information
garydgregory committed Dec 12, 2024
1 parent b743130 commit 64c14b7
Showing 1 changed file with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.TimeZone;

Expand Down Expand Up @@ -98,7 +99,78 @@ public void testArjUnarchive() throws Exception {
assertArjArchiveEntry(entry);
}
}
assertEquals(result.toString(), expected.toString());
assertEquals(expected.toString(), result.toString());
}

@Test
public void testArjUnarchiveRead() throws Exception {
final StringBuilder expected = new StringBuilder();
expected.append("test1.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>\n");
final StringBuilder result = new StringBuilder();
try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
ArjArchiveEntry entry;
while ((entry = in.getNextEntry()) != null) {
result.append(entry.getName());
int tmp;
// read() one at a time
while ((tmp = in.read()) != -1) {
result.append((char) tmp);
}
assertFalse(entry.isDirectory());
assertArjArchiveEntry(entry);
}
}
assertEquals(expected.toString(), result.toString());
}

@Test
public void testArjUnarchiveReadByteArray() throws Exception {
final StringBuilder expected = new StringBuilder();
expected.append("test1.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>\n");
final StringBuilder result = new StringBuilder();
try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
ArjArchiveEntry entry;
while ((entry = in.getNextEntry()) != null) {
result.append(entry.getName());
final byte[] tmp = new byte[2];
// read(byte[]) at a time
int count;
while ((count = in.read(tmp)) != -1) {
result.append(new String(tmp, 0, count, Charset.defaultCharset()));
}
assertFalse(entry.isDirectory());
assertArjArchiveEntry(entry);
}
}
assertEquals(expected.toString(), result.toString());
}

@Test
public void testArjUnarchiveReadByteArrayIndex() throws Exception {
final StringBuilder expected = new StringBuilder();
expected.append("test1.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>\n");
final StringBuilder result = new StringBuilder();
try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
ArjArchiveEntry entry;
while ((entry = in.getNextEntry()) != null) {
result.append(entry.getName());
final byte[] tmp = new byte[10];
// read(byte[],int,int) at a time
int count;
while ((count = in.read(tmp, 0, 2)) != -1) {
result.append(new String(tmp, 0, count, Charset.defaultCharset()));
}
assertFalse(entry.isDirectory());
assertArjArchiveEntry(entry);
}
}
assertEquals(expected.toString(), result.toString());
}

@Test
Expand Down

0 comments on commit 64c14b7

Please sign in to comment.