-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1239e98
commit 6c059a6
Showing
5 changed files
with
104 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package htsjdk.testutil; | ||
|
||
import org.testng.Assert; | ||
|
||
public interface TestCase<T> { | ||
void test(ThrowingSupplier<T> functionToTest); | ||
|
||
|
||
interface ThrowingConsumer<T> { | ||
void test(T a) throws Exception; | ||
} | ||
|
||
static <T> TestCase<T> match(final T expected) { | ||
return new ComparisonTestCase<>((T actual) -> Assert.assertEquals(actual, expected)); | ||
} | ||
|
||
static <T> TestCase<T> mismatch(final T expected) { | ||
return new ComparisonTestCase<>((T actual) -> Assert.assertNotEquals(actual, expected)); | ||
} | ||
|
||
static <T> TestCase<T> exception(final Class<? extends Exception> exceptionClass) { | ||
return functionToTest -> Assert.assertThrows(exceptionClass, functionToTest::produce); | ||
} | ||
|
||
interface ThrowingSupplier<T> { | ||
T produce() throws Exception; | ||
} | ||
} | ||
|
||
final class ComparisonTestCase<T> implements TestCase<T> { | ||
private final ThrowingConsumer<T> test; | ||
|
||
@Override | ||
public void test(ThrowingSupplier<T> supplier) { | ||
try { | ||
test.test(supplier.produce()); | ||
} catch (AssertionError e) { | ||
throw e; | ||
} catch (Exception e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
ComparisonTestCase(ThrowingConsumer<T> test) { | ||
this.test = test; | ||
} | ||
|
||
} |
55 changes: 28 additions & 27 deletions
55
src/test/java/htsjdk/tribble/util/LittleEndianInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,46 @@ | ||
package htsjdk.tribble.util; | ||
|
||
import com.google.common.io.LittleEndianDataOutputStream; | ||
import htsjdk.HtsjdkTest; | ||
import org.testng.Assert; | ||
import htsjdk.testutil.TestCase; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.EOFException; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
|
||
import static org.testng.Assert.*; | ||
import java.io.PrintStream; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class LittleEndianInputStreamTest extends HtsjdkTest { | ||
|
||
@Test(expectedExceptions = EOFException.class) | ||
public void testReadStringEOF() throws IOException { | ||
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_no_terminator.bin"; | ||
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(emptyFile)))){ | ||
in.readString(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadStringWithExtendedCharacters() throws IOException { | ||
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin"; | ||
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(emptyFile)))){ | ||
Assert.assertEquals(in.readString(), "very dràààààmatic and null terminated"); | ||
} | ||
@DataProvider | ||
public Object[][] testCases() { | ||
final String missingTerminator = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_no_terminator.bin"; | ||
final String extendedAsciiFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin"; | ||
final Object utf8File = "src/test/resources/htsjdk/tribble/util/string_with_utf8_emoji_and_null_terminator.txt"; | ||
return new Object[][]{ | ||
{missingTerminator, StandardCharsets.ISO_8859_1, TestCase.exception(EOFException.class)}, | ||
{missingTerminator, StandardCharsets.US_ASCII, TestCase.exception(EOFException.class)}, | ||
{missingTerminator, StandardCharsets.UTF_8, TestCase.exception(EOFException.class)}, | ||
{extendedAsciiFile, StandardCharsets.ISO_8859_1, TestCase.match("very dràààààmatic and null terminated")}, | ||
{extendedAsciiFile, StandardCharsets.US_ASCII, TestCase.mismatch("very dràààààmatic and null terminated")}, | ||
{extendedAsciiFile, StandardCharsets.UTF_8, TestCase.mismatch("very dràààààmatic and null terminated")}, | ||
{utf8File, StandardCharsets.UTF_8, TestCase.match("🐋 UTF8 is Great 🐋")}, | ||
{utf8File, StandardCharsets.ISO_8859_1, TestCase.mismatch("🐋 UTF8 is Great 🐋")} | ||
}; | ||
} | ||
|
||
|
||
@Test(expectedExceptions = EOFException.class) | ||
public void write() throws IOException { | ||
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin"; | ||
try(final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(new FileOutputStream(emptyFile))){ | ||
out.writeBytes("very dràààààmatic and null terminated\0"); | ||
} | ||
@Test(dataProvider = "testCases") | ||
public void testAllCases(String filename, Charset charset, TestCase<String> expected) { | ||
expected.test(() -> { | ||
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(filename)))){ | ||
return in.readString(charset); | ||
} | ||
}); | ||
} | ||
|
||
} |