|
| 1 | +/* |
| 2 | + * Copyright 2015 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.bugpatterns; |
| 18 | + |
| 19 | +import com.google.errorprone.CompilationTestHelper; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.junit.runners.JUnit4; |
| 23 | + |
| 24 | +/** Tests for {@link AsyncCacheLoaderReturnsNull}. */ |
| 25 | +@RunWith(JUnit4.class) |
| 26 | +public class AsyncCacheLoaderReturnsNullTest { |
| 27 | + private final CompilationTestHelper compilationHelper = |
| 28 | + CompilationTestHelper.newInstance(AsyncCacheLoaderReturnsNull.class, getClass()); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void positiveCase() { |
| 32 | + compilationHelper |
| 33 | + .addSourceLines( |
| 34 | + "AsyncCacheLoaderReturnsNullPositiveCases.java", |
| 35 | + """ |
| 36 | + package com.google.errorprone.bugpatterns.testdata; |
| 37 | +
|
| 38 | + import static com.google.common.util.concurrent.Futures.immediateFuture; |
| 39 | +
|
| 40 | + import com.google.common.cache.AsyncCacheLoader; |
| 41 | + import com.google.common.util.concurrent.ListenableFuture; |
| 42 | +
|
| 43 | + /** Positive cases for {@link AsyncCacheLoaderReturnsNull}. */ |
| 44 | + public class AsyncCacheLoaderReturnsNullPositiveCases { |
| 45 | + static void listenableFutures() { |
| 46 | + new AsyncCacheLoader<String, Object>() { |
| 47 | + @Override |
| 48 | + public ListenableFuture<Object> load(String key) { |
| 49 | + // BUG: Diagnostic contains: |
| 50 | + return null; |
| 51 | + } |
| 52 | + }; |
| 53 | +
|
| 54 | + new AsyncCacheLoader<Object, String>() { |
| 55 | + @Override |
| 56 | + public ListenableFuture<String> load(Object o) { |
| 57 | + if (o instanceof String) { |
| 58 | + return immediateFuture((String) o); |
| 59 | + } |
| 60 | + // BUG: Diagnostic contains: |
| 61 | + return null; |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | + } |
| 66 | + """) |
| 67 | + .doTest(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void negativeCase() { |
| 72 | + compilationHelper |
| 73 | + .addSourceLines( |
| 74 | + "AsyncCacheLoaderReturnsNullNegativeCases.java", |
| 75 | + """ |
| 76 | + package com.google.errorprone.bugpatterns.testdata; |
| 77 | +
|
| 78 | + import static com.google.common.util.concurrent.Futures.immediateFuture; |
| 79 | +
|
| 80 | + import com.google.common.base.Function; |
| 81 | + import com.google.common.cache.AsyncCacheLoader; |
| 82 | + import com.google.common.util.concurrent.ListenableFuture; |
| 83 | + import java.util.function.Supplier; |
| 84 | + import org.jspecify.annotations.Nullable; |
| 85 | +
|
| 86 | + /** Negative cases for {@link AsyncCacheLoaderReturnsNull}. */ |
| 87 | + public class AsyncCacheLoaderReturnsNullNegativeCases { |
| 88 | + static { |
| 89 | + new AsyncCacheLoader<String, Object>() { |
| 90 | + @Override |
| 91 | + public ListenableFuture<Object> load(String key) { |
| 92 | + return immediateFuture(null); |
| 93 | + } |
| 94 | + }; |
| 95 | +
|
| 96 | + new Function<String, Object>() { |
| 97 | + @Override |
| 98 | + public Object apply(String input) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + }; |
| 102 | +
|
| 103 | + new AsyncCacheLoader<String, Object>() { |
| 104 | + @Override |
| 105 | + public ListenableFuture<Object> load(String key) { |
| 106 | + return load(key, key); |
| 107 | + } |
| 108 | +
|
| 109 | + public ListenableFuture<Object> load(String input1, String input2) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + }; |
| 113 | +
|
| 114 | + new MyNonAsyncCacheLoader<String, Object>() { |
| 115 | + @Override |
| 116 | + public ListenableFuture<Object> load(String key) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + }; |
| 120 | +
|
| 121 | + new AsyncCacheLoader<String, Object>() { |
| 122 | + @Override |
| 123 | + public ListenableFuture<Object> load(String key) { |
| 124 | + Supplier<String> s = |
| 125 | + () -> { |
| 126 | + return null; |
| 127 | + }; |
| 128 | + return immediateFuture(s.get()); |
| 129 | + } |
| 130 | + }; |
| 131 | + } |
| 132 | +
|
| 133 | + interface MyNonAsyncCacheLoader<K, V> { |
| 134 | + ListenableFuture<V> load(@Nullable K key); |
| 135 | + } |
| 136 | + } |
| 137 | + """) |
| 138 | + .doTest(); |
| 139 | + } |
| 140 | +} |
0 commit comments