When creating a org.springframework.cache.support.SimpleCacheManager and explicitly adding caches to it, the configured object does not maintain the contract of the org.springframework.cache.CacheManager interface.

Specifically, getCacheNames() and getCache(String name) do not behave as one would expect, returning an empty collection and null respectively.

This issue is reproducible with version 5.1.7.RELEASE.

This issue is demonstrated with the following test:

package com.ak;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.Collections;

import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;

public class TestSimpleCacheManager {

    private static final String CACHE_NAME = "this is a cache";

    @Test
    public void testGetCacheNames() {
        SimpleCacheManager simpleCacheManager = setUpSimpleCacheManager();

        Collection<String> cacheNames = simpleCacheManager.getCacheNames();
        assertEquals(1, cacheNames.size());
        assertTrue(cacheNames.contains(CACHE_NAME));
    }

    @Test
    public void testGetCache() {
        SimpleCacheManager simpleCacheManager = setUpSimpleCacheManager();

        Cache cache = simpleCacheManager.getCache(CACHE_NAME);

        assertNotNull(cache);
    }

    private SimpleCacheManager setUpSimpleCacheManager() {
        Cache cache = new ConcurrentMapCache(CACHE_NAME);

        SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
        simpleCacheManager.setCaches(Collections.singleton(cache));
        return simpleCacheManager;
    }
}

using the following pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ak</groupId>
  <artifactId>ak-testing</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>ak-testing</name>
  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.7.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

I'd be happy to take a crack at creating a patch for this issue, should this be something that is desired to be fixed.

Thanks

Comment From: snicoll

The contract of SimpleCacheManager (and more particularly AbstractCacheManager) was designed so to be created as a bean and there is an afterProperties() callback that is invoked automatically when you are doing so. If you add that in setUpSimpleCacheManager before returning the cache manager both your tests pass.

I agree it is a bit confusing when using the API directly (especially since there are methods to mutate the cache manager once it is initialized).

Comment From: akokskis

Aha, that makes a lot more sense - thanks.

If no change is required or desired, which makes sense to me given your explanation, then I would wonder whether or not it's worth making a note of this in the documentation. I don't think I feel too strongly about this one way or another, but something to consider.

Thanks!