@Test public void createFromObjectLocatesExistingMethods() throws Exception { AdvisedTarget t = new AdvisedTarget(); // Just lie about interfaces t.setInterfaces(new Class[] { Serializable.class, MethodInvocation.class, Blah.class }); MethodInvocation mi = MethodInvocationUtils.create(t, "blah"); assertThat(mi).isNotNull(); t.setProxyTargetClass(true); mi = MethodInvocationUtils.create(t, "blah"); assertThat(mi).isNotNull(); assertThat(MethodInvocationUtils.create(t, "blah", "non-existent arg")).isNull(); }
@Test public void correctArgumentIsSelectedFromMultipleArgs() throws Exception { voter.setProcessDomainObjectClass(String.class); MethodInvocation mi = MethodInvocationUtils.create(new TestClass(), "methodTakingAListAndAString", new ArrayList<>(), "The Argument"); assertThat(voter.getDomainObjectInstance(mi)).isEqualTo("The Argument"); }
@Test public void expectedDomainObjectArgumentIsReturnedFromMethodInvocation() throws Exception { voter.setProcessDomainObjectClass(String.class); MethodInvocation mi = MethodInvocationUtils.create(new TestClass(), "methodTakingAString", "The Argument"); assertThat(voter.getDomainObjectInstance(mi)).isEqualTo("The Argument"); }
@Test public void declinesAccessUsingCreate() throws Exception { Object object = new TargetObject(); final MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", "foobar"); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); mipe.setSecurityInterceptor(interceptor); when(mds.getAttributes(mi)).thenReturn(role); doThrow(new AccessDeniedException("rejected")).when(adm).decide(token, mi, role); assertThat(mipe.isAllowed(mi, token)).isFalse(); }
@Test public void allowsAccessUsingCreate() throws Exception { Object object = new TargetObject(); final MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", "foobar"); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); when(mds.getAttributes(mi)).thenReturn(role); mipe.setSecurityInterceptor(interceptor); mipe.afterPropertiesSet(); assertThat(mipe.isAllowed(mi, token)).isTrue(); }