|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.access.expression.method; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | + |
| 20 | +import org.aopalliance.intercept.MethodInvocation; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.junit.MockitoJUnitRunner; |
| 25 | + |
| 26 | +import org.springframework.core.ParameterNameDiscoverer; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | +import org.springframework.security.core.Authentication; |
| 29 | +import org.springframework.util.ReflectionUtils; |
| 30 | + |
| 31 | +import static org.mockito.Mockito.doReturn; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author shabarijonnalagadda |
| 35 | + * |
| 36 | + */ |
| 37 | +@RunWith(MockitoJUnitRunner.class) |
| 38 | +public class MethodSecurityEvaluationContextTests { |
| 39 | + @Mock |
| 40 | + private ParameterNameDiscoverer paramNameDiscoverer; |
| 41 | + @Mock |
| 42 | + private Authentication authentication; |
| 43 | + @Mock |
| 44 | + private MethodInvocation methodInvocation; |
| 45 | + |
| 46 | + @Test |
| 47 | + public void lookupVariableWhenParameterNameNullThenNotSet() { |
| 48 | + Class<String> type = String.class; |
| 49 | + Method method = ReflectionUtils.findMethod(String.class, "contains", CharSequence.class); |
| 50 | + doReturn(new String[] {null}).when(paramNameDiscoverer).getParameterNames(method); |
| 51 | + doReturn(new Object[]{null}).when(methodInvocation).getArguments(); |
| 52 | + doReturn(type).when(methodInvocation).getThis(); |
| 53 | + doReturn(method).when(methodInvocation).getMethod(); |
| 54 | + NotNullVariableMethodSecurityEvaluationContext context= new NotNullVariableMethodSecurityEvaluationContext(authentication, methodInvocation, paramNameDiscoverer); |
| 55 | + context.lookupVariable("testVariable"); |
| 56 | + } |
| 57 | + |
| 58 | + private static class NotNullVariableMethodSecurityEvaluationContext |
| 59 | + extends MethodSecurityEvaluationContext { |
| 60 | + |
| 61 | + public NotNullVariableMethodSecurityEvaluationContext(Authentication auth, MethodInvocation mi, |
| 62 | + ParameterNameDiscoverer parameterNameDiscoverer) { |
| 63 | + super(auth, mi, parameterNameDiscoverer); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void setVariable(String name, @Nullable Object value) { |
| 68 | + if ( name == null ) { |
| 69 | + throw new IllegalArgumentException("name should not be null"); |
| 70 | + } |
| 71 | + else { |
| 72 | + super.setVariable(name, value); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments