|
| 1 | +/* |
| 2 | + * Copyright 2021 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.BugCheckerRefactoringTestHelper; |
| 20 | +import com.google.errorprone.CompilationTestHelper; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.junit.runners.JUnit4; |
| 24 | + |
| 25 | +/** {@link DistinctVarargsChecker}Test */ |
| 26 | +@RunWith(JUnit4.class) |
| 27 | +public class DistinctVarargsCheckerTest { |
| 28 | + |
| 29 | + private final CompilationTestHelper compilationHelper = |
| 30 | + CompilationTestHelper.newInstance(DistinctVarargsChecker.class, getClass()); |
| 31 | + private final BugCheckerRefactoringTestHelper refactoringHelper = |
| 32 | + BugCheckerRefactoringTestHelper.newInstance(DistinctVarargsChecker.class, getClass()); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void distinctVarargsChecker_sameVariableInFuturesVaragsMethods_shouldFlag() { |
| 36 | + compilationHelper |
| 37 | + .addSourceLines( |
| 38 | + "Test.java", |
| 39 | + "import com.google.common.util.concurrent.Futures;", |
| 40 | + "import com.google.common.util.concurrent.ListenableFuture;", |
| 41 | + "public class Test {", |
| 42 | + " void testFunction() {", |
| 43 | + " ListenableFuture firstFuture = null, secondFuture = null;", |
| 44 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 45 | + " Futures.whenAllSucceed(firstFuture, firstFuture);", |
| 46 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 47 | + " Futures.whenAllSucceed(firstFuture, firstFuture, secondFuture);", |
| 48 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 49 | + " Futures.whenAllComplete(firstFuture, firstFuture);", |
| 50 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 51 | + " Futures.whenAllComplete(firstFuture, firstFuture, secondFuture);", |
| 52 | + " }", |
| 53 | + "}") |
| 54 | + .doTest(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void distinctVarargsCheckerdifferentVariableInFuturesVaragsMethods_shouldNotFlag() { |
| 59 | + compilationHelper |
| 60 | + .addSourceLines( |
| 61 | + "Test.java", |
| 62 | + "import com.google.common.util.concurrent.Futures;", |
| 63 | + "import com.google.common.util.concurrent.ListenableFuture;", |
| 64 | + "public class Test {", |
| 65 | + " void testFunction() {", |
| 66 | + " ListenableFuture firstFuture = null, secondFuture = null;", |
| 67 | + " Futures.whenAllComplete(firstFuture);", |
| 68 | + " Futures.whenAllSucceed(firstFuture, secondFuture);", |
| 69 | + " Futures.whenAllComplete(firstFuture);", |
| 70 | + " Futures.whenAllComplete(firstFuture, secondFuture);", |
| 71 | + " }", |
| 72 | + "}") |
| 73 | + .doTest(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void distinctVarargsChecker_sameVariableInGuavaVarargMethods_shouldFlag() { |
| 78 | + compilationHelper |
| 79 | + .addSourceLines( |
| 80 | + "Test.java", |
| 81 | + "import com.google.common.collect.Ordering;", |
| 82 | + "import com.google.common.collect.ImmutableBiMap;", |
| 83 | + "import com.google.common.collect.ImmutableMap;", |
| 84 | + "import com.google.common.collect.ImmutableSortedMap;", |
| 85 | + "import com.google.common.collect.ImmutableSet;", |
| 86 | + "import com.google.common.collect.ImmutableSortedSet;", |
| 87 | + "public class Test {", |
| 88 | + " void testFunction() {", |
| 89 | + " int first = 1, second = 2;", |
| 90 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 91 | + " Ordering.explicit(first, first);", |
| 92 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 93 | + " Ordering.explicit(first, first, second);", |
| 94 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 95 | + " ImmutableMap.of(first, second, first, second);", |
| 96 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 97 | + " ImmutableSortedMap.of(first, second, first, second);", |
| 98 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 99 | + " ImmutableBiMap.of(first, second, first, second);", |
| 100 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 101 | + " ImmutableBiMap.of(first, second, second, second);", |
| 102 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 103 | + " ImmutableSet.of(first, first);", |
| 104 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 105 | + " ImmutableSet.of(first, first, second);", |
| 106 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 107 | + " ImmutableSortedSet.of(first, first);", |
| 108 | + " // BUG: Diagnostic contains: DistinctVarargsChecker", |
| 109 | + " ImmutableSortedSet.of(first, first, second);", |
| 110 | + " }", |
| 111 | + "}") |
| 112 | + .doTest(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void distinctVarargsChecker_differentVariableInGuavaVarargMethods_shouldNotFlag() { |
| 117 | + compilationHelper |
| 118 | + .addSourceLines( |
| 119 | + "Test.java", |
| 120 | + "import com.google.common.collect.Ordering;", |
| 121 | + "import com.google.common.collect.ImmutableBiMap;", |
| 122 | + "import com.google.common.collect.ImmutableMap;", |
| 123 | + "import com.google.common.collect.ImmutableSortedMap;", |
| 124 | + "import com.google.common.collect.ImmutableSet;", |
| 125 | + "import com.google.common.collect.ImmutableSortedSet;", |
| 126 | + "public class Test {", |
| 127 | + " void testFunction() {", |
| 128 | + " int first = 1, second = 2, third = 3, fourth = 4;", |
| 129 | + " Ordering.explicit(first);", |
| 130 | + " Ordering.explicit(first, second);", |
| 131 | + " ImmutableMap.of(first, second);", |
| 132 | + " ImmutableSortedMap.of(first, second);", |
| 133 | + " ImmutableBiMap.of(first, second, third, fourth);", |
| 134 | + " ImmutableSet.of(first);", |
| 135 | + " ImmutableSet.of(first, second);", |
| 136 | + " ImmutableSortedSet.of(first);", |
| 137 | + " ImmutableSortedSet.of(first, second);", |
| 138 | + " }", |
| 139 | + "}") |
| 140 | + .doTest(); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void distinctVarargsChecker_sameVariableInImmutableSetVarargsMethod_shouldRefactor() { |
| 145 | + refactoringHelper |
| 146 | + .addInputLines( |
| 147 | + "Test.java", |
| 148 | + "import com.google.common.collect.ImmutableSet;", |
| 149 | + "import com.google.common.collect.ImmutableSortedSet;", |
| 150 | + "public class Test {", |
| 151 | + " void testFunction() {", |
| 152 | + " int first = 1, second = 2;", |
| 153 | + " ImmutableSet.of(first, first);", |
| 154 | + " ImmutableSet.of(first, first, second);", |
| 155 | + " ImmutableSortedSet.of(first, first);", |
| 156 | + " ImmutableSortedSet.of(first, first, second);", |
| 157 | + " }", |
| 158 | + "}") |
| 159 | + .addOutputLines( |
| 160 | + "Test.java", |
| 161 | + "import com.google.common.collect.ImmutableSet;", |
| 162 | + "import com.google.common.collect.ImmutableSortedSet;", |
| 163 | + "public class Test {", |
| 164 | + " void testFunction() {", |
| 165 | + " int first = 1, second = 2;", |
| 166 | + " ImmutableSet.of(first);", |
| 167 | + " ImmutableSet.of(first, second);", |
| 168 | + " ImmutableSortedSet.of(first);", |
| 169 | + " ImmutableSortedSet.of(first, second);", |
| 170 | + " }", |
| 171 | + "}") |
| 172 | + .doTest(); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void distinctVarargsChecker_differentVarsInImmutableSetVarargsMethod_shouldNotRefactor() { |
| 177 | + refactoringHelper |
| 178 | + .addInputLines( |
| 179 | + "Test.java", |
| 180 | + "import com.google.common.collect.ImmutableSet;", |
| 181 | + "import com.google.common.collect.ImmutableSortedSet;", |
| 182 | + "public class Test {", |
| 183 | + " void testFunction() {", |
| 184 | + " int first = 1, second = 2;", |
| 185 | + " ImmutableSet.of(first);", |
| 186 | + " ImmutableSet.of(first, second);", |
| 187 | + " ImmutableSortedSet.of(first);", |
| 188 | + " ImmutableSortedSet.of(first, second);", |
| 189 | + " }", |
| 190 | + "}") |
| 191 | + .addOutputLines( |
| 192 | + "Test.java", |
| 193 | + "import com.google.common.collect.ImmutableSet;", |
| 194 | + "import com.google.common.collect.ImmutableSortedSet;", |
| 195 | + "public class Test {", |
| 196 | + " void testFunction() {", |
| 197 | + " int first = 1, second = 2;", |
| 198 | + " ImmutableSet.of(first);", |
| 199 | + " ImmutableSet.of(first, second);", |
| 200 | + " ImmutableSortedSet.of(first);", |
| 201 | + " ImmutableSortedSet.of(first, second);", |
| 202 | + " }", |
| 203 | + "}") |
| 204 | + .doTest(); |
| 205 | + } |
| 206 | +} |
0 commit comments