Revisit Assert to avoid single-arg assert methods (with refined messages)

Issue: SPR-15196
This commit is contained in:
Juergen Hoeller
2017-01-30 22:15:53 +01:00
parent 768802fa96
commit 1b2dc3638f
118 changed files with 708 additions and 828 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -28,8 +28,8 @@ import org.springframework.util.ObjectUtils;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;
import org.springframework.web.socket.sockjs.SockJsService;
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
@@ -45,7 +45,7 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
private final TaskScheduler sockJsTaskScheduler;
private MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<>();
private final MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<>();
private HandshakeHandler handshakeHandler;
@@ -63,8 +63,8 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
@Override
public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) {
Assert.notNull(handler);
Assert.notEmpty(paths);
Assert.notNull(handler, "WebSocketHandler must not be null");
Assert.notEmpty(paths, "Paths must not be empty");
this.handlerMap.put(handler, Arrays.asList(paths));
return this;
}
@@ -108,13 +108,14 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
}
if (!this.allowedOrigins.isEmpty()) {
this.sockJsServiceRegistration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
this.sockJsServiceRegistration.setAllowedOrigins(
this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
}
return this.sockJsServiceRegistration;
}
protected HandshakeInterceptor[] getInterceptors() {
List<HandshakeInterceptor> interceptors = new ArrayList<>();
List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
interceptors.addAll(this.interceptors);
interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
@@ -126,7 +127,7 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
for (String path : this.handlerMap.get(wsHandler)) {
String pathPattern = path.endsWith("/") ? path + "**" : path + "/**";
String pathPattern = (path.endsWith("/") ? path + "**" : path + "/**");
addSockJsServiceMapping(mappings, sockJsService, wsHandler, pathPattern);
}
}
@@ -145,9 +146,10 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
}
private HandshakeHandler getOrCreateHandshakeHandler() {
return (this.handshakeHandler != null) ? this.handshakeHandler : new DefaultHandshakeHandler();
return (this.handshakeHandler != null ? this.handshakeHandler : new DefaultHandshakeHandler());
}
protected abstract M createMappings();
protected abstract void addSockJsServiceMapping(M mappings, SockJsService sockJsService,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -60,6 +60,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
return Ordered.LOWEST_PRECEDENCE;
}
// SmartApplicationListener methods
@Override
@@ -69,7 +70,6 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
@Override
public void onApplicationEvent(ApplicationEvent event) {
AbstractSubProtocolEvent subProtocolEvent = (AbstractSubProtocolEvent) event;
Message<?> message = subProtocolEvent.getMessage();
SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
@@ -129,6 +129,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
return true;
}
// SimpUserRegistry methods
@Override
@@ -169,12 +170,10 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
private final String name;
private final Map<String, SimpSession> userSessions =
new ConcurrentHashMap<>(1);
private final Map<String, SimpSession> userSessions = new ConcurrentHashMap<>(1);
public LocalSimpUser(String userName) {
Assert.notNull(userName);
Assert.notNull(userName, "User name must not be null");
this.name = userName;
}
@@ -208,13 +207,8 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof SimpUser)) {
return false;
}
return this.name.equals(((SimpUser) other).getName());
return (this == other ||
(other instanceof SimpUser && this.name.equals(((SimpUser) other).getName())));
}
@Override
@@ -228,6 +222,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
}
}
private static class LocalSimpSession implements SimpSession {
private final String id;
@@ -236,10 +231,9 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
private final Map<String, SimpSubscription> subscriptions = new ConcurrentHashMap<>(4);
public LocalSimpSession(String id, LocalSimpUser user) {
Assert.notNull(id);
Assert.notNull(user);
Assert.notNull(id, "Id must not be null");
Assert.notNull(user, "User must not be null");
this.id = id;
this.user = user;
}
@@ -268,19 +262,14 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
}
@Override
public int hashCode() {
return this.id.hashCode();
public boolean equals(Object other) {
return (this == other ||
(other instanceof SimpSubscription && this.id.equals(((SimpSubscription) other).getId())));
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof SimpSubscription)) {
return false;
}
return this.id.equals(((SimpSubscription) other).getId());
public int hashCode() {
return this.id.hashCode();
}
@Override
@@ -289,6 +278,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
}
}
private static class LocalSimpSubscription implements SimpSubscription {
private final String id;
@@ -297,11 +287,10 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
private final String destination;
public LocalSimpSubscription(String id, String destination, LocalSimpSession session) {
Assert.notNull(id);
Assert.hasText(destination);
Assert.notNull(session);
Assert.notNull(id, "Id must not be null");
Assert.hasText(destination, "Destination must not be empty");
Assert.notNull(session, "Session must not be null");
this.id = id;
this.destination = destination;
this.session = session;
@@ -322,22 +311,22 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
return this.destination;
}
@Override
public int hashCode() {
return 31 * this.id.hashCode() + getSession().hashCode();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof SimpSubscription)) {
if (!(other instanceof SimpSubscription)) {
return false;
}
SimpSubscription otherSubscription = (SimpSubscription) other;
return (getSession().getId().equals(otherSubscription.getSession().getId()) &&
this.id.equals(otherSubscription.getId()));
return (this.id.equals(otherSubscription.getId()) &&
getSession().getId().equals(otherSubscription.getSession().getId()));
}
@Override
public int hashCode() {
return this.id.hashCode() * 31 + getSession().getId().hashCode();
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -174,10 +174,10 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, Serv
String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user,
WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {
Assert.isInstanceOf(ServletServerHttpRequest.class, request);
Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
Assert.isInstanceOf(ServletServerHttpResponse.class, response);
Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
Assert.isTrue(this.factoryAdapter.getFactory().isUpgradeRequest(servletRequest, servletResponse),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -71,12 +71,12 @@ public abstract class AbstractStandardUpgradeStrategy implements RequestUpgradeS
}
protected final HttpServletRequest getHttpServletRequest(ServerHttpRequest request) {
Assert.isTrue(request instanceof ServletServerHttpRequest);
Assert.isTrue(request instanceof ServletServerHttpRequest, "ServletServerHttpRequest required");
return ((ServletServerHttpRequest) request).getServletRequest();
}
protected final HttpServletResponse getHttpServletResponse(ServerHttpResponse response) {
Assert.isTrue(response instanceof ServletServerHttpResponse);
Assert.isTrue(response instanceof ServletServerHttpResponse, "ServletServerHttpResponse required");
return ((ServletServerHttpResponse) response).getServletResponse();
}
@@ -92,8 +92,8 @@ public abstract class AbstractStandardUpgradeStrategy implements RequestUpgradeS
protected List<WebSocketExtension> getInstalledExtensions(WebSocketContainer container) {
List<WebSocketExtension> result = new ArrayList<>();
for (Extension ext : container.getInstalledExtensions()) {
result.add(new StandardToWebSocketExtensionAdapter(ext));
for (Extension extension : container.getInstalledExtensions()) {
result.add(new StandardToWebSocketExtensionAdapter(extension));
}
return result;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -81,7 +81,7 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport {
* time the transports connects.
*/
public void setTaskExecutor(TaskExecutor taskExecutor) {
Assert.notNull(this.taskExecutor);
Assert.notNull(taskExecutor, "TaskExecutor must not be null");
this.taskExecutor = taskExecutor;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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,7 @@ package org.springframework.web.socket.sockjs.client;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -82,7 +82,7 @@ public class WebSocketTransport implements Transport, Lifecycle {
URI url = request.getTransportUrl();
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
if (logger.isDebugEnabled()) {
logger.debug("Starting WebSocket session url=" + url);
logger.debug("Starting WebSocket session on " + url);
}
this.webSocketClient.doHandshake(handler, headers, url).addCallback(
new ListenableFutureCallback<WebSocketSession>() {
@@ -144,16 +144,16 @@ public class WebSocketTransport implements Transport, Lifecycle {
private final WebSocketClientSockJsSession sockJsSession;
private final AtomicInteger connectCount = new AtomicInteger(0);
private final AtomicBoolean connected = new AtomicBoolean(false);
public ClientSockJsWebSocketHandler(WebSocketClientSockJsSession session) {
Assert.notNull(session);
Assert.notNull(session, "Session must not be null");
this.sockJsSession = session;
}
@Override
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
Assert.isTrue(this.connectCount.compareAndSet(0, 1));
Assert.state(this.connected.compareAndSet(false, true), "Already connected");
this.sockJsSession.initializeDelegateSession(webSocketSession);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -162,7 +162,7 @@ public abstract class AbstractSockJsSession implements SockJsSession {
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
Assert.state(!isClosed(), "Cannot send a message when session is closed");
Assert.isInstanceOf(TextMessage.class, message, "SockJS supports text messages only: " + message);
Assert.isInstanceOf(TextMessage.class, message, "SockJS supports text messages only");
sendMessageInternal(((TextMessage) message).getPayload());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -58,7 +58,7 @@ public abstract class StreamingSockJsSession extends AbstractHttpSockJsSession {
boolean initialRequest) throws IOException {
byte[] prelude = getPrelude(request);
Assert.notNull(prelude);
Assert.state(prelude != null, "Prelude expected");
response.getBody().write(prelude);
response.flush();