@Test public void specialMethod() { long id = 7L; String name = "method"; String desc = "()V"; boolean enhancedExceptionSensor = false; SubstitutionDescriptor substitutionDescriptor = mock(SubstitutionDescriptor.class); when(specialInstrumentationPoint.getId()).thenReturn(id); when(specialInstrumentationPoint.getSubstitutionDescriptor()).thenReturn(substitutionDescriptor); MethodVisitor methodVisitor = factory.getMethodVisitor(specialInstrumentationPoint, superMethodVisitor, 0, name, desc, enhancedExceptionSensor); assertThat(methodVisitor, is(instanceOf(SpecialMethodInstrumenter.class))); }
@Override public MethodVisitor answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); SpecialInstrumentationPoint sip = (SpecialInstrumentationPoint) arguments[0]; return getSpecialMethodInstrumenter((MethodVisitor) arguments[1], (Integer) arguments[2], (String) arguments[3], (String) arguments[4], sip.getId(), sip.getSubstitutionDescriptor()); } };
@Test public void doubleNullParameter() throws Exception { String methodName = "doubleNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Double.valueOf(5.3D))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 5.3D); verifyNoMoreInteractions(hookDispatcher); }
@Test public void doubleNullParameterResultOnBefore() throws Exception { String methodName = "doubleNullParameter"; Double returnValue = Double.valueOf(45223.412D); long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void doubleNullParameterResultOnBeforeOtherPrimitive() throws Exception { String methodName = "doubleNullParameter"; Float returnValue = Float.valueOf(45223.412F); long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Double.valueOf(5.3D))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 5.3D); verifyNoMoreInteractions(hookDispatcher); }
@Test public void exceptionHandledResultReturned() throws Exception { String methodName = "exceptionHandledResultReturned"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) 3)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3); verifyNoMoreInteractions(hookDispatcher); }
@Test public void noParameters() throws Exception { String methodName = "intNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(9); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Integer.valueOf(3))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intNullParameterResultOnAfterOtherPrimitive() throws Exception { String methodName = "intNullParameter"; Double returnValue = Double.valueOf(4552236.22121D); long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3)).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Integer.valueOf(3))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intNullParameterResultOnBeforeObject() throws Exception { String methodName = "intNullParameter"; Object returnValue = "returnValue"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Integer.valueOf(3))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3); verifyNoMoreInteractions(hookDispatcher); }
@Test public void noChangeTwoPrimitveParameters() throws Exception { String methodName = "returnBooleanParameter"; Object[] parameters = { "int", "boolean" }; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName, int.class, boolean.class); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, parameters); assertThat(result, is(parameters[1])); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, parameters); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, parameters, parameters[1]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringNullParameterStatic() throws Exception { String methodName = "stringNullParameterStatic"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) methodName)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, null, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, null, new Object[0], methodName); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringArrayNullParameter() throws Exception { String methodName = "stringArrayNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) new String[] { "test123", "bla" })); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], new String[] { "test123", "bla" }); verifyNoMoreInteractions(hookDispatcher); }
@Test public void noResult() throws Exception { String methodName = "stringNullParameter"; long methodId = 3L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); // call this method via reflection as we would get a class cast // exception by casting to the concrete class. Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) methodName)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], methodName); verifyNoMoreInteractions(hookDispatcher); }
@Test public void hookNoStatic() throws Exception { String methodName = "stringNullParameter"; long methodId = 3L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); // call this method via reflection as we would get a class cast // exception by casting to the concrete class. Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) methodName)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], methodName); verifyNoMoreInteractions(hookDispatcher); }
@Test public void exceptionHandledResultChanged() throws Exception { String methodName = "exceptionHandledResultReturned"; long methodId = 9L; int returnValue = 112; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3)).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringOneParameter() throws Exception { String methodName = "stringOneParameter"; Object[] parameters = { "java.lang.String" }; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName, String.class); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, parameters); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, parameters); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, parameters, "stringOneParameter"); verifyNoMoreInteractions(hookDispatcher); }
@Test public void noChangeArrayParameter() throws Exception { String methodName = "returnArrayParameter"; Object[] parameters = { "array" }; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName, Object[].class); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, parameters); assertThat(result, is(parameters[0])); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, parameters); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, parameters, parameters[0]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringNullParameterStaticResultOnAfter() throws Exception { String methodName = "stringNullParameterStatic"; Object returnValue = "returnValue"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); when(hookDispatcher.dispatchSpecialMethodAfterBody(methodId, null, new Object[0], methodName)).thenReturn(returnValue); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is(returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, null, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, null, new Object[0], methodName); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intArrayNullParameterResultBeforeWrongArray() throws Exception { String methodName = "intArrayNullParameter"; long[] returnValue = new long[] { 4, 5, 6, 7, 8, 9 }; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) new int[] { 1, 2, 3 })); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], new int[] { 1, 2, 3 }); verifyNoMoreInteractions(hookDispatcher); }
@Test public void hookStaticVoidResultOnBeforeNull() throws Exception { String methodName = "voidNullParameterStatic"; long methodId = 7L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, null, new Object[0])).thenReturn(null); // call this method via reflection as we would get a class cast // exception by casting to the concrete class. this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, null, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, null, new Object[0], null); verifyNoMoreInteractions(hookDispatcher); }