Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
import static org.junit.Assert.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -137,20 +139,17 @@ public void remove() {
}

public void testCanCatchJdkBug6529795InTargetIterator() {
try {
/* Choose 4 steps to get sequence [next, next, next, remove] */
new IteratorTester<Integer>(
4, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) {
@Override
protected Iterator<Integer> newTargetIterator() {
Iterator<Integer> iterator = newArrayList(1, 2).iterator();
return new IteratorWithJdkBug6529795<>(iterator);
}
}.test();
} catch (AssertionError e) {
return;
}
fail("Should have caught jdk6 bug in target iterator");
IteratorTester<Integer> tester =
new IteratorTester<Integer>(
// We choose 4 steps to get the sequence [next, next, next, remove].
4, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) {
@Override
protected Iterator<Integer> newTargetIterator() {
Iterator<Integer> iterator = newArrayList(1, 2).iterator();
return new IteratorWithJdkBug6529795<>(iterator);
}
};
assertFailure(tester);
}

private static final int STEPS = 3;
Expand Down Expand Up @@ -202,13 +201,7 @@ protected void verify(List<Integer> elements) {
throw new AssertionError(message);
}
};
AssertionError actual = null;
try {
tester.test();
} catch (AssertionError e) {
actual = e;
}
assertNotNull("verify() should be able to cause test failure", actual);
AssertionError actual = assertFailure(tester);
assertTrue(
"AssertionError should have info about why test failed",
actual.getCause().getMessage().contains(message));
Expand Down Expand Up @@ -318,13 +311,9 @@ public boolean hasNext() {
assertFailure(tester);
}

private static void assertFailure(IteratorTester<?> tester) {
try {
tester.test();
} catch (AssertionError expected) {
return;
}
fail();
@CanIgnoreReturnValue
private static AssertionError assertFailure(IteratorTester<?> tester) {
return assertThrows(AssertionError.class, tester::test);
}

private static final class ThrowingIterator<E> implements Iterator<E> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Equivalence;
Expand Down Expand Up @@ -105,17 +104,15 @@ public void test_symmetric() {

equivalenceMock.replay();

try {
tester.addEquivalenceGroup(group1Item1, group1Item2).test();
} catch (AssertionFailedError expected) {
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+ "TestObject{group=1, item=1} [group 1, item 1]");
return;
}
fail();
AssertionFailedError expected =
assertThrows(
AssertionFailedError.class,
() -> tester.addEquivalenceGroup(group1Item1, group1Item2).test());
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+ "TestObject{group=1, item=1} [group 1, item 1]");
}

@Test
Expand All @@ -137,17 +134,15 @@ public void test_transitive() {

equivalenceMock.replay();

try {
tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test();
} catch (AssertionFailedError expected) {
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+ "TestObject{group=1, item=3} [group 1, item 3]");
return;
}
fail();
AssertionFailedError expected =
assertThrows(
AssertionFailedError.class,
() -> tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test());
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+ "TestObject{group=1, item=3} [group 1, item 3]");
}

@Test
Expand All @@ -163,17 +158,15 @@ public void test_inequivalence() {

equivalenceMock.replay();

try {
tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test();
} catch (AssertionFailedError expected) {
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to "
+ "TestObject{group=2, item=1} [group 2, item 1]");
return;
}
fail();
AssertionFailedError expected =
assertThrows(
AssertionFailedError.class,
() -> tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test());
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to "
+ "TestObject{group=2, item=1} [group 2, item 1]");
}

@Test
Expand All @@ -189,18 +182,14 @@ public void test_hash() {

equivalenceMock.replay();

try {
tester.addEquivalenceGroup(group1Item1, group1Item2).test();
} catch (AssertionFailedError expected) {
String expectedMessage =
"the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be "
+ "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]";
if (!expected.getMessage().contains(expectedMessage)) {
fail("<" + expected.getMessage() + "> expected to contain <" + expectedMessage + ">");
}
return;
}
fail();
AssertionFailedError expected =
assertThrows(
AssertionFailedError.class,
() -> tester.addEquivalenceGroup(group1Item1, group1Item2).test());
String expectedMessage =
"the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be "
+ "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]";
assertThat(expected).hasMessageThat().contains(expectedMessage);
}

