From 66a5742df360cea5799bced763e9ce08828b5af7 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 23 May 2022 10:07:03 +0100 Subject: [PATCH] Polishing --- ...rceHttpRequestHandlerIntegrationTests.java | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java index a55bd6f8434e..ef3e1f610488 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -86,7 +86,7 @@ void cssFile(boolean usePathPatterns, String pathPrefix) throws Exception { } @ParameterizedTest - @MethodSource("argumentSource") + @MethodSource("argumentSource") // gh-26775 void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception { MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo with spaces.css"); MockHttpServletResponse response = new MockHttpServletResponse(); @@ -130,40 +130,35 @@ static class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + ClassPathResource classPathLocation = new ClassPathResource("", getClass()); String path = getPath(classPathLocation); - registerClasspathLocation("/cp/**", classPathLocation, registry); - registerFileSystemLocation("/fs/**", path, registry); - registerUrlLocation("/url/**", "file:" + path, registry); - } - - protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) { - registry.addResourceHandler(pattern).addResourceLocations(resource); - } - - protected void registerFileSystemLocation(String pattern, String path, ResourceHandlerRegistry registry) { - FileSystemResource fileSystemLocation = new FileSystemResource(path); - registry.addResourceHandler(pattern).addResourceLocations(fileSystemLocation); + registry.addResourceHandler("/cp/**").addResourceLocations(classPathLocation); + registry.addResourceHandler("/fs/**").addResourceLocations(new FileSystemResource(path)); + registry.addResourceHandler("/url/**").addResourceLocations(urlResource(path)); } - protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) { + private String getPath(ClassPathResource resource) { try { - UrlResource urlLocation = new UrlResource(path); - registry.addResourceHandler(pattern).addResourceLocations(urlLocation); + return resource.getFile().getCanonicalPath() + .replace('\\', '/') + .replace("classes/java", "resources") + "/"; } - catch (MalformedURLException ex) { + catch (IOException ex) { throw new IllegalStateException(ex); } } - private String getPath(ClassPathResource resource) { + private UrlResource urlResource(String path) { + UrlResource urlResource; try { - return resource.getFile().getCanonicalPath().replace('\\', '/').replace("classes/java", "resources") + "/"; + urlResource = new UrlResource("file:" + path); } - catch (IOException ex) { + catch (MalformedURLException ex) { throw new IllegalStateException(ex); } + return urlResource; } }