|
| 1 | +/* |
| 2 | + * Copyright 2002-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.web.testfixture.servlet; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.EnumSet; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.LinkedHashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import jakarta.servlet.DispatcherType; |
| 31 | +import jakarta.servlet.FilterRegistration; |
| 32 | + |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | + |
| 35 | +/** |
| 36 | + * Mock implementation of {@link FilterRegistration}. |
| 37 | + * |
| 38 | + * @author Rossen Stoyanchev |
| 39 | + * @since 6.2 |
| 40 | + */ |
| 41 | +public class MockFilterRegistration implements FilterRegistration { |
| 42 | + |
| 43 | + private final String name; |
| 44 | + |
| 45 | + private final String className; |
| 46 | + |
| 47 | + private final Map<String, String> initParameters = new LinkedHashMap<>(); |
| 48 | + |
| 49 | + private final List<String> servletNames = new ArrayList<>(); |
| 50 | + |
| 51 | + private final List<String> urlPatterns = new ArrayList<>(); |
| 52 | + |
| 53 | + |
| 54 | + public MockFilterRegistration(String className) { |
| 55 | + this(className, ""); |
| 56 | + } |
| 57 | + |
| 58 | + public MockFilterRegistration(String className, String name) { |
| 59 | + this.name = name; |
| 60 | + this.className = className; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Override |
| 65 | + public String getName() { |
| 66 | + return this.name; |
| 67 | + } |
| 68 | + |
| 69 | + @Nullable |
| 70 | + @Override |
| 71 | + public String getClassName() { |
| 72 | + return this.className; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean setInitParameter(String name, String value) { |
| 77 | + return (this.initParameters.putIfAbsent(name, value) != null); |
| 78 | + } |
| 79 | + |
| 80 | + @Nullable |
| 81 | + @Override |
| 82 | + public String getInitParameter(String name) { |
| 83 | + return this.initParameters.get(name); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Set<String> setInitParameters(Map<String, String> initParameters) { |
| 88 | + Set<String> existingParameterNames = new LinkedHashSet<>(); |
| 89 | + for (Map.Entry<String, String> entry : initParameters.entrySet()) { |
| 90 | + if (this.initParameters.get(entry.getKey()) != null) { |
| 91 | + existingParameterNames.add(entry.getKey()); |
| 92 | + } |
| 93 | + } |
| 94 | + if (existingParameterNames.isEmpty()) { |
| 95 | + this.initParameters.putAll(initParameters); |
| 96 | + } |
| 97 | + return existingParameterNames; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Map<String, String> getInitParameters() { |
| 102 | + return Collections.unmodifiableMap(this.initParameters); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void addMappingForServletNames( |
| 107 | + EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) { |
| 108 | + |
| 109 | + this.servletNames.addAll(Arrays.asList(servletNames)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Collection<String> getServletNameMappings() { |
| 114 | + return Collections.unmodifiableCollection(this.servletNames); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void addMappingForUrlPatterns( |
| 119 | + EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) { |
| 120 | + |
| 121 | + this.urlPatterns.addAll(Arrays.asList(urlPatterns)); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Collection<String> getUrlPatternMappings() { |
| 126 | + return Collections.unmodifiableCollection(this.urlPatterns); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments