MethodInvocation mi = createFromClass(null, clazz, methodName, null, null);
@Test public void createFromClassReturnsMethodWithNoArgInfoForMethodWithNoArgs() { new MethodInvocationUtils(); MethodInvocation mi = MethodInvocationUtils.createFromClass(String.class, "length"); assertThat(mi).isNotNull(); }
@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(); }
MethodInvocation mi = createFromClass(null, clazz, methodName, null, null);
@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(expected = IllegalArgumentException.class) public void exceptionIsRaisedIfArgInfoOmittedAndMethodNameIsNotUnique() { MethodInvocationUtils.createFromClass(BusinessServiceImpl.class, "methodReturningAList"); }
@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 createFromClassReturnsMethodIfArgInfoOmittedAndMethodNameIsUnique() { MethodInvocation mi = MethodInvocationUtils.createFromClass( BusinessServiceImpl.class, "methodReturningAnArray"); assertThat(mi).isNotNull(); }
@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 createFromClassReturnsMethodIfGivenArgInfoForMethodWithArgs() { MethodInvocation mi = MethodInvocationUtils.createFromClass(null, String.class, "compareTo", new Class<?>[] { String.class }, new Object[] { "" }); assertThat(mi).isNotNull(); }
@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(); }
return createFromClass(object, target, methodName, classArgs, args);
return createFromClass(object, target, methodName, classArgs, args);
@Test public void allowsAccessUsingCreateFromClass() throws Exception { final MethodInvocation mi = MethodInvocationUtils.createFromClass( new OtherTargetObject(), ITargetObject.class, "makeLowerCase", new Class[] { String.class }, new Object[] { "Hello world" }); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); mipe.setSecurityInterceptor(interceptor); when(mds.getAttributes(mi)).thenReturn(role); assertThat(mipe.isAllowed(mi, token)).isTrue(); }
@Test public void declinesAccessUsingCreateFromClass() throws Exception { final MethodInvocation mi = MethodInvocationUtils.createFromClass( new OtherTargetObject(), ITargetObject.class, "makeLowerCase", new Class[] { String.class }, new Object[] { "helloWorld" }); 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(); } }
/** * Generates a <code>MethodInvocation</code> for the specified <code>methodName</code> on the passed class. * * If a method with this name, taking no arguments does not exist, it will check through the declared * methods on the class, until one is found matching the supplied name. If more than one method name matches, * an <tt>IllegalArgumentException</tt> will be raised. * * @param clazz the class of object that will be used to find the relevant <code>Method</code> * @param methodName the name of the method to find * * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem */ public static MethodInvocation createFromClass(Class<?> clazz, String methodName) { MethodInvocation mi = createFromClass(null, clazz, methodName, null, null); if (mi == null) { for (Method m : clazz.getDeclaredMethods()) { if (m.getName().equals(methodName)) { if (mi != null) { throw new IllegalArgumentException("The class " + clazz + " has more than one method named" + " '" + methodName + "'"); } mi = new SimpleMethodInvocation(null, m); } } } return mi; }
MethodInvocation mi = createFromClass(null, clazz, methodName, null, null);
return createFromClass(object, target, methodName, classArgs, args);
return createFromClass(object, target, methodName, classArgs, args);
final Class<?> targetClass = AopUtils.getTargetClass(view); final Method method = ClassUtils.getMethod(targetClass, "enter", com.vaadin.navigator.ViewChangeListener.ViewChangeEvent.class); final MethodInvocation methodInvocation = MethodInvocationUtils.createFromClass(targetClass, method.getName());