0

我正在重写我所在项目的代码中的瓶颈,这样做我正在创建一个包含自填充 Ehcache 的顶级项目。我正在尝试编写一个测试以确保建立基本调用链,但是当测试执行时,它会在从缓存中检索项目时进行。

以下是设置和测试,以供参考使用 Mockito 进行模拟:

@Before
public void SetUp()
{
    testCache = new Cache(getTestCacheConfiguration());
    recordingFactory = new EntryCreationRecordingCache();
    service = new Service<Request, Response>(testCache, recordingFactory); 
}

@Test
public void retrievesResultsFromSuppliedCache()
{
    ResultType resultType = mock(ResultType.class);
    Response expectedResponse = mock(Response.class);
    addToExpectedResults(resultType, expectedResponse);
    Request request = mock(Request.class);
    when(request.getResultType()).thenReturn(resultType);

    assertThat(service.getResponse(request), sameInstance(expectedResponse));
    assertTrue(recordingFactory.requestList.contains(request));
}

private void addToExpectedResults(ResultType resultType,
        Response response) {
    recordingFactory.responseMap.put(resultType, response);

}

private CacheConfiguration getTestCacheConfiguration() {
    CacheConfiguration cacheConfiguration = new CacheConfiguration("TEST_CACHE", 10);
    cacheConfiguration.setLoggingEnabled(false);
    return cacheConfiguration;
}

private class EntryCreationRecordingCache extends ResponseFactory{

    public final Map<ResultType, Response> responseMap = new ConcurrentHashMap<ResultType, Response>();
    public final List<Request> requestList = new ArrayList<Request>();

    @Override
    protected Map<ResultType, Response> generateResponse(Request request) {
        requestList.add(request);
        return responseMap;
    }
}

这是服务类

public class Service<K extends Request, V extends Response> {

    private Ehcache cache;

    public Service(Ehcache cache, ResponseFactory factory) {
        this.cache = new SelfPopulatingCache(cache, factory);
    }

    @SuppressWarnings("unchecked")
    public V getResponse(K request)
    {
        ResultType resultType = request.getResultType();
        Element cacheEntry = cache.get(request);
        V response = null;
        if(cacheEntry != null){
            Map<ResultType, Response> resultTypeMap = (Map<ResultType, Response>) cacheEntry.getValue();
            try{
                response = (V) resultTypeMap.get(resultType);
            }catch(NullPointerException e){
                throw new RuntimeException("Result type not found for Result Type: " + resultType);
            }catch(ClassCastException e){
                throw new RuntimeException("Incorrect Response Type for Result Type: " + resultType);
            }
        }
        return response;
    }
}

这是响应工厂:

public abstract class ResponseFactory implements CacheEntryFactory{

    @Override
    public final Object createEntry(Object request) throws Exception {
        return generateResponse((Request)request);
    }

    protected abstract Map<ResultType,Response> generateResponse(Request request);
}
4

2 回答 2

0

折腾了一段时间后,我发现缓存没有被初始化。创建一个 CacheManager 并将缓存添加到它解决了这个问题。

于 2010-05-19T17:11:28.683 回答
0

我也遇到了 EHCache 挂起的问题,尽管只是在一个 hello-world 示例中。将此添加到最后修复它(应用程序正常结束)。

CacheManager.getInstance().removeAllCaches();

https://stackoverflow.com/a/20731502/2736496

于 2013-12-22T16:47:56.903 回答