Skip to content

Commit 1708bdd

Browse files
committed
spring-projectsGH-3869: Redesign the test for the advice.
1 parent a9dc3b2 commit 1708bdd

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/ContextHolderRequestHandlerAdvice.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ public class ContextHolderRequestHandlerAdvice extends AbstractRequestHandlerAdv
3535

3636
public Consumer<Object> contextClearHook;
3737

38+
/**
39+
* Constructor for the ContextHolderRequestHandlerAdvice.
40+
*
41+
* @param keyProvider The key provider function.
42+
* @param contextSetHook The context set hook function.
43+
* @param contextClearHook The context clear hook function.
44+
*/
45+
public ContextHolderRequestHandlerAdvice(final Function<Message<?>, Object> keyProvider, final Consumer<Object> contextSetHook, final Consumer<Object> contextClearHook) {
46+
this.keyProvider = keyProvider;
47+
this.contextSetHook = contextSetHook;
48+
this.contextClearHook = contextClearHook;
49+
}
50+
3851
/**
3952
* Returns the key provider function.
4053
* @return The key provider function.
@@ -90,12 +103,12 @@ protected Object doInvoke(ExecutionCallback callback, Object target, Message<?>
90103
Object result;
91104
final Object key = this.keyProvider.apply(message);
92105
try {
93-
result = callback.execute();
94106
setContext(key);
107+
result = callback.execute();
95108
}
96109
catch (Exception e) {
97110
result = e;
98-
logger.error("Failure setting context holder for " + message + ": " + e.getMessage());
111+
logger.error("Failure setting context for " + message + ": " + e.getMessage());
99112
}
100113
finally {
101114
contextClearHook(key);

spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/ContextHolderRequestHandlerAdviceTests.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import java.util.function.Consumer;
22+
import java.util.function.Function;
2123

2224
import org.junit.jupiter.api.Test;
2325

@@ -53,32 +55,24 @@ public class ContextHolderRequestHandlerAdviceTests {
5355
TestSessionFactory bar;
5456

5557
@Autowired
56-
ContextHolderRequestHandlerAdvice advice;
57-
58-
@Autowired
59-
MessageChannel in;
60-
61-
@Autowired
62-
PollableChannel out;
63-
64-
@Autowired
65-
DefaultSessionFactoryLocator<String> sessionFactoryLocator;
66-
67-
@Autowired
68-
TestService testService;
58+
Config config;
6959

7060
@Test
7161
public void testFlow() {
72-
final Map<String, Object> headers = Map.of("foo", "foo");
73-
this.in.send(new GenericMessage<>("foo", headers));
74-
Message<?> received = out.receive(0);
62+
final Map<String, Object> headers = Map.of("test-key", "foo");
63+
config.in().send(new GenericMessage<>("any-payload", headers));
64+
Message<?> received = config.out().receive(0);
7565
assertThat(received).isNotNull();
66+
assertThat(Config.testSessionDuringHandling).isNotNull().isEqualTo(foo.getSession());
67+
assertThat(config.dsf().getSession()).isEqualTo(bar.getSession());
7668
}
7769

7870
@Configuration
7971
@EnableIntegration
8072
public static class Config {
8173

74+
public static Session<String> testSessionDuringHandling = null;
75+
8276
@Bean
8377
MessageChannel in() {
8478
return MessageChannels.direct("in").get();
@@ -107,11 +101,13 @@ TestSessionFactory bar() {
107101
@Bean
108102
ContextHolderRequestHandlerAdvice advice() {
109103
final DelegatingSessionFactory<String> dsf = dsf();
110-
final var contextHolderRequestHandlerAdvice = new ContextHolderRequestHandlerAdvice();
111-
contextHolderRequestHandlerAdvice.setKeyProvider(m -> m.getHeaders().get("foo"));
112-
contextHolderRequestHandlerAdvice.setContextSetHook(dsf::setThreadKey);
113-
contextHolderRequestHandlerAdvice.setContextClearHook(c -> dsf.clearThreadKey());
114-
return contextHolderRequestHandlerAdvice;
104+
final Function<Message<?>, Object> keyProviderFn = m -> m.getHeaders().get("test-key");
105+
final Consumer<Object> contextSetHookFn = key -> {
106+
dsf.setThreadKey(key);
107+
testSessionDuringHandling = dsf.getSession();
108+
};
109+
final Consumer<Object> contextClearHookFn = key -> dsf.clearThreadKey();
110+
return new ContextHolderRequestHandlerAdvice(keyProviderFn, contextSetHookFn, contextClearHookFn);
115111
}
116112

117113
@Bean
@@ -134,9 +130,6 @@ public SessionFactoryLocator<String> sessionFactoryLocator() {
134130
@Component
135131
public static class TestService {
136132

137-
@Autowired
138-
private ContextHolderRequestHandlerAdvice advice;
139-
140133
@ServiceActivator(inputChannel = "in", outputChannel = "out", adviceChain = "advice")
141134
public String test(Message<String> message) {
142135
return message.getPayload();

0 commit comments

Comments
 (0)