Merge branch '5.3.x'

This commit is contained in:
rstoyanchev 2022-06-08 10:43:36 +01:00
commit 875ee546e5
6 changed files with 247 additions and 136 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@ -22,6 +22,7 @@ import java.util.function.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.NoOpLog;
/**
* Implementation of {@link Log} that wraps a list of loggers and delegates
* to the first one for which logging is enabled at the given level.
@ -35,35 +36,114 @@ final class CompositeLog implements Log {
private static final Log NO_OP_LOG = new NoOpLog();
private final Log fatalLogger;
private final Log errorLogger;
private final Log warnLogger;
private final Log infoLogger;
private final Log debugLogger;
private final Log traceLogger;
private final List<Log> loggers;
/**
* Constructor with list of loggers. For optimal performance, the constructor
* checks and remembers which logger is on for each log category.
* Package-private constructor with list of loggers.
* @param loggers the loggers to use
*/
public CompositeLog(List<Log> loggers) {
this.fatalLogger = initLogger(loggers, Log::isFatalEnabled);
this.errorLogger = initLogger(loggers, Log::isErrorEnabled);
this.warnLogger = initLogger(loggers, Log::isWarnEnabled);
this.infoLogger = initLogger(loggers, Log::isInfoEnabled);
this.debugLogger = initLogger(loggers, Log::isDebugEnabled);
this.traceLogger = initLogger(loggers, Log::isTraceEnabled);
CompositeLog(List<Log> loggers) {
this.loggers = loggers;
}
private static Log initLogger(List<Log> loggers, Predicate<Log> predicate) {
for (Log logger : loggers) {
@Override
public boolean isFatalEnabled() {
return isEnabled(Log::isFatalEnabled);
}
@Override
public boolean isErrorEnabled() {
return isEnabled(Log::isErrorEnabled);
}
@Override
public boolean isWarnEnabled() {
return isEnabled(Log::isWarnEnabled);
}
@Override
public boolean isInfoEnabled() {
return isEnabled(Log::isInfoEnabled);
}
@Override
public boolean isDebugEnabled() {
return isEnabled(Log::isDebugEnabled);
}
@Override
public boolean isTraceEnabled() {
return isEnabled(Log::isTraceEnabled);
}
private boolean isEnabled(Predicate<Log> predicate) {
return (getLogger(predicate) != NO_OP_LOG);
}
@Override
public void fatal(Object message) {
getLogger(Log::isFatalEnabled).fatal(message);
}
@Override
public void fatal(Object message, Throwable ex) {
getLogger(Log::isFatalEnabled).fatal(message, ex);
}
@Override
public void error(Object message) {
getLogger(Log::isErrorEnabled).error(message);
}
@Override
public void error(Object message, Throwable ex) {
getLogger(Log::isErrorEnabled).error(message, ex);
}
@Override
public void warn(Object message) {
getLogger(Log::isWarnEnabled).warn(message);
}
@Override
public void warn(Object message, Throwable ex) {
getLogger(Log::isWarnEnabled).warn(message, ex);
}
@Override
public void info(Object message) {
getLogger(Log::isInfoEnabled).info(message);
}
@Override
public void info(Object message, Throwable ex) {
getLogger(Log::isInfoEnabled).info(message, ex);
}
@Override
public void debug(Object message) {
getLogger(Log::isDebugEnabled).debug(message);
}
@Override
public void debug(Object message, Throwable ex) {
getLogger(Log::isDebugEnabled).debug(message, ex);
}
@Override
public void trace(Object message) {
getLogger(Log::isTraceEnabled).trace(message);
}
@Override
public void trace(Object message, Throwable ex) {
getLogger(Log::isTraceEnabled).trace(message, ex);
}
private Log getLogger(Predicate<Log> predicate) {
for (Log logger : this.loggers) {
if (predicate.test(logger)) {
return logger;
}
@ -71,95 +151,4 @@ final class CompositeLog implements Log {
return NO_OP_LOG;
}
@Override
public boolean isFatalEnabled() {
return (this.fatalLogger != NO_OP_LOG);
}
@Override
public boolean isErrorEnabled() {
return (this.errorLogger != NO_OP_LOG);
}
@Override
public boolean isWarnEnabled() {
return (this.warnLogger != NO_OP_LOG);
}
@Override
public boolean isInfoEnabled() {
return (this.infoLogger != NO_OP_LOG);
}
@Override
public boolean isDebugEnabled() {
return (this.debugLogger != NO_OP_LOG);
}
@Override
public boolean isTraceEnabled() {
return (this.traceLogger != NO_OP_LOG);
}
@Override
public void fatal(Object message) {
this.fatalLogger.fatal(message);
}
@Override
public void fatal(Object message, Throwable ex) {
this.fatalLogger.fatal(message, ex);
}
@Override
public void error(Object message) {
this.errorLogger.error(message);
}
@Override
public void error(Object message, Throwable ex) {
this.errorLogger.error(message, ex);
}
@Override
public void warn(Object message) {
this.warnLogger.warn(message);
}
@Override
public void warn(Object message, Throwable ex) {
this.warnLogger.warn(message, ex);
}
@Override
public void info(Object message) {
this.infoLogger.info(message);
}
@Override
public void info(Object message, Throwable ex) {
this.infoLogger.info(message, ex);
}
@Override
public void debug(Object message) {
this.debugLogger.debug(message);
}
@Override
public void debug(Object message, Throwable ex) {
this.debugLogger.debug(message, ex);
}
@Override
public void trace(Object message) {
this.traceLogger.trace(message);
}
@Override
public void trace(Object message, Throwable ex) {
this.traceLogger.trace(message, ex);
}
}

View File

@ -0,0 +1,86 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.log;
import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.when;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Unit tests for {@link CompositeLog}.
* @author Rossen Stoyanchev
*/
public class CompositeLogTests {
private final Log logger1 = mock(Log.class);
private final Log logger2 = mock(Log.class);
private final CompositeLog compositeLog = new CompositeLog(Arrays.asList(logger1, logger2));
@Test
void useFirstLogger() {
when(logger1.isInfoEnabled()).thenReturn(true);
when(logger2.isInfoEnabled()).thenReturn(true);
this.compositeLog.info("info message");
verify(this.logger1).isInfoEnabled();
verify(this.logger1).info("info message");
verifyNoMoreInteractions(this.logger1);
verifyNoMoreInteractions(this.logger2);
}
@Test
void useSecondLogger() {
when(logger1.isInfoEnabled()).thenReturn(false);
when(logger2.isInfoEnabled()).thenReturn(true);
this.compositeLog.info("info message");
verify(this.logger1).isInfoEnabled();
verify(this.logger2).isInfoEnabled();
verify(this.logger2).info("info message");
verifyNoMoreInteractions(this.logger1);
verifyNoMoreInteractions(this.logger2);
}
@Test
void useNeitherLogger() {
when(logger1.isInfoEnabled()).thenReturn(false);
when(logger2.isInfoEnabled()).thenReturn(false);
this.compositeLog.info("info message");
verify(this.logger1).isInfoEnabled();
verify(this.logger2).isInfoEnabled();
verifyNoMoreInteractions(this.logger1);
verifyNoMoreInteractions(this.logger2);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@ -151,7 +151,7 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
}
// Parse the multipart request in order to adapt to Servlet Part's
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(uri);
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(httpMethod, uri);
Assert.notNull(bytes, "No multipart content");
ReactiveHttpInputMessage inputMessage = MockServerHttpRequest.post(uri.toString())

View File

@ -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.
@ -69,12 +69,8 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
}
/**
* Package-private constructor. Use static factory methods in
* {@link MockMvcRequestBuilders}.
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
* see {@link #with(RequestPostProcessor)} and the
* {@link RequestPostProcessor} extension point.
* @param uri the URL
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
* with a {@link URI}.
* @since 4.0.3
*/
MockMultipartHttpServletRequestBuilder(URI uri) {
@ -82,6 +78,16 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
super.contentType(MediaType.MULTIPART_FORM_DATA);
}
/**
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
* with a {@link URI} and an {@link HttpMethod}.
* @since 5.3.21
*/
MockMultipartHttpServletRequestBuilder(HttpMethod httpMethod, URI uri) {
super(httpMethod, uri);
super.contentType(MediaType.MULTIPART_FORM_DATA);
}
/**
* Create a new MockMultipartFile with the given content.

View File

@ -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.
@ -215,7 +215,7 @@ public abstract class MockMvcRequestBuilders {
}
/**
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
* Variant of {@link #multipart(String, Object...)} with a {@link URI}.
* @param uri the URL
* @since 5.0
*/
@ -223,6 +223,16 @@ public abstract class MockMvcRequestBuilders {
return new MockMultipartHttpServletRequestBuilder(uri);
}
/**
* Variant of {@link #multipart(String, Object...)} with a {@link URI} and
* an {@link HttpMethod}.
* @param httpMethod the HTTP method to use
* @param uri the URL
* @since 5.3.21
*/
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, URI uri) {
return new MockMultipartHttpServletRequestBuilder(httpMethod, uri);
}
/**
* Create a {@link RequestBuilder} for an async dispatch from the

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@ -38,8 +38,8 @@ import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.filter.OncePerRequestFilter;
@ -79,6 +79,18 @@ public class MultipartControllerTests {
MockMvcWebTestClient.resultActionsFor(exchangeResult)
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", json));
// Now try the same with HTTP PUT
exchangeResult = testClient.put().uri("/multipartfile-via-put")
.bodyValue(bodyBuilder.build())
.exchange()
.expectStatus().isFound()
.expectBody().isEmpty();
// Further assertions on the server response
MockMvcWebTestClient.resultActionsFor(exchangeResult)
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", json));
}
@Test
@ -283,10 +295,11 @@ public class MultipartControllerTests {
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Controller
private static class MultipartController {
@RequestMapping(value = "/multipartfile", method = RequestMethod.POST)
@PostMapping("/multipartfile")
public String processMultipartFile(@RequestParam(required = false) MultipartFile file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@ -300,7 +313,14 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/multipartfilearray", method = RequestMethod.POST)
@PutMapping("/multipartfile-via-put")
public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
return processMultipartFile(file, json, model);
}
@PostMapping("/multipartfilearray")
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@ -316,7 +336,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/multipartfilelist", method = RequestMethod.POST)
@PostMapping("/multipartfilelist")
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@ -332,7 +352,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/optionalfile", method = RequestMethod.POST)
@PostMapping("/optionalfile")
public String processOptionalFile(@RequestParam Optional<MultipartFile> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
@ -344,7 +364,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/optionalfilearray", method = RequestMethod.POST)
@PostMapping("/optionalfilearray")
public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
@ -358,7 +378,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/optionalfilelist", method = RequestMethod.POST)
@PostMapping("/optionalfilelist")
public String processOptionalFileList(@RequestParam Optional<List<MultipartFile>> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
@ -372,7 +392,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/part", method = RequestMethod.POST)
@PostMapping("/part")
public String processPart(@RequestParam Part part,
@RequestPart Map<String, String> json, Model model) throws IOException {
@ -382,7 +402,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/json", method = RequestMethod.POST)
@PostMapping("/json")
public String processMultipart(@RequestPart Map<String, String> json, Model model) {
model.addAttribute("json", json);
return "redirect:/index";