@Test public void unexpectedExceptionNotTrowing() throws Exception { String methodName = "unexpectedExceptionNotThrowing"; Object[] parameters = { "java.lang.Object" }; 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); try { this.callMethod(testClass, methodName, parameters); } catch (Throwable t) { } ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, parameters); verify(hookDispatcher).dispatchSpecialMethodAfterBody(eq(methodId), eq(testClass), eq(parameters), captor.capture()); assertThat(captor.getValue(), is(instanceOf(Exception.class))); verifyNoMoreInteractions(hookDispatcher); }
@Test public void hookNoStaticResultOnBeforeWrong() throws Exception { String methodName = "stringNullParameter"; Object returnValue = Boolean.valueOf(false); 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); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); // 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 unexpectedExceptionTrowing() throws Exception { String methodName = "unexpectedExceptionThrowing"; 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); try { this.callMethod(testClass, methodName, null); } catch (Throwable t) { // NOPMD } ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(eq(methodId), eq(testClass), eq(new Object[0]), captor.capture()); assertThat(captor.getValue(), is(instanceOf(Exception.class))); verifyNoMoreInteractions(hookDispatcher); }
@Test public void hookStaticVoidResultOnAfter() throws Exception { String methodName = "voidNullParameterStatic"; Object returnValue = "returnValue"; 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.dispatchSpecialMethodAfterBody(methodId, null, new Object[0], null)).thenReturn(returnValue); // 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); }
@Test public void hookNoStaticResultOnBefore() throws Exception { String methodName = "stringNullParameter"; Object returnValue = "returnValue"; 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); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); // 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(returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void doubleNullParameterResultOnAfterObject() throws Exception { String methodName = "doubleNullParameter"; Object returnValue = "whatever"; 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], 5.3D)).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 intNullParameterResultOnAfter() throws Exception { String methodName = "intNullParameter"; Object returnValue = Integer.valueOf(11); 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(returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 3); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringNullParameterStaticResultOnAfterWrong() throws Exception { String methodName = "stringNullParameterStatic"; Object returnValue = true; 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((Object) methodName)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, null, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, null, new Object[0], methodName); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringArrayNullParameterResultOnAfter() throws Exception { String methodName = "stringArrayNullParameter"; String[] returnValue = new String[] { "something", "totally", "sexy" }; 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], new String[] { "test123", "bla" })).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], new String[] { "test123", "bla" }); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intNullParameterResultOnAfterObject() 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.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 intNullParameterResultOnBeforeOtherPrimitive() 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.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 stringNullParameterStaticResultOnBeforeWrong() throws Exception { String methodName = "stringNullParameterStatic"; Object returnValue = true; 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.dispatchSpecialMethodBeforeBody(methodId, null, new Object[0])).thenReturn(returnValue); // 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 hookStaticVoidResultOnBefore() throws Exception { String methodName = "voidNullParameterStatic"; Object returnValue = "returnValue"; 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(returnValue); // 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]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intArrayNullParameterResultBefore() throws Exception { String methodName = "intArrayNullParameter"; int[] returnValue = new int[] { 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) returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringNullParameterStaticResultOnBefore() 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.dispatchSpecialMethodBeforeBody(methodId, null, new Object[0])).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]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void hookStaticVoid() 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); // 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); }
@Test public void hookNoStaticResultOnAfter() throws Exception { String methodName = "stringNullParameter"; Object returnValue = "returnValue"; 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); when(hookDispatcher.dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], methodName)).thenReturn(returnValue); // 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(returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], methodName); verifyNoMoreInteractions(hookDispatcher); }
@Test public void hookNoStaticResultOnAfterWrong() throws Exception { String methodName = "stringNullParameter"; Object returnValue = Long.valueOf(1L); 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); when(hookDispatcher.dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], methodName)).thenReturn(returnValue); // 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 intNullParameterResultOnBefore() throws Exception { String methodName = "intNullParameter"; Object returnValue = Integer.valueOf(11); 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(returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intArrayNullParameter() throws Exception { String methodName = "intArrayNullParameter"; 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 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); }