Skip to content

Commit

Permalink
run pmd static analyzer agaisnt the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Sep 19, 2024
1 parent 0499355 commit f7dbf59
Show file tree
Hide file tree
Showing 71 changed files with 571 additions and 321 deletions.
4 changes: 2 additions & 2 deletions .github/scripts/analyze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eux

./gradlew \
forbiddenApis -DforbiddenApis \
pmdJavaPoet pmdMain pmdCodeGen pmdJmh -Dpmd \
forbiddenApis forbiddenApisTest -DforbiddenApis \
pmdJavaPoet pmdMain pmdCodeGen pmdJmh pmdTest -Dpmd \
spotbugsJavaPoet spotbugsMain spotbugsCodeGen spotbugsJmh -Dspotbugs \
"$@"
10 changes: 5 additions & 5 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
forbiddenApis:
runs-on: ubuntu-latest
env:
JAVA_VERSION: 21
JAVA_VERSION: 22
steps:
- name: Harden Runner
uses: step-security/harden-runner@5c7944e73c4c2a096b17a9cb74d65b6c2bbafbde # v2.9.1
Expand All @@ -36,12 +36,12 @@ jobs:
uses: ./.github/actions/run-gradle
with:
java: ${{ env.JAVA_VERSION }}
arguments: forbiddenApis -DforbiddenApis
arguments: forbiddenApis forbiddenApisTest -DforbiddenApis

pmd:
runs-on: ubuntu-latest
env:
JAVA_VERSION: 22
JAVA_VERSION: 23
steps:
- name: Harden Runner
uses: step-security/harden-runner@5c7944e73c4c2a096b17a9cb74d65b6c2bbafbde # v2.9.1
Expand All @@ -54,12 +54,12 @@ jobs:
uses: ./.github/actions/run-gradle
with:
java: ${{ env.JAVA_VERSION }}
arguments: pmdJavaPoet pmdMain pmdCodeGen pmdJmh -Dpmd
arguments: pmdJavaPoet pmdMain pmdCodeGen pmdJmh pmdTest -Dpmd

spotbugs:
runs-on: ubuntu-latest
env:
JAVA_VERSION: 22
JAVA_VERSION: 23
steps:
- name: Harden Runner
uses: step-security/harden-runner@5c7944e73c4c2a096b17a9cb74d65b6c2bbafbde # v2.9.1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 11, 22, 24, GraalVM ]
java: [ 11, 23, 24, GraalVM ]
env:
JAVA_VERSION: ${{ matrix.java }}
steps:
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
- simulator:check
- jcache:check
- guava:check
java: [ 11, 22 ]
java: [ 11, 23 ]
include:
- suite: caffeine:weakKeysAndStrongValuesStatsSyncGuavaSlowTest
java: 11
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependency-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions: read-all

env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
JAVA_VERSION: 22
JAVA_VERSION: 23

jobs:
dependency-check:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependency-submission-pr-retreive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
JAVA_VERSION: 22
JAVA_VERSION: 23

jobs:
submit-dependency-graph:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/qodana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
java: ${{ env.JAVA_VERSION }}
arguments: build -x test
- name: Qodana - Code Inspection
uses: JetBrains/qodana-action@c5a69b02e6c1adb092153f7a479169a4b9f3a1cf # v2024.1.9
uses: JetBrains/qodana-action@5983adcc5fdb505e156e5f756a80f2f979fb1ac0 # v2024.2.2
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
with:
Expand Down
6 changes: 5 additions & 1 deletion caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ for (scenario in Scenario.all()) {
if (slow == Slow.Enabled) {
maxParallelForks = 2
includeGroups.add("slow")
jvmArgs("-XX:+UseParallelGC")
if (java.toolchain.languageVersion.get().canCompileOrRun(23)) {
jvmArgs("-XX:+UseShenandoahGC")
} else {
jvmArgs("-XX:+UseParallelGC")
}
} else {
parallel = "methods"
excludeGroups("slow", "isolated", "lincheck")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ protected long getCurrentBufferCapacity(long mask) {
}
}

@SuppressWarnings({"MultiVariableDeclaration", "OvershadowingSubclassFields"})
@SuppressWarnings({"MultiVariableDeclaration",
"OvershadowingSubclassFields", "PMD.OneDeclarationPerLine"})
abstract class MpscChunkedArrayQueue<E> extends MpscChunkedArrayQueueColdProducerFields<E> {
byte p000, p001, p002, p003, p004, p005, p006, p007;
byte p008, p009, p010, p011, p012, p013, p014, p015;
Expand Down Expand Up @@ -112,7 +113,7 @@ abstract class MpscChunkedArrayQueueColdProducerFields<E> extends BaseMpscLinked
}
}

@SuppressWarnings("MultiVariableDeclaration")
@SuppressWarnings({"MultiVariableDeclaration", "PMD.OneDeclarationPerLine"})
abstract class BaseMpscLinkedArrayQueuePad1<E> extends AbstractQueue<E> {
byte p000, p001, p002, p003, p004, p005, p006, p007;
byte p008, p009, p010, p011, p012, p013, p014, p015;
Expand All @@ -135,7 +136,8 @@ abstract class BaseMpscLinkedArrayQueueProducerFields<E> extends BaseMpscLinkedA
protected long producerIndex;
}

@SuppressWarnings({"MultiVariableDeclaration", "OvershadowingSubclassFields"})
@SuppressWarnings({"MultiVariableDeclaration",
"OvershadowingSubclassFields", "PMD.OneDeclarationPerLine"})
abstract class BaseMpscLinkedArrayQueuePad2<E> extends BaseMpscLinkedArrayQueueProducerFields<E> {
byte p000, p001, p002, p003, p004, p005, p006, p007;
byte p008, p009, p010, p011, p012, p013, p014, p015;
Expand All @@ -161,7 +163,8 @@ abstract class BaseMpscLinkedArrayQueueConsumerFields<E> extends BaseMpscLinkedA
protected long consumerIndex;
}

@SuppressWarnings({"MultiVariableDeclaration", "OvershadowingSubclassFields"})
@SuppressWarnings({"MultiVariableDeclaration",
"OvershadowingSubclassFields", "PMD.OneDeclarationPerLine"})
abstract class BaseMpscLinkedArrayQueuePad3<E> extends BaseMpscLinkedArrayQueueConsumerFields<E> {
byte p000, p001, p002, p003, p004, p005, p006, p007;
byte p008, p009, p010, p011, p012, p013, p014, p015;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

import org.eclipse.collections.impl.factory.Sets;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -1009,10 +1008,9 @@ public void computeIfAbsent_recursive(Map<Int, Int> map, CacheContext context) {
return map.computeIfAbsent(key, this);
}
};
try {
map.computeIfAbsent(context.absentKey(), mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class,
() -> map.computeIfAbsent(context.absentKey(), mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@CacheSpec
Expand All @@ -1023,10 +1021,9 @@ public void computeIfAbsent_pingpong(Map<Int, Int> map, CacheContext context) {
return map.computeIfAbsent(key.negate(), this);
}
};
try {
map.computeIfAbsent(context.absentKey(), mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class,
() -> map.computeIfAbsent(context.absentKey(), mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@Test(dataProvider = "caches")
Expand Down Expand Up @@ -1308,10 +1305,9 @@ public void compute_recursive(Map<Int, Int> map, CacheContext context) {
return map.compute(key, this);
}
};
try {
map.compute(context.absentKey(), mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class,
() -> map.compute(context.absentKey(), mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@Test(dataProvider = "caches")
Expand All @@ -1324,20 +1320,15 @@ public void compute_pingpong(Map<Int, Int> map, CacheContext context) {
return map.compute(key.equals(key1) ? key2 : key1, this);
}
};
try {
map.compute(key1, mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class, () -> map.compute(key1, mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@CacheSpec
@Test(dataProvider = "caches")
public void compute_error(Map<Int, Int> map, CacheContext context) {
try {
map.compute(context.absentKey(), (key, value) -> { throw new IllegalStateException(); });
Assert.fail();
} catch (IllegalStateException e) { /* ignored */ }

assertThrows(IllegalStateException.class, () ->
map.compute(context.absentKey(), (key, value) -> { throw new IllegalStateException(); }));
assertThat(map).isEqualTo(context.original());
assertThat(context).stats().hits(0).misses(0).success(0).failures(1);
assertThat(map.compute(context.absentKey(), (k, v) -> intern(k.negate())))
Expand Down Expand Up @@ -3095,6 +3086,7 @@ public void writeThroughEntry_equals_hashCode_toString() {
assertThat(entry.toString()).isNotEqualTo(other.toString());
}

@SuppressWarnings("PMD.DoNotExtendJavaLangError")
static final class ExpectedError extends Error {
private static final long serialVersionUID = 1L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

import org.eclipse.collections.impl.factory.Sets;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -829,10 +828,9 @@ public void computeIfAbsent_recursive(AsyncCache<Int, Int> cache, CacheContext c
return cache.asMap().computeIfAbsent(key, this);
}
};
try {
cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class,
() -> cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@CacheSpec
Expand All @@ -843,10 +841,9 @@ public void computeIfAbsent_pingpong(AsyncCache<Int, Int> cache, CacheContext co
return cache.asMap().computeIfAbsent(key.negate(), this);
}
};
try {
cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class,
() -> cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@Test(dataProvider = "caches")
Expand Down Expand Up @@ -1085,10 +1082,9 @@ public void compute_recursive(AsyncCache<Int, Int> cache, CacheContext context)
return cache.asMap().compute(key, this);
}
};
try {
cache.asMap().compute(context.absentKey(), mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class,
() -> cache.asMap().compute(context.absentKey(), mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@Test(dataProvider = "caches")
Expand All @@ -1101,10 +1097,8 @@ public void compute_pingpong(AsyncCache<Int, Int> cache, CacheContext context) {
return cache.asMap().compute(key.equals(key1) ? key2 : key1, this);
}
};
try {
cache.asMap().compute(key1, mappingFunction);
Assert.fail();
} catch (StackOverflowError | IllegalStateException e) { /* ignored */ }
var error = assertThrows(Throwable.class, () -> cache.asMap().compute(key1, mappingFunction));
assertThat(error.getClass()).isAnyOf(StackOverflowError.class, IllegalStateException.class);
}

@Test(dataProvider = "caches")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class AsyncTest {
private static final long ONE_MINUTE = TimeUnit.MINUTES.toNanos(1);

@Test
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
public void reflectivelyConstruct() throws ReflectiveOperationException {
var constructor = Async.class.getDeclaredConstructor();
constructor.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void overflow() {
}

@Test
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
public void reflectivelyConstruct() throws ReflectiveOperationException {
var constructor = BBHeader.class.getDeclaredConstructor();
constructor.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public void clear_pendingWrites_weakKeys(
/* --------------- Maintenance --------------- */

@Test
@SuppressWarnings("PMD.UnusedAssignment")
public void cleanupTask_allowGc() {
var cache = new BoundedLocalCache<Object, Object>(
Caffeine.newBuilder(), /* cacheLoader= */ null, /* isAsync= */ false) {};
Expand Down Expand Up @@ -2206,7 +2207,7 @@ public void refreshIfNeeded_liveliness(CacheContext context) {

// Capture the refresh parameters, should not be retired/dead sentinel entry
var refreshEntry = new AtomicReference<Map.Entry<Object, Object>>();
var cache = asBoundedLocalCache(context.build(new CacheLoader<Object, Object>() {
var cache = asBoundedLocalCache(context.build(new CacheLoader<>() {
@Override public Int load(Object key) {
throw new AssertionError();
}
Expand Down Expand Up @@ -2526,6 +2527,7 @@ private static void checkBrokenEqualityMessage(
/* --------------- Miscellaneous --------------- */

@Test
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
public void reflectivelyConstruct() throws ReflectiveOperationException {
var constructor = BLCHeader.class.getDeclaredConstructor();
constructor.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public void getAllPresent_ordered(Cache<Int, Int> cache, CacheContext context) {
@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY)
public void getAllPresent_jdk8186171(Cache<Object, Int> cache, CacheContext context) {
class Key {
@SuppressWarnings("PMD.OverrideBothEqualsAndHashcode")
final class Key {
@Override public int hashCode() {
return 0; // to put keys in one bucket
}
Expand Down Expand Up @@ -582,7 +583,8 @@ public void getAll_present_ordered_exceeds(Cache<Int, Int> cache, CacheContext c
@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY)
public void getAll_jdk8186171(CacheContext context) {
class Key {
@SuppressWarnings("PMD.OverrideBothEqualsAndHashcode")
final class Key {
@Override public int hashCode() {
return 0; // to put keys in one bucket
}
Expand Down Expand Up @@ -942,6 +944,7 @@ public void serialize(Cache<Int, Int> cache, CacheContext context) {

@CheckNoStats
@Test(dataProvider = "caches")
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY)
public void readObject(CacheContext context) throws NoSuchMethodException {
var cache = context.isAsync() ? context.asyncCache() : context.cache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
*
* @author Adam Winer
*/
@SuppressWarnings("PreferJavaTimeOverload")
@SuppressWarnings({"PMD.DetachedTestCase",
"PMD.JUnit4TestShouldUseTestAnnotation", "PreferJavaTimeOverload"})
public class CaffeineSpecGuavaTest extends TestCase {

public void testParse_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ public void loading_nullLoader() {
@Test
public void async_weakValues() {
var builder = Caffeine.newBuilder().weakValues();
assertThrows(IllegalStateException.class, () -> builder.buildAsync());
assertThrows(IllegalStateException.class, builder::buildAsync);
}

@Test
public void async_softValues() {
var builder = Caffeine.newBuilder().softValues();
assertThrows(IllegalStateException.class, () -> builder.buildAsync());
assertThrows(IllegalStateException.class, builder::buildAsync);
}

@Test
Expand Down
Loading

0 comments on commit f7dbf59

Please sign in to comment.