|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import org.junit.jupiter.api.Test;
|
22 | 22 |
|
23 | 23 | import org.springframework.cache.Cache;
|
| 24 | +import org.springframework.cache.Cache.ValueWrapper; |
24 | 25 | import org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests;
|
25 | 26 |
|
26 | 27 | import static org.assertj.core.api.Assertions.assertThat;
|
| 28 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
27 | 29 |
|
28 | 30 | /**
|
29 | 31 | * @author Ben Manes
|
@@ -61,6 +63,34 @@ protected Object getNativeCache() {
|
61 | 63 | return nativeCache;
|
62 | 64 | }
|
63 | 65 |
|
| 66 | + @Test |
| 67 | + void testLoadingCacheGet() { |
| 68 | + Object value = new Object(); |
| 69 | + CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder() |
| 70 | + .build(key -> value)); |
| 71 | + ValueWrapper valueWrapper = loadingCache.get(new Object()); |
| 72 | + assertThat(valueWrapper).isNotNull(); |
| 73 | + assertThat(valueWrapper.get()).isEqualTo(value); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testLoadingCacheGetWithType() { |
| 78 | + String value = "value"; |
| 79 | + CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder() |
| 80 | + .build(key -> value)); |
| 81 | + String valueWrapper = loadingCache.get(new Object(), String.class); |
| 82 | + assertThat(valueWrapper).isNotNull(); |
| 83 | + assertThat(valueWrapper).isEqualTo(value); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testLoadingCacheGetWithWrongType() { |
| 88 | + String value = "value"; |
| 89 | + CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder() |
| 90 | + .build(key -> value)); |
| 91 | + assertThatIllegalStateException().isThrownBy(() -> loadingCache.get(new Object(), Long.class)); |
| 92 | + } |
| 93 | + |
64 | 94 | @Test
|
65 | 95 | public void testPutIfAbsentNullValue() throws Exception {
|
66 | 96 | CaffeineCache cache = getCache();
|
|
0 commit comments