Skip to content

Commit

Permalink
enable nullaway on unit tests (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Dec 16, 2024
1 parent e512b50 commit cffad6b
Show file tree
Hide file tree
Showing 107 changed files with 884 additions and 402 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jitwatch.out
.settings
.project
.gradle
.kotlin
.vscode
.idea
*.iml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3163,6 +3163,7 @@ <T> T expireAfterAccessOrder(boolean oldest, Function<@Nullable V, @Nullable V>
* @param mappingFunction the mapping function to compute a value
* @return the computed value
*/
@SuppressWarnings("NullAway")
<T> T snapshot(Iterable<Node<K, V>> iterable, Function<@Nullable V, @Nullable V> transformer,
Function<Stream<CacheEntry<K, V>>, T> mappingFunction) {
requireNonNull(mappingFunction);
Expand Down Expand Up @@ -3901,10 +3902,10 @@ public void run() {
*/
// public final void quietlyComplete() {}

@Override public void setRawResult(@Nullable Void v) {}
@Override public void complete(@Nullable Void value) {}
@Override public void completeExceptionally(Throwable ex) {}
@Override public void setRawResult(@Nullable Void value) {}
@Override public @Nullable Void getRawResult() { return null; }
@Override public void completeExceptionally(@Nullable Throwable t) {}
@Override public boolean cancel(boolean mayInterruptIfRunning) { return false; }
}

Expand Down Expand Up @@ -4009,6 +4010,7 @@ static final class BoundedPolicy<K, V> implements Policy<K, V> {
@Override public @Nullable V getIfPresentQuietly(K key) {
return transformer.apply(cache.getIfPresentQuietly(key));
}
@SuppressWarnings("NullAway")
@Override public @Nullable CacheEntry<K, V> getEntryIfPresentQuietly(K key) {
Node<K, V> node = cache.data.get(cache.nodeFactory.newLookupKey(key));
return (node == null) ? null : cache.nodeToCacheEntry(node, transformer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ interface LinkedDeque<E> extends Deque<E> {
*
* @param e the linked element
*/
boolean isFirst(E e);
boolean isFirst(@Nullable E e);

/**
* Returns if the element is at the back of the deque.
*
* @param e the linked element
*/
boolean isLast(E e);
boolean isLast(@Nullable E e);

/**
* Moves the element to the front of the deque so that it becomes the first element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -1841,7 +1842,9 @@ public void keySet_removeAll_partial(Map<Int, Int> map, CacheContext context) {
assertThat(map.keySet().removeAll(context.firstMiddleLastKeys())).isTrue();
assertThat(map).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -2017,7 +2020,9 @@ public void keySet_retainAll_partial(Map<Int, Int> map, CacheContext context) {
assertThat(map.keySet().retainAll(expected.keySet())).isTrue();
assertThat(map).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -2149,6 +2154,7 @@ public void keySpliterator_estimateSize(Map<Int, Int> map, CacheContext context)
/* --------------- Values --------------- */

@CheckNoStats
@SuppressWarnings("NullAway")
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void values_toArray_null(Map<Int, Int> map, CacheContext context) {
Expand Down Expand Up @@ -2249,7 +2255,8 @@ public void values_removeAll_none_populated(Map<Int, Int> map, CacheContext cont
public void values_removeAll_partial(Map<Int, Int> map, CacheContext context) {
var expected = new HashMap<>(context.original());
expected.keySet().removeAll(context.firstMiddleLastKeys());
var removed = Maps.asMap(context.firstMiddleLastKeys(), context.original()::get);
var removed = Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key)));

assertThat(map.values().removeAll(removed.values())).isTrue();
assertThat(map).isEqualTo(expected);
Expand Down Expand Up @@ -2418,7 +2425,8 @@ public void values_retainAll_none_populated(Map<Int, Int> map, CacheContext cont
public void values_retainAll_partial(Map<Int, Int> map, CacheContext context) {
var expected = new HashMap<>(context.original());
expected.keySet().removeAll(context.firstMiddleLastKeys());
var removed = Maps.asMap(context.firstMiddleLastKeys(), context.original()::get);
var removed = Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key)));

assertThat(map.values().retainAll(expected.values())).isTrue();
assertThat(map).isEqualTo(expected);
Expand Down Expand Up @@ -2678,7 +2686,8 @@ public void entrySet_removeAll_none_populated(Map<Int, Int> map, CacheContext co
@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL)
public void entrySet_removeAll_partial(Map<Int, Int> map, CacheContext context) {
var removed = Maps.asMap(context.firstMiddleLastKeys(), context.original()::get);
var removed = Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key)));
var expected = new HashMap<>(context.original());
expected.entrySet().removeAll(removed.entrySet());

Expand Down Expand Up @@ -2888,7 +2897,8 @@ public void entrySet_retainAll_none_populated(Map<Int, Int> map, CacheContext co
@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL)
public void entrySet_retainAll_partial(Map<Int, Int> map, CacheContext context) {
var removed = Maps.asMap(context.firstMiddleLastKeys(), context.original()::get);
var removed = Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key)));
var expected = new HashMap<>(context.original());
expected.entrySet().removeAll(removed.entrySet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -292,8 +293,8 @@ public void put_insert(AsyncCache<Int, Int> cache, CacheContext context) {
public void put_replace_sameValue(AsyncCache<Int, Int> cache, CacheContext context) {
var replaced = new HashMap<Int, Int>();
for (Int key : context.firstMiddleLastKeys()) {
var oldValue = cache.asMap().get(key);
var value = cache.asMap().get(key).thenApply(val -> intern(new Int(val)));
var oldValue = requireNonNull(cache.asMap().get(key));
var value = oldValue.thenApply(val -> intern(new Int(val)));
assertThat(cache.asMap().put(key, value)).isSameInstanceAs(oldValue);
assertThat(cache).containsEntry(key, value);
replaced.put(key, context.original().get(key));
Expand All @@ -307,7 +308,7 @@ public void put_replace_sameValue(AsyncCache<Int, Int> cache, CacheContext conte
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL })
public void put_replace_sameInstance(AsyncCache<Int, Int> cache, CacheContext context) {
for (Int key : context.firstMiddleLastKeys()) {
var value = cache.asMap().get(key);
var value = requireNonNull(cache.asMap().get(key));
assertThat(cache.asMap().put(key, value)).isSameInstanceAs(value);
assertThat(cache).containsEntry(key, value);
}
Expand Down Expand Up @@ -435,7 +436,7 @@ public void putIfAbsent_nullKeyAndValue(AsyncCache<Int, Int> cache, CacheContext
removalListener = { Listener.DISABLED, Listener.REJECTING })
public void putIfAbsent_present(AsyncCache<Int, Int> cache, CacheContext context) {
for (Int key : context.firstMiddleLastKeys()) {
var value = cache.asMap().get(key);
var value = requireNonNull(cache.asMap().get(key));
assertThat(cache.asMap().putIfAbsent(key, key.asFuture())).isEqualTo(value);
assertThat(cache).containsEntry(key, value);
}
Expand Down Expand Up @@ -589,8 +590,8 @@ public void replace_failure(AsyncCache<Int, Int> cache, CacheContext context) {
public void replace_sameValue(AsyncCache<Int, Int> cache, CacheContext context) {
var replaced = new HashMap<Int, Int>();
for (Int key : context.firstMiddleLastKeys()) {
var oldValue = cache.asMap().get(key);
var newValue = cache.asMap().get(key).thenApply(val -> intern(new Int(val)));
var oldValue = requireNonNull(cache.asMap().get(key));
var newValue = oldValue.thenApply(val -> intern(new Int(val)));
assertThat(cache.asMap().replace(key, newValue)).isSameInstanceAs(oldValue);
assertThat(cache).containsEntry(key, newValue);
replaced.put(key, context.original().get(key));
Expand Down Expand Up @@ -718,8 +719,8 @@ public void replaceConditionally_wrongOldValue(AsyncCache<Int, Int> cache, Cache
public void replaceConditionally_sameValue(AsyncCache<Int, Int> cache, CacheContext context) {
var replaced = new HashMap<Int, Int>();
for (Int key : context.firstMiddleLastKeys()) {
var oldValue = cache.asMap().get(key);
var newValue = cache.asMap().get(key).thenApply(val -> intern(new Int(val)));
var oldValue = requireNonNull(cache.asMap().get(key));
var newValue = oldValue.thenApply(val -> intern(new Int(val)));
assertThat(cache.asMap().replace(key, oldValue, newValue)).isTrue();
assertThat(cache).containsEntry(key, newValue);
replaced.put(key, context.original().get(key));
Expand Down Expand Up @@ -1310,7 +1311,7 @@ public void merge_absent(AsyncCache<Int, Int> cache, CacheContext context) {
public void merge_sameValue(AsyncCache<Int, Int> cache, CacheContext context) {
var replaced = new HashMap<Int, CompletableFuture<Int>>();
for (Int key : context.firstMiddleLastKeys()) {
var value = cache.asMap().get(key).thenApply(Int::new);
var value = requireNonNull(cache.asMap().get(key)).thenApply(Int::new);
var result = cache.asMap().merge(key, key.negate().asFuture(), (oldValue, v) -> value);
assertThat(result).isSameInstanceAs(value);
replaced.put(key, value);
Expand Down Expand Up @@ -1391,12 +1392,12 @@ public void equals(AsyncCache<Int, Int> cache, CacheContext context) {
assertThat(cache.asMap().equals(map)).isTrue();
assertThat(map.equals(cache.asMap())).isTrue();

var absent = Maps.asMap(context.absentKeys(), CompletableFuture::completedFuture);
var absent = Maps.toMap(context.absentKeys(), CompletableFuture::completedFuture);
assertThat(cache.asMap().equals(absent)).isFalse();
assertThat(absent.equals(cache.asMap())).isFalse();

if (!cache.asMap().isEmpty()) {
var other = Maps.asMap(cache.asMap().keySet(), CompletableFuture::completedFuture);
var other = Maps.toMap(cache.asMap().keySet(), CompletableFuture::completedFuture);
assertThat(cache.asMap().equals(other)).isFalse();
assertThat(other.equals(cache.asMap())).isFalse();
}
Expand Down Expand Up @@ -1566,7 +1567,9 @@ public void keySet_removeAll_partial(AsyncCache<Int, Int> cache, CacheContext co
assertThat(cache.asMap().keySet().removeAll(context.firstMiddleLastKeys())).isTrue();
assertThat(cache.synchronous().asMap()).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -1739,7 +1742,9 @@ public void keySet_retainAll_partial(AsyncCache<Int, Int> cache, CacheContext co
assertThat(cache.asMap().keySet().retainAll(expected.keySet())).isTrue();
assertThat(cache.asMap()).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -1872,6 +1877,7 @@ public void keySpliterator_estimateSize(AsyncCache<Int, Int> cache, CacheContext
/* ---------------- Values -------------- */

@CheckNoStats
@SuppressWarnings("NullAway")
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DISABLED, Listener.REJECTING })
public void values_toArray_null(AsyncCache<Int, Int> cache, CacheContext context) {
Expand Down Expand Up @@ -1978,12 +1984,15 @@ public void values_removeAll_none_populated(AsyncCache<Int, Int> cache, CacheCon
public void values_removeAll_partial(AsyncCache<Int, Int> cache, CacheContext context) {
var expected = new HashMap<>(context.original());
expected.keySet().removeAll(context.firstMiddleLastKeys());
var removed = Maps.asMap(context.firstMiddleLastKeys(), cache.asMap()::get);
var removed = Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(cache.asMap().get(key)));

assertThat(cache.asMap().values().removeAll(removed.values())).isTrue();
assertThat(cache.synchronous().asMap()).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -2026,7 +2035,7 @@ public void values_remove_none(AsyncCache<Int, Int> cache, CacheContext context)
@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL)
public void values_remove(AsyncCache<Int, Int> cache, CacheContext context) {
var future = cache.asMap().get(context.firstKey());
var future = requireNonNull(cache.asMap().get(context.firstKey()));
assertThat(cache.asMap().values().remove(future)).isTrue();
var expected = new HashMap<>(context.original());
expected.remove(context.firstKey());
Expand Down Expand Up @@ -2167,7 +2176,9 @@ public void values_retainAll_partial(AsyncCache<Int, Int> cache, CacheContext co
assertThat(cache.asMap().values().retainAll(expected.values())).isTrue();
assertThat(cache.asMap()).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -2438,14 +2449,17 @@ public void entrySet_removeAll_none_populated(AsyncCache<Int, Int> cache, CacheC
@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL)
public void entrySet_removeAll_partial(AsyncCache<Int, Int> cache, CacheContext context) {
var removed = Maps.asMap(context.firstMiddleLastKeys(), cache.asMap()::get);
var removed = Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(cache.asMap().get(key)));
var expected = new HashMap<>(context.original());
expected.keySet().removeAll(removed.keySet());

assertThat(cache.asMap().entrySet().removeAll(removed.entrySet())).isTrue();
assertThat(cache.synchronous().asMap()).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down Expand Up @@ -2656,7 +2670,9 @@ public void entrySet_retainAll_partial(AsyncCache<Int, Int> cache, CacheContext
assertThat(cache.asMap().entrySet().retainAll(expected.entrySet())).isTrue();
assertThat(cache.asMap()).isEqualTo(expected);
assertThat(context).removalNotifications().withCause(EXPLICIT)
.contains(Maps.asMap(context.firstMiddleLastKeys(), context.original()::get)).exclusively();
.contains(Maps.toMap(context.firstMiddleLastKeys(),
key -> requireNonNull(context.original().get(key))))
.exclusively();
}

@CheckNoStats
Expand Down
Loading

0 comments on commit cffad6b

Please sign in to comment.