|
| 1 | +/* |
| 2 | + * Copyright 2004-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 | + |
| 17 | +package org.springframework.webflow.action; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | +import org.springframework.test.annotation.DirtiesContext; |
| 28 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 29 | +import org.springframework.webflow.config.AbstractFlowConfiguration; |
| 30 | +import org.springframework.webflow.definition.registry.FlowDefinitionRegistry; |
| 31 | +import org.springframework.webflow.execution.Event; |
| 32 | +import org.springframework.webflow.execution.RequestContext; |
| 33 | +import org.springframework.webflow.executor.FlowExecutor; |
| 34 | +import org.springframework.webflow.test.MockExternalContext; |
| 35 | + |
| 36 | +/** |
| 37 | + * Integration tests for {@link MultiAction}. |
| 38 | + * |
| 39 | + * @author Sam Brannen |
| 40 | + * @since 3.0.1 |
| 41 | + * @see <a href="https://github.com/spring-projects/spring-webflow/issues/1802">gh-1802</a> |
| 42 | + */ |
| 43 | +@SpringJUnitConfig |
| 44 | +@DirtiesContext |
| 45 | +class MultiActionIntegrationTests { |
| 46 | + |
| 47 | + private static final String WITH_REQUEST_CONTEXT = "withRequestContext"; |
| 48 | + |
| 49 | + private static final String WITHOUT_REQUEST_CONTEXT = "withoutRequestContext"; |
| 50 | + |
| 51 | + |
| 52 | + @Autowired |
| 53 | + FlowExecutor flowExecutor; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + CountingMultiAction multiAction; |
| 57 | + |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void resetCounters() { |
| 61 | + multiAction.counterWithRequestContext = 0; |
| 62 | + multiAction.counterWithoutRequestContext = 0; |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void spelExpressionsWithRequestContext() { |
| 67 | + assertCounters(0, 0); |
| 68 | + launchFlowExecution(WITH_REQUEST_CONTEXT); |
| 69 | + assertCounters(2, 0); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void spelExpressionsWithoutRequestContext() { |
| 74 | + assertCounters(0, 0); |
| 75 | + launchFlowExecution(WITHOUT_REQUEST_CONTEXT); |
| 76 | + assertCounters(0, 2); |
| 77 | + } |
| 78 | + |
| 79 | + private void launchFlowExecution(String flowId) { |
| 80 | + flowExecutor.launchExecution(flowId, null, new MockExternalContext()); |
| 81 | + } |
| 82 | + |
| 83 | + private void assertCounters(int counterWithRequestContext, int counterWithoutRequestContext) { |
| 84 | + assertEquals(counterWithRequestContext, multiAction.counterWithRequestContext, "counterWithRequestContext"); |
| 85 | + assertEquals(counterWithoutRequestContext, multiAction.counterWithoutRequestContext, "counterWithoutRequestContext"); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + @Configuration |
| 91 | + static class WebFlowConfig extends AbstractFlowConfiguration { |
| 92 | + |
| 93 | + @Bean |
| 94 | + CountingMultiAction countingMultiAction() { |
| 95 | + return new CountingMultiAction(); |
| 96 | + } |
| 97 | + |
| 98 | + @Bean |
| 99 | + FlowExecutor flowExecutor() { |
| 100 | + return getFlowExecutorBuilder(flowRegistry()).build(); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + FlowDefinitionRegistry flowRegistry() { |
| 105 | + return getFlowDefinitionRegistryBuilder() |
| 106 | + .setBasePath("classpath:/org/springframework/webflow/action") |
| 107 | + .addFlowLocation("multi-action-with-request-context.xml", WITH_REQUEST_CONTEXT) |
| 108 | + .addFlowLocation("multi-action-without-request-context.xml", WITHOUT_REQUEST_CONTEXT) |
| 109 | + .build(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Component("countingMultiAction") |
| 114 | + static class CountingMultiAction extends MultiAction { |
| 115 | + |
| 116 | + int counterWithRequestContext = 0; |
| 117 | + |
| 118 | + int counterWithoutRequestContext = 0; |
| 119 | + |
| 120 | + public Event incrementWithRequestContext(RequestContext context) { |
| 121 | + counterWithRequestContext++; |
| 122 | + return success(); |
| 123 | + } |
| 124 | + |
| 125 | + public Event incrementWithoutRequestContext() { |
| 126 | + counterWithoutRequestContext++; |
| 127 | + return success(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments