Skip to content

Commit

Permalink
Use assertThrows even in GWT/J2CL/J2KT-compatible code, `common.col…
Browse files Browse the repository at this point in the history
…lect` edition.

(continuing the path started in cl/675634517)

And clean up a few other warnings.

RELNOTES=n/a
PiperOrigin-RevId: 686155720
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 15, 2024
1 parent 77243d6 commit 0a29129
Show file tree
Hide file tree
Showing 109 changed files with 2,378 additions and 5,033 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.collect;

import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
Expand All @@ -42,11 +44,7 @@ public void testFilteredKeysIllegalPut() {
filtered.put("b", 2);
assertEquals(ImmutableMap.of("a", 1, "b", 2), filtered);

try {
filtered.put("yyy", 3);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> filtered.put("yyy", 3));
}

public void testFilteredKeysIllegalPutAll() {
Expand All @@ -56,11 +54,9 @@ public void testFilteredKeysIllegalPutAll() {
filtered.put("b", 2);
assertEquals(ImmutableMap.of("a", 1, "b", 2), filtered);

try {
filtered.putAll(ImmutableMap.of("c", 3, "zzz", 4, "b", 5));
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class,
() -> filtered.putAll(ImmutableMap.of("c", 3, "zzz", 4, "b", 5)));

assertEquals(ImmutableMap.of("a", 1, "b", 2), filtered);
}
Expand Down Expand Up @@ -91,11 +87,7 @@ public void testFilteredValuesIllegalPut() {
unfiltered.put("c", 5);
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);

try {
filtered.put("yyy", 3);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> filtered.put("yyy", 3));
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}

Expand All @@ -107,11 +99,9 @@ public void testFilteredValuesIllegalPutAll() {
unfiltered.put("c", 5);
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);

try {
filtered.putAll(ImmutableMap.of("c", 4, "zzz", 5, "b", 6));
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class,
() -> filtered.putAll(ImmutableMap.of("c", 4, "zzz", 5, "b", 6)));
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}

Expand All @@ -123,11 +113,7 @@ public void testFilteredValuesIllegalSetValue() {
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);

Entry<String, Integer> entry = filtered.entrySet().iterator().next();
try {
entry.setValue(5);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> entry.setValue(5));

assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}
Expand Down Expand Up @@ -158,11 +144,7 @@ public void testFilteredEntriesIllegalPut() {
filtered.put("chicken", 7);
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);

try {
filtered.put("cow", 7);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> filtered.put("cow", 7));
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
}

Expand All @@ -177,11 +159,9 @@ public void testFilteredEntriesIllegalPutAll() {
filtered.put("chicken", 7);
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);

try {
filtered.putAll(ImmutableMap.of("sheep", 5, "cow", 7));
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class,
() -> filtered.putAll(ImmutableMap.of("sheep", 5, "cow", 7)));
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.common.collect;

import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;
import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
Expand Down Expand Up @@ -132,20 +133,12 @@ public void testCopyOf_arrayOfOneElement() {
}

public void testCopyOf_nullArray() {
try {
copyOf((String[]) null);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> copyOf((String[]) null));
}

public void testCopyOf_arrayContainingOnlyNull() {
@Nullable String[] array = new @Nullable String[] {null};
try {
copyOf((String[]) array);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> copyOf((String[]) array));
}

public void testCopyOf_collection_empty() {
Expand Down Expand Up @@ -178,11 +171,7 @@ public void testCopyOf_collection_general() {

public void testCopyOf_collectionContainingNull() {
Collection<@Nullable String> c = MinimalCollection.of("a", null, "b");
try {
copyOf((Collection<String>) c);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> copyOf((Collection<String>) c));
}

enum TestEnum {
Expand Down Expand Up @@ -228,11 +217,7 @@ public void testCopyOf_iterator_general() {

public void testCopyOf_iteratorContainingNull() {
Iterator<@Nullable String> c = Iterators.forArray("a", null, "b");
try {
copyOf((Iterator<String>) c);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> copyOf((Iterator<String>) c));
}

private static class CountingIterable implements Iterable<String> {
Expand Down Expand Up @@ -398,76 +383,59 @@ public void testComplexBuilder() {
abstract int getComplexBuilderSetLastElement();

public void testBuilderAddHandlesNullsCorrectly() {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
try {
builder.add((String) null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
assertThrows(NullPointerException.class, () -> builder.add((String) null));
}

builder = this.<String>builder();
try {
builder.add((String[]) null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
assertThrows(NullPointerException.class, () -> builder.add((String[]) null));
}

builder = this.<String>builder();
try {
builder.add("a", (String) null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
assertThrows(NullPointerException.class, () -> builder.add("a", (String) null));
}

builder = this.<String>builder();
try {
builder.add("a", "b", (String) null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
assertThrows(NullPointerException.class, () -> builder.add("a", "b", (String) null));
}

builder = this.<String>builder();
try {
builder.add("a", "b", "c", null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
assertThrows(NullPointerException.class, () -> builder.add("a", "b", "c", null));
}

builder = this.<String>builder();
try {
builder.add("a", "b", null, "c");
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
assertThrows(NullPointerException.class, () -> builder.add("a", "b", null, "c"));
}
}

public void testBuilderAddAllHandlesNullsCorrectly() {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
try {
builder.addAll((Iterable<String>) null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
assertThrows(NullPointerException.class, () -> builder.addAll((Iterable<String>) null));
}

try {
builder.addAll((Iterator<String>) null);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
{
ImmutableSet.Builder<String> builder = this.<String>builder();
assertThrows(NullPointerException.class, () -> builder.addAll((Iterator<String>) null));
}

builder = this.<String>builder();
{
ImmutableSet.Builder<String> builder = this.<String>builder();
List<@Nullable String> listWithNulls = asList("a", null, "b");
try {
builder.addAll((List<String>) listWithNulls);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
assertThrows(NullPointerException.class, () -> builder.addAll((List<String>) listWithNulls));
}

{
ImmutableSet.Builder<String> builder = this.<String>builder();
Iterable<@Nullable String> iterableWithNulls = MinimalIterable.of("a", null, "b");
try {
builder.addAll((Iterable<String>) iterableWithNulls);
fail("expected NullPointerException"); // COV_NF_LINE
} catch (NullPointerException expected) {
assertThrows(
NullPointerException.class, () -> builder.addAll((Iterable<String>) iterableWithNulls));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.collect;

import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import junit.framework.TestCase;

Expand All @@ -32,45 +34,27 @@ public abstract class AbstractImmutableTableTest extends TestCase {

public final void testClear() {
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
try {
testInstance.clear();
fail();
} catch (UnsupportedOperationException e) {
// success
}
assertThrows(UnsupportedOperationException.class, () -> testInstance.clear());
}
}

public final void testPut() {
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
try {
testInstance.put('a', 1, "blah");
fail();
} catch (UnsupportedOperationException e) {
// success
}
assertThrows(UnsupportedOperationException.class, () -> testInstance.put('a', 1, "blah"));
}
}

public final void testPutAll() {
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
try {
testInstance.putAll(ImmutableTable.of('a', 1, "blah"));
fail();
} catch (UnsupportedOperationException e) {
// success
}
assertThrows(
UnsupportedOperationException.class,
() -> testInstance.putAll(ImmutableTable.of('a', 1, "blah")));
}
}

public final void testRemove() {
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
try {
testInstance.remove('a', 1);
fail();
} catch (UnsupportedOperationException e) {
// success
}
assertThrows(UnsupportedOperationException.class, () -> testInstance.remove('a', 1));
}
}

Expand Down
Loading

0 comments on commit 0a29129

Please sign in to comment.