Skip to content

Commit 875ee54

Browse files
committed
Merge branch '5.3.x'
2 parents 90e0bd1 + 8fcc7ab commit 875ee54

File tree

6 files changed

+181
-70
lines changed

6 files changed

+181
-70
lines changed
Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.impl.NoOpLog;
2424

25+
2526
/**
2627
* Implementation of {@link Log} that wraps a list of loggers and delegates
2728
* to the first one for which logging is enabled at the given level.
@@ -35,131 +36,119 @@ final class CompositeLog implements Log {
3536
private static final Log NO_OP_LOG = new NoOpLog();
3637

3738

38-
private final Log fatalLogger;
39-
40-
private final Log errorLogger;
41-
42-
private final Log warnLogger;
43-
44-
private final Log infoLogger;
45-
46-
private final Log debugLogger;
47-
48-
private final Log traceLogger;
39+
private final List<Log> loggers;
4940

5041

5142
/**
52-
* Constructor with list of loggers. For optimal performance, the constructor
53-
* checks and remembers which logger is on for each log category.
43+
* Package-private constructor with list of loggers.
5444
* @param loggers the loggers to use
5545
*/
56-
public CompositeLog(List<Log> loggers) {
57-
this.fatalLogger = initLogger(loggers, Log::isFatalEnabled);
58-
this.errorLogger = initLogger(loggers, Log::isErrorEnabled);
59-
this.warnLogger = initLogger(loggers, Log::isWarnEnabled);
60-
this.infoLogger = initLogger(loggers, Log::isInfoEnabled);
61-
this.debugLogger = initLogger(loggers, Log::isDebugEnabled);
62-
this.traceLogger = initLogger(loggers, Log::isTraceEnabled);
63-
}
64-
65-
private static Log initLogger(List<Log> loggers, Predicate<Log> predicate) {
66-
for (Log logger : loggers) {
67-
if (predicate.test(logger)) {
68-
return logger;
69-
}
70-
}
71-
return NO_OP_LOG;
46+
CompositeLog(List<Log> loggers) {
47+
this.loggers = loggers;
7248
}
7349

7450

7551
@Override
7652
public boolean isFatalEnabled() {
77-
return (this.fatalLogger != NO_OP_LOG);
53+
return isEnabled(Log::isFatalEnabled);
7854
}
7955

8056
@Override
8157
public boolean isErrorEnabled() {
82-
return (this.errorLogger != NO_OP_LOG);
58+
return isEnabled(Log::isErrorEnabled);
8359
}
8460

8561
@Override
8662
public boolean isWarnEnabled() {
87-
return (this.warnLogger != NO_OP_LOG);
63+
return isEnabled(Log::isWarnEnabled);
8864
}
8965

9066
@Override
9167
public boolean isInfoEnabled() {
92-
return (this.infoLogger != NO_OP_LOG);
68+
return isEnabled(Log::isInfoEnabled);
9369
}
9470

9571
@Override
9672
public boolean isDebugEnabled() {
97-
return (this.debugLogger != NO_OP_LOG);
73+
return isEnabled(Log::isDebugEnabled);
9874
}
9975

10076
@Override
10177
public boolean isTraceEnabled() {
102-
return (this.traceLogger != NO_OP_LOG);
78+
return isEnabled(Log::isTraceEnabled);
79+
}
80+
81+
private boolean isEnabled(Predicate<Log> predicate) {
82+
return (getLogger(predicate) != NO_OP_LOG);
10383
}
10484

10585
@Override
10686
public void fatal(Object message) {
107-
this.fatalLogger.fatal(message);
87+
getLogger(Log::isFatalEnabled).fatal(message);
10888
}
10989

11090
@Override
11191
public void fatal(Object message, Throwable ex) {
112-
this.fatalLogger.fatal(message, ex);
92+
getLogger(Log::isFatalEnabled).fatal(message, ex);
11393
}
11494

11595
@Override
11696
public void error(Object message) {
117-
this.errorLogger.error(message);
97+
getLogger(Log::isErrorEnabled).error(message);
11898
}
11999

120100
@Override
121101
public void error(Object message, Throwable ex) {
122-
this.errorLogger.error(message, ex);
102+
getLogger(Log::isErrorEnabled).error(message, ex);
123103
}
124104

125105
@Override
126106
public void warn(Object message) {
127-
this.warnLogger.warn(message);
107+
getLogger(Log::isWarnEnabled).warn(message);
128108
}
129109

130110
@Override
131111
public void warn(Object message, Throwable ex) {
132-
this.warnLogger.warn(message, ex);
112+
getLogger(Log::isWarnEnabled).warn(message, ex);
133113
}
134114

135115
@Override
136116
public void info(Object message) {
137-
this.infoLogger.info(message);
117+
getLogger(Log::isInfoEnabled).info(message);
138118
}
139119

140120
@Override
141121
public void info(Object message, Throwable ex) {
142-
this.infoLogger.info(message, ex);
122+
getLogger(Log::isInfoEnabled).info(message, ex);
143123
}
144124

145125
@Override
146126
public void debug(Object message) {
147-
this.debugLogger.debug(message);
127+
getLogger(Log::isDebugEnabled).debug(message);
148128
}
149129

150130
@Override
151131
public void debug(Object message, Throwable ex) {
152-
this.debugLogger.debug(message, ex);
132+
getLogger(Log::isDebugEnabled).debug(message, ex);
153133
}
154134

155135
@Override
156136
public void trace(Object message) {
157-
this.traceLogger.trace(message);
137+
getLogger(Log::isTraceEnabled).trace(message);
158138
}
159139

160140
@Override
161141
public void trace(Object message, Throwable ex) {
162-
this.traceLogger.trace(message, ex);
142+
getLogger(Log::isTraceEnabled).trace(message, ex);
143+
}
144+
145+
private Log getLogger(Predicate<Log> predicate) {
146+
for (Log logger : this.loggers) {
147+
if (predicate.test(logger)) {
148+
return logger;
149+
}
150+
}
151+
return NO_OP_LOG;
163152
}
164153

165154
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2022 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.core.log;
18+
19+
import java.util.Arrays;
20+
21+
import org.apache.commons.logging.Log;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.mockito.BDDMockito.when;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.verifyNoMoreInteractions;
28+
29+
30+
/**
31+
* Unit tests for {@link CompositeLog}.
32+
* @author Rossen Stoyanchev
33+
*/
34+
public class CompositeLogTests {
35+
36+
private final Log logger1 = mock(Log.class);
37+
38+
private final Log logger2 = mock(Log.class);
39+
40+
private final CompositeLog compositeLog = new CompositeLog(Arrays.asList(logger1, logger2));
41+
42+
43+
@Test
44+
void useFirstLogger() {
45+
when(logger1.isInfoEnabled()).thenReturn(true);
46+
when(logger2.isInfoEnabled()).thenReturn(true);
47+
48+
this.compositeLog.info("info message");
49+
50+
verify(this.logger1).isInfoEnabled();
51+
verify(this.logger1).info("info message");
52+
53+
verifyNoMoreInteractions(this.logger1);
54+
verifyNoMoreInteractions(this.logger2);
55+
}
56+
57+
@Test
58+
void useSecondLogger() {
59+
when(logger1.isInfoEnabled()).thenReturn(false);
60+
when(logger2.isInfoEnabled()).thenReturn(true);
61+
62+
this.compositeLog.info("info message");
63+
64+
verify(this.logger1).isInfoEnabled();
65+
verify(this.logger2).isInfoEnabled();
66+
verify(this.logger2).info("info message");
67+
68+
verifyNoMoreInteractions(this.logger1);
69+
verifyNoMoreInteractions(this.logger2);
70+
}
71+
72+
@Test
73+
void useNeitherLogger() {
74+
when(logger1.isInfoEnabled()).thenReturn(false);
75+
when(logger2.isInfoEnabled()).thenReturn(false);
76+
77+
this.compositeLog.info("info message");
78+
79+
verify(this.logger1).isInfoEnabled();
80+
verify(this.logger2).isInfoEnabled();
81+
82+
verifyNoMoreInteractions(this.logger1);
83+
verifyNoMoreInteractions(this.logger2);
84+
}
85+
86+
}

spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcHttpConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -151,7 +151,7 @@ private MockHttpServletRequestBuilder initRequestBuilder(
151151
}
152152

153153
// Parse the multipart request in order to adapt to Servlet Part's
154-
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(uri);
154+
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(httpMethod, uri);
155155

156156
Assert.notNull(bytes, "No multipart content");
157157
ReactiveHttpInputMessage inputMessage = MockServerHttpRequest.post(uri.toString())

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilder.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,19 +69,25 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
6969
}
7070

