diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2021-03-11 11:33:49 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2021-03-11 11:33:49 +0000 |
commit | 19898ae400aa068af37ff754a9ebe7e801707fdd (patch) | |
tree | 4fd9f51c4d415f85835b6a5b530364def3f8ff67 | |
parent | 3d053b53f856516962626a847da1fcac61900bb4 (diff) | |
parent | 0e225e320bc1cdaba9c9f937cd9dccf7660e7522 (diff) | |
download | GeoTZ-19898ae400aa068af37ff754a9ebe7e801707fdd.tar.gz |
Merge "Use more generic assertThrows instead of assertThrowsExceptionName." into sc-dev
20 files changed, 300 insertions, 236 deletions
diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/block/BlockFileTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/block/BlockFileTest.java index 403684c..c1135c2 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/block/BlockFileTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/block/BlockFileTest.java @@ -16,7 +16,8 @@ package com.android.timezone.location.storage.block; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIndexOutOfBoundsException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.argThat; @@ -109,10 +110,10 @@ public class BlockFileTest { try (BlockFileReader bfr = BlockFileReader.open(false, blockFile, magic, 2)) { - assertThrowsIndexOutOfBoundsException(() -> bfr.getBlockInfo(-1)); - assertThrowsIndexOutOfBoundsException(() -> bfr.getBlock(-1)); - assertThrowsIndexOutOfBoundsException(() -> bfr.getBlockInfo(blockCount)); - assertThrowsIndexOutOfBoundsException(() -> bfr.getBlock(blockCount)); + assertThrows(IndexOutOfBoundsException.class, () -> bfr.getBlockInfo(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> bfr.getBlock(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> bfr.getBlockInfo(blockCount)); + assertThrows(IndexOutOfBoundsException.class, () -> bfr.getBlock(blockCount)); } } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/io/TypedStreamsTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/io/TypedStreamsTest.java index bdc4302..a50928d 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/io/TypedStreamsTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/io/TypedStreamsTest.java @@ -16,7 +16,8 @@ package com.android.timezone.location.storage.io; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -142,26 +143,40 @@ public class TypedStreamsTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); TypedOutputStream tos = new TypedOutputStream(baos); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(1, 0x00000000000001FFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(1, 0xE0000000000000FFL)); - - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(2, 0x000000000001FFFFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(2, 0xE00000000000FFFFL)); - - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(3, 0x0000000001FFFFFFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(3, 0xE000000000FFFFFFL)); - - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(4, 0x00000001FFFFFFFFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(4, 0xE0000000FFFFFFFFL)); - - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(5, 0x000001FFFFFFFFFFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(5, 0xE00000FFFFFFFFFFL)); - - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(6, 0x0001FFFFFFFFFFFFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(6, 0xE000FFFFFFFFFFFFL)); - - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(7, 0x01FFFFFFFFFFFFFFL)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(7, 0xE0FFFFFFFFFFFFFFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(1, 0x00000000000001FFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(1, 0xE0000000000000FFL)); + + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(2, 0x000000000001FFFFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(2, 0xE00000000000FFFFL)); + + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(3, 0x0000000001FFFFFFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(3, 0xE000000000FFFFFFL)); + + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(4, 0x00000001FFFFFFFFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(4, 0xE0000000FFFFFFFFL)); + + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(5, 0x000001FFFFFFFFFFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(5, 0xE00000FFFFFFFFFFL)); + + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(6, 0x0001FFFFFFFFFFFFL)); + assertThrows(IllegalArgumentException.class, () -> tos.writeVarByteValue(6, + 0xE000FFFFFFFFFFFFL)); + + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(7, 0x01FFFFFFFFFFFFFFL)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(7, 0xE0FFFFFFFFFFFFFFL)); } @@ -170,17 +185,19 @@ public class TypedStreamsTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); TypedOutputStream tos = new TypedOutputStream(baos); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(0, Long.MAX_VALUE)); - assertThrowsIllegalArgumentException(() -> tos.writeVarByteValue(9, Long.MAX_VALUE)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(0, Long.MAX_VALUE)); + assertThrows(IllegalArgumentException.class, + () -> tos.writeVarByteValue(9, Long.MAX_VALUE)); - assertThrowsIllegalArgumentException(() -> tos.writeByte(Byte.MIN_VALUE - 1)); - assertThrowsIllegalArgumentException(() -> tos.writeByte(Byte.MAX_VALUE + 1)); + assertThrows(IllegalArgumentException.class, () -> tos.writeByte(Byte.MIN_VALUE - 1)); + assertThrows(IllegalArgumentException.class, () -> tos.writeByte(Byte.MAX_VALUE + 1)); - assertThrowsIllegalArgumentException(() -> tos.writeChar(Character.MIN_VALUE - 1)); - assertThrowsIllegalArgumentException(() -> tos.writeChar(Character.MAX_VALUE + 1)); + assertThrows(IllegalArgumentException.class, () -> tos.writeChar(Character.MIN_VALUE - 1)); + assertThrows(IllegalArgumentException.class, () -> tos.writeChar(Character.MAX_VALUE + 1)); - assertThrowsIllegalArgumentException(() -> tos.writeUnsignedByte(-1)); - assertThrowsIllegalArgumentException(() -> tos.writeUnsignedByte(256)); + assertThrows(IllegalArgumentException.class, () -> tos.writeUnsignedByte(-1)); + assertThrows(IllegalArgumentException.class, () -> tos.writeUnsignedByte(256)); assertEquals(0, baos.toByteArray().length); } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2LevelRangeTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2LevelRangeTest.java index 7b222b8..b95e2a9 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2LevelRangeTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2LevelRangeTest.java @@ -17,7 +17,8 @@ package com.android.timezone.location.storage.s2; import static com.android.timezone.location.storage.s2.S2Support.cellId; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -33,12 +34,12 @@ public class S2LevelRangeTest { long nextLowCellId = cellId(12, 3, 101); long highCellId = cellId(12, 5, 110); new S2LevelRange(highCellId, lowCellId); - assertThrowsIllegalArgumentException(() -> new S2LevelRange(lowCellId, lowCellId)); + assertThrows(IllegalArgumentException.class, () -> new S2LevelRange(lowCellId, lowCellId)); new S2LevelRange(lowCellId, nextLowCellId); new S2LevelRange(lowCellId, highCellId); long highCellIdDifferentLevel = cellId(15, 2, 0); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> new S2LevelRange(lowCellId, highCellIdDifferentLevel)); } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2SupportTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2SupportTest.java index a9943b4..cf1f356 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2SupportTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/s2/S2SupportTest.java @@ -16,7 +16,8 @@ package com.android.timezone.location.storage.s2; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertEquals; import com.android.timezone.location.storage.util.BitwiseUtils; @@ -27,8 +28,8 @@ public class S2SupportTest { @Test public void cellIdToString() { - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString(0)); - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString(0)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11100000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L)); // Valid face IDs S2Support.cellIdToString( @@ -44,21 +45,21 @@ public class S2SupportTest { S2Support.cellIdToString( 0b10110000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L); // Invalid face ID - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11010000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L)); - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11110000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L)); // Final bit in a bad position. - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11101000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L)); - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11101010_00000000_00000000_00000000_00000000_00000000_00000000_00000000L)); - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11101010_10000000_00000000_00000000_00000000_00000000_00000000_00000000L)); - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11101010_10100000_00000000_00000000_00000000_00000000_00000000_00000000L)); - assertThrowsIllegalArgumentException(() -> S2Support.cellIdToString( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellIdToString( 0b11101010_10100000_00000000_00000000_00000000_00000000_00000000_00000010L)); // Valid @@ -71,40 +72,42 @@ public class S2SupportTest { @Test public void cellId() { // Invalid levels - assertThrowsIllegalArgumentException(() -> S2Support.cellId(-1, 0, 0)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(-1, 0, 0)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(S2Support.MAX_S2_LEVEL + 1, 0, 0)); // level 0 (face only) assertCellIdEquals(0b00010000L << 56, S2Support.cellId(0, 0, 0)); assertCellIdEquals(0b00110000L << 56, S2Support.cellId(0, 1, 0)); assertCellIdEquals(0b10110000L << 56, S2Support.cellId(0, S2Support.MAX_FACE_ID, 0)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(0, -1, 0)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(0, -1, 0)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(0, S2Support.MAX_FACE_ID + 1, 0)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(0, 0, -1)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(0, 0, 1)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(0, S2Support.MAX_FACE_ID, 1)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(0, 0, -1)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(0, 0, 1)); + assertThrows(IllegalArgumentException.class, + () -> S2Support.cellId(0, S2Support.MAX_FACE_ID, 1)); // level 1, 2 bits for index. - assertThrowsIllegalArgumentException(() -> S2Support.cellId(1, -1, 0)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(1, -1, 0)); assertCellIdEquals(0b00000100L << 56, S2Support.cellId(1, 0, 0)); assertCellIdEquals(0b00100100L << 56, S2Support.cellId(1, 1, 0)); assertCellIdEquals(0b10100100L << 56, S2Support.cellId(1, S2Support.MAX_FACE_ID, 0)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(1, S2Support.MAX_FACE_ID + 1, 0)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(1, S2Support.MAX_FACE_ID, -1)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(1, 0, -1)); + assertThrows(IllegalArgumentException.class, + () -> S2Support.cellId(1, S2Support.MAX_FACE_ID, -1)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(1, 0, -1)); assertCellIdEquals(0b00001100L << 56, S2Support.cellId(1, 0, 1)); assertCellIdEquals(0b00010100L << 56, S2Support.cellId(1, 0, 2)); assertCellIdEquals(0b00011100L << 56, S2Support.cellId(1, 0, 3)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(1, 0, 4)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(1, 0, 4)); assertCellIdEquals(0b00101100L << 56, S2Support.cellId(1, 1, 1)); assertCellIdEquals(0b00110100L << 56, S2Support.cellId(1, 1, 2)); assertCellIdEquals(0b00111100L << 56, S2Support.cellId(1, 1, 3)); - assertThrowsIllegalArgumentException(() -> S2Support.cellId(1, 0, 4)); + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(1, 0, 4)); // Level 30 - 3 face bits + 60 "storage bits" + 1-bit (1) assertCellIdEquals( @@ -125,7 +128,7 @@ public class S2SupportTest { assertCellIdEquals( 0b00111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111L, S2Support.cellId(30, 1, 0x0FFFFFFFFFFFFFFFL /* (2^61)-1 */)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> S2Support.cellId(30, 1, 0x1000000000000000L /* 2^61 */)); } @@ -140,23 +143,25 @@ public class S2SupportTest { @Test public void validateCellId() { // At the trailing 1. - assertThrowsIllegalArgumentException(() -> S2Support.validateCellId(0b00000000)); + assertThrows(IllegalArgumentException.class, () -> S2Support.validateCellId(0b00000000)); S2Support.validateCellId(0b00000001); - assertThrowsIllegalArgumentException(() -> S2Support.validateCellId(0b00000010)); + assertThrows(IllegalArgumentException.class, () -> S2Support.validateCellId(0b00000010)); S2Support.validateCellId(0b00000100); - assertThrowsIllegalArgumentException(() -> S2Support.validateCellId(0b00001000)); + assertThrows(IllegalArgumentException.class, () -> S2Support.validateCellId(0b00001000)); // Max valid face ID (101 = 5) S2Support.validateCellId((0b10100000L << 56) | 0b00000001L); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> S2Support.validateCellId((0b10100000L << 56) | 0b00000010)); S2Support.validateCellId((0b10100000L << 56) | 0b00000100L); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> S2Support.validateCellId((0b10100000L << 56) | 0b00001000)); // Invalid face ID (111 == 6) - assertThrowsIllegalArgumentException(() -> S2Support.validateCellId(0b11110000L << 56)); - assertThrowsIllegalArgumentException(() -> S2Support.validateCellId(0b11100100L << 56)); + assertThrows(IllegalArgumentException.class, + () -> S2Support.validateCellId(0b11110000L << 56)); + assertThrows(IllegalArgumentException.class, + () -> S2Support.validateCellId(0b11100100L << 56)); } @Test @@ -165,8 +170,10 @@ public class S2SupportTest { assertEquals(0b00010000L << 56, S2Support.offsetCellId(0b00010000L << 56, 0)); assertEquals(0b00000100L << 56, S2Support.offsetCellId(0b00000100L << 56, 0)); - assertThrowsIllegalArgumentException(() -> S2Support.offsetCellId(0b00010000L << 56, 6)); - assertThrowsIllegalArgumentException(() -> S2Support.offsetCellId(0b10111100L << 56, 24)); + assertThrows(IllegalArgumentException.class, + () -> S2Support.offsetCellId(0b00010000L << 56, 6)); + assertThrows(IllegalArgumentException.class, + () -> S2Support.offsetCellId(0b10111100L << 56, 24)); // Try level 0 behavior. assertEquals(0b00110000L << 56, S2Support.offsetCellId(0b00010000L << 56, 1)); @@ -192,11 +199,11 @@ public class S2SupportTest { @Test public void getS2Level() { assertEquals(30, S2Support.getS2Level(0b00000001L)); - assertThrowsIllegalArgumentException(() -> S2Support.getS2Level(0b00000010L)); + assertThrows(IllegalArgumentException.class, () -> S2Support.getS2Level(0b00000010L)); assertEquals(29, S2Support.getS2Level(0b00000100L)); assertEquals(1, S2Support.getS2Level(0b00000100L << 56)); - assertThrowsIllegalArgumentException(() -> S2Support.getS2Level(0b00001000L << 56)); + assertThrows(IllegalArgumentException.class, () -> S2Support.getS2Level(0b00001000L << 56)); assertEquals(0, S2Support.getS2Level(0b00010000L << 56)); } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/s2/SortedS2RangesTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/s2/SortedS2RangesTest.java index 38ddd81..0f8b159 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/s2/SortedS2RangesTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/s2/SortedS2RangesTest.java @@ -17,7 +17,8 @@ package com.android.timezone.location.storage.s2; import static com.android.timezone.location.storage.s2.S2Support.cellId; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -34,7 +35,7 @@ public class SortedS2RangesTest { cellId(s2Level, 1, 1001)); S2LevelRange range1 = new S2LevelRange(cellId(s2Level, 1, 1001), cellId(s2Level, 1, 1002)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> SortedS2Ranges.createFromSorted(s2Level, range1, range0)); } @@ -59,7 +60,7 @@ public class SortedS2RangesTest { @Test public void testOverlapsIllegal() { int s2Level = 15; - assertThrowsIllegalArgumentException(() -> SortedS2Ranges.createFromSorted(s2Level, + assertThrows(IllegalArgumentException.class, () -> SortedS2Ranges.createFromSorted(s2Level, new S2LevelRange(cellId(s2Level, 1, 1000), cellId(s2Level, 1, 1002)), new S2LevelRange(cellId(s2Level, 1, 1001), cellId(s2Level, 1, 1002)) )); @@ -69,7 +70,7 @@ public class SortedS2RangesTest { public void testMixedLevelsIllegal() { S2LevelRange level16Range = new S2LevelRange(cellId(16, 1, 1000), cellId(16, 1, 1001)); int s2Level = 15; - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> SortedS2Ranges.createFromSorted(s2Level, level16Range)); } } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/IntValuePackedTableTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/IntValuePackedTableTest.java index 7e93ec5..574c5ab 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/IntValuePackedTableTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/IntValuePackedTableTest.java @@ -16,9 +16,9 @@ package com.android.timezone.location.storage.table.packed; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIndexOutOfBoundsException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; import static com.android.timezone.location.storage.testing.TestSupport.setOf; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -63,8 +63,10 @@ public class IntValuePackedTableTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); boolean signedValue = false; try (PackedTableWriter writer = PackedTableWriter.create(baos, 5, 9, signedValue, null)) { - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, maxUnsignedInt + 1)); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, minUnsignedInt - 1)); + assertThrows(IllegalArgumentException.class, + () -> writer.addEntry(1, maxUnsignedInt + 1)); + assertThrows(IllegalArgumentException.class, + () -> writer.addEntry(1, minUnsignedInt - 1)); writer.addEntry(1, maxUnsignedInt); writer.addEntry(2, minUnsignedInt); } @@ -84,8 +86,10 @@ public class IntValuePackedTableTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); boolean signedValue = true; try (PackedTableWriter writer = PackedTableWriter.create(baos, 5, 8, signedValue, null)) { - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, maxSignedInt + 1)); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, minSignedInt - 1)); + assertThrows(IllegalArgumentException.class, + () -> writer.addEntry(1, maxSignedInt + 1)); + assertThrows(IllegalArgumentException.class, + () -> writer.addEntry(1, minSignedInt - 1)); writer.addEntry(1, maxSignedInt); writer.addEntry(2, minSignedInt); } @@ -105,8 +109,8 @@ public class IntValuePackedTableTest { boolean signedValue = true; try (PackedTableWriter writer = PackedTableWriter.create(baos, 5, 8, signedValue, null)) { writer.addEntry(100, 1); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, 2)); - assertThrowsIllegalArgumentException(() -> writer.addEntry(99, 2)); + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(1, 2)); + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(99, 2)); writer.addEntry(100, 2); writer.addEntry(101, 3); } @@ -168,8 +172,9 @@ public class IntValuePackedTableTest { assertEquals(1234, intValuePackedTable.getEntryByIndex(0).getValue()); assertEquals(2345, intValuePackedTable.getEntryByIndex(1).getValue()); assertEquals(3456, intValuePackedTable.getEntryByIndex(2).getValue()); - assertThrowsIndexOutOfBoundsException(() -> intValuePackedTable.getEntryByIndex(-1)); - assertThrowsIndexOutOfBoundsException(() -> intValuePackedTable.getEntryByIndex(3)); + assertThrows(IndexOutOfBoundsException.class, + () -> intValuePackedTable.getEntryByIndex(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> intValuePackedTable.getEntryByIndex(3)); assertNull(intValuePackedTable.getEntry(0)); assertNull(intValuePackedTable.getEntry(4)); } @@ -220,10 +225,11 @@ public class IntValuePackedTableTest { assertArrayEquals(new byte[0], intValuePackedTable.getSharedData()); int negativeKey = -1; - assertThrowsIllegalArgumentException(() -> intValuePackedTable.getEntry(negativeKey)); + assertThrows(IllegalArgumentException.class, + () -> intValuePackedTable.getEntry(negativeKey)); int keyTooBig = (int) BitwiseUtils.maxUnsignedValue(keyBits) + 1; - assertThrowsIllegalArgumentException(() -> intValuePackedTable.getEntry(keyTooBig)); + assertThrows(IllegalArgumentException.class, () -> intValuePackedTable.getEntry(keyTooBig)); } @Test diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/PackedTableReaderWriterTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/PackedTableReaderWriterTest.java index 9d828b5..0938f46 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/PackedTableReaderWriterTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/table/packed/PackedTableReaderWriterTest.java @@ -16,9 +16,10 @@ package com.android.timezone.location.storage.table.packed; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalStateException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; +import static com.android.timezone.location.storage.testing.TestSupport.listOf; import static com.android.timezone.location.storage.testing.TestSupport.setOf; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -134,10 +135,10 @@ public class PackedTableReaderWriterTest { BlockData blockData = new BlockData(createByteBuffer(baos.toByteArray())); PackedTableReader tableReader = new PackedTableReader(blockData); - assertThrowsIllegalStateException(() -> tableReader.getEntry(maxKey).getIntValue()); + assertThrows(IllegalStateException.class, () -> tableReader.getEntry(maxKey).getIntValue()); assertEquals(maxRepresentableValue, tableReader.getEntry(maxKey).getLongValue()); - assertThrowsIllegalStateException(() -> tableReader.getEntry(minKey).getIntValue()); + assertThrows(IllegalStateException.class, () -> tableReader.getEntry(minKey).getIntValue()); assertEquals(minRepresentableValue, tableReader.getEntry(minKey).getLongValue()); } @@ -150,8 +151,10 @@ public class PackedTableReaderWriterTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); boolean signedValue = false; PackedTableWriter writer = PackedTableWriter.create(baos, 5, 9, signedValue, null); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, maxUnsignedInt + 1)); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, minUnsignedInt - 1)); + + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(1, maxUnsignedInt + 1)); + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(1, minUnsignedInt - 1)); + writer.addEntry(1, maxUnsignedInt); writer.addEntry(2, minUnsignedInt); writer.close(); @@ -172,8 +175,10 @@ public class PackedTableReaderWriterTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); boolean signedValue = true; PackedTableWriter writer = PackedTableWriter.create(baos, 5, 8, signedValue, null); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, maxSignedInt + 1)); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, minSignedInt - 1)); + + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(1, maxSignedInt + 1)); + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(1, minSignedInt - 1)); + writer.addEntry(1, maxSignedInt); writer.addEntry(2, minSignedInt); writer.close(); @@ -191,8 +196,10 @@ public class PackedTableReaderWriterTest { boolean signedValue = true; PackedTableWriter writer = PackedTableWriter.create(baos, 5, 8, signedValue, null); writer.addEntry(100, 1); - assertThrowsIllegalArgumentException(() -> writer.addEntry(1, 2)); - assertThrowsIllegalArgumentException(() -> writer.addEntry(99, 2)); + + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(1, 2)); + assertThrows(IllegalArgumentException.class, () -> writer.addEntry(99, 2)); + writer.addEntry(100, 2); writer.addEntry(101, 3); writer.close(); @@ -283,10 +290,10 @@ public class PackedTableReaderWriterTest { assertArrayEquals(new byte[0], tableReader.getSharedData()); int negativeKey = -1; - assertThrowsIllegalArgumentException(() -> tableReader.getEntry(negativeKey)); + assertThrows(IllegalArgumentException.class, () -> tableReader.getEntry(negativeKey)); int keyTooBig = (int) BitwiseUtils.maxUnsignedValue(keyBits) + 1; - assertThrowsIllegalArgumentException(() -> tableReader.getEntry(keyTooBig)); + assertThrows(IllegalArgumentException.class, () -> tableReader.getEntry(keyTooBig)); } @Test @@ -340,7 +347,7 @@ public class PackedTableReaderWriterTest { // findIntValueEntry PackedTableReader.Entry oneOfMany = tableReader.findIntValueEntry(new IntValueEntryKeyMatcher(5)); - List<Integer> expectedValuesForKey = Arrays.asList(2, 3, 4, 5, 6, 7, 8); + List<Integer> expectedValuesForKey = listOf(2, 3, 4, 5, 6, 7, 8); assertTrue(oneOfMany.getIntValue() + " not expected", expectedValuesForKey.contains(oneOfMany.getIntValue())); PackedTableReader.Entry currentEntry = oneOfMany; @@ -424,7 +431,7 @@ public class PackedTableReaderWriterTest { PackedTableReader.Entry entry = tableReader.getEntry(3); assertEquals(3, entry.getKey()); assertEquals(1, entry.getIndex()); - assertThrowsIllegalStateException(entry::getIntValue); + assertThrows(IllegalStateException.class, entry::getIntValue); assertEquals(4567890, entry.getLongValue()); } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/testing/MoreAsserts.java b/s2storage/src/test/java/com/android/timezone/location/storage/testing/MoreAsserts.java new file mode 100644 index 0000000..cd71b5e --- /dev/null +++ b/s2storage/src/test/java/com/android/timezone/location/storage/testing/MoreAsserts.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.timezone.location.storage.testing; + +public final class MoreAsserts { + + private MoreAsserts() {} + + @FunctionalInterface + public interface ThrowingRunnable { + void run() throws Throwable; + } + + // Remove once JUnit is updated to 4.13 + public static <T extends Throwable> T assertThrows( + Class<T> expectedThrowable, + ThrowingRunnable runnable) { + try { + runnable.run(); + } catch (Throwable actualThrown) { + if (expectedThrowable.isInstance(actualThrown)) { + @SuppressWarnings("unchecked") T retVal = (T) actualThrown; + return retVal; + } else { + String expected = formatClass(expectedThrowable); + Class<? extends Throwable> actualThrowable = actualThrown.getClass(); + String actual = formatClass(actualThrowable); + + String mismatchExceptionMessage = "Unexpected exception type thrown: " + actual + + ", expected:" + expected; + throw new AssertionError(mismatchExceptionMessage, actualThrown); + } + } + String notThrownMessage = "Expected " + + formatClass(expectedThrowable) + " to be thrown, but nothing was thrown"; + throw new AssertionError(notThrownMessage); + } + + private static String formatClass(Class<?> value) { + String className = value.getCanonicalName(); + return className == null ? value.getName() : className; + } +} diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/testing/TestSupport.java b/s2storage/src/test/java/com/android/timezone/location/storage/testing/TestSupport.java index 33763b8..59163e9 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/testing/TestSupport.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/testing/TestSupport.java @@ -16,9 +16,6 @@ package com.android.timezone.location.storage.testing; -import org.junit.Assert; - -import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -29,42 +26,6 @@ public class TestSupport { private TestSupport() { } - @FunctionalInterface - public interface Testable { - - void run() throws IOException; - } - - public static void assertThrowsIllegalArgumentException(Testable runnable) { - try { - runnable.run(); - Assert.fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } catch (Exception e) { - Assert.fail("Unexpected exception: " + e.getMessage()); - } - } - - public static void assertThrowsIllegalStateException(Testable runnable) { - try { - runnable.run(); - Assert.fail("Expected IllegalStateException"); - } catch (IllegalStateException expected) { - } catch (Exception e) { - Assert.fail("Unexpected exception: " + e.getMessage()); - } - } - - public static void assertThrowsIndexOutOfBoundsException(Testable runnable) { - try { - runnable.run(); - Assert.fail("Expected IllegalArgumentException"); - } catch (IndexOutOfBoundsException expected) { - } catch (Exception e) { - Assert.fail("Unexpected exception: " + e.getMessage()); - } - } - @SafeVarargs public static <E> List<E> listOf(E... values) { return Arrays.asList(values); diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/BankedTzIdSetsTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/BankedTzIdSetsTest.java index 338cc1d..5dc2838 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/BankedTzIdSetsTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/BankedTzIdSetsTest.java @@ -16,8 +16,9 @@ package com.android.timezone.location.storage.tzs2range; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIndexOutOfBoundsException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; import static com.android.timezone.location.storage.testing.TestSupport.listOf; + import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -48,7 +49,7 @@ public class BankedTzIdSetsTest { assertEquals(2, bankedTzIdSets.getBankCount()); assertEquals(stringsByIndex, bankedTzIdSets.getStringsByIndex()); - assertThrowsIndexOutOfBoundsException(() -> bankedTzIdSets.getBank(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> bankedTzIdSets.getBank(-1)); BankedTzIdSets.Bank bank1 = bankedTzIdSets.getBank(0); assertEquals(3, bank1.getTzIdSetCount()); @@ -70,6 +71,6 @@ public class BankedTzIdSetsTest { assertEquals(listOf("Two", "Three", "Four", "Five"), bank2.getTzIdSet(3).getTzIds()); assertEquals(listOf(1, 2, 3, 4), bank2.getTzIdSet(3).getStringIds()); - assertThrowsIndexOutOfBoundsException(() -> bankedTzIdSets.getBank(2)); + assertThrows(IndexOutOfBoundsException.class, () -> bankedTzIdSets.getBank(2)); } } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/HeaderBlockTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/HeaderBlockTest.java index cf5ed9c..02af01f 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/HeaderBlockTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/HeaderBlockTest.java @@ -17,6 +17,7 @@ package com.android.timezone.location.storage.tzs2range; import static com.android.timezone.location.storage.testing.TestSupport.listOf; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableBlockTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableBlockTest.java index 5ff17e8..ffedad9 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableBlockTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableBlockTest.java @@ -16,9 +16,8 @@ package com.android.timezone.location.storage.tzs2range; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalStateException; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIndexOutOfBoundsException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -57,7 +56,7 @@ public class SuffixTableBlockTest { SuffixTableWriter suffixTableWriter = SuffixTableWriter.createPopulated(fileFormat, suffixTableSharedData); - assertThrowsIllegalStateException(suffixTableWriter::close); + assertThrows(IllegalStateException.class, suffixTableWriter::close); } @Test @@ -82,12 +81,14 @@ public class SuffixTableBlockTest { { SuffixTableRange badStartCellId = new SuffixTableRange(invalidStartCellId, validEndCellId, 1); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(badStartCellId)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(badStartCellId)); } { SuffixTableRange badEndCellId = new SuffixTableRange(validStartCellId, invalidEndCellId, 1); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(badEndCellId)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(badEndCellId)); } } @@ -118,7 +119,8 @@ public class SuffixTableBlockTest { 2); suffixTableWriter.addRange(suffixTableRange2); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange2)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange2)); // Try similar checks at the top end of the table. SuffixTableRange suffixTableRange3 = new SuffixTableRange( @@ -127,9 +129,12 @@ public class SuffixTableBlockTest { 3); suffixTableWriter.addRange(suffixTableRange3); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange1)); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange2)); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange3)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange1)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange2)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange3)); // Now "complete" the table: there can be no entry after this one. SuffixTableRange suffixTableRange4 = new SuffixTableRange( @@ -138,12 +143,17 @@ public class SuffixTableBlockTest { 4); suffixTableWriter.addRange(suffixTableRange4); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange4)); - - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange1)); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange2)); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange3)); - assertThrowsIllegalArgumentException(() -> suffixTableWriter.addRange(suffixTableRange4)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange4)); + + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange1)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange2)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange3)); + assertThrows(IllegalArgumentException.class, + () -> suffixTableWriter.addRange(suffixTableRange4)); } @Test @@ -156,8 +166,10 @@ public class SuffixTableBlockTest { assertEquals(tablePrefix, suffixTableBlock.getPrefix()); assertNull(suffixTableBlock.findEntryByCellId(fileFormat.createCellId(tablePrefix, 1))); assertEquals(0, suffixTableBlock.getEntryCount()); - assertThrowsIndexOutOfBoundsException(() -> suffixTableBlock.getEntryByIndex(0)); - assertThrowsIndexOutOfBoundsException(() -> suffixTableBlock.getEntryByIndex(1)); + assertThrows(IndexOutOfBoundsException.class, + () -> suffixTableBlock.getEntryByIndex(0)); + assertThrows(IndexOutOfBoundsException.class, + () -> suffixTableBlock.getEntryByIndex(1)); } @Test @@ -221,8 +233,10 @@ public class SuffixTableBlockTest { findEntryByCellId(fileFormat, suffixTableBlock, tablePrefix, maxSuffix)); assertEquals(4, suffixTableBlock.getEntryCount()); - assertThrowsIndexOutOfBoundsException(() -> suffixTableBlock.getEntryByIndex(-1)); - assertThrowsIndexOutOfBoundsException(() -> suffixTableBlock.getEntryByIndex(4)); + assertThrows(IndexOutOfBoundsException.class, + () -> suffixTableBlock.getEntryByIndex(-1)); + assertThrows(IndexOutOfBoundsException.class, + () -> suffixTableBlock.getEntryByIndex(4)); assertEquals(entry1, suffixTableBlock.getEntryByIndex(0).getSuffixTableRange()); assertEquals(entry2, suffixTableBlock.getEntryByIndex(1).getSuffixTableRange()); @@ -254,11 +268,9 @@ public class SuffixTableBlockTest { SuffixTableBlock suffixTableBlock = SuffixTableBlock.createPopulated(fileFormat, blockReadback.getBlockData()); - assertThrowsIllegalArgumentException( - () -> suffixTableBlock.findEntryByCellId( + assertThrows(IllegalArgumentException.class, () -> suffixTableBlock.findEntryByCellId( fileFormat.createCellId(tablePrefix - 1, 0))); - assertThrowsIllegalArgumentException( - () -> suffixTableBlock.findEntryByCellId( + assertThrows(IllegalArgumentException.class, () -> suffixTableBlock.findEntryByCellId( fileFormat.createCellId(tablePrefix + 1, 0))); } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableRangeTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableRangeTest.java index 66e20ac..a51b344 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableRangeTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/SuffixTableRangeTest.java @@ -17,7 +17,8 @@ package com.android.timezone.location.storage.tzs2range; import static com.android.timezone.location.storage.s2.S2Support.cellId; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -33,9 +34,9 @@ public class SuffixTableRangeTest { long startCellId = cellId(12, 5, 1); long endCellId = cellId(12, 5, 2); int tzIdSetId = 100; - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> new SuffixTableRange(startCellId, startCellId, tzIdSetId)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> new SuffixTableRange(endCellId, startCellId, tzIdSetId)); } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileFormatTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileFormatTest.java index 5e30b99..7bfb741 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileFormatTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileFormatTest.java @@ -17,7 +17,8 @@ package com.android.timezone.location.storage.tzs2range; import static com.android.timezone.location.storage.s2.S2Support.cellId; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -95,11 +96,11 @@ public class TzS2RangeFileFormatTest { entryRangeLengthBitCount); // Too many bits for prefixValue - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> tzS2RangeFileFormat.createCellId(0b1000_00000000, 0b10000000_00000000)); // Too many bits for suffixValue - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> tzS2RangeFileFormat.createCellId(0b1000_00000000, 0b100000000_00000000)); // Some valid cases. @@ -151,11 +152,11 @@ public class TzS2RangeFileFormatTest { // ID: suffixTableEntryBitCount(24) - suffixBitCount(16) - entryRangeLengthBitCount(5) = 3 // Too many bits for rangeLength - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> tzS2RangeFileFormat.createSuffixTableValue(0b100000, 0b101)); // Too many bits for tzIdSetId - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> tzS2RangeFileFormat.createSuffixTableValue(0b10101, 0b1101)); // Some valid cases. diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileTest.java index 1167a31..1cfe41e 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeFileTest.java @@ -18,6 +18,7 @@ package com.android.timezone.location.storage.tzs2range; import static com.android.timezone.location.storage.s2.S2Support.FACE_BIT_COUNT; import static com.android.timezone.location.storage.testing.TestSupport.listOf; + import static org.junit.Assert.assertEquals; import com.android.timezone.location.storage.tzs2range.read.TzS2RangeFileReader; diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeTest.java index 55328cd..2d27d04 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/TzS2RangeTest.java @@ -17,8 +17,9 @@ package com.android.timezone.location.storage.tzs2range; import static com.android.timezone.location.storage.s2.S2Support.cellId; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; import static com.android.timezone.location.storage.testing.TestSupport.listOf; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -36,8 +37,10 @@ public class TzS2RangeTest { long startCellId = cellId(12, 5, 1); long endCellId = cellId(12, 5, 2); List<String> tzIds = listOf("One", "Two"); - assertThrowsIllegalArgumentException(() -> new TzS2Range(startCellId, startCellId, tzIds)); - assertThrowsIllegalArgumentException(() -> new TzS2Range(endCellId, startCellId, tzIds)); + assertThrows(IllegalArgumentException.class, + () -> new TzS2Range(startCellId, startCellId, tzIds)); + assertThrows(IllegalArgumentException.class, + () -> new TzS2Range(endCellId, startCellId, tzIds)); } @Test diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/BankedTzIdSetsPackerTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/BankedTzIdSetsPackerTest.java index db5359d..6b4a37a 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/BankedTzIdSetsPackerTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/BankedTzIdSetsPackerTest.java @@ -17,9 +17,10 @@ package com.android.timezone.location.storage.tzs2range.write; import static com.android.timezone.location.storage.s2.S2Support.cellId; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; import static com.android.timezone.location.storage.testing.TestSupport.listOf; import static com.android.timezone.location.storage.testing.TestSupport.setOf; + import static org.junit.Assert.assertEquals; import com.android.timezone.location.storage.tzs2range.BankedTzIdSets; @@ -79,8 +80,7 @@ public class BankedTzIdSetsPackerTest { BankedTzIdSetsPacker.BankHelper bank1_1Helper = packer.addTzIdSets(idSet1_1); assertEquals(0, bank1_1Helper.getId()); - List<List<String>> idSet1_2 = listOf( - listOf("One", "Two", "Three", "Four", "Five")); + List<List<String>> idSet1_2 = listOf(listOf("One", "Two", "Three", "Four", "Five")); BankedTzIdSetsPacker.BankHelper bank1_2Helper = packer.addTzIdSets(idSet1_2); assertEquals(0, bank1_2Helper.getId()); @@ -90,8 +90,7 @@ public class BankedTzIdSetsPackerTest { BankedTzIdSetsPacker.BankHelper bank1_2Helper2 = packer.addTzIdSets(idSet1_2); assertEquals(0, bank1_2Helper2.getId()); - List<List<String>> idSet2 = listOf( - listOf("Two")); + List<List<String>> idSet2 = listOf(listOf("Two")); BankedTzIdSetsPacker.BankHelper bank2Helper = packer.addTzIdSets(idSet2); assertEquals(1, bank2Helper.getId()); } @@ -105,20 +104,20 @@ public class BankedTzIdSetsPackerTest { listOf("One", "Two"), listOf("One", "Two", "Three"), listOf("One", "Two", "Three", "Four")); - assertThrowsIllegalArgumentException(() -> packer.addTzIdSets(idSet1_1)); + assertThrows(IllegalArgumentException.class, () -> packer.addTzIdSets(idSet1_1)); } @Test public void extractUniqueTzIdSets() { - List<TzS2Range> ranges = listOf( - new TzS2Range(cellId(12, 1, 1), cellId(12, 1, 2), listOf("One", "Two", "Three")), + List<TzS2Range> ranges = listOf(new TzS2Range(cellId(12, 1, 1), cellId(12, 1, 2), + listOf("One", "Two", "Three")), new TzS2Range(cellId(12, 1, 2), cellId(12, 1, 3), listOf("One")), new TzS2Range(cellId(12, 1, 3), cellId(12, 1, 4), listOf("Two")), new TzS2Range(cellId(12, 1, 4), cellId(12, 1, 5), listOf("One")), - new TzS2Range(cellId(12, 1, 5), cellId(12, 1, 6), listOf("One", "Two", "Three")), + new TzS2Range(cellId(12, 1, 5), cellId(12, 1, 6), + listOf("One", "Two", "Three")), new TzS2Range(cellId(12, 1, 6), cellId(12, 1, 7), listOf("Two", "Three")), - new TzS2Range(cellId(12, 1, 7), cellId(12, 1, 8), listOf("Three")) - ); + new TzS2Range(cellId(12, 1, 7), cellId(12, 1, 8), listOf("Three"))); List<List<String>> uniqueTzIdSetsList = BankedTzIdSetsPacker.extractUniqueTzIdSets(ranges); Set<List<String>> uniqueTzIdSetsSet = new HashSet<>(uniqueTzIdSetsList); assertEquals(uniqueTzIdSetsList.size(), uniqueTzIdSetsSet.size()); diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/PushBackIteratorTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/PushBackIteratorTest.java index a3607b4..8d4ca50 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/PushBackIteratorTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/tzs2range/write/PushBackIteratorTest.java @@ -16,11 +16,12 @@ package com.android.timezone.location.storage.tzs2range.write; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; import static com.android.timezone.location.storage.testing.TestSupport.listOf; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; @@ -61,11 +62,8 @@ public class PushBackIteratorTest { assertTrue(iterator.hasNext()); assertEquals("Four", iterator.next()); assertFalse(iterator.hasNext()); - try { - iterator.next(); - fail(); - } catch (NoSuchElementException expected) { - } + + assertThrows(NoSuchElementException.class, iterator::next); // iterator = Empty iterator.pushBack("Four"); @@ -79,25 +77,14 @@ public class PushBackIteratorTest { PushBackIterator<String> iterator = new PushBackIterator<>(values.iterator()); assertEquals("One", iterator.next()); - try { - iterator.remove(); - fail(); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, iterator::remove); iterator.pushBack("One"); iterator.pushBack("Zero"); - try { - iterator.remove(); - fail(); - } catch (UnsupportedOperationException expected) { - } + + assertThrows(UnsupportedOperationException.class, iterator::remove); assertEquals("Zero", iterator.next()); - try { - iterator.remove(); - fail(); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, iterator::remove); } } diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/util/BitwiseUtilsTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/util/BitwiseUtilsTest.java index 41ae81e..af6a65d 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/util/BitwiseUtilsTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/util/BitwiseUtilsTest.java @@ -16,7 +16,8 @@ package com.android.timezone.location.storage.util; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; + import static org.junit.Assert.assertEquals; import org.junit.Assert; @@ -26,8 +27,8 @@ public class BitwiseUtilsTest { @Test public void maxUnsignedValue() { - assertThrowsIllegalArgumentException(() -> BitwiseUtils.maxUnsignedValue(0)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.maxUnsignedValue(64)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.maxUnsignedValue(0)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.maxUnsignedValue(64)); assertEquals(1, BitwiseUtils.maxUnsignedValue(1)); assertEquals(3, BitwiseUtils.maxUnsignedValue(2)); @@ -38,8 +39,8 @@ public class BitwiseUtilsTest { @Test public void getLowBitsMask() { - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getLowBitsMask(-1)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getLowBitsMask(65)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getLowBitsMask(-1)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getLowBitsMask(65)); assertEquals(0x0, BitwiseUtils.getLowBitsMask(0)); assertEquals(0x1, BitwiseUtils.getLowBitsMask(1)); @@ -50,10 +51,10 @@ public class BitwiseUtilsTest { @Test public void getMidBitsMask() { - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getMidBitsMask(0, 1)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getMidBitsMask(65, 1)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getMidBitsMask(64, 65)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getMidBitsMask(16, 17)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getMidBitsMask(0, 1)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getMidBitsMask(65, 1)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getMidBitsMask(64, 65)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getMidBitsMask(16, 17)); assertEquals(0b1000, BitwiseUtils.getMidBitsMask(4, 1)); assertEquals(0b1100, BitwiseUtils.getMidBitsMask(4, 2)); @@ -144,15 +145,15 @@ public class BitwiseUtilsTest { assertEquals(0xFFFFFFFFFFFFFFFCL, BitwiseUtils.getHighBitsMask(62)); assertEquals(0xFFFFFFFFFFFFFFFEL, BitwiseUtils.getHighBitsMask(63)); assertEquals(0xFFFFFFFFFFFFFFFFL, BitwiseUtils.getHighBitsMask(64)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getHighBitsMask(65)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getHighBitsMask(0)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getHighBitsMask(65)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getHighBitsMask(0)); } @Test public void extractLowBits() { - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.extractLowBits(65, 0xFFFFFFFFFFFFFFFFL)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.extractLowBits(0, 0xFFFFFFFFFFFFFFFFL)); assertEquals(0xFFFFFFFFFFFFFFFFL, BitwiseUtils.extractLowBits(64, 0xFFFFFFFFFFFFFFFFL)); @@ -164,8 +165,8 @@ public class BitwiseUtilsTest { @Test public void getSingleBitMask() { - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getSingleBitMask(64)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.getSingleBitMask(-1)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getSingleBitMask(64)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.getSingleBitMask(-1)); assertEquals(0x8000000000000000L, BitwiseUtils.getSingleBitMask(63)); assertEquals(0x4000000000000000L, BitwiseUtils.getSingleBitMask(62)); @@ -176,9 +177,9 @@ public class BitwiseUtilsTest { @Test public void signExtendToLong() { - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.signExtendToLong(65, 0x555555555555555L)); - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.signExtendToLong(0, 0x555555555555555L)); assertEquals(0xF555555555555555L, BitwiseUtils.signExtendToLong(64, 0xF555555555555555L)); @@ -208,7 +209,7 @@ public class BitwiseUtilsTest { assertEquals("1", BitwiseUtils.toUnsignedString(1, 1)); assertEquals("10", BitwiseUtils.toUnsignedString(2, 2)); - assertThrowsIllegalArgumentException(() -> BitwiseUtils.toUnsignedString(1, 2)); + assertThrows(IllegalArgumentException.class, () -> BitwiseUtils.toUnsignedString(1, 2)); } @Test diff --git a/s2storage/src/test/java/com/android/timezone/location/storage/util/ConditionsTest.java b/s2storage/src/test/java/com/android/timezone/location/storage/util/ConditionsTest.java index a0b9ab9..e6102f2 100644 --- a/s2storage/src/test/java/com/android/timezone/location/storage/util/ConditionsTest.java +++ b/s2storage/src/test/java/com/android/timezone/location/storage/util/ConditionsTest.java @@ -16,8 +16,7 @@ package com.android.timezone.location.storage.util; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalArgumentException; -import static com.android.timezone.location.storage.testing.TestSupport.assertThrowsIllegalStateException; +import static com.android.timezone.location.storage.testing.MoreAsserts.assertThrows; import org.junit.Test; @@ -34,9 +33,9 @@ public class ConditionsTest { Conditions.checkStateInRange("foo", validValue, "min", minIncl, "max", maxIncl); } for (int invalidValue : invalidValues) { - assertThrowsIllegalStateException( + assertThrows(IllegalStateException.class, () -> Conditions.checkStateInRange("foo", invalidValue, "min", minIncl, "max", - maxIncl)); + maxIncl)); } } @@ -52,10 +51,10 @@ public class ConditionsTest { Conditions.checkArgInRange("foo", validValue, minIncl, maxIncl); } for (int invalidValue : invalidValues) { - assertThrowsIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> Conditions.checkArgInRange("foo", invalidValue, "min", minIncl, "max", - maxIncl)); - assertThrowsIllegalArgumentException( + maxIncl)); + assertThrows(IllegalArgumentException.class, () -> Conditions.checkArgInRange("foo", invalidValue, minIncl, maxIncl)); } } |