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

Remove restricted static imports

Replace static imports with class referenced methods. With the exception
of a few well known static imports, checkstyle restricts the static
imports that a class can use. For example, `asList(...)` would be
replaced with `Arrays.asList(...)`.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-07-27 21:34:26 -07:00
committed by Rob Winch
parent 9a3fa6e812
commit e9130489a6
252 changed files with 2216 additions and 2222 deletions
@@ -15,13 +15,13 @@
*/
package org.springframework.security.messaging.util.matcher;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static java.util.Arrays.asList;
import static org.apache.commons.logging.LogFactory.getLog;
import static org.springframework.util.Assert.notEmpty;
import org.springframework.util.Assert;
/**
* Abstract {@link MessageMatcher} containing multiple {@link MessageMatcher}
@@ -30,7 +30,7 @@ import static org.springframework.util.Assert.notEmpty;
*/
abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
protected final Log LOGGER = getLog(getClass());
protected final Log LOGGER = LogFactory.getLog(getClass());
private final List<MessageMatcher<T>> messageMatchers;
@@ -39,7 +39,7 @@ abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
* @param messageMatchers the {@link MessageMatcher} instances to try
*/
AbstractMessageMatcherComposite(List<MessageMatcher<T>> messageMatchers) {
notEmpty(messageMatchers, "messageMatchers must contain a value");
Assert.notEmpty(messageMatchers, "messageMatchers must contain a value");
if (messageMatchers.contains(null)) {
throw new IllegalArgumentException("messageMatchers cannot contain null values");
}
@@ -53,7 +53,7 @@ abstract class AbstractMessageMatcherComposite<T> implements MessageMatcher<T> {
*/
@SafeVarargs
AbstractMessageMatcherComposite(MessageMatcher<T>... messageMatchers) {
this(asList(messageMatchers));
this(Arrays.asList(messageMatchers));
}
public List<MessageMatcher<T>> getMessageMatchers() {
@@ -31,8 +31,7 @@ import org.springframework.security.messaging.access.intercept.MessageSecurityMe
import org.springframework.security.messaging.util.matcher.MessageMatcher;
import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.springframework.security.messaging.access.expression.ExpressionBasedMessageSecurityMetadataSourceFactory.createExpressionMessageMetadataSource;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
@@ -67,7 +66,8 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
this.matcherToExpression.put(this.matcher1, this.expression1);
this.matcherToExpression.put(this.matcher2, this.expression2);
this.source = createExpressionMessageMetadataSource(this.matcherToExpression);
this.source = ExpressionBasedMessageSecurityMetadataSourceFactory
.createExpressionMessageMetadataSource(this.matcherToExpression);
this.rootObject = new MessageSecurityExpressionRoot(this.authentication, this.message);
}
@@ -81,7 +81,7 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
@Test
public void createExpressionMessageMetadataSourceMatchFirst() {
when(this.matcher1.matches(this.message)).thenReturn(true);
given(this.matcher1.matches(this.message)).willReturn(true);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
@@ -94,7 +94,7 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
@Test
public void createExpressionMessageMetadataSourceMatchSecond() {
when(this.matcher2.matches(this.message)).thenReturn(true);
given(this.matcher2.matches(this.message)).willReturn(true);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
@@ -27,6 +27,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.messaging.Message;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.expression.SecurityExpressionHandler;
@@ -39,9 +40,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.springframework.security.access.AccessDecisionVoter.ACCESS_ABSTAIN;
import static org.springframework.security.access.AccessDecisionVoter.ACCESS_DENIED;
import static org.springframework.security.access.AccessDecisionVoter.ACCESS_GRANTED;
@RunWith(MockitoJUnitRunner.class)
public class MessageExpressionVoterTests {
@@ -79,19 +77,22 @@ public class MessageExpressionVoterTests {
@Test
public void voteGranted() {
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(true);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
}
@Test
public void voteDenied() {
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(false);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_DENIED);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_DENIED);
}
@Test
public void voteAbstain() {
this.attributes = Arrays.<ConfigAttribute>asList(new SecurityConfig("ROLE_USER"));
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_ABSTAIN);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_ABSTAIN);
}
@Test
@@ -126,7 +127,8 @@ public class MessageExpressionVoterTests {
.willReturn(this.evaluationContext);
given(this.expression.getValue(this.evaluationContext, Boolean.class)).willReturn(true);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
verify(this.expressionHandler).createEvaluationContext(this.authentication, this.message);
}
@@ -142,7 +144,8 @@ public class MessageExpressionVoterTests {
given(configAttribute.postProcess(this.evaluationContext, this.message)).willReturn(this.evaluationContext);
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(true);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
verify(configAttribute).postProcess(this.evaluationContext, this.message);
}
@@ -32,7 +32,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.messaging.util.matcher.MessageMatcher;
import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class DefaultMessageSecurityMetadataSourceTests {
@@ -73,14 +73,14 @@ public class DefaultMessageSecurityMetadataSourceTests {
@Test
public void getAttributesFirst() {
when(this.matcher1.matches(this.message)).thenReturn(true);
given(this.matcher1.matches(this.message)).willReturn(true);
assertThat(this.source.getAttributes(this.message)).containsOnly(this.config1);
}
@Test
public void getAttributesSecond() {
when(this.matcher1.matches(this.message)).thenReturn(true);
given(this.matcher1.matches(this.message)).willReturn(true);
assertThat(this.source.getAttributes(this.message)).containsOnly(this.config2);
}
@@ -35,7 +35,6 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.core.context.SecurityContextHolder.clearContext;
@RunWith(MockitoJUnitRunner.class)
public class SecurityContextChannelInterceptorTests {
@@ -69,7 +68,7 @@ public class SecurityContextChannelInterceptorTests {
@After
public void cleanup() {
clearContext();
SecurityContextHolder.clearContext();
}
@Test(expected = IllegalArgumentException.class)
@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.commons.logging.Log;
@@ -54,8 +55,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import static java.util.stream.Collectors.joining;
/**
* NOTE: This class is a replica of the same class in spring-web so it can be used for
* tests in spring-messaging.
@@ -216,13 +215,14 @@ public final class ResolvableMethod {
private String formatMethod() {
return (method().getName() + Arrays.stream(this.method.getParameters()).map(this::formatParameter)
.collect(joining(",\n\t", "(\n\t", "\n)")));
.collect(Collectors.joining(",\n\t", "(\n\t", "\n)")));
}
private String formatParameter(Parameter param) {
Annotation[] anns = param.getAnnotations();
return (anns.length > 0
? Arrays.stream(anns).map(this::formatAnnotation).collect(joining(",", "[", "]")) + " " + param
? Arrays.stream(anns).map(this::formatAnnotation).collect(Collectors.joining(",", "[", "]")) + " "
+ param
: param.toString());
}
@@ -427,8 +427,8 @@ public final class ResolvableMethod {
}
private String formatMethods(Set<Method> methods) {
return "\nMatched:\n"
+ methods.stream().map(Method::toGenericString).collect(joining(",\n\t", "[\n\t", "\n]"));
return "\nMatched:\n" + methods.stream().map(Method::toGenericString)
.collect(Collectors.joining(",\n\t", "[\n\t", "\n]"));
}
public ResolvableMethod mockCall(Consumer<T> invoker) {
@@ -504,7 +504,8 @@ public final class ResolvableMethod {
}
private String formatFilters() {
return this.filters.stream().map(Object::toString).collect(joining(",\n\t\t", "[\n\t\t", "\n\t]"));
return this.filters.stream().map(Object::toString)
.collect(Collectors.joining(",\n\t\t", "[\n\t\t", "\n\t]"));
}
}