Skip to content

Commit

Permalink
Add module descriptor for guava adapters
Browse files Browse the repository at this point in the history
This change required removing the split test packages. The import of
Guava's test suite is now under a compatibility package. The JUnit
tests applying to Caffeine (jsr166's suite, testlib suite) are now
moved into the caffeine module's test package. The Eclipse workaround
is now applied to all projects.
  • Loading branch information
ben-manes committed May 2, 2021
1 parent 99bf56b commit 7b28541
Show file tree
Hide file tree
Showing 44 changed files with 278 additions and 134 deletions.
7 changes: 1 addition & 6 deletions caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ idea.module {
}

eclipse.classpath.file.whenMerged {
entries.findAll {
it instanceof SourceFolder && it.output == 'bin/codeGen'
}*.output = 'bin/main'
// Exclude module-info when compiling through Eclipse
def main = entries.find { it instanceof SourceFolder && it.path == 'src/main/java' }
main.excludes.add('module-info.java')
entries.findAll { it instanceof SourceFolder && it.output == 'bin/codeGen' }*.output = 'bin/main'
}

plugins.withType(EclipsePlugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.github.benmanes.caffeine.cache.stats;

import static java.util.Objects.requireNonNull;

import java.util.concurrent.atomic.LongAdder;

import com.github.benmanes.caffeine.cache.Cache;
Expand Down Expand Up @@ -71,6 +73,7 @@ public void recordLoadFailure(long loadTime) {

@Override
public void recordEviction(int weight, RemovalCause cause) {
requireNonNull(cause);
evictionCount.increment();
evictionWeight.add(weight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.github.benmanes.caffeine.cache.stats;

import static java.util.Objects.requireNonNull;

import org.checkerframework.checker.index.qual.NonNegative;

import com.github.benmanes.caffeine.cache.RemovalCause;
Expand All @@ -40,7 +42,9 @@ public void recordLoadSuccess(long loadTime) {}
public void recordLoadFailure(long loadTime) {}

@Override
public void recordEviction(@NonNegative int weight, RemovalCause cause) {}
public void recordEviction(@NonNegative int weight, RemovalCause cause) {
requireNonNull(cause);
}

@Override
public CacheStats snapshot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void recordLoadFailure(long loadTime) {

@Override
public void recordEviction(int weight, RemovalCause cause) {
requireNonNull(cause);
try {
delegate.recordEviction(weight, cause);
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.github.benmanes.caffeine.cache;

import static com.github.benmanes.caffeine.cache.MapTestFactory.asynchronousGenerator;
import static com.github.benmanes.caffeine.cache.MapTestFactory.synchronousGenerator;
import static com.github.benmanes.caffeine.cache.testing.MapTestFactory.asynchronousGenerator;
import static com.github.benmanes.caffeine.cache.testing.MapTestFactory.synchronousGenerator;

import com.github.benmanes.caffeine.guava.CaffeinatedGuava;
import com.github.benmanes.caffeine.cache.testing.MapTestFactory;

import junit.framework.Test;
import junit.framework.TestCase;
Expand All @@ -31,15 +31,14 @@
*/
public final class CaffeineMapTests extends TestCase {

public static Test suite() throws Exception {
public static Test suite() {
TestSuite suite = new TestSuite();
addGuavaViewTests(suite);
addUnboundedTests(suite);
addBoundedTests(suite);
return suite;
}

private static void addUnboundedTests(TestSuite suite) throws Exception {
private static void addUnboundedTests(TestSuite suite) {
suite.addTest(MapTestFactory.suite("UnboundedCache", synchronousGenerator(() -> {
Cache<String, String> cache = Caffeine.newBuilder().build();
return cache.asMap();
Expand All @@ -54,7 +53,7 @@ private static void addUnboundedTests(TestSuite suite) throws Exception {
})));
}

private static void addBoundedTests(TestSuite suite) throws Exception {
private static void addBoundedTests(TestSuite suite) {
suite.addTest(MapTestFactory.suite("BoundedCache", synchronousGenerator(() -> {
Cache<String, String> cache = Caffeine.newBuilder().maximumSize(Long.MAX_VALUE).build();
return cache.asMap();
Expand All @@ -72,17 +71,4 @@ private static void addBoundedTests(TestSuite suite) throws Exception {
return cache.asMap();
})));
}

private static void addGuavaViewTests(TestSuite suite) throws Exception {
suite.addTest(MapTestFactory.suite("GuavaView", synchronousGenerator(() -> {
com.google.common.cache.Cache<String, String> cache = CaffeinatedGuava.build(
Caffeine.newBuilder().maximumSize(Long.MAX_VALUE));
return cache.asMap();
})));
suite.addTest(MapTestFactory.suite("GuavaLoadingView", synchronousGenerator(() -> {
com.google.common.cache.Cache<String, String> cache = CaffeinatedGuava.build(
Caffeine.newBuilder().maximumSize(Long.MAX_VALUE), key -> null);
return cache.asMap();
})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @author [email protected] (Ben Manes)
*/
public class PackageSanityTests extends AbstractPackageSanityTests {
public final class PackageSanityTests extends AbstractPackageSanityTests {

public PackageSanityTests() {
publicApiOnly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.base;
package com.github.benmanes.caffeine.cache.stats;

import com.google.common.testing.AbstractPackageSanityTests;

Expand All @@ -22,9 +22,9 @@
*
* @author [email protected] (Ben Manes)
*/
public class PackageSanityTests extends AbstractPackageSanityTests {
public final class PackageSanityTests extends AbstractPackageSanityTests {

public PackageSanityTests() {
ignoreClasses(clazz -> clazz.getSimpleName().contains("Test"));
ignoreClasses(clazz -> clazz.getSimpleName().endsWith("Test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.cache;
package com.github.benmanes.caffeine.cache.testing;

import java.util.List;
import java.util.Map;
Expand All @@ -36,7 +36,7 @@
*
* @author [email protected] (Ben Manes)
*/
final class MapTestFactory {
public final class MapTestFactory {

private MapTestFactory() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package jsr166;
package com.github.benmanes.caffeine.jsr166;

import static java.util.Spliterator.CONCURRENT;
import static java.util.Spliterator.DISTINCT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
package jsr166;
package com.github.benmanes.caffeine.jsr166;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
package jsr166;
package com.github.benmanes.caffeine.jsr166;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
Expand Down
2 changes: 1 addition & 1 deletion caffeine/testing.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ task isolatedTests(type: Test, group: 'Cache tests') {
it.onlyIf { !System.env.'CI' }
}

task osgiTests(type: Test, group: 'Cache tests', description: 'Isolated OSGi tests') {
task junitTests(type: Test, group: 'Cache tests', description: 'JUnit tests') {
useJUnit()
tasks.test.dependsOn(it)
systemProperty 'caffeine.osgi.jar', project(':caffeine').jar.archivePath.path
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ ext {
},
expiringMap: "net.jodah:expiringmap:${versions.expiringMap}",
fastfilter: "com.github.FastFilter:fastfilter_java:${versions.fastfilter}",
fastutil: "it.unimi.dsi:fastutil:${versions.fastutil}",
fastutil: "it.unimi.dsi:fastutil-core:${versions.fastutil}",
flipTables: "com.jakewharton.fliptables:fliptables:${versions.flipTables}",
googleJavaFormat: "com.google.googlejavaformat:google-java-format:${versions.googleJavaFormat}",
guava: "com.google.guava:guava:${versions.guava}",
Expand Down
12 changes: 12 additions & 0 deletions gradle/eclipse.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
*
* @see https://gradle.org/docs/current/userguide/eclipse_plugin.html
*/
import org.gradle.plugins.ide.eclipse.model.SourceFolder

apply plugin: 'eclipse'

// Exclude module-info when compiling through Eclipse
afterEvaluate {
eclipse.classpath.file.whenMerged {
def main = entries.find { it instanceof SourceFolder && it.path == 'src/main/java' }
if (main != null) {
main.excludes.add('module-info.java')
}
}
}

// For the Gradle Eclipse plugin
eclipse.project.file.withXml { provider ->
ignoreDerivedResources(provider.asNode())
Expand Down
2 changes: 1 addition & 1 deletion guava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
}

compileJava {
modularity.inferModulePath = false
modularity.inferModulePath = true
}

jar.manifest {
Expand Down
4 changes: 4 additions & 0 deletions guava/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module com.github.benmanes.caffeine.guava {
requires transitive com.github.benmanes.caffeine;
requires transitive com.google.common;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.guava.CaffeinatedGuavaCache.CacheLoaderException;
import com.github.benmanes.caffeine.guava.compatability.TestingCacheLoaders;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.TestingCacheLoaders;
import com.google.common.testing.SerializableTester;
import com.google.common.util.concurrent.MoreExecutors;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.guava;

import static com.github.benmanes.caffeine.guava.MapTestFactory.synchronousGenerator;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.Cache;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Guava testlib map tests for the {@link Cache#asMap()} view.
*
* @author [email protected] (Ben Manes)
*/
public final class GuavaMapTests extends TestCase {

public static Test suite() {
TestSuite suite = new TestSuite();
addGuavaViewTests(suite);
return suite;
}

private static void addGuavaViewTests(TestSuite suite) {
suite.addTest(MapTestFactory.suite("GuavaView", synchronousGenerator(() -> {
Cache<String, String> cache = CaffeinatedGuava.build(
Caffeine.newBuilder().maximumSize(Long.MAX_VALUE));
return cache.asMap();
})));
suite.addTest(MapTestFactory.suite("GuavaLoadingView", synchronousGenerator(() -> {
Cache<String, String> cache = CaffeinatedGuava.build(
Caffeine.newBuilder().maximumSize(Long.MAX_VALUE), key -> null);
return cache.asMap();
})));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2015 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.guava;

import java.util.Map;
import java.util.function.Supplier;

import com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder;
import com.google.common.collect.testing.TestMapGenerator;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;

import junit.framework.Test;

/**
* A JUnit test suite factory for the map tests from Guava's testlib.
*
* @author [email protected] (Ben Manes)
*/
final class MapTestFactory {

private MapTestFactory() {}

/**
* Returns a test suite.
*
* @param name the name of the cache type under test
* @param generator the map generator
* @return a suite of tests
*/
public static Test suite(String name, TestMapGenerator<?, ?> generator) {
return ConcurrentMapTestSuiteBuilder
.using(generator)
.named(name)
.withFeatures(
MapFeature.GENERAL_PURPOSE,
MapFeature.ALLOWS_NULL_ENTRY_QUERIES,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
CollectionSize.ANY)
.createTestSuite();
}

/** Returns a map generator for synchronous values. */
public static TestStringMapGenerator synchronousGenerator(
Supplier<Map<String, String>> supplier) {
return new TestStringMapGenerator() {
@Override protected Map<String, String> create(Map.Entry<String, String>[] entries) {
Map<String, String> map = supplier.get();
for (Map.Entry<String, String> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author [email protected] (Ben Manes)
*/
public class PackageSanityTests extends AbstractPackageSanityTests {
public final class PackageSanityTests extends AbstractPackageSanityTests {

public PackageSanityTests() {
publicApiOnly();
Expand Down
Loading

0 comments on commit 7b28541

Please sign in to comment.