7171
/**
72-
* Package-private constructor. Use static factory methods in
73-
* {@link MockMvcRequestBuilders}.
74-
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
75-
* see {@link #with(RequestPostProcessor)} and the
76-
* {@link RequestPostProcessor} extension point.
77-
* @param uri the URL
72+
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
73+
* with a {@link URI}.
7874
* @since 4.0.3
7975
*/
8076
MockMultipartHttpServletRequestBuilder(URI uri) {
8177
super(HttpMethod.POST, uri);
8278
super.contentType(MediaType.MULTIPART_FORM_DATA);
8379
}
8480

81+
/**
82+
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
83+
* with a {@link URI} and an {@link HttpMethod}.
84+
* @since 5.3.21
85+
*/
86+
MockMultipartHttpServletRequestBuilder(HttpMethod httpMethod, URI uri) {
87+
super(httpMethod, uri);
88+
super.contentType(MediaType.MULTIPART_FORM_DATA);
89+
}
90+
8591

8692
/**
8793
* Create a new MockMultipartFile with the given content.

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -215,14 +215,24 @@ public static MockMultipartHttpServletRequestBuilder multipart(String urlTemplat
215215
}
216216

217217
/**
218-
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
218+
* Variant of {@link #multipart(String, Object...)} with a {@link URI}.
219219
* @param uri the URL
220220
* @since 5.0
221221
*/
222222
public static MockMultipartHttpServletRequestBuilder multipart(URI uri) {
223223
return new MockMultipartHttpServletRequestBuilder(uri);
224224
}
225225

226+
/**
227+
* Variant of {@link #multipart(String, Object...)} with a {@link URI} and
228+
* an {@link HttpMethod}.
229+
* @param httpMethod the HTTP method to use
230+
* @param uri the URL
231+
* @since 5.3.21
232+
*/
233+
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, URI uri) {
234+
return new MockMultipartHttpServletRequestBuilder(httpMethod, uri);
235+
}
226236

227237
/**
228238
* Create a {@link RequestBuilder} for an async dispatch from the

0 commit comments

Comments
 (0)