|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 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 | + * http://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.boot.web.servlet; |
| 18 | + |
| 19 | +import javax.servlet.Filter; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; |
| 25 | +import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; |
| 26 | +import org.springframework.boot.testutil.MockFilter; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * Integration tests for {@link Filter} registration. |
| 34 | + * |
| 35 | + * @author Andy Wilkinson |
| 36 | + */ |
| 37 | +public class FilterRegistrationIntegrationTests { |
| 38 | + |
| 39 | + private AnnotationConfigEmbeddedWebApplicationContext context; |
| 40 | + |
| 41 | + @After |
| 42 | + public void cleanUp() { |
| 43 | + if (this.context != null) { |
| 44 | + this.context.close(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void normalFiltersAreRegistered() { |
| 50 | + load(FilterConfiguration.class); |
| 51 | + assertThat(this.context.getServletContext().getFilterRegistrations()).hasSize(1); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void scopedTargetFiltersAreNotRegistered() { |
| 56 | + load(ScopedTargetFilterConfiguration.class); |
| 57 | + assertThat(this.context.getServletContext().getFilterRegistrations()).isEmpty(); |
| 58 | + } |
| 59 | + |
| 60 | + private void load(Class<?> configuration) { |
| 61 | + this.context = new AnnotationConfigEmbeddedWebApplicationContext( |
| 62 | + ContainerConfiguration.class, configuration); |
| 63 | + } |
| 64 | + |
| 65 | + @Configuration |
| 66 | + static class ContainerConfiguration { |
| 67 | + |
| 68 | + @Bean |
| 69 | + public TomcatEmbeddedServletContainerFactory servletContainerFactory() { |
| 70 | + return new TomcatEmbeddedServletContainerFactory(0); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + @Configuration |
| 76 | + static class ScopedTargetFilterConfiguration { |
| 77 | + |
| 78 | + @Bean(name = "scopedTarget.myFilter") |
| 79 | + public Filter myFilter() { |
| 80 | + return new MockFilter(); |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @Configuration |
| 86 | + static class FilterConfiguration { |
| 87 | + |
| 88 | + @Bean |
| 89 | + public Filter myFilter() { |
| 90 | + return new MockFilter(); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments