-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow jcache to load its configuration from a uri (fixes #877)
- Loading branch information
Showing
8 changed files
with
170 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,29 +50,29 @@ | |
* @author [email protected] (Ben Manes) | ||
*/ | ||
final class CacheFactory { | ||
|
||
private CacheFactory() {} | ||
|
||
/** | ||
* Returns if the cache definition is found in the external settings file. | ||
* | ||
* @param cacheManager the owner | ||
* @param cacheName the name of the cache | ||
* @return {@code true} if a definition exists | ||
*/ | ||
public static boolean isDefinedExternally(String cacheName) { | ||
return TypesafeConfigurator.cacheNames(rootConfig()).contains(cacheName); | ||
public static boolean isDefinedExternally(CacheManager cacheManager, String cacheName) { | ||
return TypesafeConfigurator.cacheNames(rootConfig(cacheManager)).contains(cacheName); | ||
} | ||
|
||
/** | ||
* Returns a newly created cache instance if a definition is found in the external settings file. | ||
* | ||
* @param cacheManager the owner of the cache instance | ||
* @param cacheManager the owner | ||
* @param cacheName the name of the cache | ||
* @return a new cache instance or null if the named cache is not defined in the settings file | ||
*/ | ||
public static @Nullable <K, V> CacheProxy<K, V> tryToCreateFromExternalSettings( | ||
CacheManager cacheManager, String cacheName) { | ||
return TypesafeConfigurator.<K, V>from(rootConfig(), cacheName) | ||
return TypesafeConfigurator.<K, V>from(rootConfig(cacheManager), cacheName) | ||
.map(configuration -> createCache(cacheManager, cacheName, configuration)) | ||
.orElse(null); | ||
} | ||
|
@@ -87,24 +87,25 @@ public static boolean isDefinedExternally(String cacheName) { | |
*/ | ||
public static <K, V> CacheProxy<K, V> createCache(CacheManager cacheManager, | ||
String cacheName, Configuration<K, V> configuration) { | ||
CaffeineConfiguration<K, V> config = resolveConfigurationFor(configuration); | ||
CaffeineConfiguration<K, V> config = resolveConfigurationFor(cacheManager, configuration); | ||
return new Builder<>(cacheManager, cacheName, config).build(); | ||
} | ||
|
||
/** Returns the resolved configuration. */ | ||
private static Config rootConfig() { | ||
return requireNonNull(TypesafeConfigurator.configSource().get()); | ||
private static Config rootConfig(CacheManager cacheManager) { | ||
return requireNonNull(TypesafeConfigurator.configSource().apply( | ||
cacheManager.getURI(), cacheManager.getClassLoader())); | ||
} | ||
|
||
/** Copies the configuration and overlays it on top of the default settings. */ | ||
@SuppressWarnings("PMD.AccessorMethodGeneration") | ||
private static <K, V> CaffeineConfiguration<K, V> resolveConfigurationFor( | ||
Configuration<K, V> configuration) { | ||
CacheManager cacheManager, Configuration<K, V> configuration) { | ||
if (configuration instanceof CaffeineConfiguration<?, ?>) { | ||
return new CaffeineConfiguration<>((CaffeineConfiguration<K, V>) configuration); | ||
} | ||
|
||
CaffeineConfiguration<K, V> template = TypesafeConfigurator.defaults(rootConfig()); | ||
CaffeineConfiguration<K, V> template = TypesafeConfigurator.defaults(rootConfig(cacheManager)); | ||
if (configuration instanceof CompleteConfiguration<?, ?>) { | ||
CompleteConfiguration<K, V> complete = (CompleteConfiguration<K, V>) configuration; | ||
template.setReadThrough(complete.isReadThrough()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,38 +15,116 @@ | |
*/ | ||
package com.github.benmanes.caffeine.jcache.configuration; | ||
|
||
import static com.github.benmanes.caffeine.jcache.configuration.TypesafeConfigurator.configSource; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static com.google.common.truth.Truth8.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Optional; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
|
||
import javax.cache.Cache; | ||
import javax.cache.Caching; | ||
import javax.cache.expiry.Duration; | ||
import javax.cache.expiry.ExpiryPolicy; | ||
|
||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import com.github.benmanes.caffeine.jcache.copy.JavaSerializationCopier; | ||
import com.google.common.collect.Iterables; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigException; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
/** | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
public final class TypesafeConfigurationTest { | ||
final BiFunction<URI, ClassLoader, Config> defaultConfigSource = configSource(); | ||
final ClassLoader classloader = Thread.currentThread().getContextClassLoader(); | ||
|
||
@BeforeMethod | ||
public void before() { | ||
TypesafeConfigurator.setConfigSource(defaultConfigSource); | ||
} | ||
|
||
@Test | ||
public void setConfigSource_supplier() { | ||
TypesafeConfigurator.setConfigSource(() -> null); | ||
assertThat(configSource()).isNotSameInstanceAs(defaultConfigSource); | ||
|
||
assertThrows(NullPointerException.class, () -> | ||
TypesafeConfigurator.setConfigSource((Supplier<Config>) null)); | ||
} | ||
|
||
@Test | ||
public void setConfigSource_function() { | ||
TypesafeConfigurator.setConfigSource((uri, classloader) -> null); | ||
assertThat(configSource()).isNotSameInstanceAs(defaultConfigSource); | ||
|
||
assertThrows(NullPointerException.class, () -> | ||
TypesafeConfigurator.setConfigSource((BiFunction<URI, ClassLoader, Config>) null)); | ||
} | ||
|
||
@Test | ||
public void configSource_null() { | ||
assertThrows(NullPointerException.class, () -> configSource().apply(null, null)); | ||
assertThrows(NullPointerException.class, () -> configSource().apply(null, classloader)); | ||
assertThrows(NullPointerException.class, () -> configSource().apply(URI.create(""), null)); | ||
} | ||
|
||
@Test | ||
public void configSource_load() { | ||
assertThat(configSource().apply(URI.create(getClass().getName()), classloader)) | ||
.isSameInstanceAs(ConfigFactory.load()); | ||
} | ||
|
||
@Test | ||
public void configSource_classpath_present() { | ||
var inferred = configSource().apply(URI.create("custom.properties"), classloader); | ||
assertThat(inferred.getInt("caffeine.jcache.classpath.policy.maximum.size")).isEqualTo(500); | ||
|
||
var explicit = configSource().apply(URI.create("classpath:custom.properties"), classloader); | ||
assertThat(explicit.getInt("caffeine.jcache.classpath.policy.maximum.size")).isEqualTo(500); | ||
} | ||
|
||
@Test | ||
public void configSource_classpath_absent() { | ||
assertThrows(ConfigException.IO.class, () -> | ||
configSource().apply(URI.create("absent.conf"), classloader)); | ||
assertThrows(ConfigException.IO.class, () -> | ||
configSource().apply(URI.create("classpath:absent.conf"), classloader)); | ||
} | ||
|
||
@Test | ||
public void configSource_classpath_invalid() { | ||
assertThrows(ConfigException.Parse.class, () -> | ||
configSource().apply(URI.create("invalid.conf"), classloader)); | ||
} | ||
|
||
@Test | ||
public void configSource_file() throws URISyntaxException { | ||
var config = configSource().apply( | ||
getClass().getResource("/custom.properties").toURI(), classloader); | ||
assertThat(config.getInt("caffeine.jcache.classpath.policy.maximum.size")).isEqualTo(500); | ||
} | ||
|
||
@Test | ||
public void configSource_file_absent() { | ||
assertThrows(ConfigException.IO.class, () -> | ||
configSource().apply(URI.create("file:/absent.conf"), classloader)); | ||
} | ||
|
||
@Test | ||
public void configSource() { | ||
Config config = ConfigFactory.load(); | ||
Supplier<Config> configSource = () -> config; | ||
TypesafeConfigurator.setConfigSource(configSource); | ||
assertThat(TypesafeConfigurator.configSource()).isEqualTo(configSource); | ||
public void configSource_file_invalid() { | ||
assertThrows(ConfigException.IO.class, () -> | ||
configSource().apply(URI.create("file:/invalid.conf"), classloader)); | ||
} | ||
|
||
@Test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
caffeine.jcache.classpath.policy.maximum.size = 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is not a hocon configuration file |