Add test case for ArgumentTypeNameMatch.java

This commit is contained in:
wusheng 2017-02-28 11:43:12 +08:00
parent 7df2104294
commit a5f2aa1f47
2 changed files with 43 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package com.a.eye.skywalking.api.plugin.bytebuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.matcher.ElementMatcher;
/**
@ -39,8 +40,9 @@ public class ArgumentTypeNameMatch implements ElementMatcher<MethodDescription>
*/
@Override
public boolean matches(MethodDescription target) {
if (target.getParameters().size() > index) {
return target.getParameters().get(index).getType().asErasure().getName().equals(argumentTypeName);
ParameterList<?> parameters = target.getParameters();
if (parameters.size() > index) {
return parameters.get(index).getType().asErasure().getName().equals(argumentTypeName);
}
return false;

View File

@ -0,0 +1,39 @@
package com.a.eye.skywalking.api.plugin.bytebuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterDescription;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
/**
* @author wusheng
*/
public class ArgumentTypeNameMatchTest {
@Test
public void testMatches() throws IllegalAccessException {
MethodDescription methodDescription = Mockito.mock(MethodDescription.class, Mockito.RETURNS_DEEP_STUBS);
ParameterDescription parameterDescription = Mockito.mock(ParameterDescription.class, Mockito.RETURNS_DEEP_STUBS);
when(methodDescription.getParameters().get(0)).thenReturn(parameterDescription);
when(methodDescription.getParameters().size()).thenReturn(1);
when(parameterDescription.getType().asErasure().getName()).thenReturn("com.a.eye.TestClass");
ArgumentTypeNameMatch matcher = ((ArgumentTypeNameMatch)ArgumentTypeNameMatch.takesArgumentWithType(0, "com.a.eye.TestClass"));
Assert.assertTrue(matcher.matches(methodDescription));
ArgumentTypeNameMatch matcher2 = ((ArgumentTypeNameMatch)ArgumentTypeNameMatch.takesArgumentWithType(0, "com.a.eye.TestClass2"));
Assert.assertFalse(matcher2.matches(methodDescription));
}
@Test
public void testMatchesWithNoParameters(){
MethodDescription methodDescription = Mockito.mock(MethodDescription.class, Mockito.RETURNS_DEEP_STUBS);
ParameterDescription parameterDescription = Mockito.mock(ParameterDescription.class, Mockito.RETURNS_DEEP_STUBS);
when(methodDescription.getParameters().size()).thenReturn(0);
ArgumentTypeNameMatch matcher2 = ((ArgumentTypeNameMatch)ArgumentTypeNameMatch.takesArgumentWithType(0, "com.a.eye.TestClass"));
Assert.assertFalse(matcher2.matches(methodDescription));
}
}