Skip to content

Commit 24ab6f6

Browse files
committed
Add FilterRegistration to spring-web textFixtures
See gh-33252
1 parent d2225c2 commit 24ab6f6

File tree

2 files changed

+153
-19
lines changed

2 files changed

+153
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockServletContext.java

+24-19
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public class MockServletContext implements ServletContext {
145145
@Nullable
146146
private String responseCharacterEncoding;
147147

148+
private final Map<String, FilterRegistration> filterRegistrations = new LinkedHashMap<>();
149+
148150
private final Map<String, MediaType> mimeTypes = new LinkedHashMap<>();
149151

150152

@@ -224,6 +226,7 @@ public void registerContext(String contextPath, ServletContext context) {
224226
}
225227

226228
@Override
229+
@Nullable
227230
public ServletContext getContext(String contextPath) {
228231
if (this.contextPath.equals(contextPath)) {
229232
return this;
@@ -376,6 +379,7 @@ public RequestDispatcher getRequestDispatcher(String path) {
376379
}
377380

378381
@Override
382+
@Nullable
379383
public RequestDispatcher getNamedDispatcher(String path) {
380384
return this.namedRequestDispatchers.get(path);
381385
}
@@ -465,6 +469,7 @@ public String getServerInfo() {
465469
}
466470

467471
@Override
472+
@Nullable
468473
public String getInitParameter(String name) {
469474
Assert.notNull(name, "Parameter name must not be null");
470475
return this.initParameters.get(name);
@@ -601,6 +606,25 @@ public String getResponseCharacterEncoding() {
601606
return this.responseCharacterEncoding;
602607
}
603608

609+
/**
610+
* Add a {@link FilterRegistration}.
611+
* @since 6.2
612+
*/
613+
public void addFilterRegistration(FilterRegistration registration) {
614+
this.filterRegistrations.put(registration.getName(), registration);
615+
}
616+
617+
@Override
618+
@Nullable
619+
public FilterRegistration getFilterRegistration(String filterName) {
620+
return this.filterRegistrations.get(filterName);
621+
}
622+
623+
@Override
624+
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
625+
return Collections.unmodifiableMap(this.filterRegistrations);
626+
}
627+
604628

605629
//---------------------------------------------------------------------
606630
// Unsupported Servlet 3.0 registration methods
@@ -675,25 +699,6 @@ public <T extends Filter> T createFilter(Class<T> c) throws ServletException {
675699
throw new UnsupportedOperationException();
676700
}
677701

678-
/**
679-
* This method always returns {@code null}.
680-
* @see jakarta.servlet.ServletContext#getFilterRegistration(java.lang.String)
681-
*/
682-
@Override
683-
@Nullable
684-
public FilterRegistration getFilterRegistration(String filterName) {
685-
return null;
686-
}
687-
688-
/**
689-
* This method always returns an {@linkplain Collections#emptyMap empty map}.
690-
* @see jakarta.servlet.ServletContext#getFilterRegistrations()
691-
*/
692-
@Override
693-
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
694-
return Collections.emptyMap();
695-
}
696-
697702
@Override
698703
public void addListener(Class<? extends EventListener> listenerClass) {
699704
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)