From 8e5bf4b22a5426df605aa763e3acdeed4c8e8f1f Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 2 May 2017 13:15:31 +0200 Subject: [PATCH] Polish CachePublicMetrics CachePublicMetrics wasn't explicitly tested and was still using field injection. This commit improves the situation in preparation of the fix for gh-8984 --- .../actuate/endpoint/CachePublicMetrics.java | 11 ++- .../endpoint/CachePublicMetricsTests.java | 97 +++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java index 25ab754826..ac03cd88e6 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/CachePublicMetrics.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,15 @@ public class CachePublicMetrics implements PublicMetrics { @Autowired private Collection> statisticsProviders; + public CachePublicMetrics() { + } + + public CachePublicMetrics(Map cacheManagers, + Collection> statisticsProviders) { + this.cacheManagers = cacheManagers; + this.statisticsProviders = statisticsProviders; + } + @Override public Collection> metrics() { Collection> metrics = new HashSet>(); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java new file mode 100644 index 0000000000..91c060d605 --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/CachePublicMetricsTests.java @@ -0,0 +1,97 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * 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 org.springframework.boot.actuate.endpoint; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.boot.actuate.cache.CacheStatisticsProvider; +import org.springframework.boot.actuate.cache.CaffeineCacheStatisticsProvider; +import org.springframework.boot.actuate.cache.ConcurrentMapCacheStatisticsProvider; +import org.springframework.boot.actuate.metrics.Metric; +import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +/** + * Tests for {@link CachePublicMetrics} + * + * @author Stephane Nicoll + */ +public class CachePublicMetricsTests { + + private Map cacheManagers = new HashMap(); + + @Before + public void setup() { + this.cacheManagers.put("cacheManager", + new ConcurrentMapCacheManager("foo", "bar")); + } + + @Test + public void cacheMetricsWithMatchingProvider() { + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, + providers(new ConcurrentMapCacheStatisticsProvider())); + Map metrics = metrics(cpm); + assertThat(metrics).containsOnly(entry("cache.foo.size", 0L), + entry("cache.bar.size", 0L)); + } + + @Test + public void cacheMetricsWithNoMatchingProvider() { + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, + providers(new CaffeineCacheStatisticsProvider())); + Map metrics = metrics(cpm); + assertThat(metrics).isEmpty(); + } + + @Test + public void cacheMetricsWithMultipleCacheManagers() { + this.cacheManagers.put("anotherCacheManager", + new ConcurrentMapCacheManager("foo")); + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, + providers(new ConcurrentMapCacheStatisticsProvider())); + Map metrics = metrics(cpm); + assertThat(metrics).containsOnly(entry("cache.cacheManager_foo.size", 0L), + entry("cache.bar.size", 0L), + entry("cache.anotherCacheManager_foo.size", 0L)); + } + + + private Map metrics(CachePublicMetrics cpm) { + Collection> metrics = cpm.metrics(); + assertThat(metrics).isNotNull(); + Map result = new HashMap(); + for (Metric metric : metrics) { + result.put(metric.getName(), metric.getValue()); + } + return result; + } + + private Collection> providers( + CacheStatisticsProvider... providers) { + return Arrays.asList(providers); + } + +}