8 changed files with 65 additions and 50 deletions
@ -1,42 +0,0 @@
@@ -1,42 +0,0 @@
|
||||
package org.springframework.security.access.expression; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.expression.AccessException; |
||||
import org.springframework.expression.EvaluationContext; |
||||
import org.springframework.expression.PropertyAccessor; |
||||
import org.springframework.expression.TypedValue; |
||||
|
||||
/** |
||||
* General property accessor which resolves properties as bean names within an {@code ApplicationContext}. |
||||
*/ |
||||
final class ApplicationContextPropertyAccessor implements PropertyAccessor { |
||||
private final ApplicationContext ctx; |
||||
|
||||
ApplicationContextPropertyAccessor(ApplicationContext ctx) { |
||||
this.ctx = ctx; |
||||
} |
||||
|
||||
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException { |
||||
if (ctx == null) { |
||||
return false; |
||||
} |
||||
|
||||
return ctx.containsBean(name); |
||||
} |
||||
|
||||
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { |
||||
return new TypedValue(ctx.getBean(name)); |
||||
} |
||||
|
||||
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { |
||||
return false; |
||||
} |
||||
|
||||
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { |
||||
} |
||||
|
||||
public Class[] getSpecificTargetClasses() { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
package org.springframework.security.access.expression; |
||||
|
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
import org.junit.*; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.expression.Expression; |
||||
import org.springframework.security.core.Authentication; |
||||
|
||||
import java.util.*; |
||||
|
||||
/** |
||||
* @author Luke Taylor |
||||
*/ |
||||
public class AbstractSecurityExpressionHandlerTests { |
||||
private AbstractSecurityExpressionHandler<Object> handler; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
handler = new AbstractSecurityExpressionHandler<Object>() { |
||||
@Override |
||||
protected SecurityExpressionRoot createSecurityExpressionRoot(Authentication authentication, Object o) { |
||||
return new SecurityExpressionRoot(authentication) {}; |
||||
} |
||||
}; |
||||
} |
||||
|
||||
@Test |
||||
public void beanNamesAreCorrectlyResolved() throws Exception { |
||||
handler.setApplicationContext(new AnnotationConfigApplicationContext(TestConfiguration.class)); |
||||
|
||||
Expression expression = handler.getExpressionParser().parseExpression("@number10.compareTo(@number20) < 0"); |
||||
assertTrue((Boolean) expression.getValue(handler.createEvaluationContext(mock(Authentication.class), new Object()))); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
class TestConfiguration { |
||||
|
||||
@Bean |
||||
Integer number10() { |
||||
return 10; |
||||
} |
||||
|
||||
@Bean |
||||
Integer number20() { |
||||
return 20; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue