Added checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-04 15:55:35 +01:00
parent c6d238085f
commit a8cbf77794
362 changed files with 8885 additions and 6508 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 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.
@@ -31,6 +31,9 @@ public interface MessageCollector {
/**
* Obtain a queue that will receive messages sent to the given channel.
* @param channel message channel
* @return blocking queue for stored message
*/
BlockingQueue<Message<?>> forChannel(MessageChannel channel);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2019 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.
@@ -32,7 +32,8 @@ public class MessageCollectorAutoConfiguration {
@Bean
public MessageCollector messageCollector(BinderFactory binderFactory) {
return ((TestSupportBinder) binderFactory.getBinder("test", MessageChannel.class)).messageCollector();
return ((TestSupportBinder) binderFactory.getBinder("test", MessageChannel.class))
.messageCollector();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2019 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.
@@ -33,9 +33,12 @@ import org.springframework.core.env.MapPropertySource;
public class TestBinderEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
Map<String, Object> propertiesToAdd = new HashMap<>();
propertiesToAdd.put("spring.cloud.stream.binders.test.defaultCandidate", "false");
environment.getPropertySources().addLast(new MapPropertySource("testBinderConfig", propertiesToAdd));
environment.getPropertySources()
.addLast(new MapPropertySource("testBinderConfig", propertiesToAdd));
}
}

View File

@@ -64,15 +64,16 @@ import org.springframework.util.StringUtils;
* @author Soby Chacko
* @see MessageQueueMatcher
*/
public class TestSupportBinder implements Binder<MessageChannel, ConsumerProperties, ProducerProperties> {
public class TestSupportBinder
implements Binder<MessageChannel, ConsumerProperties, ProducerProperties> {
private final MessageCollectorImpl messageCollector = new MessageCollectorImpl();
private final ConcurrentMap<String, MessageChannel> messageChannels = new ConcurrentHashMap<>();
@Override
public Binding<MessageChannel> bindConsumer(String name, String group, MessageChannel inboundBindTarget,
ConsumerProperties properties) {
public Binding<MessageChannel> bindConsumer(String name, String group,
MessageChannel inboundBindTarget, ConsumerProperties properties) {
return new TestBinding(inboundBindTarget, null);
}
@@ -81,9 +82,10 @@ public class TestSupportBinder implements Binder<MessageChannel, ConsumerPropert
* retrieval and assertion in tests.
*/
@Override
public Binding<MessageChannel> bindProducer(String name, MessageChannel outboundBindTarget,
ProducerProperties properties) {
final BlockingQueue<Message<?>> queue = messageCollector.register(outboundBindTarget, properties.isUseNativeEncoding());
public Binding<MessageChannel> bindProducer(String name,
MessageChannel outboundBindTarget, ProducerProperties properties) {
final BlockingQueue<Message<?>> queue = this.messageCollector
.register(outboundBindTarget, properties.isUseNativeEncoding());
((SubscribableChannel) outboundBindTarget).subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
@@ -91,11 +93,11 @@ public class TestSupportBinder implements Binder<MessageChannel, ConsumerPropert
}
});
this.messageChannels.put(name, outboundBindTarget);
return new TestBinding(outboundBindTarget, messageCollector);
return new TestBinding(outboundBindTarget, this.messageCollector);
}
public MessageCollector messageCollector() {
return messageCollector;
return this.messageCollector;
}
public MessageChannel getChannelForName(String name) {
@@ -111,29 +113,36 @@ public class TestSupportBinder implements Binder<MessageChannel, ConsumerPropert
private final Map<MessageChannel, BlockingQueue<Message<?>>> results = new HashMap<>();
private BlockingQueue<Message<?>> register(MessageChannel channel, boolean useNativeEncoding) {
// we need to add this interceptor to ensure MessageCollector's compatibility with
private BlockingQueue<Message<?>> register(MessageChannel channel,
boolean useNativeEncoding) {
// we need to add this interceptor to ensure MessageCollector's compatibility
// with
// previous versions of SCSt when native encoding is disabled.
if (!useNativeEncoding) {
((AbstractMessageChannel) channel).addInterceptor(new InboundMessageConvertingInterceptor());
((AbstractMessageChannel) channel)
.addInterceptor(new InboundMessageConvertingInterceptor());
}
LinkedBlockingDeque<Message<?>> result = new LinkedBlockingDeque<>();
Assert.isTrue(!results.containsKey(channel), "Channel [" + channel + "] was already bound");
results.put(channel, result);
Assert.isTrue(!this.results.containsKey(channel),
"Channel [" + channel + "] was already bound");
this.results.put(channel, result);
return result;
}
private void unregister(MessageChannel channel) {
Assert.notNull(results.remove(channel),
"Trying to unregister a mapping for an unknown channel [" + channel + "]");
Assert.notNull(this.results.remove(channel),
"Trying to unregister a mapping for an unknown channel [" + channel
+ "]");
}
@Override
public BlockingQueue<Message<?>> forChannel(MessageChannel channel) {
BlockingQueue<Message<?>> queue = results.get(channel);
Assert.notNull(queue, "Channel [" + channel + "] was not bound by " + TestSupportBinder.class);
BlockingQueue<Message<?>> queue = this.results.get(channel);
Assert.notNull(queue, "Channel [" + channel + "] was not bound by "
+ TestSupportBinder.class);
return queue;
}
}
/**
@@ -145,56 +154,80 @@ public class TestSupportBinder implements Binder<MessageChannel, ConsumerPropert
private final MessageCollectorImpl messageCollector;
private TestBinding(MessageChannel target, MessageCollectorImpl messageCollector) {
private TestBinding(MessageChannel target,
MessageCollectorImpl messageCollector) {
this.target = target;
this.messageCollector = messageCollector;
}
@Override
public void unbind() {
if (messageCollector != null) {
messageCollector.unregister(target);
if (this.messageCollector != null) {
this.messageCollector.unregister(this.target);
}
}
}
/**
* This is really an interceptor to maintain MessageCollector's backward compatibility
* with the behavior established in 1.3
* - BINDER_ORIGINAL_CONTENT_TYPE
* - Kryo and Java deserialization
* - byte[] to String conversion
* - etc
* with the behavior established in 1.3 - BINDER_ORIGINAL_CONTENT_TYPE - Kryo and Java
* deserialization - byte[] to String conversion - etc.
*/
private final static class InboundMessageConvertingInterceptor implements ChannelInterceptor {
private final static class InboundMessageConvertingInterceptor
implements ChannelInterceptor {
private final DefaultContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
private final CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory();
/*
* Candidate to go into some utils class
*/
private static boolean equalTypeAndSubType(MimeType m1, MimeType m2) {
return m1 != null && m2 != null && m1.getType().equalsIgnoreCase(m2.getType())
&& m1.getSubtype().equalsIgnoreCase(m2.getSubtype());
}
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
Class<?> targetClass = null;
MessageConverter converter = null;
MimeType contentType = message.getHeaders().containsKey(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)
? MimeType.valueOf(message.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE).toString())
: MimeType.valueOf(contentTypeResolver.resolve(message.getHeaders()).toString());
MimeType contentType = message.getHeaders()
.containsKey(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)
? MimeType.valueOf(message.getHeaders()
.get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)
.toString())
: MimeType.valueOf(this.contentTypeResolver
.resolve(message.getHeaders()).toString());
if (contentType != null){
if (equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentType) ||
equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT, contentType)) {
// for Java and Kryo de-serialization we need to reset the content type
message = MessageBuilder.fromMessage(message).setHeader(MessageHeaders.CONTENT_TYPE, contentType).build();
converter = equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentType)
? converterFactory.getMessageConverterForType(contentType)
: converterFactory.getMessageConverterForAllRegistered();
if (contentType != null) {
if (equalTypeAndSubType(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT,
contentType)
|| equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT,
contentType)) {
// for Java and Kryo de-serialization we need to reset the content
// type
message = MessageBuilder.fromMessage(message)
.setHeader(MessageHeaders.CONTENT_TYPE, contentType).build();
converter = equalTypeAndSubType(
MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT, contentType)
? this.converterFactory
.getMessageConverterForType(contentType)
: this.converterFactory
.getMessageConverterForAllRegistered();
String targetClassName = contentType.getParameter("type");
if (StringUtils.hasText(targetClassName)) {
try {
targetClass = Class.forName(targetClassName, false, Thread.currentThread().getContextClassLoader());
targetClass = Class.forName(targetClassName, false,
Thread.currentThread().getContextClassLoader());
}
catch (Exception e) {
throw new IllegalStateException("Failed to determine class name for contentType: "
+ message.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE), e);
throw new IllegalStateException(
"Failed to determine class name for contentType: "
+ message.getHeaders().get(
BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE),
e);
}
}
}
@@ -202,41 +235,41 @@ public class TestSupportBinder implements Binder<MessageChannel, ConsumerPropert
}
Object payload;
if (converter != null){
Assert.isTrue(!(equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT, contentType) && targetClass == null),
if (converter != null) {
Assert.isTrue(
!(equalTypeAndSubType(MessageConverterUtils.X_JAVA_OBJECT,
contentType) && targetClass == null),
"Cannot deserialize into message since 'contentType` is not "
+ "encoded with the actual target type."
+ "Consider 'application/x-java-object; type=foo.bar.MyClass'");
+ "encoded with the actual target type."
+ "Consider 'application/x-java-object; type=foo.bar.MyClass'");
payload = converter.fromMessage(message, targetClass);
}
else {
MimeType deserializeContentType = contentTypeResolver.resolve(message.getHeaders());
MimeType deserializeContentType = this.contentTypeResolver
.resolve(message.getHeaders());
if (deserializeContentType == null) {
deserializeContentType = contentType;
}
payload = deserializeContentType == null ? message.getPayload() : this.deserializePayload(message.getPayload(), deserializeContentType);
payload = deserializeContentType == null ? message.getPayload() : this
.deserializePayload(message.getPayload(), deserializeContentType);
}
message = MessageBuilder.withPayload(payload)
.copyHeaders(message.getHeaders())
.setHeader(MessageHeaders.CONTENT_TYPE, contentType)
.removeHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)
.build();
.removeHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE).build();
return message;
}
private Object deserializePayload(Object payload, MimeType contentType) {
if (payload instanceof byte[] && ("text".equalsIgnoreCase(contentType.getType()) ||
equalTypeAndSubType(MimeTypeUtils.APPLICATION_JSON, contentType))) {
payload = new String((byte[])payload, StandardCharsets.UTF_8);
if (payload instanceof byte[]
&& ("text".equalsIgnoreCase(contentType.getType())
|| equalTypeAndSubType(MimeTypeUtils.APPLICATION_JSON,
contentType))) {
payload = new String((byte[]) payload, StandardCharsets.UTF_8);
}
return payload;
}
/*
* Candidate to go into some utils class
*/
private static boolean equalTypeAndSubType(MimeType m1, MimeType m2) {
return m1 != null && m2 != null && m1.getType().equalsIgnoreCase(m2.getType()) && m1.getSubtype().equalsIgnoreCase(m2.getSubtype());
}
}
}

View File

@@ -59,4 +59,5 @@ public class TestSupportBinderAutoConfiguration {
}
};
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2019 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.
@@ -40,7 +40,7 @@ public class TestSupportBinderConfiguration {
@Bean
public Binder<MessageChannel, ?, ?> binder() {
return messageChannelBinder;
return this.messageChannelBinder;
}
}

View File

@@ -57,6 +57,7 @@ import org.springframework.messaging.Message;
* </pre>
* </p>
*
* @param <T> return type
* @author Eric Bottard
* @author Janne Valkealahti
*/
@@ -72,7 +73,8 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
private Map<BlockingQueue<Message<?>>, T> actuallyReceived = new HashMap<>();
public MessageQueueMatcher(Matcher<T> delegate, long timeout, TimeUnit unit, Extractor<Message<?>, T> extractor) {
public MessageQueueMatcher(Matcher<T> delegate, long timeout, TimeUnit unit,
Extractor<Message<?>, T> extractor) {
this.delegate = delegate;
this.timeout = timeout;
this.unit = (unit != null ? unit : TimeUnit.SECONDS);
@@ -80,7 +82,8 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <P> MessageQueueMatcher<P> receivesMessageThat(Matcher<Message<P>> messageMatcher) {
public static <P> MessageQueueMatcher<P> receivesMessageThat(
Matcher<Message<P>> messageMatcher) {
return new MessageQueueMatcher(messageMatcher, 5, TimeUnit.SECONDS,
new Extractor<Message<P>, Message<P>>("a message that ") {
@Override
@@ -91,7 +94,8 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <P> MessageQueueMatcher<P> receivesPayloadThat(Matcher<P> payloadMatcher) {
public static <P> MessageQueueMatcher<P> receivesPayloadThat(
Matcher<P> payloadMatcher) {
return new MessageQueueMatcher(payloadMatcher, 5, TimeUnit.SECONDS,
new Extractor<Message<P>, P>("a message whose payload ") {
@Override
@@ -107,10 +111,10 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
BlockingQueue<Message<?>> queue = (BlockingQueue<Message<?>>) item;
Message<?> received = null;
try {
if (timeout > 0) {
received = queue.poll(timeout, unit);
if (this.timeout > 0) {
received = queue.poll(this.timeout, this.unit);
}
else if (timeout == 0) {
else if (this.timeout == 0) {
received = queue.poll();
}
else {
@@ -120,21 +124,22 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
catch (InterruptedException e) {
return false;
}
T unwrapped = extractor.apply(received);
actuallyReceived.put(queue, unwrapped);
return delegate.matches(unwrapped);
T unwrapped = this.extractor.apply(received);
this.actuallyReceived.put(queue, unwrapped);
return this.delegate.matches(unwrapped);
}
@Override
public void describeMismatch(Object item, Description description) {
@SuppressWarnings("unchecked")
BlockingQueue<Message<?>> queue = (BlockingQueue<Message<?>>) item;
T value = actuallyReceived.get(queue);
T value = this.actuallyReceived.get(queue);
if (value != null) {
description.appendText("received: ").appendValue(value);
}
else {
description.appendText("timed out after " + timeout + " " + unit.name().toLowerCase());
description.appendText("timed out after " + this.timeout + " "
+ this.unit.name().toLowerCase());
}
}
@@ -152,14 +157,19 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
@Override
public void describeTo(Description description) {
description.appendText("Channel to receive ").appendDescriptionOf(extractor).appendDescriptionOf(delegate);
description.appendText("Channel to receive ").appendDescriptionOf(this.extractor)
.appendDescriptionOf(this.delegate);
}
/**
* A transformation to be applied to a received message before asserting, <i>e.g.</i>
* to only inspect the payload.
*
* @param <R> input type
* @param <T> return type
*/
public static abstract class Extractor<R, T> implements Function<R, T>, SelfDescribing {
public static abstract class Extractor<R, T>
implements Function<R, T>, SelfDescribing {
private final String behaviorDescription;
@@ -169,8 +179,9 @@ public class MessageQueueMatcher<T> extends BaseMatcher<BlockingQueue<Message<?>
@Override
public void describeTo(Description description) {
description.appendText(behaviorDescription);
description.appendText(this.behaviorDescription);
}
}
}