|
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. |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 20 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
21 | 21 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
22 | 23 | import static org.assertj.core.api.Assertions.fail; |
23 | 24 | import static org.mockito.AdditionalAnswers.returnsFirstArg; |
24 | 25 | import static org.mockito.ArgumentMatchers.anyString; |
|
51 | 52 | import org.springframework.beans.factory.BeanFactory; |
52 | 53 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
53 | 54 | import org.springframework.context.ApplicationContext; |
| 55 | +import org.springframework.context.Lifecycle; |
54 | 56 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
55 | 57 | import org.springframework.context.annotation.Bean; |
56 | 58 | import org.springframework.context.annotation.Configuration; |
@@ -1165,6 +1167,31 @@ public String myMethod(List<Employee<Person>> msg) { |
1165 | 1167 | assertThat(result).isEqualTo("Foo,Bar"); |
1166 | 1168 | } |
1167 | 1169 |
|
| 1170 | + @Test |
| 1171 | + void lifecycleOnly() { |
| 1172 | + assertThatIllegalStateException().isThrownBy(() -> |
| 1173 | + new MethodInvokingMessageProcessor(new LifecycleBean(), (String) null)) |
| 1174 | + .withMessageContaining("no eligible methods"); |
| 1175 | + } |
| 1176 | + |
| 1177 | + @Test |
| 1178 | + void lifecycleOnlyExplicitMethod() { |
| 1179 | + LifecycleBean targetObject = new LifecycleBean(); |
| 1180 | + MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(targetObject, "start"); |
| 1181 | + processor.setBeanFactory(mock(BeanFactory.class)); |
| 1182 | + Object result = processor.processMessage(new GenericMessage<>("testing")); |
| 1183 | + assertThat(targetObject.startCalled).isTrue(); |
| 1184 | + } |
| 1185 | + |
| 1186 | + @Test |
| 1187 | + void lifecycleWithValidStartMethod() { |
| 1188 | + MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(new LifeCycleWithCustomStart(), |
| 1189 | + (String) null); |
| 1190 | + processor.setBeanFactory(mock(BeanFactory.class)); |
| 1191 | + Object result = processor.processMessage(new GenericMessage<>("testing")); |
| 1192 | + assertThat(result).isEqualTo("TESTING"); |
| 1193 | + } |
| 1194 | + |
1168 | 1195 | public static class Employee<T> { |
1169 | 1196 |
|
1170 | 1197 | private T entity; |
@@ -1586,4 +1613,32 @@ public Integer getBaz() { |
1586 | 1613 |
|
1587 | 1614 | } |
1588 | 1615 |
|
| 1616 | + public static class LifecycleBean implements Lifecycle { |
| 1617 | + |
| 1618 | + boolean startCalled; |
| 1619 | + |
| 1620 | + @Override |
| 1621 | + public void start() { |
| 1622 | + this.startCalled = true; |
| 1623 | + } |
| 1624 | + |
| 1625 | + @Override |
| 1626 | + public void stop() { |
| 1627 | + } |
| 1628 | + |
| 1629 | + @Override |
| 1630 | + public boolean isRunning() { |
| 1631 | + return false; |
| 1632 | + } |
| 1633 | + |
| 1634 | + } |
| 1635 | + |
| 1636 | + public static class LifeCycleWithCustomStart extends LifecycleBean { |
| 1637 | + |
| 1638 | + public String start(String in) { |
| 1639 | + return in.toUpperCase(); |
| 1640 | + } |
| 1641 | + |
| 1642 | + } |
| 1643 | + |
1589 | 1644 | } |
0 commit comments