1
0
mirror of synced 2026-05-22 13:23:17 +00:00

Always use 'this.' when accessing fields

Apply an Eclipse cleanup rules to ensure that fields are always accessed
using `this.`. This aligns with the style used by Spring Framework and
helps users quickly see the difference between a local and member
variable.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-07-26 11:51:05 -07:00
committed by Rob Winch
parent 6894ff5d12
commit 8866fa6fb0
793 changed files with 8689 additions and 8459 deletions
@@ -41,7 +41,7 @@ public class DefaultMessageSecurityExpressionHandler<T> extends AbstractSecurity
Message<T> invocation) {
MessageSecurityExpressionRoot root = new MessageSecurityExpressionRoot(authentication, invocation);
root.setPermissionEvaluator(getPermissionEvaluator());
root.setTrustResolver(trustResolver);
root.setTrustResolver(this.trustResolver);
root.setRoleHierarchy(getRoleHierarchy());
return root;
}
@@ -52,7 +52,7 @@ class MessageExpressionConfigAttribute implements ConfigAttribute, EvaluationCon
}
Expression getAuthorizeExpression() {
return authorizeExpression;
return this.authorizeExpression;
}
public String getAttribute() {
@@ -61,13 +61,13 @@ class MessageExpressionConfigAttribute implements ConfigAttribute, EvaluationCon
@Override
public String toString() {
return authorizeExpression.getExpressionString();
return this.authorizeExpression.getExpressionString();
}
@Override
public EvaluationContext postProcess(EvaluationContext ctx, Message<?> message) {
if (matcher instanceof SimpDestinationMessageMatcher) {
final Map<String, String> variables = ((SimpDestinationMessageMatcher) matcher)
if (this.matcher instanceof SimpDestinationMessageMatcher) {
final Map<String, String> variables = ((SimpDestinationMessageMatcher) this.matcher)
.extractPathVariables(message);
for (Map.Entry<String, String> entry : variables.entrySet()) {
ctx.setVariable(entry.getKey(), entry.getValue());
@@ -52,7 +52,7 @@ public class MessageExpressionVoter<T> implements AccessDecisionVoter<Message<T>
return ACCESS_ABSTAIN;
}
EvaluationContext ctx = expressionHandler.createEvaluationContext(authentication, message);
EvaluationContext ctx = this.expressionHandler.createEvaluationContext(authentication, message);
ctx = attr.postProcess(ctx, message);
return ExpressionUtils.evaluateAsBoolean(attr.getAuthorizeExpression(), ctx) ? ACCESS_GRANTED : ACCESS_DENIED;
@@ -61,7 +61,7 @@ public final class ChannelSecurityInterceptor extends AbstractSecurityIntercepto
@Override
public SecurityMetadataSource obtainSecurityMetadataSource() {
return metadataSource;
return this.metadataSource;
}
public Message<?> preSend(Message<?> message, MessageChannel channel) {
@@ -52,7 +52,7 @@ public final class DefaultMessageSecurityMetadataSource implements MessageSecuri
@SuppressWarnings({ "rawtypes", "unchecked" })
public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
final Message message = (Message) object;
for (Map.Entry<MessageMatcher<?>, Collection<ConfigAttribute>> entry : messageMap.entrySet()) {
for (Map.Entry<MessageMatcher<?>, Collection<ConfigAttribute>> entry : this.messageMap.entrySet()) {
if (entry.getKey().matches(message)) {
return entry.getValue();
}
@@ -63,7 +63,7 @@ public final class DefaultMessageSecurityMetadataSource implements MessageSecuri
public Collection<ConfigAttribute> getAllConfigAttributes() {
Set<ConfigAttribute> allAttributes = new HashSet<>();
for (Collection<ConfigAttribute> entry : messageMap.values()) {
for (Collection<ConfigAttribute> entry : this.messageMap.values()) {
allAttributes.addAll(entry);
}
@@ -115,7 +115,7 @@ public final class SecurityContextChannelInterceptor extends ChannelInterceptorA
}
contextStack.push(currentContext);
Object user = message.getHeaders().get(authenticationHeaderName);
Object user = message.getHeaders().get(this.authenticationHeaderName);
Authentication authentication;
if ((user instanceof Authentication)) {
@@ -141,7 +141,7 @@ public final class SecurityContextChannelInterceptor extends ChannelInterceptorA
SecurityContext originalContext = contextStack.pop();
try {
if (EMPTY_CONTEXT.equals(originalContext)) {
if (this.EMPTY_CONTEXT.equals(originalContext)) {
SecurityContextHolder.clearContext();
ORIGINAL_CONTEXT.remove();
}
@@ -57,12 +57,12 @@ abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
}
public List<MessageMatcher<T>> getMessageMatchers() {
return messageMatchers;
return this.messageMatchers;
}
@Override
public String toString() {
return getClass().getSimpleName() + "[messageMatchers=" + messageMatchers + "]";
return getClass().getSimpleName() + "[messageMatchers=" + this.messageMatchers + "]";
}
}
@@ -47,15 +47,15 @@ public final class AndMessageMatcher<T> extends AbstractMessageMatcherComposite<
public boolean matches(Message<? extends T> message) {
for (MessageMatcher<T> matcher : getMessageMatchers()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Trying to match using " + matcher);
if (this.LOGGER.isDebugEnabled()) {
this.LOGGER.debug("Trying to match using " + matcher);
}
if (!matcher.matches(message)) {
LOGGER.debug("Did not match");
this.LOGGER.debug("Did not match");
return false;
}
}
LOGGER.debug("All messageMatchers returned true");
this.LOGGER.debug("All messageMatchers returned true");
return true;
}
@@ -47,15 +47,15 @@ public final class OrMessageMatcher<T> extends AbstractMessageMatcherComposite<T
public boolean matches(Message<? extends T> message) {
for (MessageMatcher<T> matcher : getMessageMatchers()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Trying to match using " + matcher);
if (this.LOGGER.isDebugEnabled()) {
this.LOGGER.debug("Trying to match using " + matcher);
}
if (matcher.matches(message)) {
LOGGER.debug("matched");
this.LOGGER.debug("matched");
return true;
}
}
LOGGER.debug("No matches found");
this.LOGGER.debug("No matches found");
return false;
}
@@ -117,27 +117,28 @@ public final class SimpDestinationMessageMatcher implements MessageMatcher<Objec
}
public boolean matches(Message<?> message) {
if (!messageTypeMatcher.matches(message)) {
if (!this.messageTypeMatcher.matches(message)) {
return false;
}
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
return destination != null && matcher.match(pattern, destination);
return destination != null && this.matcher.match(this.pattern, destination);
}
public Map<String, String> extractPathVariables(Message<?> message) {
final String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
return destination != null ? matcher.extractUriTemplateVariables(pattern, destination) : Collections.emptyMap();
return destination != null ? this.matcher.extractUriTemplateVariables(this.pattern, destination)
: Collections.emptyMap();
}
public MessageMatcher<Object> getMessageTypeMatcher() {
return messageTypeMatcher;
return this.messageTypeMatcher;
}
@Override
public String toString() {
return "SimpDestinationMessageMatcher [matcher=" + matcher + ", messageTypeMatcher=" + messageTypeMatcher
+ ", pattern=" + pattern + "]";
return "SimpDestinationMessageMatcher [matcher=" + this.matcher + ", messageTypeMatcher="
+ this.messageTypeMatcher + ", pattern=" + this.pattern + "]";
}
private boolean isTypeWithDestination(SimpMessageType type) {
@@ -49,7 +49,7 @@ public class SimpMessageTypeMatcher implements MessageMatcher<Object> {
MessageHeaders headers = message.getHeaders();
SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
return typeToMatch == messageType;
return this.typeToMatch == messageType;
}
@Override
@@ -73,7 +73,7 @@ public class SimpMessageTypeMatcher implements MessageMatcher<Object> {
@Override
public String toString() {
return "SimpMessageTypeMatcher [typeToMatch=" + typeToMatch + "]";
return "SimpMessageTypeMatcher [typeToMatch=" + this.typeToMatch + "]";
}
}
@@ -42,7 +42,7 @@ public final class CsrfChannelInterceptor extends ChannelInterceptorAdapter {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
if (!matcher.matches(message)) {
if (!this.matcher.matches(message)) {
return message;
}
@@ -54,55 +54,55 @@ public class DefaultMessageSecurityExpressionHandlerTests {
@Before
public void setup() {
handler = new DefaultMessageSecurityExpressionHandler<>();
this.handler = new DefaultMessageSecurityExpressionHandler<>();
message = new GenericMessage<>("");
authentication = new AnonymousAuthenticationToken("key", "anonymous",
this.message = new GenericMessage<>("");
this.authentication = new AnonymousAuthenticationToken("key", "anonymous",
AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
}
// SEC-2705
@Test
public void trustResolverPopulated() {
EvaluationContext context = handler.createEvaluationContext(authentication, message);
Expression expression = handler.getExpressionParser().parseExpression("authenticated");
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("authenticated");
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isFalse();
}
@Test(expected = IllegalArgumentException.class)
public void trustResolverNull() {
handler.setTrustResolver(null);
this.handler.setTrustResolver(null);
}
@Test
public void trustResolverCustom() {
handler.setTrustResolver(trustResolver);
EvaluationContext context = handler.createEvaluationContext(authentication, message);
Expression expression = handler.getExpressionParser().parseExpression("authenticated");
when(trustResolver.isAnonymous(authentication)).thenReturn(false);
this.handler.setTrustResolver(this.trustResolver);
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("authenticated");
when(this.trustResolver.isAnonymous(this.authentication)).thenReturn(false);
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}
@Test
public void roleHierarchy() {
authentication = new TestingAuthenticationToken("admin", "pass", "ROLE_ADMIN");
this.authentication = new TestingAuthenticationToken("admin", "pass", "ROLE_ADMIN");
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
handler.setRoleHierarchy(roleHierarchy);
EvaluationContext context = handler.createEvaluationContext(authentication, message);
Expression expression = handler.getExpressionParser().parseExpression("hasRole('ROLE_USER')");
this.handler.setRoleHierarchy(roleHierarchy);
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("hasRole('ROLE_USER')");
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}
@Test
public void permissionEvaluator() {
handler.setPermissionEvaluator(permissionEvaluator);
EvaluationContext context = handler.createEvaluationContext(authentication, message);
Expression expression = handler.getExpressionParser().parseExpression("hasPermission(message, 'read')");
when(permissionEvaluator.hasPermission(authentication, message, "read")).thenReturn(true);
this.handler.setPermissionEvaluator(this.permissionEvaluator);
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("hasPermission(message, 'read')");
when(this.permissionEvaluator.hasPermission(this.authentication, this.message, "read")).thenReturn(true);
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}
@@ -61,47 +61,47 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
@Before
public void setup() {
expression1 = "permitAll";
expression2 = "denyAll";
matcherToExpression = new LinkedHashMap<>();
matcherToExpression.put(matcher1, expression1);
matcherToExpression.put(matcher2, expression2);
this.expression1 = "permitAll";
this.expression2 = "denyAll";
this.matcherToExpression = new LinkedHashMap<>();
this.matcherToExpression.put(this.matcher1, this.expression1);
this.matcherToExpression.put(this.matcher2, this.expression2);
source = createExpressionMessageMetadataSource(matcherToExpression);
rootObject = new MessageSecurityExpressionRoot(authentication, message);
this.source = createExpressionMessageMetadataSource(this.matcherToExpression);
this.rootObject = new MessageSecurityExpressionRoot(this.authentication, this.message);
}
@Test
public void createExpressionMessageMetadataSourceNoMatch() {
Collection<ConfigAttribute> attrs = source.getAttributes(message);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
assertThat(attrs).isNull();
}
@Test
public void createExpressionMessageMetadataSourceMatchFirst() {
when(matcher1.matches(message)).thenReturn(true);
when(this.matcher1.matches(this.message)).thenReturn(true);
Collection<ConfigAttribute> attrs = source.getAttributes(message);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
assertThat(attrs).hasSize(1);
ConfigAttribute attr = attrs.iterator().next();
assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class);
assertThat(((MessageExpressionConfigAttribute) attr).getAuthorizeExpression().getValue(rootObject))
assertThat(((MessageExpressionConfigAttribute) attr).getAuthorizeExpression().getValue(this.rootObject))
.isEqualTo(true);
}
@Test
public void createExpressionMessageMetadataSourceMatchSecond() {
when(matcher2.matches(message)).thenReturn(true);
when(this.matcher2.matches(this.message)).thenReturn(true);
Collection<ConfigAttribute> attrs = source.getAttributes(message);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
assertThat(attrs).hasSize(1);
ConfigAttribute attr = attrs.iterator().next();
assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class);
assertThat(((MessageExpressionConfigAttribute) attr).getAuthorizeExpression().getValue(rootObject))
assertThat(((MessageExpressionConfigAttribute) attr).getAuthorizeExpression().getValue(this.rootObject))
.isEqualTo(false);
}
@@ -47,34 +47,34 @@ public class MessageExpressionConfigAttributeTests {
@Before
public void setup() {
attribute = new MessageExpressionConfigAttribute(expression, matcher);
this.attribute = new MessageExpressionConfigAttribute(this.expression, this.matcher);
}
@Test(expected = IllegalArgumentException.class)
public void constructorNullExpression() {
new MessageExpressionConfigAttribute(null, matcher);
new MessageExpressionConfigAttribute(null, this.matcher);
}
@Test(expected = IllegalArgumentException.class)
public void constructorNullMatcher() {
new MessageExpressionConfigAttribute(expression, null);
new MessageExpressionConfigAttribute(this.expression, null);
}
@Test
public void getAuthorizeExpression() {
assertThat(attribute.getAuthorizeExpression()).isSameAs(expression);
assertThat(this.attribute.getAuthorizeExpression()).isSameAs(this.expression);
}
@Test
public void getAttribute() {
assertThat(attribute.getAttribute()).isNull();
assertThat(this.attribute.getAttribute()).isNull();
}
@Test
public void toStringUsesExpressionString() {
when(expression.getExpressionString()).thenReturn("toString");
when(this.expression.getExpressionString()).thenReturn("toString");
assertThat(attribute.toString()).isEqualTo(expression.getExpressionString());
assertThat(this.attribute.toString()).isEqualTo(this.expression.getExpressionString());
}
@Test
@@ -84,8 +84,8 @@ public class MessageExpressionConfigAttributeTests {
.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1").build();
EvaluationContext context = mock(EvaluationContext.class);
attribute = new MessageExpressionConfigAttribute(expression, matcher);
attribute.postProcess(context, message);
this.attribute = new MessageExpressionConfigAttribute(this.expression, matcher);
this.attribute.postProcess(context, message);
verify(context).setVariable("topic", "someTopic");
}
@@ -70,77 +70,80 @@ public class MessageExpressionVoterTests {
@Before
public void setup() {
attributes = Arrays.<ConfigAttribute>asList(new MessageExpressionConfigAttribute(expression, matcher));
this.attributes = Arrays
.<ConfigAttribute>asList(new MessageExpressionConfigAttribute(this.expression, this.matcher));
voter = new MessageExpressionVoter();
this.voter = new MessageExpressionVoter();
}
@Test
public void voteGranted() {
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_GRANTED);
when(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
}
@Test
public void voteDenied() {
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(false);
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_DENIED);
when(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(false);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_DENIED);
}
@Test
public void voteAbstain() {
attributes = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_ABSTAIN);
this.attributes = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_ABSTAIN);
}
@Test
public void supportsObjectClassFalse() {
assertThat(voter.supports(Object.class)).isFalse();
assertThat(this.voter.supports(Object.class)).isFalse();
}
@Test
public void supportsMessageClassTrue() {
assertThat(voter.supports(Message.class)).isTrue();
assertThat(this.voter.supports(Message.class)).isTrue();
}
@Test
public void supportsSecurityConfigFalse() {
assertThat(voter.supports(new SecurityConfig("ROLE_USER"))).isFalse();
assertThat(this.voter.supports(new SecurityConfig("ROLE_USER"))).isFalse();
}
@Test
public void supportsMessageExpressionConfigAttributeTrue() {
assertThat(voter.supports(new MessageExpressionConfigAttribute(expression, matcher))).isTrue();
assertThat(this.voter.supports(new MessageExpressionConfigAttribute(this.expression, this.matcher))).isTrue();
}
@Test(expected = IllegalArgumentException.class)
public void setExpressionHandlerNull() {
voter.setExpressionHandler(null);
this.voter.setExpressionHandler(null);
}
@Test
public void customExpressionHandler() {
voter.setExpressionHandler(expressionHandler);
when(expressionHandler.createEvaluationContext(authentication, message)).thenReturn(evaluationContext);
when(expression.getValue(evaluationContext, Boolean.class)).thenReturn(true);
this.voter.setExpressionHandler(this.expressionHandler);
when(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
.thenReturn(this.evaluationContext);
when(this.expression.getValue(this.evaluationContext, Boolean.class)).thenReturn(true);
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_GRANTED);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
verify(expressionHandler).createEvaluationContext(authentication, message);
verify(this.expressionHandler).createEvaluationContext(this.authentication, this.message);
}
@Test
public void postProcessEvaluationContext() {
final MessageExpressionConfigAttribute configAttribute = mock(MessageExpressionConfigAttribute.class);
voter.setExpressionHandler(expressionHandler);
when(expressionHandler.createEvaluationContext(authentication, message)).thenReturn(evaluationContext);
when(configAttribute.getAuthorizeExpression()).thenReturn(expression);
attributes = Arrays.<ConfigAttribute>asList(configAttribute);
when(configAttribute.postProcess(evaluationContext, message)).thenReturn(evaluationContext);
when(expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
this.voter.setExpressionHandler(this.expressionHandler);
when(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
.thenReturn(this.evaluationContext);
when(configAttribute.getAuthorizeExpression()).thenReturn(this.expression);
this.attributes = Arrays.<ConfigAttribute>asList(configAttribute);
when(configAttribute.postProcess(this.evaluationContext, this.message)).thenReturn(this.evaluationContext);
when(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
assertThat(voter.vote(authentication, message, attributes)).isEqualTo(ACCESS_GRANTED);
verify(configAttribute).postProcess(evaluationContext, message);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
verify(configAttribute).postProcess(this.evaluationContext, this.message);
}
}
@@ -72,13 +72,13 @@ public class ChannelSecurityInterceptorTests {
@Before
public void setup() {
attrs = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
interceptor = new ChannelSecurityInterceptor(source);
interceptor.setAccessDecisionManager(accessDecisionManager);
interceptor.setRunAsManager(runAsManager);
this.attrs = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
this.interceptor = new ChannelSecurityInterceptor(this.source);
this.interceptor.setAccessDecisionManager(this.accessDecisionManager);
this.interceptor.setRunAsManager(this.runAsManager);
originalAuth = new TestingAuthenticationToken("user", "pass", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(originalAuth);
this.originalAuth = new TestingAuthenticationToken("user", "pass", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(this.originalAuth);
}
@After
@@ -93,85 +93,87 @@ public class ChannelSecurityInterceptorTests {
@Test
public void getSecureObjectClass() {
assertThat(interceptor.getSecureObjectClass()).isEqualTo(Message.class);
assertThat(this.interceptor.getSecureObjectClass()).isEqualTo(Message.class);
}
@Test
public void obtainSecurityMetadataSource() {
assertThat(interceptor.obtainSecurityMetadataSource()).isEqualTo(source);
assertThat(this.interceptor.obtainSecurityMetadataSource()).isEqualTo(this.source);
}
@Test
public void preSendNullAttributes() {
assertThat(interceptor.preSend(message, channel)).isSameAs(message);
assertThat(this.interceptor.preSend(this.message, this.channel)).isSameAs(this.message);
}
@Test
public void preSendGrant() {
when(source.getAttributes(message)).thenReturn(attrs);
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
Message<?> result = interceptor.preSend(message, channel);
Message<?> result = this.interceptor.preSend(this.message, this.channel);
assertThat(result).isSameAs(message);
assertThat(result).isSameAs(this.message);
}
@Test(expected = AccessDeniedException.class)
public void preSendDeny() {
when(source.getAttributes(message)).thenReturn(attrs);
doThrow(new AccessDeniedException("")).when(accessDecisionManager).decide(any(Authentication.class),
eq(message), eq(attrs));
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
doThrow(new AccessDeniedException("")).when(this.accessDecisionManager).decide(any(Authentication.class),
eq(this.message), eq(this.attrs));
interceptor.preSend(message, channel);
this.interceptor.preSend(this.message, this.channel);
}
@SuppressWarnings("unchecked")
@Test
public void preSendPostSendRunAs() {
when(source.getAttributes(message)).thenReturn(attrs);
when(runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class))).thenReturn(runAs);
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
when(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
.thenReturn(this.runAs);
Message<?> preSend = interceptor.preSend(message, channel);
Message<?> preSend = this.interceptor.preSend(this.message, this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(runAs);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.runAs);
interceptor.postSend(preSend, channel, true);
this.interceptor.postSend(preSend, this.channel, true);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(originalAuth);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.originalAuth);
}
@Test
public void afterSendCompletionNotTokenMessageNoExceptionThrown() {
interceptor.afterSendCompletion(message, channel, true, null);
this.interceptor.afterSendCompletion(this.message, this.channel, true, null);
}
@SuppressWarnings("unchecked")
@Test
public void preSendFinallySendRunAs() {
when(source.getAttributes(message)).thenReturn(attrs);
when(runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class))).thenReturn(runAs);
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
when(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
.thenReturn(this.runAs);
Message<?> preSend = interceptor.preSend(message, channel);
Message<?> preSend = this.interceptor.preSend(this.message, this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(runAs);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.runAs);
interceptor.afterSendCompletion(preSend, channel, true, new RuntimeException());
this.interceptor.afterSendCompletion(preSend, this.channel, true, new RuntimeException());
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(originalAuth);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.originalAuth);
}
@Test
public void preReceive() {
assertThat(interceptor.preReceive(channel)).isTrue();
assertThat(this.interceptor.preReceive(this.channel)).isTrue();
}
@Test
public void postReceive() {
assertThat(interceptor.postReceive(message, channel)).isSameAs(message);
assertThat(this.interceptor.postReceive(this.message, this.channel)).isSameAs(this.message);
}
@Test
public void afterReceiveCompletionNullExceptionNoExceptionThrown() {
interceptor.afterReceiveCompletion(message, channel, null);
this.interceptor.afterReceiveCompletion(this.message, this.channel, null);
}
}
@@ -59,45 +59,45 @@ public class DefaultMessageSecurityMetadataSourceTests {
@Before
public void setup() {
messageMap = new LinkedHashMap<>();
messageMap.put(matcher1, Arrays.<ConfigAttribute>asList(config1));
messageMap.put(matcher2, Arrays.<ConfigAttribute>asList(config2));
this.messageMap = new LinkedHashMap<>();
this.messageMap.put(this.matcher1, Arrays.<ConfigAttribute>asList(this.config1));
this.messageMap.put(this.matcher2, Arrays.<ConfigAttribute>asList(this.config2));
source = new DefaultMessageSecurityMetadataSource(messageMap);
this.source = new DefaultMessageSecurityMetadataSource(this.messageMap);
}
@Test
public void getAttributesNull() {
assertThat(source.getAttributes(message)).isNull();
assertThat(this.source.getAttributes(this.message)).isNull();
}
@Test
public void getAttributesFirst() {
when(matcher1.matches(message)).thenReturn(true);
when(this.matcher1.matches(this.message)).thenReturn(true);
assertThat(source.getAttributes(message)).containsOnly(config1);
assertThat(this.source.getAttributes(this.message)).containsOnly(this.config1);
}
@Test
public void getAttributesSecond() {
when(matcher1.matches(message)).thenReturn(true);
when(this.matcher1.matches(this.message)).thenReturn(true);
assertThat(source.getAttributes(message)).containsOnly(config2);
assertThat(this.source.getAttributes(this.message)).containsOnly(this.config2);
}
@Test
public void getAllConfigAttributes() {
assertThat(source.getAllConfigAttributes()).containsOnly(config1, config2);
assertThat(this.source.getAllConfigAttributes()).containsOnly(this.config1, this.config2);
}
@Test
public void supportsFalse() {
assertThat(source.supports(Object.class)).isFalse();
assertThat(this.source.supports(Object.class)).isFalse();
}
@Test
public void supportsTrue() {
assertThat(source.supports(Message.class)).isTrue();
assertThat(this.source.supports(Message.class)).isTrue();
}
}
@@ -48,7 +48,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
@Before
public void setup() {
resolver = new AuthenticationPrincipalArgumentResolver();
this.resolver = new AuthenticationPrincipalArgumentResolver();
}
@After
@@ -58,59 +58,60 @@ public class AuthenticationPrincipalArgumentResolverTests {
@Test
public void supportsParameterNoAnnotation() {
assertThat(resolver.supportsParameter(showUserNoAnnotation())).isFalse();
assertThat(this.resolver.supportsParameter(showUserNoAnnotation())).isFalse();
}
@Test
public void supportsParameterAnnotation() {
assertThat(resolver.supportsParameter(showUserAnnotationObject())).isTrue();
assertThat(this.resolver.supportsParameter(showUserAnnotationObject())).isTrue();
}
@Test
public void supportsParameterCustomAnnotation() {
assertThat(resolver.supportsParameter(showUserCustomAnnotation())).isTrue();
assertThat(this.resolver.supportsParameter(showUserCustomAnnotation())).isTrue();
}
@Test
public void resolveArgumentNullAuthentication() throws Exception {
assertThat(resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
assertThat(this.resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
}
@Test
public void resolveArgumentNullPrincipal() throws Exception {
setAuthenticationPrincipal(null);
assertThat(resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
assertThat(this.resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
}
@Test
public void resolveArgumentString() throws Exception {
setAuthenticationPrincipal("john");
assertThat(resolver.resolveArgument(showUserAnnotationString(), null)).isEqualTo(expectedPrincipal);
assertThat(this.resolver.resolveArgument(showUserAnnotationString(), null)).isEqualTo(this.expectedPrincipal);
}
@Test
public void resolveArgumentPrincipalStringOnObject() throws Exception {
setAuthenticationPrincipal("john");
assertThat(resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(expectedPrincipal);
assertThat(this.resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(this.expectedPrincipal);
}
@Test
public void resolveArgumentUserDetails() throws Exception {
setAuthenticationPrincipal(new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER")));
assertThat(resolver.resolveArgument(showUserAnnotationUserDetails(), null)).isEqualTo(expectedPrincipal);
assertThat(this.resolver.resolveArgument(showUserAnnotationUserDetails(), null))
.isEqualTo(this.expectedPrincipal);
}
@Test
public void resolveArgumentCustomUserPrincipal() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
assertThat(resolver.resolveArgument(showUserAnnotationCustomUserPrincipal(), null))
.isEqualTo(expectedPrincipal);
assertThat(this.resolver.resolveArgument(showUserAnnotationCustomUserPrincipal(), null))
.isEqualTo(this.expectedPrincipal);
}
@Test
public void resolveArgumentCustomAnnotation() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
assertThat(resolver.resolveArgument(showUserCustomAnnotation(), null)).isEqualTo(expectedPrincipal);
assertThat(this.resolver.resolveArgument(showUserCustomAnnotation(), null)).isEqualTo(this.expectedPrincipal);
}
@Test
@@ -133,25 +134,25 @@ public class AuthenticationPrincipalArgumentResolverTests {
@Test
public void resolveArgumentNullOnInvalidType() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
assertThat(resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
assertThat(this.resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
}
@Test(expected = ClassCastException.class)
public void resolveArgumentErrorOnInvalidType() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
resolver.resolveArgument(showUserAnnotationErrorOnInvalidType(), null);
this.resolver.resolveArgument(showUserAnnotationErrorOnInvalidType(), null);
}
@Test(expected = ClassCastException.class)
public void resolveArgumentCustomserErrorOnInvalidType() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
resolver.resolveArgument(showUserAnnotationCurrentUserErrorOnInvalidType(), null);
this.resolver.resolveArgument(showUserAnnotationCurrentUserErrorOnInvalidType(), null);
}
@Test
public void resolveArgumentObject() throws Exception {
setAuthenticationPrincipal(new Object());
assertThat(resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(expectedPrincipal);
assertThat(this.resolver.resolveArgument(showUserAnnotationObject(), null)).isEqualTo(this.expectedPrincipal);
}
private MethodParameter showUserNoAnnotation() {
@@ -303,7 +304,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
private void setAuthenticationPrincipal(Object principal) {
this.expectedPrincipal = principal;
SecurityContextHolder.getContext()
.setAuthentication(new TestingAuthenticationToken(expectedPrincipal, "password", "ROLE_USER"));
.setAuthentication(new TestingAuthenticationToken(this.expectedPrincipal, "password", "ROLE_USER"));
}
}
@@ -59,12 +59,12 @@ public class SecurityContextChannelInterceptorTests {
@Before
public void setup() {
authentication = new TestingAuthenticationToken("user", "pass", "ROLE_USER");
messageBuilder = MessageBuilder.withPayload("payload");
expectedAnonymous = new AnonymousAuthenticationToken("key", "anonymous",
this.authentication = new TestingAuthenticationToken("user", "pass", "ROLE_USER");
this.messageBuilder = MessageBuilder.withPayload("payload");
this.expectedAnonymous = new AnonymousAuthenticationToken("key", "anonymous",
AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
interceptor = new SecurityContextChannelInterceptor();
this.interceptor = new SecurityContextChannelInterceptor();
}
@After
@@ -80,35 +80,35 @@ public class SecurityContextChannelInterceptorTests {
@Test
public void preSendCustomHeader() {
String headerName = "header";
interceptor = new SecurityContextChannelInterceptor(headerName);
messageBuilder.setHeader(headerName, authentication);
this.interceptor = new SecurityContextChannelInterceptor(headerName);
this.messageBuilder.setHeader(headerName, this.authentication);
interceptor.preSend(messageBuilder.build(), channel);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
@Test
public void preSendUserSet() {
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
interceptor.preSend(messageBuilder.build(), channel);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
@Test(expected = IllegalArgumentException.class)
public void setAnonymousAuthenticationNull() {
interceptor.setAnonymousAuthentication(null);
this.interceptor.setAnonymousAuthentication(null);
}
@Test
public void preSendUsesCustomAnonymous() {
expectedAnonymous = new AnonymousAuthenticationToken("customKey", "customAnonymous",
this.expectedAnonymous = new AnonymousAuthenticationToken("customKey", "customAnonymous",
AuthorityUtils.createAuthorityList("ROLE_CUSTOM"));
interceptor.setAnonymousAuthentication(expectedAnonymous);
this.interceptor.setAnonymousAuthentication(this.expectedAnonymous);
interceptor.preSend(messageBuilder.build(), channel);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@@ -116,9 +116,9 @@ public class SecurityContextChannelInterceptorTests {
// SEC-2845
@Test
public void preSendUserNotAuthentication() {
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, principal);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.principal);
interceptor.preSend(messageBuilder.build(), channel);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@@ -126,7 +126,7 @@ public class SecurityContextChannelInterceptorTests {
// SEC-2845
@Test
public void preSendUserNotSet() {
interceptor.preSend(messageBuilder.build(), channel);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@@ -134,42 +134,42 @@ public class SecurityContextChannelInterceptorTests {
// SEC-2845
@Test
public void preSendUserNotSetCustomAnonymous() {
interceptor.preSend(messageBuilder.build(), channel);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@Test
public void afterSendCompletion() {
SecurityContextHolder.getContext().setAuthentication(authentication);
SecurityContextHolder.getContext().setAuthentication(this.authentication);
interceptor.afterSendCompletion(messageBuilder.build(), channel, true, null);
this.interceptor.afterSendCompletion(this.messageBuilder.build(), this.channel, true, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@Test
public void afterSendCompletionNullAuthentication() {
interceptor.afterSendCompletion(messageBuilder.build(), channel, true, null);
this.interceptor.afterSendCompletion(this.messageBuilder.build(), this.channel, true, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@Test
public void beforeHandleUserSet() {
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
// SEC-2845
@Test
public void beforeHandleUserNotAuthentication() {
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, principal);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.principal);
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertAnonymous();
}
@@ -177,23 +177,23 @@ public class SecurityContextChannelInterceptorTests {
// SEC-2845
@Test
public void beforeHandleUserNotSet() {
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertAnonymous();
}
@Test
public void afterMessageHandledUserNotSet() {
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@Test
public void afterMessageHandled() {
SecurityContextHolder.getContext().setAuthentication(authentication);
SecurityContextHolder.getContext().setAuthentication(this.authentication);
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@@ -204,12 +204,12 @@ public class SecurityContextChannelInterceptorTests {
TestingAuthenticationToken original = new TestingAuthenticationToken("original", "original", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(original);
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication);
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(original);
}
@@ -226,23 +226,23 @@ public class SecurityContextChannelInterceptorTests {
TestingAuthenticationToken origional = new TestingAuthenticationToken("original", "origional", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(origional);
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication);
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
// start send websocket
messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, null);
interceptor.beforeHandle(messageBuilder.build(), channel, handler);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, null);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo(anonymous.getName());
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
// end send websocket
interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(origional);
}
@@ -252,9 +252,9 @@ public class SecurityContextChannelInterceptorTests {
assertThat(currentAuthentication).isInstanceOf(AnonymousAuthenticationToken.class);
AnonymousAuthenticationToken anonymous = (AnonymousAuthenticationToken) currentAuthentication;
assertThat(anonymous.getName()).isEqualTo(expectedAnonymous.getName());
assertThat(anonymous.getAuthorities()).containsOnlyElementsOf(expectedAnonymous.getAuthorities());
assertThat(anonymous.getKeyHash()).isEqualTo(expectedAnonymous.getKeyHash());
assertThat(anonymous.getName()).isEqualTo(this.expectedAnonymous.getName());
assertThat(anonymous.getAuthorities()).containsOnlyElementsOf(this.expectedAnonymous.getAuthorities());
assertThat(anonymous.getKeyHash()).isEqualTo(this.expectedAnonymous.getKeyHash());
}
}
@@ -308,8 +308,8 @@ public final class ResolvableMethod {
@SafeVarargs
public final Builder<T> annotPresent(Class<? extends Annotation>... annotationTypes) {
String message = "annotationPresent=" + Arrays.toString(annotationTypes);
addFilter(message, method -> Arrays.stream(annotationTypes)
.allMatch(annotType -> AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null));
addFilter(message, candidate -> Arrays.stream(annotationTypes)
.allMatch(annotType -> AnnotatedElementUtils.findMergedAnnotation(candidate, annotType) != null));
return this;
}
@@ -319,13 +319,13 @@ public final class ResolvableMethod {
@SafeVarargs
public final Builder<T> annotNotPresent(Class<? extends Annotation>... annotationTypes) {
String message = "annotationNotPresent=" + Arrays.toString(annotationTypes);
addFilter(message, method -> {
addFilter(message, candidate -> {
if (annotationTypes.length != 0) {
return Arrays.stream(annotationTypes).noneMatch(
annotType -> AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null);
annotType -> AnnotatedElementUtils.findMergedAnnotation(candidate, annotType) != null);
}
else {
return method.getAnnotations().length == 0;
return candidate.getAnnotations().length == 0;
}
});
return this;
@@ -76,44 +76,44 @@ public class AndMessageMatcherTests {
@Test
public void matchesSingleTrue() {
when(delegate.matches(message)).thenReturn(true);
matcher = new AndMessageMatcher<>(delegate);
when(this.delegate.matches(this.message)).thenReturn(true);
this.matcher = new AndMessageMatcher<>(this.delegate);
assertThat(matcher.matches(message)).isTrue();
assertThat(this.matcher.matches(this.message)).isTrue();
}
@Test
public void matchesMultiTrue() {
when(delegate.matches(message)).thenReturn(true);
when(delegate2.matches(message)).thenReturn(true);
matcher = new AndMessageMatcher<>(delegate, delegate2);
when(this.delegate.matches(this.message)).thenReturn(true);
when(this.delegate2.matches(this.message)).thenReturn(true);
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
assertThat(matcher.matches(message)).isTrue();
assertThat(this.matcher.matches(this.message)).isTrue();
}
@Test
public void matchesSingleFalse() {
when(delegate.matches(message)).thenReturn(false);
matcher = new AndMessageMatcher<>(delegate);
when(this.delegate.matches(this.message)).thenReturn(false);
this.matcher = new AndMessageMatcher<>(this.delegate);
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(this.message)).isFalse();
}
@Test
public void matchesMultiBothFalse() {
when(delegate.matches(message)).thenReturn(false);
matcher = new AndMessageMatcher<>(delegate, delegate2);
when(this.delegate.matches(this.message)).thenReturn(false);
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(this.message)).isFalse();
}
@Test
public void matchesMultiSingleFalse() {
when(delegate.matches(message)).thenReturn(true);
when(delegate2.matches(message)).thenReturn(false);
matcher = new AndMessageMatcher<>(delegate, delegate2);
when(this.delegate.matches(this.message)).thenReturn(true);
when(this.delegate2.matches(this.message)).thenReturn(false);
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(this.message)).isFalse();
}
}
@@ -76,43 +76,43 @@ public class OrMessageMatcherTests {
@Test
public void matchesSingleTrue() {
when(delegate.matches(message)).thenReturn(true);
matcher = new OrMessageMatcher<>(delegate);
when(this.delegate.matches(this.message)).thenReturn(true);
this.matcher = new OrMessageMatcher<>(this.delegate);
assertThat(matcher.matches(message)).isTrue();
assertThat(this.matcher.matches(this.message)).isTrue();
}
@Test
public void matchesMultiTrue() {
when(delegate.matches(message)).thenReturn(true);
matcher = new OrMessageMatcher<>(delegate, delegate2);
when(this.delegate.matches(this.message)).thenReturn(true);
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
assertThat(matcher.matches(message)).isTrue();
assertThat(this.matcher.matches(this.message)).isTrue();
}
@Test
public void matchesSingleFalse() {
when(delegate.matches(message)).thenReturn(false);
matcher = new OrMessageMatcher<>(delegate);
when(this.delegate.matches(this.message)).thenReturn(false);
this.matcher = new OrMessageMatcher<>(this.delegate);
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(this.message)).isFalse();
}
@Test
public void matchesMultiBothFalse() {
when(delegate.matches(message)).thenReturn(false);
when(delegate2.matches(message)).thenReturn(false);
matcher = new OrMessageMatcher<>(delegate, delegate2);
when(this.delegate.matches(this.message)).thenReturn(false);
when(this.delegate2.matches(this.message)).thenReturn(false);
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(this.message)).isFalse();
}
@Test
public void matchesMultiSingleFalse() {
when(delegate.matches(message)).thenReturn(true);
matcher = new OrMessageMatcher<>(delegate, delegate2);
when(this.delegate.matches(this.message)).thenReturn(true);
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
assertThat(matcher.matches(message)).isTrue();
assertThat(this.matcher.matches(this.message)).isTrue();
}
}
@@ -36,9 +36,9 @@ public class SimpDestinationMessageMatcherTests {
@Before
public void setup() {
messageBuilder = MessageBuilder.withPayload("M");
matcher = new SimpDestinationMessageMatcher("/**");
pathMatcher = new AntPathMatcher();
this.messageBuilder = MessageBuilder.withPayload("M");
this.matcher = new SimpDestinationMessageMatcher("/**");
this.pathMatcher = new AntPathMatcher();
}
@Test(expected = IllegalArgumentException.class)
@@ -52,96 +52,96 @@ public class SimpDestinationMessageMatcherTests {
@Test
public void matchesDoesNotMatchNullDestination() {
assertThat(matcher.matches(messageBuilder.build())).isFalse();
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse();
}
@Test
public void matchesAllWithDestination() {
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
assertThat(matcher.matches(messageBuilder.build())).isTrue();
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesSpecificWithDestination() {
matcher = new SimpDestinationMessageMatcher("/destination/1");
this.matcher = new SimpDestinationMessageMatcher("/destination/1");
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
assertThat(matcher.matches(messageBuilder.build())).isTrue();
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesFalseWithDestination() {
matcher = new SimpDestinationMessageMatcher("/nomatch");
this.matcher = new SimpDestinationMessageMatcher("/nomatch");
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
assertThat(matcher.matches(messageBuilder.build())).isFalse();
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse();
}
@Test
public void matchesFalseMessageTypeNotDisconnectType() {
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", pathMatcher);
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher);
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.DISCONNECT);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.DISCONNECT);
assertThat(matcher.matches(messageBuilder.build())).isFalse();
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse();
}
@Test
public void matchesTrueMessageType() {
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", pathMatcher);
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher);
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
assertThat(matcher.matches(messageBuilder.build())).isTrue();
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesTrueSubscribeType() {
matcher = SimpDestinationMessageMatcher.createSubscribeMatcher("/match", pathMatcher);
this.matcher = SimpDestinationMessageMatcher.createSubscribeMatcher("/match", this.pathMatcher);
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.SUBSCRIBE);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.SUBSCRIBE);
assertThat(matcher.matches(messageBuilder.build())).isTrue();
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesNullMessageType() {
matcher = new SimpDestinationMessageMatcher("/match");
this.matcher = new SimpDestinationMessageMatcher("/match");
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
assertThat(matcher.matches(messageBuilder.build())).isTrue();
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void extractPathVariablesFromDestination() {
matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
this.matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1");
messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
assertThat(matcher.extractPathVariables(messageBuilder.build()).get("topic")).isEqualTo("someTopic");
assertThat(this.matcher.extractPathVariables(this.messageBuilder.build()).get("topic")).isEqualTo("someTopic");
}
@Test
public void extractedVariablesAreEmptyInNullDestination() {
matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
assertThat(matcher.extractPathVariables(messageBuilder.build())).isEmpty();
this.matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
assertThat(this.matcher.extractPathVariables(this.messageBuilder.build())).isEmpty();
}
@Test
public void typeConstructorParameterIsTransmitted() {
matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", pathMatcher);
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher);
MessageMatcher<Object> expectedTypeMatcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE);
assertThat(matcher.getMessageTypeMatcher()).isEqualTo(expectedTypeMatcher);
assertThat(this.matcher.getMessageTypeMatcher()).isEqualTo(expectedTypeMatcher);
}
@@ -31,7 +31,7 @@ public class SimpMessageTypeMatcherTests {
@Before
public void setup() {
matcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE);
this.matcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE);
}
@Test(expected = IllegalArgumentException.class)
@@ -44,7 +44,7 @@ public class SimpMessageTypeMatcherTests {
Message<String> message = MessageBuilder.withPayload("Hi")
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE).build();
assertThat(matcher.matches(message)).isTrue();
assertThat(this.matcher.matches(message)).isTrue();
}
@Test
@@ -52,14 +52,14 @@ public class SimpMessageTypeMatcherTests {
Message<String> message = MessageBuilder.withPayload("Hi")
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.CONNECT).build();
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(message)).isFalse();
}
@Test
public void matchesMessageNullFalse() {
Message<String> message = MessageBuilder.withPayload("Hi").build();
assertThat(matcher.matches(message)).isFalse();
assertThat(this.matcher.matches(message)).isFalse();
}
}
@@ -48,106 +48,106 @@ public class CsrfChannelInterceptorTests {
@Before
public void setup() {
token = new DefaultCsrfToken("header", "param", "token");
interceptor = new CsrfChannelInterceptor();
this.token = new DefaultCsrfToken("header", "param", "token");
this.interceptor = new CsrfChannelInterceptor();
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
messageHeaders.setNativeHeader(token.getHeaderName(), token.getToken());
messageHeaders.setSessionAttributes(new HashMap<>());
messageHeaders.getSessionAttributes().put(CsrfToken.class.getName(), token);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
this.messageHeaders.setNativeHeader(this.token.getHeaderName(), this.token.getToken());
this.messageHeaders.setSessionAttributes(new HashMap<>());
this.messageHeaders.getSessionAttributes().put(CsrfToken.class.getName(), this.token);
}
@Test
public void preSendValidToken() {
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresConnectAck() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresDisconnect() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresDisconnectAck() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresHeartbeat() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresMessage() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresOther() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.OTHER);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.OTHER);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresSubscribe() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresUnsubscribe() {
messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.UNSUBSCRIBE);
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.UNSUBSCRIBE);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = InvalidCsrfTokenException.class)
public void preSendNoToken() {
messageHeaders.removeNativeHeader(token.getHeaderName());
this.messageHeaders.removeNativeHeader(this.token.getHeaderName());
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = InvalidCsrfTokenException.class)
public void preSendInvalidToken() {
messageHeaders.setNativeHeader(token.getHeaderName(), token.getToken() + "invalid");
this.messageHeaders.setNativeHeader(this.token.getHeaderName(), this.token.getToken() + "invalid");
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = MissingCsrfTokenException.class)
public void preSendMissingToken() {
messageHeaders.getSessionAttributes().clear();
this.messageHeaders.getSessionAttributes().clear();
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = MissingCsrfTokenException.class)
public void preSendMissingTokenNullSessionAttributes() {
messageHeaders.setSessionAttributes(null);
this.messageHeaders.setSessionAttributes(null);
interceptor.preSend(message(), channel);
this.interceptor.preSend(message(), this.channel);
}
private Message<String> message() {
Map<String, Object> headersToCopy = messageHeaders.toMap();
Map<String, Object> headersToCopy = this.messageHeaders.toMap();
return MessageBuilder.withPayload("hi").copyHeaders(headersToCopy).build();
}
@@ -56,29 +56,29 @@ public class CsrfTokenHandshakeInterceptorTests {
@Before
public void setup() {
httpRequest = new MockHttpServletRequest();
attributes = new HashMap<>();
request = new ServletServerHttpRequest(httpRequest);
this.httpRequest = new MockHttpServletRequest();
this.attributes = new HashMap<>();
this.request = new ServletServerHttpRequest(this.httpRequest);
interceptor = new CsrfTokenHandshakeInterceptor();
this.interceptor = new CsrfTokenHandshakeInterceptor();
}
@Test
public void beforeHandshakeNoAttribute() throws Exception {
interceptor.beforeHandshake(request, response, wsHandler, attributes);
this.interceptor.beforeHandshake(this.request, this.response, this.wsHandler, this.attributes);
assertThat(attributes).isEmpty();
assertThat(this.attributes).isEmpty();
}
@Test
public void beforeHandshake() throws Exception {
CsrfToken token = new DefaultCsrfToken("header", "param", "token");
httpRequest.setAttribute(CsrfToken.class.getName(), token);
this.httpRequest.setAttribute(CsrfToken.class.getName(), token);
interceptor.beforeHandshake(request, response, wsHandler, attributes);
this.interceptor.beforeHandshake(this.request, this.response, this.wsHandler, this.attributes);
assertThat(attributes.keySet()).containsOnly(CsrfToken.class.getName());
assertThat(attributes.values()).containsOnly(token);
assertThat(this.attributes.keySet()).containsOnly(CsrfToken.class.getName());
assertThat(this.attributes.values()).containsOnly(token);
}
}