|
| 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 | +package org.springframework.context.event |
| 17 | + |
| 18 | +import org.assertj.core.api.Assertions |
| 19 | +import org.junit.jupiter.api.Test |
| 20 | +import org.mockito.Mockito |
| 21 | +import org.springframework.context.ApplicationEvent |
| 22 | +import org.springframework.core.ResolvableType |
| 23 | +import org.springframework.util.ReflectionUtils |
| 24 | +import java.lang.reflect.Method |
| 25 | +import kotlin.coroutines.Continuation |
| 26 | + |
| 27 | +/** |
| 28 | + * Kotlin tests for [ApplicationListenerMethodAdapter]. |
| 29 | + * |
| 30 | + * @author Sebastien Deleuze |
| 31 | + */ |
| 32 | +class KotlinApplicationListenerMethodAdapterTests { |
| 33 | + |
| 34 | + private val sampleEvents = Mockito.spy(SampleEvents()) |
| 35 | + |
| 36 | + @Test |
| 37 | + fun rawListener() { |
| 38 | + val method = ReflectionUtils.findMethod(SampleEvents::class.java, "handleRaw", ApplicationEvent::class.java, Continuation::class.java)!! |
| 39 | + supportsEventType(true, method, ResolvableType.forClass(ApplicationEvent::class.java)) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun listenerWithMoreThanOneParameter() { |
| 44 | + val method = ReflectionUtils.findMethod(SampleEvents::class.java, "moreThanOneParameter", |
| 45 | + String::class.java, Int::class.java, Continuation::class.java)!! |
| 46 | + Assertions.assertThatIllegalStateException().isThrownBy { |
| 47 | + createTestInstance( |
| 48 | + method |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private fun supportsEventType(match: Boolean, method: Method, eventType: ResolvableType) { |
| 54 | + val adapter: ApplicationListenerMethodAdapter = createTestInstance(method) |
| 55 | + Assertions.assertThat(adapter.supportsEventType(eventType)) |
| 56 | + .`as`("Wrong match for event '$eventType' on $method").isEqualTo(match) |
| 57 | + } |
| 58 | + |
| 59 | + private fun createTestInstance(method: Method): ApplicationListenerMethodAdapter { |
| 60 | + return StaticApplicationListenerMethodAdapter(method, this.sampleEvents) |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private class StaticApplicationListenerMethodAdapter(method: Method, private val targetBean: Any) : |
| 65 | + ApplicationListenerMethodAdapter("unused", targetBean.javaClass, method) { |
| 66 | + public override fun getTargetBean(): Any { |
| 67 | + return targetBean |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Suppress("RedundantSuspendModifier", "UNUSED_PARAMETER") |
| 72 | + private class SampleEvents { |
| 73 | + |
| 74 | + @EventListener |
| 75 | + suspend fun handleRaw(event: ApplicationEvent) { |
| 76 | + } |
| 77 | + |
| 78 | + @EventListener |
| 79 | + suspend fun moreThanOneParameter(foo: String, bar: Int) { |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments