|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cache |
| 18 | + |
| 19 | +import kotlinx.coroutines.runBlocking |
| 20 | +import org.assertj.core.api.Assertions.assertThat |
| 21 | +import org.junit.jupiter.api.Test |
| 22 | +import org.springframework.beans.testfixture.beans.TestBean |
| 23 | +import org.springframework.cache.CacheReproTests.* |
| 24 | +import org.springframework.cache.annotation.CacheEvict |
| 25 | +import org.springframework.cache.annotation.CachePut |
| 26 | +import org.springframework.cache.annotation.Cacheable |
| 27 | +import org.springframework.cache.annotation.EnableCaching |
| 28 | +import org.springframework.cache.concurrent.ConcurrentMapCacheManager |
| 29 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext |
| 30 | +import org.springframework.context.annotation.Bean |
| 31 | +import org.springframework.context.annotation.Configuration |
| 32 | + |
| 33 | +class KotlinCacheReproTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + fun spr14235AdaptsToSuspendingFunction() { |
| 37 | + runBlocking { |
| 38 | + val context = AnnotationConfigApplicationContext( |
| 39 | + Spr14235Config::class.java, |
| 40 | + Spr14235SuspendingService::class.java |
| 41 | + ) |
| 42 | + val bean = context.getBean(Spr14235SuspendingService::class.java) |
| 43 | + val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!! |
| 44 | + val tb: TestBean = bean.findById("tb1") |
| 45 | + assertThat(bean.findById("tb1")).isSameAs(tb) |
| 46 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb) |
| 47 | + bean.clear() |
| 48 | + val tb2: TestBean = bean.findById("tb1") |
| 49 | + assertThat(tb2).isNotSameAs(tb) |
| 50 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb2) |
| 51 | + bean.clear() |
| 52 | + bean.insertItem(tb) |
| 53 | + assertThat(bean.findById("tb1")).isSameAs(tb) |
| 54 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb) |
| 55 | + context.close() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun spr14235AdaptsToSuspendingFunctionWithSync() { |
| 61 | + runBlocking { |
| 62 | + val context = AnnotationConfigApplicationContext( |
| 63 | + Spr14235Config::class.java, |
| 64 | + Spr14235SuspendingServiceSync::class.java |
| 65 | + ) |
| 66 | + val bean = context.getBean(Spr14235SuspendingServiceSync::class.java) |
| 67 | + val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!! |
| 68 | + val tb = bean.findById("tb1") |
| 69 | + assertThat(bean.findById("tb1")).isSameAs(tb) |
| 70 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb) |
| 71 | + cache.clear() |
| 72 | + val tb2 = bean.findById("tb1") |
| 73 | + assertThat(tb2).isNotSameAs(tb) |
| 74 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb2) |
| 75 | + cache.clear() |
| 76 | + bean.insertItem(tb) |
| 77 | + assertThat(bean.findById("tb1")).isSameAs(tb) |
| 78 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb) |
| 79 | + context.close() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun spr15271FindsOnInterfaceWithInterfaceProxy() { |
| 85 | + val context = AnnotationConfigApplicationContext(Spr15271ConfigA::class.java) |
| 86 | + val bean = context.getBean(Spr15271Interface::class.java) |
| 87 | + val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!! |
| 88 | + val tb = TestBean("tb1") |
| 89 | + bean.insertItem(tb) |
| 90 | + assertThat(bean.findById("tb1").get()).isSameAs(tb) |
| 91 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb) |
| 92 | + context.close() |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun spr15271FindsOnInterfaceWithCglibProxy() { |
| 97 | + val context = AnnotationConfigApplicationContext(Spr15271ConfigB::class.java) |
| 98 | + val bean = context.getBean(Spr15271Interface::class.java) |
| 99 | + val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!! |
| 100 | + val tb = TestBean("tb1") |
| 101 | + bean.insertItem(tb) |
| 102 | + assertThat(bean.findById("tb1").get()).isSameAs(tb) |
| 103 | + assertThat(cache["tb1"]!!.get()).isSameAs(tb) |
| 104 | + context.close() |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + open class Spr14235SuspendingService { |
| 109 | + |
| 110 | + @Cacheable(value = ["itemCache"]) |
| 111 | + open suspend fun findById(id: String): TestBean { |
| 112 | + return TestBean(id) |
| 113 | + } |
| 114 | + |
| 115 | + @CachePut(cacheNames = ["itemCache"], key = "#item.name") |
| 116 | + open suspend fun insertItem(item: TestBean): TestBean { |
| 117 | + return item |
| 118 | + } |
| 119 | + |
| 120 | + @CacheEvict(cacheNames = ["itemCache"], allEntries = true) |
| 121 | + open suspend fun clear() { |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + open class Spr14235SuspendingServiceSync { |
| 127 | + @Cacheable(value = ["itemCache"], sync = true) |
| 128 | + open suspend fun findById(id: String): TestBean { |
| 129 | + return TestBean(id) |
| 130 | + } |
| 131 | + |
| 132 | + @CachePut(cacheNames = ["itemCache"], key = "#item.name") |
| 133 | + open suspend fun insertItem(item: TestBean): TestBean { |
| 134 | + return item |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + @Configuration(proxyBeanMethods = false) |
| 140 | + @EnableCaching |
| 141 | + class Spr14235Config { |
| 142 | + @Bean |
| 143 | + fun cacheManager(): CacheManager { |
| 144 | + return ConcurrentMapCacheManager() |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments