From a5f2aa1f4714741e2cb1e701059698ab570cd5b2 Mon Sep 17 00:00:00 2001 From: wusheng Date: Tue, 28 Feb 2017 11:43:12 +0800 Subject: [PATCH] Add test case for ArgumentTypeNameMatch.java --- .../bytebuddy/ArgumentTypeNameMatch.java | 6 ++- .../bytebuddy/ArgumentTypeNameMatchTest.java | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java index 6d8c8c6af..018de2136 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java @@ -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 */ @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; diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java new file mode 100644 index 000000000..8721ae449 --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java @@ -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)); + } +}