Browse Source

Polishing

pull/1658/merge
Juergen Hoeller 7 years ago
parent
commit
bfddbbe731
  1. 3
      spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java
  2. 53
      spring-test/src/main/java/org/springframework/test/context/TestContextManager.java
  3. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.java
  4. 21
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java
  5. 18
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java
  6. 8
      spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java

3
spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@ -161,6 +161,7 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport { @@ -161,6 +161,7 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
return this;
}
@Override
public Object getProperty(String property) {
if (this.definitionWrapper.isReadableProperty(property)) {

53
spring-test/src/main/java/org/springframework/test/context/TestContextManager.java

@ -94,13 +94,8 @@ public class TestContextManager { @@ -94,13 +94,8 @@ public class TestContextManager {
private final TestContext testContext;
private final ThreadLocal<TestContext> testContextHolder = new ThreadLocal<TestContext>() {
@Override
protected TestContext initialValue() {
return copyTestContext(TestContextManager.this.testContext);
}
};
private final ThreadLocal<TestContext> testContextHolder =
ThreadLocal.withInitial(() -> copyTestContext(TestContextManager.this.testContext));
private final List<TestExecutionListener> testExecutionListeners = new ArrayList<>();
@ -364,11 +359,13 @@ public class TestContextManager { @@ -364,11 +359,13 @@ public class TestContextManager {
* @see #getTestExecutionListeners()
* @see Throwable#addSuppressed(Throwable)
*/
public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception {
public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception)
throws Exception {
String callbackName = "afterTestExecution";
prepareForAfterCallback(callbackName, testInstance, testMethod, exception);
Throwable afterTestExecutionException = null;
// Traverse the TestExecutionListeners in reverse order to ensure proper
// "wrapper"-style execution of listeners.
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) {
@ -385,6 +382,7 @@ public class TestContextManager { @@ -385,6 +382,7 @@ public class TestContextManager {
}
}
}
if (afterTestExecutionException != null) {
ReflectionUtils.rethrowException(afterTestExecutionException);
}
@ -414,9 +412,8 @@ public class TestContextManager { @@ -414,9 +412,8 @@ public class TestContextManager {
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which has just been executed on the
* test instance
* @param exception the exception that was thrown during execution of the
* test method or by a TestExecutionListener, or {@code null} if none
* was thrown
* @param exception the exception that was thrown during execution of the test
* method or by a TestExecutionListener, or {@code null} if none was thrown
* @throws Exception if a registered TestExecutionListener throws an exception
* @see #beforeTestMethod
* @see #beforeTestExecution
@ -424,11 +421,13 @@ public class TestContextManager { @@ -424,11 +421,13 @@ public class TestContextManager {
* @see #getTestExecutionListeners()
* @see Throwable#addSuppressed(Throwable)
*/
public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception {
public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception)
throws Exception {
String callbackName = "afterTestMethod";
prepareForAfterCallback(callbackName, testInstance, testMethod, exception);
Throwable afterTestMethodException = null;
// Traverse the TestExecutionListeners in reverse order to ensure proper
// "wrapper"-style execution of listeners.
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) {
@ -445,6 +444,7 @@ public class TestContextManager { @@ -445,6 +444,7 @@ public class TestContextManager {
}
}
}
if (afterTestMethodException != null) {
ReflectionUtils.rethrowException(afterTestMethodException);
}
@ -510,33 +510,36 @@ public class TestContextManager { @@ -510,33 +510,36 @@ public class TestContextManager {
@Nullable Throwable exception) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", callbackName, testInstance,
testMethod, exception));
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]",
callbackName, testInstance, testMethod, exception));
}
getTestContext().updateState(testInstance, testMethod, exception);
}
private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Object testInstance, Method testMethod) throws Exception {
logException(ex, callbackName, testExecutionListener, testInstance, testMethod);
ReflectionUtils.rethrowException(ex);
}
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Class<?> testClass) {
private void logException(
Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " +
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener,
testClass), ex);
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener,
testClass), ex);
}
}
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Object testInstance, Method testMethod) {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " +
"TestExecutionListener [%s] for test method [%s] and test instance [%s]",
callbackName, testExecutionListener, testMethod, testInstance), ex);
"TestExecutionListener [%s] for test method [%s] and test instance [%s]",
callbackName, testExecutionListener, testMethod, testInstance), ex);
}
}
@ -546,8 +549,8 @@ public class TestContextManager { @@ -546,8 +549,8 @@ public class TestContextManager {
* <em>copy constructor</em>.
*/
private static TestContext copyTestContext(TestContext testContext) {
Constructor<? extends TestContext> constructor = ClassUtils.getConstructorIfAvailable(testContext.getClass(),
testContext.getClass());
Constructor<? extends TestContext> constructor =
ClassUtils.getConstructorIfAvailable(testContext.getClass(), testContext.getClass());
if (constructor != null) {
try {
@ -563,7 +566,7 @@ public class TestContextManager { @@ -563,7 +566,7 @@ public class TestContextManager {
}
}
// fallback to original instance
// Fallback to original instance
return testContext;
}

5
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -20,7 +20,6 @@ import java.util.ArrayList; @@ -20,7 +20,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable;
@ -77,7 +76,7 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit @@ -77,7 +76,7 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
}
/**
* Return the underlying conditions, possibly empty but never {@code null}.
* Return the underlying conditions (possibly empty but never {@code null}).
*/
public List<RequestCondition<?>> getConditions() {
return unwrap();

21
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.condition; @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.condition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@ -49,7 +48,6 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con @@ -49,7 +48,6 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
private final static ConsumesRequestCondition PRE_FLIGHT_MATCH = new ConsumesRequestCondition();
private final List<ConsumeMediaTypeExpression> expressions;
@ -159,7 +157,7 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con @@ -159,7 +157,7 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
* @param request the current request
* @return the same instance if the condition contains no expressions;
* or a new condition with matching expressions only;
* or {@code null} if no expressions match.
* or {@code null} if no expressions match
*/
@Override
@Nullable
@ -170,23 +168,20 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con @@ -170,23 +168,20 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
if (isEmpty()) {
return this;
}
MediaType contentType;
try {
contentType = StringUtils.hasLength(request.getContentType()) ?
contentType = (StringUtils.hasLength(request.getContentType()) ?
MediaType.parseMediaType(request.getContentType()) :
MediaType.APPLICATION_OCTET_STREAM;
MediaType.APPLICATION_OCTET_STREAM);
}
catch (InvalidMediaTypeException ex) {
return null;
}
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
for (Iterator<ConsumeMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) {
ConsumeMediaTypeExpression expression = iterator.next();
if (!expression.match(contentType)) {
iterator.remove();
}
}
return (result.isEmpty()) ? null : new ConsumesRequestCondition(result);
result.removeIf(expression -> !expression.match(contentType));
return (!result.isEmpty() ? new ConsumesRequestCondition(result) : null);
}
/**

18
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.condition; @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.condition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@ -48,14 +47,14 @@ import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.Hea @@ -48,14 +47,14 @@ import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.Hea
*/
public final class ProducesRequestCondition extends AbstractRequestCondition<ProducesRequestCondition> {
private final static ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition();
private static final ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition();
private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition();
private static final List<ProduceMediaTypeExpression> MEDIA_TYPE_ALL_LIST =
Collections.singletonList(new ProduceMediaTypeExpression("*/*"));
private final List<ProduceMediaTypeExpression> expressions;
private final ContentNegotiationManager contentNegotiationManager;
@ -194,6 +193,7 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro @@ -194,6 +193,7 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
if (isEmpty()) {
return this;
}
List<MediaType> acceptedMediaTypes;
try {
acceptedMediaTypes = getAcceptedMediaTypes(request);
@ -201,13 +201,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro @@ -201,13 +201,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
catch (HttpMediaTypeException ex) {
return null;
}
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(expressions);
for (Iterator<ProduceMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) {
ProduceMediaTypeExpression expression = iterator.next();
if (!expression.match(acceptedMediaTypes)) {
iterator.remove();
}
}
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
result.removeIf(expression -> !expression.match(acceptedMediaTypes));
if (!result.isEmpty()) {
return new ProducesRequestCondition(result, this.contentNegotiationManager);
}

8
spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -112,6 +112,7 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat @@ -112,6 +112,7 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
return (start > 0 ? (System.currentTimeMillis() - start) : 0);
}
@Override
public void sendMessage(WebSocketMessage<?> message) throws IOException {
if (shouldNotSend()) {
@ -124,10 +125,9 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat @@ -124,10 +125,9 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
do {
if (!tryFlushMessageBuffer()) {
if (logger.isTraceEnabled()) {
String text = String.format("Another send already in progress: " +
logger.trace(String.format("Another send already in progress: " +
"session id '%s':, \"in-progress\" send time %d (ms), buffer size %d bytes",
getId(), getTimeSinceSendStarted(), getBufferSize());
logger.trace(text);
getId(), getTimeSinceSendStarted(), getBufferSize()));
}
checkSessionLimits();
break;

Loading…
Cancel
Save