|
| 1 | +/* |
| 2 | + * Copyright 2024 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.modulith.events; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springframework.context.event.ApplicationListenerMethodAdapter; |
| 24 | +import org.springframework.context.event.EventListener; |
| 25 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Unit tests for {@link ApplicationModuleListener}. |
| 29 | + * |
| 30 | + * @author Oliver Drotbohm |
| 31 | + */ |
| 32 | +class ApplicationModuleListenerUnitTests { |
| 33 | + |
| 34 | + @Test // GH-518 |
| 35 | + void exposesEventListenerCondition() throws Exception { |
| 36 | + |
| 37 | + var method = Sample.class.getDeclaredMethod("someMethod", Object.class); |
| 38 | + var annotation = AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class); |
| 39 | + |
| 40 | + assertThat(annotation.condition()).isEqualTo("#{false}"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test // GH-518 |
| 44 | + void exposesConditionToAdapter() throws Exception { |
| 45 | + |
| 46 | + var method = Sample.class.getDeclaredMethod("someMethod", Object.class); |
| 47 | + |
| 48 | + var adapter = new CustomAdapter("someName", Sample.class, method) {}; |
| 49 | + |
| 50 | + assertThat(adapter.getCondition()).isEqualTo("#{false}"); |
| 51 | + } |
| 52 | + |
| 53 | + static class Sample { |
| 54 | + |
| 55 | + @ApplicationModuleListener(condition = "#{false}") |
| 56 | + void someMethod(Object event) {} |
| 57 | + } |
| 58 | + |
| 59 | + static class CustomAdapter extends ApplicationListenerMethodAdapter { |
| 60 | + |
| 61 | + public CustomAdapter(String beanName, Class<?> targetClass, Method method) { |
| 62 | + super(beanName, targetClass, method); |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + * (non-Javadoc) |
| 67 | + * @see org.springframework.context.event.ApplicationListenerMethodAdapter#getCondition() |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public String getCondition() { |
| 71 | + return super.getCondition(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments