Polishing

(cherry picked from commit 4ef428d)
This commit is contained in:
Juergen Hoeller 2016-08-30 23:57:11 +02:00
parent d8f7347000
commit acbb2544bf
6 changed files with 41 additions and 27 deletions

View File

@ -77,7 +77,7 @@ public class AnnotationDrivenEventListenerTests {
private EventCollector eventCollector; private EventCollector eventCollector;
private CountDownLatch countDownLatch; // 1 call by default private CountDownLatch countDownLatch; // 1 call by default
@After @After
@ -93,16 +93,23 @@ public class AnnotationDrivenEventListenerTests {
load(TestEventListener.class); load(TestEventListener.class);
TestEvent event = new TestEvent(this, "test"); TestEvent event = new TestEvent(this, "test");
TestEventListener listener = this.context.getBean(TestEventListener.class); TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector.assertNoEventReceived(listener); this.eventCollector.assertNoEventReceived(listener);
this.context.publishEvent(event); this.context.publishEvent(event);
this.eventCollector.assertEvent(listener, event); this.eventCollector.assertEvent(listener, event);
this.eventCollector.assertTotalEventsCount(1); this.eventCollector.assertTotalEventsCount(1);
this.eventCollector.clear();
this.context.publishEvent(event);
this.eventCollector.assertEvent(listener, event);
this.eventCollector.assertTotalEventsCount(1);
} }
@Test @Test
public void simpleEventXmlConfig() { public void simpleEventXmlConfig() {
this.context = new ClassPathXmlApplicationContext( this.context = new ClassPathXmlApplicationContext(
"org/springframework/context/event/simple-event-configuration.xml"); "org/springframework/context/event/simple-event-configuration.xml");
TestEvent event = new TestEvent(this, "test"); TestEvent event = new TestEvent(this, "test");
TestEventListener listener = this.context.getBean(TestEventListener.class); TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector = getEventCollector(this.context); this.eventCollector = getEventCollector(this.context);
@ -116,7 +123,6 @@ public class AnnotationDrivenEventListenerTests {
@Test @Test
public void metaAnnotationIsDiscovered() { public void metaAnnotationIsDiscovered() {
load(MetaAnnotationListenerTestBean.class); load(MetaAnnotationListenerTestBean.class);
MetaAnnotationListenerTestBean bean = this.context.getBean(MetaAnnotationListenerTestBean.class); MetaAnnotationListenerTestBean bean = this.context.getBean(MetaAnnotationListenerTestBean.class);
this.eventCollector.assertNoEventReceived(bean); this.eventCollector.assertNoEventReceived(bean);

View File

@ -30,6 +30,7 @@ import static org.junit.Assert.*;
* Test utility to collect and assert events. * Test utility to collect and assert events.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Juergen Hoeller
*/ */
@Component @Component
public class EventCollector { public class EventCollector {
@ -73,7 +74,7 @@ public class EventCollector {
*/ */
public void assertEvent(String listenerId, Object... events) { public void assertEvent(String listenerId, Object... events) {
List<Object> actual = this.content.getOrDefault(listenerId, Collections.emptyList()); List<Object> actual = this.content.getOrDefault(listenerId, Collections.emptyList());
assertEquals("wrong number of events", events.length, actual.size()); assertEquals("Wrong number of events", events.length, actual.size());
for (int i = 0; i < events.length; i++) { for (int i = 0; i < events.length; i++) {
assertEquals("Wrong event at index " + i, events[i], actual.get(i)); assertEquals("Wrong event at index " + i, events[i], actual.get(i));
} }
@ -97,8 +98,15 @@ public class EventCollector {
for (Map.Entry<String, List<Object>> entry : this.content.entrySet()) { for (Map.Entry<String, List<Object>> entry : this.content.entrySet()) {
actual += entry.getValue().size(); actual += entry.getValue().size();
} }
assertEquals("Wrong number of total events (" + this.content.size() + ") " + assertEquals("Wrong number of total events (" + this.content.size() +
"registered listener(s)", number, actual); ") registered listener(s)", number, actual);
}
/**
* Clear the collected events, allowing for reuse of the collector.
*/
public void clear() {
this.content.clear();
} }
} }

View File

@ -31,9 +31,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static java.lang.String.*;
import static org.springframework.util.StringUtils.*;
/** /**
* Abstract base class for {@link Environment} implementations. Supports the notion of * Abstract base class for {@link Environment} implementations. Supports the notion of
* reserved default profile names and enables specifying active and default profiles * reserved default profile names and enables specifying active and default profiles
@ -124,7 +121,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
public AbstractEnvironment() { public AbstractEnvironment() {
customizePropertySources(this.propertySources); customizePropertySources(this.propertySources);
if (this.logger.isDebugEnabled()) { if (this.logger.isDebugEnabled()) {
this.logger.debug(format( this.logger.debug(String.format(
"Initialized %s with PropertySources %s", getClass().getSimpleName(), this.propertySources)); "Initialized %s with PropertySources %s", getClass().getSimpleName(), this.propertySources));
} }
} }
@ -242,7 +239,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
if (this.activeProfiles.isEmpty()) { if (this.activeProfiles.isEmpty()) {
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME); String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
if (StringUtils.hasText(profiles)) { if (StringUtils.hasText(profiles)) {
setActiveProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); setActiveProfiles(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(profiles)));
} }
} }
return this.activeProfiles; return this.activeProfiles;
@ -264,7 +262,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
@Override @Override
public void addActiveProfile(String profile) { public void addActiveProfile(String profile) {
if (this.logger.isDebugEnabled()) { if (this.logger.isDebugEnabled()) {
this.logger.debug(format("Activating profile '%s'", profile)); this.logger.debug(String.format("Activating profile '%s'", profile));
} }
validateProfile(profile); validateProfile(profile);
doGetActiveProfiles(); doGetActiveProfiles();
@ -296,7 +294,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
if (this.defaultProfiles.equals(getReservedDefaultProfiles())) { if (this.defaultProfiles.equals(getReservedDefaultProfiles())) {
String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME); String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
if (StringUtils.hasText(profiles)) { if (StringUtils.hasText(profiles)) {
setDefaultProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); setDefaultProfiles(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(profiles)));
} }
} }
return this.defaultProfiles; return this.defaultProfiles;
@ -393,7 +392,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
} }
catch (AccessControlException ex) { catch (AccessControlException ex) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(format("Caught AccessControlException when accessing system " + logger.info(String.format("Caught AccessControlException when accessing system " +
"environment variable [%s]; its value will be returned [null]. Reason: %s", "environment variable [%s]; its value will be returned [null]. Reason: %s",
attributeName, ex.getMessage())); attributeName, ex.getMessage()));
} }
@ -434,7 +433,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
} }
catch (AccessControlException ex) { catch (AccessControlException ex) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(format("Caught AccessControlException when accessing system " + logger.info(String.format("Caught AccessControlException when accessing system " +
"property [%s]; its value will be returned [null]. Reason: %s", "property [%s]; its value will be returned [null]. Reason: %s",
attributeName, ex.getMessage())); attributeName, ex.getMessage()));
} }
@ -575,7 +574,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
@Override @Override
public String toString() { public String toString() {
return format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}", return String.format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}",
getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles, getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles,
this.propertySources); this.propertySources);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 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.
@ -61,6 +61,7 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
private final FormHttpMessageConverter formConverter = new AllEncompassingFormHttpMessageConverter(); private final FormHttpMessageConverter formConverter = new AllEncompassingFormHttpMessageConverter();
/** /**
* The default character set to use for reading form data. * The default character set to use for reading form data.
*/ */
@ -68,6 +69,7 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
this.formConverter.setCharset(charset); this.formConverter.setCharset(charset);
} }
@Override @Override
protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response, protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException { FilterChain filterChain) throws ServletException, IOException {
@ -104,29 +106,30 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
} }
} }
private static class HttpPutFormContentRequestWrapper extends HttpServletRequestWrapper { private static class HttpPutFormContentRequestWrapper extends HttpServletRequestWrapper {
private MultiValueMap<String, String> formParameters; private MultiValueMap<String, String> formParameters;
public HttpPutFormContentRequestWrapper(HttpServletRequest request, MultiValueMap<String, String> parameters) { public HttpPutFormContentRequestWrapper(HttpServletRequest request, MultiValueMap<String, String> parameters) {
super(request); super(request);
this.formParameters = (parameters != null) ? parameters : new LinkedMultiValueMap<String, String>(); this.formParameters = (parameters != null ? parameters : new LinkedMultiValueMap<String, String>());
} }
@Override @Override
public String getParameter(String name) { public String getParameter(String name) {
String queryStringValue = super.getParameter(name); String queryStringValue = super.getParameter(name);
String formValue = this.formParameters.getFirst(name); String formValue = this.formParameters.getFirst(name);
return (queryStringValue != null) ? queryStringValue : formValue; return (queryStringValue != null ? queryStringValue : formValue);
} }
@Override @Override
public Map<String, String[]> getParameterMap() { public Map<String, String[]> getParameterMap() {
Map<String, String[]> result = new LinkedHashMap<String, String[]>(); Map<String, String[]> result = new LinkedHashMap<String, String[]>();
Enumeration<String> names = this.getParameterNames(); Enumeration<String> names = getParameterNames();
while (names.hasMoreElements()) { while (names.hasMoreElements()) {
String name = names.nextElement(); String name = names.nextElement();
result.put(name, this.getParameterValues(name)); result.put(name, getParameterValues(name));
} }
return result; return result;
} }
@ -150,7 +153,7 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
return formValues.toArray(new String[formValues.size()]); return formValues.toArray(new String[formValues.size()]);
} }
else { else {
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>(queryStringValues.length + formValues.size());
result.addAll(Arrays.asList(queryStringValues)); result.addAll(Arrays.asList(queryStringValues));
result.addAll(formValues); result.addAll(formValues);
return result.toArray(new String[result.size()]); return result.toArray(new String[result.size()]);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 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.
@ -107,8 +107,7 @@ public class ServerEndpointRegistration extends ServerEndpointConfig.Configurato
@Override @Override
public Class<? extends Endpoint> getEndpointClass() { public Class<? extends Endpoint> getEndpointClass() {
return (this.endpoint != null) ? return (this.endpoint != null ? this.endpoint.getClass() : this.endpointProvider.getHandlerType());
this.endpoint.getClass() : ((Class<? extends Endpoint>) this.endpointProvider.getHandlerType());
} }
public Endpoint getEndpoint() { public Endpoint getEndpoint() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -17,7 +17,6 @@
package org.springframework.web.socket.server.standard; package org.springframework.web.socket.server.standard;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -65,7 +64,7 @@ public class TomcatRequestUpgradeStrategy extends AbstractStandardUpgradeStrateg
Map<String, String> pathParams = Collections.<String, String> emptyMap(); Map<String, String> pathParams = Collections.<String, String> emptyMap();
ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint); ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
endpointConfig.setSubprotocols(Arrays.asList(selectedProtocol)); endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
endpointConfig.setExtensions(selectedExtensions); endpointConfig.setExtensions(selectedExtensions);
try { try {