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

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -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() { public List<RequestCondition<?>> getConditions() {
return unwrap(); return unwrap();

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -49,7 +48,6 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
private final static ConsumesRequestCondition PRE_FLIGHT_MATCH = new ConsumesRequestCondition(); private final static ConsumesRequestCondition PRE_FLIGHT_MATCH = new ConsumesRequestCondition();
private final List<ConsumeMediaTypeExpression> expressions; private final List<ConsumeMediaTypeExpression> expressions;
@ -159,7 +157,7 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
* @param request the current request * @param request the current request
* @return the same instance if the condition contains no expressions; * @return the same instance if the condition contains no expressions;
* or a new condition with matching expressions only; * or a new condition with matching expressions only;
* or {@code null} if no expressions match. * or {@code null} if no expressions match
*/ */
@Override @Override
@Nullable @Nullable
@ -170,23 +168,20 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
if (isEmpty()) { if (isEmpty()) {
return this; return this;
} }
MediaType contentType; MediaType contentType;
try { try {
contentType = StringUtils.hasLength(request.getContentType()) ? contentType = (StringUtils.hasLength(request.getContentType()) ?
MediaType.parseMediaType(request.getContentType()) : MediaType.parseMediaType(request.getContentType()) :
MediaType.APPLICATION_OCTET_STREAM; MediaType.APPLICATION_OCTET_STREAM);
} }
catch (InvalidMediaTypeException ex) { catch (InvalidMediaTypeException ex) {
return null; return null;
} }
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(this.expressions); Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
for (Iterator<ConsumeMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) { result.removeIf(expression -> !expression.match(contentType));
ConsumeMediaTypeExpression expression = iterator.next(); return (!result.isEmpty() ? new ConsumesRequestCondition(result) : null);
if (!expression.match(contentType)) {
iterator.remove();
}
}
return (result.isEmpty()) ? null : new ConsumesRequestCondition(result);
} }
/** /**

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -48,14 +47,14 @@ import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.Hea
*/ */
public final class ProducesRequestCondition extends AbstractRequestCondition<ProducesRequestCondition> { 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 ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition();
private static final List<ProduceMediaTypeExpression> MEDIA_TYPE_ALL_LIST = private static final List<ProduceMediaTypeExpression> MEDIA_TYPE_ALL_LIST =
Collections.singletonList(new ProduceMediaTypeExpression("*/*")); Collections.singletonList(new ProduceMediaTypeExpression("*/*"));
private final List<ProduceMediaTypeExpression> expressions; private final List<ProduceMediaTypeExpression> expressions;
private final ContentNegotiationManager contentNegotiationManager; private final ContentNegotiationManager contentNegotiationManager;
@ -194,6 +193,7 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
if (isEmpty()) { if (isEmpty()) {
return this; return this;
} }
List<MediaType> acceptedMediaTypes; List<MediaType> acceptedMediaTypes;
try { try {
acceptedMediaTypes = getAcceptedMediaTypes(request); acceptedMediaTypes = getAcceptedMediaTypes(request);
@ -201,13 +201,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
catch (HttpMediaTypeException ex) { catch (HttpMediaTypeException ex) {
return null; return null;
} }
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(expressions);
for (Iterator<ProduceMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) { Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
ProduceMediaTypeExpression expression = iterator.next(); result.removeIf(expression -> !expression.match(acceptedMediaTypes));
if (!expression.match(acceptedMediaTypes)) {
iterator.remove();
}
}
if (!result.isEmpty()) { if (!result.isEmpty()) {
return new ProducesRequestCondition(result, this.contentNegotiationManager); return new ProducesRequestCondition(result, this.contentNegotiationManager);
} }

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -112,6 +112,7 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
return (start > 0 ? (System.currentTimeMillis() - start) : 0); return (start > 0 ? (System.currentTimeMillis() - start) : 0);
} }
@Override @Override
public void sendMessage(WebSocketMessage<?> message) throws IOException { public void sendMessage(WebSocketMessage<?> message) throws IOException {
if (shouldNotSend()) { if (shouldNotSend()) {
@ -124,10 +125,9 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
do { do {
if (!tryFlushMessageBuffer()) { if (!tryFlushMessageBuffer()) {
if (logger.isTraceEnabled()) { 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", "session id '%s':, \"in-progress\" send time %d (ms), buffer size %d bytes",
getId(), getTimeSinceSendStarted(), getBufferSize()); getId(), getTimeSinceSendStarted(), getBufferSize()));
logger.trace(text);
} }
checkSessionLimits(); checkSessionLimits();
break; break;

Loading…
Cancel
Save