mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-12 21:33:30 +00:00
Polish Logging and Tests
Removing debug statements which would have prematurely terminated the stream, changing to AssertJ, and adding another test. Issue: gh-3743
This commit is contained in:
parent
92e68a589a
commit
50d26c9d28
@ -175,21 +175,12 @@ public class DefaultMethodSecurityExpressionHandler extends
|
||||
|
||||
if (filterTarget instanceof Stream) {
|
||||
final Stream<?> original = (Stream<?>) filterTarget;
|
||||
if (debug) {
|
||||
logger.debug("Filtering stream with " + original.count() + " elements");
|
||||
}
|
||||
|
||||
Stream<?> filtered = original.filter(filterObject -> {
|
||||
return original.filter(filterObject -> {
|
||||
rootObject.setFilterObject(filterObject);
|
||||
return ExpressionUtils.evaluateAsBoolean(filterExpression, ctx);
|
||||
})
|
||||
.onClose(original::close);
|
||||
|
||||
if (debug) {
|
||||
logger.debug("Retaining elements: " + filtered.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
|
@ -15,11 +15,13 @@
|
||||
*/
|
||||
package org.springframework.security.access.expression.method;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -85,9 +87,22 @@ public class DefaultMethodSecurityExpressionHandlerTests {
|
||||
|
||||
Object filtered = handler.filter(stream, expression, context);
|
||||
|
||||
Assert.assertTrue("response was wrong type", Stream.class.isAssignableFrom(filtered.getClass()));
|
||||
assertThat(filtered).isInstanceOf(Stream.class);
|
||||
List<String> list = ((Stream<String>) filtered).collect(Collectors.toList());
|
||||
Assert.assertEquals(2, list.size());
|
||||
Assert.assertFalse("contains filtered element", list.contains("2"));
|
||||
assertThat(list).containsExactly("1", "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterStreamWhenClosedThenUpstreamGetsClosed() {
|
||||
final Stream<?> upstream = mock(Stream.class);
|
||||
doReturn(Stream.<String>empty()).when(upstream).filter(any());
|
||||
|
||||
Expression expression = handler.getExpressionParser().parseExpression("true");
|
||||
|
||||
EvaluationContext context = handler.createEvaluationContext(authentication,
|
||||
methodInvocation);
|
||||
|
||||
((Stream) handler.filter(upstream, expression, context)).close();
|
||||
verify(upstream).close();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user