Polishing in MappingContentNegotiationStrategy hierarchy and tests

See gh-36925
This commit is contained in:
rstoyanchev
2026-06-22 10:42:51 +01:00
parent ee81785afc
commit cc0ca1b6a5
3 changed files with 34 additions and 44 deletions
@@ -57,12 +57,7 @@ public class MappingMediaTypeFileExtensionResolver implements MediaTypeFileExten
public MappingMediaTypeFileExtensionResolver(@Nullable Map<String, MediaType> mediaTypes) {
if (mediaTypes != null) {
Set<String> allFileExtensions = CollectionUtils.newHashSet(mediaTypes.size());
mediaTypes.forEach((extension, mediaType) -> {
String lowerCaseExtension = extension.toLowerCase(Locale.ROOT);
this.mediaTypes.put(lowerCaseExtension, mediaType);
addFileExtension(mediaType, lowerCaseExtension);
allFileExtensions.add(lowerCaseExtension);
});
mediaTypes.forEach((extension, mediaType) -> addMapping(extension.toLowerCase(Locale.ROOT), mediaType));
this.allFileExtensions.addAll(allFileExtensions);
}
}
@@ -82,16 +77,12 @@ public class MappingMediaTypeFileExtensionResolver implements MediaTypeFileExten
protected void addMapping(String extension, MediaType mediaType) {
MediaType previous = this.mediaTypes.putIfAbsent(extension, mediaType);
if (previous == null) {
addFileExtension(mediaType, extension);
this.fileExtensions.computeIfAbsent(
mediaType, key -> new CopyOnWriteArrayList<>()).add(extension);
this.allFileExtensions.add(extension);
}
}
private void addFileExtension(MediaType mediaType, String extension) {
this.fileExtensions.computeIfAbsent(mediaType, key -> new CopyOnWriteArrayList<>())
.add(extension);
}
@Override
public List<String> resolveFileExtensions(MediaType mediaType) {
@@ -23,9 +23,11 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* A test fixture with a test subclass of AbstractMappingContentNegotiationStrategy.
@@ -36,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class MappingContentNegotiationStrategyTests {
@Test
void resolveMediaTypes() throws Exception {
void resolveMediaTypesFromRegisteredExtensions() throws Exception {
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("json", mapping);
@@ -47,27 +49,7 @@ class MappingContentNegotiationStrategyTests {
}
@Test
void resolveMediaTypesNoMatch() throws Exception {
Map<String, MediaType> mapping = null;
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("blah", mapping);
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
assertThat(mediaTypes).isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
}
@Test
void resolveMediaTypesNoKey() throws Exception {
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy(null, mapping);
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
assertThat(mediaTypes).isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
}
@Test
void resolveMediaTypesHandleNoMatch() throws Exception {
void resolveMediaTypesViaMediaTypeFactory() throws Exception {
Map<String, MediaType> mapping = null;
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("xml", mapping);
@@ -77,6 +59,25 @@ class MappingContentNegotiationStrategyTests {
assertThat(mediaTypes.get(0).toString()).isEqualTo("application/xml");
}
@Test
void resolveMediaTypesUnknownKey() {
Map<String, MediaType> mapping = null;
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("blah", mapping);
assertThatThrownBy(() -> strategy.resolveMediaTypes(null))
.isInstanceOf(HttpMediaTypeNotAcceptableException.class);
}
@Test
void resolveMediaTypesNullKey() throws Exception {
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy(null, mapping);
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
assertThat(mediaTypes).isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
}
private static class TestMappingContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
@@ -92,10 +93,6 @@ class MappingContentNegotiationStrategyTests {
return this.extension;
}
@Override
protected MediaType handleNoMatch(NativeWebRequest request, String mappingKey) {
return "xml".equals(mappingKey) ? MediaType.APPLICATION_XML : null;
}
}
}
@@ -68,13 +68,15 @@ class MappingMediaTypeFileExtensionResolverTests {
}
@Test
void allFileExtensions() {
Map<String, MediaType> mappings = new HashMap<>();
mappings.put("json", MediaType.APPLICATION_JSON);
mappings.put("JsOn", MediaType.APPLICATION_JSON);
mappings.put("jSoN", MediaType.APPLICATION_JSON);
void mappingsAreCaseInsensitive() {
Map<String, MediaType> map = new HashMap<>();
map.put("json", MediaType.APPLICATION_JSON);
map.put("JsOn", MediaType.APPLICATION_JSON);
map.put("jSoN", MediaType.APPLICATION_JSON);
MappingMediaTypeFileExtensionResolver resolver = new MappingMediaTypeFileExtensionResolver(mappings);
MappingMediaTypeFileExtensionResolver resolver = new MappingMediaTypeFileExtensionResolver(map);
assertThat(resolver.getAllFileExtensions()).containsExactly("json");
}
}