|
| 1 | +/* |
| 2 | + * Copyright 2014-2020 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.session.web.http; |
| 18 | + |
| 19 | +import org.aspectj.lang.annotation.AfterReturning; |
| 20 | +import org.aspectj.lang.annotation.Aspect; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.mockito.Mockito; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.context.annotation.EnableAspectJAutoProxy; |
| 28 | +import org.springframework.mock.web.MockFilterChain; |
| 29 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 30 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 31 | +import org.springframework.session.SessionRepository; |
| 32 | +import org.springframework.session.web.http.OncePerRequestFilterAopTests.Config; |
| 33 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 36 | + |
| 37 | +@SpringJUnitConfig(classes = Config.class) |
| 38 | +class OncePerRequestFilterAopTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + void doFilterOnce(@Autowired final OncePerRequestFilter filter) { |
| 42 | + assertThatCode(() -> filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), |
| 43 | + new MockFilterChain())).as("`doFilter` does not throw NPE with the bean is being proxied by Spring AOP") |
| 44 | + .doesNotThrowAnyException(); |
| 45 | + } |
| 46 | + |
| 47 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 48 | + @Configuration |
| 49 | + @EnableAspectJAutoProxy(proxyTargetClass = true) |
| 50 | + @Aspect |
| 51 | + public static class Config { |
| 52 | + |
| 53 | + @Bean |
| 54 | + public SessionRepository sessionRepository() { |
| 55 | + return Mockito.mock(SessionRepository.class); |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public SessionRepositoryFilter filter() { |
| 60 | + return new SessionRepositoryFilter(sessionRepository()); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterReturning("execution(* SessionRepositoryFilter.doFilterInternal(..))") |
| 64 | + public void doInternalFilterPointcut() { |
| 65 | + // no op |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments