Skip to content

Commit

Permalink
JCache did not register statistics and management bean (fixes #87)
Browse files Browse the repository at this point in the history
Seems to be a gap in the TCK where we correctly registered when
CacheManager#createCache was called, but not when creating through
getCache(). Added @eiden's test case to augment the TCK.
  • Loading branch information
ben-manes committed Jun 7, 2016
1 parent 7ee54e1 commit 28118c5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
6 changes: 3 additions & 3 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ ext {
benchmark_versions = [
cache2k: '0.26-BETA',
concurrentlinkedhashmap: '1.4.2',
ehcache2: '2.10.2.1.7',
ehcache3: '3.0.1',
elastic_search: '5.0.0-alpha2',
ehcache2: '2.10.2.2.21',
ehcache3: '3.0.2',
elastic_search: '5.0.0-alpha3',
infinispan: '9.0.0.Alpha2',
jackrabbit: '1.5.2',
jamm: '0.3.1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,14 @@ public <K, V> Cache<K, V> getCache(String cacheName) {
requireNonNull(cacheName);
requireNotClosed();

CacheProxy<?, ?> cache = caches.computeIfAbsent(cacheName,
cacheFactory::tryToCreateFromExternalSettings);
CacheProxy<?, ?> cache = caches.computeIfAbsent(cacheName, name -> {
CacheProxy<?, ?> created = cacheFactory.tryToCreateFromExternalSettings(name);
if (created != null) {
created.enableManagement(created.getConfiguration().isManagementEnabled());
created.enableStatistics(created.getConfiguration().isStatisticsEnabled());
}
return created;
});

@SuppressWarnings("unchecked")
CacheProxy<K, V> castedCache = (CacheProxy<K, V>) cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2016 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.jcache;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import java.lang.management.ManagementFactory;
import java.util.function.Supplier;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.spi.CachingProvider;
import javax.management.ObjectName;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.github.benmanes.caffeine.jcache.configuration.TypesafeConfigurator;
import com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider;
import com.typesafe.config.ConfigFactory;

/**
* @author eiden (Christoffer Eide)
* @author [email protected] (Ben Manes)
*/
public final class CacheManagerTest {
private static final String PROVIDER_NAME = CaffeineCachingProvider.class.getName();

private CacheManager cacheManager;

@BeforeClass
public void beforeClass() {
CachingProvider provider = Caching.getCachingProvider(PROVIDER_NAME);
cacheManager = provider.getCacheManager(
provider.getDefaultURI(), provider.getDefaultClassLoader());
}

@Test
public void jmxBeanIsRegistered_createCache() throws Exception {
checkConfigurationJmx(() -> cacheManager.createCache("cache-not-in-config-file",
TypesafeConfigurator.from(ConfigFactory.load(), "test-cache").get()));
}

@Test
public void jmxBeanIsRegistered_getCache() throws Exception {
checkConfigurationJmx(() -> cacheManager.getCache("test-cache"));
}

private void checkConfigurationJmx(Supplier<Cache<?, ?>> cacheSupplier) throws Exception {
Cache<?, ?> cache = cacheSupplier.get();

@SuppressWarnings("unchecked")
CompleteConfiguration<?, ?> configuration = cache.getConfiguration(CompleteConfiguration.class);
assertThat(configuration.isManagementEnabled(), is(true));
assertThat(configuration.isStatisticsEnabled(), is(true));

String name = "javax.cache:Cache=%s,CacheManager=%s,type=CacheStatistics";
ManagementFactory.getPlatformMBeanServer().getObjectInstance(
new ObjectName(String.format(name, cache.getName(), PROVIDER_NAME)));
}
}

0 comments on commit 28118c5

Please sign in to comment.