/** An object with a friendly {@link #toString()}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertThrows;

import com.google.common.annotations.GwtCompatible;
import junit.framework.TestCase;
Expand Down Expand Up @@ -86,15 +87,9 @@ public void testThrowingTearDown() {
assertEquals(false, tearDownOne.ran);
assertEquals(false, tearDownTwo.ran);

try {
stack.runTearDown();
fail("runTearDown should have thrown an exception");
} catch (RuntimeException expected) {
assertThat(expected).hasMessageThat().isEqualTo("two");
assertThat(getOnlyElement(asList(expected.getSuppressed())))
.hasMessageThat()
.isEqualTo("one");
}
RuntimeException expected = assertThrows(RuntimeException.class, () -> stack.runTearDown());
assertThat(expected).hasMessageThat().isEqualTo("two");
assertThat(getOnlyElement(asList(expected.getSuppressed()))).hasMessageThat().isEqualTo("one");

assertEquals(true, tearDownOne.ran);
assertEquals(true, tearDownTwo.ran);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,13 @@ private <T> void assertFailure(
Class<T> interfaceType,
Function<T, ? extends T> wrapperFunction,
String... expectedMessages) {
try {
tester.testForwarding(interfaceType, wrapperFunction);
} catch (AssertionFailedError expected) {
for (String message : expectedMessages) {
assertThat(expected).hasMessageThat().contains(message);
}
return;
AssertionFailedError expected =
assertThrows(
AssertionFailedError.class,
() -> tester.testForwarding(interfaceType, wrapperFunction));
for (String message : expectedMessages) {
assertThat(expected).hasMessageThat().contains(message);
}
fail("expected failure not reported");
}

private static class ForwardingRunnable implements Runnable {
Expand Down
29 changes: 10 additions & 19 deletions android/guava-tests/test/com/google/common/hash/HashTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.Ints;
Expand Down Expand Up @@ -533,25 +534,15 @@ static void assertHasherByteBufferPreservesByteOrder(HashFunction hashFunction)
}

static void assertHashBytesThrowsCorrectExceptions(HashFunction hashFunction) {
{
HashCode unused = hashFunction.hashBytes(new byte[64], 0, 0);
}

try {
hashFunction.hashBytes(new byte[128], -1, 128);
Assert.fail();
} catch (IndexOutOfBoundsException expected) {
}
try {
hashFunction.hashBytes(new byte[128], 64, 256 /* too long len */);
Assert.fail();
} catch (IndexOutOfBoundsException expected) {
}
try {
hashFunction.hashBytes(new byte[64], 0, -1);
Assert.fail();
} catch (IndexOutOfBoundsException expected) {
}
HashCode unused = hashFunction.hashBytes(new byte[64], 0, 0);

assertThrows(
IndexOutOfBoundsException.class, () -> hashFunction.hashBytes(new byte[128], -1, 128));
assertThrows(
IndexOutOfBoundsException.class,
() -> hashFunction.hashBytes(new byte[128], 64, 256 /* too long len */));
assertThrows(
IndexOutOfBoundsException.class, () -> hashFunction.hashBytes(new byte[64], 0, -1));
}

static void assertIndependentHashers(HashFunction hashFunction) {
Expand Down
16 changes: 6 additions & 10 deletions android/guava-tests/test/com/google/common/math/LongMathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,10 @@ public void testLog10Exact() {
for (long x : POSITIVE_LONG_CANDIDATES) {
int floor = LongMath.log10(x, FLOOR);
boolean expectedSuccess = LongMath.pow(10, floor) == x;
try {
if (expectedSuccess) {
assertThat(LongMath.log10(x, UNNECESSARY)).isEqualTo(floor);
assertThat(expectedSuccess).isTrue();
} catch (ArithmeticException e) {
if (expectedSuccess) {
failFormat("expected log10(%s, UNNECESSARY) = %s; got ArithmeticException", x, floor);
}
} else {
assertThrows(ArithmeticException.class, () -> LongMath.log10(x, UNNECESSARY));
}
}
}
Expand Down Expand Up @@ -351,11 +348,10 @@ public void testSqrtExactMatchesFloorOrThrows() {
long sqrtFloor = sqrt(x, FLOOR);
// We only expect an exception if x was not a perfect square.
boolean isPerfectSquare = sqrtFloor * sqrtFloor == x;
try {
if (isPerfectSquare) {
assertThat(sqrt(x, UNNECESSARY)).isEqualTo(sqrtFloor);
assertThat(isPerfectSquare).isTrue();
} catch (ArithmeticException e) {
assertThat(isPerfectSquare).isFalse();
} else {
assertThrows(ArithmeticException.class, () -> sqrt(x, UNNECESSARY));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) {
byte[] sharedArray = new byte[arraysDim2];
Arrays.fill(arrays, sharedArray);

try {
Bytes.concat(arrays);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Bytes.concat(arrays));
}

public void testEnsureCapacity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,11 @@ public void testSaturatedCast() {
}

private void assertCastFails(long value) {
try {
Chars.checkedCast(value);
fail("Cast to char should have failed: " + value);
} catch (IllegalArgumentException ex) {
assertWithMessage("%s not found in exception text: %s", value, ex.getMessage())
.that(ex.getMessage().contains(String.valueOf(value)))
.isTrue();
}
IllegalArgumentException ex =
assertThrows(IllegalArgumentException.class, () -> Chars.checkedCast(value));
assertWithMessage("%s not found in exception text: %s", value, ex.getMessage())
.that(ex.getMessage().contains(String.valueOf(value)))
.isTrue();
}

// We need to test that our method behaves like the JDK method.
Expand Down Expand Up @@ -243,11 +240,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) {
char[] sharedArray = new char[arraysDim2];
Arrays.fill(arrays, sharedArray);

try {
Chars.concat(arrays);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Chars.concat(arrays));
}

@GwtIncompatible // Chars.fromByteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) {
double[] sharedArray = new double[arraysDim2];
Arrays.fill(arrays, sharedArray);

try {
Doubles.concat(arrays);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Doubles.concat(arrays));
}

public void testEnsureCapacity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,7 @@ private static void testConcatOverflow(int arraysDim1, int arraysDim2) {
float[] sharedArray = new float[arraysDim2];
Arrays.fill(arrays, sharedArray);

try {
Floats.concat(arrays);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Floats.concat(arrays));
}

public void testEnsureCapacity() {
Expand Down
Loading
Loading