private void verifyMethodBehavior(String methodName, Object... methodArgs) throws InvocationTargetException, IllegalAccessException { String doMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1); Method doMethod = findMethodWithName(BaseInboundHandlerWithTracingAndMdcSupport.class, doMethodName); PipelineContinuationBehavior doMethodDeafultReturnVal = (PipelineContinuationBehavior)doMethod.invoke(handler, methodArgs); assertThat(doMethodDeafultReturnVal, is(PipelineContinuationBehavior.CONTINUE)); verifyMethodBehaviorDetails(methodName, methodArgs, null, false); verifyMethodBehaviorDetails(methodName, methodArgs, null, true); verifyMethodBehaviorDetails(methodName, methodArgs, PipelineContinuationBehavior.CONTINUE, false); verifyMethodBehaviorDetails(methodName, methodArgs, PipelineContinuationBehavior.CONTINUE, true); verifyMethodBehaviorDetails(methodName, methodArgs, PipelineContinuationBehavior.DO_NOT_FIRE_CONTINUE_EVENT, false); verifyMethodBehaviorDetails(methodName, methodArgs, PipelineContinuationBehavior.DO_NOT_FIRE_CONTINUE_EVENT, true); }
@Test public void channelActive_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelActive", ctxMock); }
Method mainMethod = findMethodWithName(BaseInboundHandlerWithTracingAndMdcSupport.class, methodName); String methodNameWithFirstCharCapitalized = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); String doMethodName = "do" + methodNameWithFirstCharCapitalized; Method doMethod = findMethodWithName(BaseInboundHandlerWithTracingAndMdcSupport.class, doMethodName); String fireEventMethodName = "fire" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1); Method fireEventMethod = ("handlerAdded".equals(methodName) || "handlerRemoved".equals(methodName)) ? null : findMethodWithName(ChannelHandlerContext.class, fireEventMethodName); Whitebox.setInternalState(handlerSpy, "isDefaultDo" + methodNameWithFirstCharCapitalized + "Impl", isDefaultMethodImpl); HandlerMethodToExecute expectedHandlerMethodToExecute = constructHandlerMethodToExecuteFromMethodName(methodName); doReturn(argsEligibleForLinkUnlink).when(handlerSpy).argsAreEligibleForLinkingAndUnlinkingDistributedTracingInfo( eq(expectedHandlerMethodToExecute), eq(ctxMock), anyObject(), any(Throwable.class)
@After public void afterMethod() { resetTracingAndMdc(); }
private void verifyMethodBehaviorDetails( String methodName, Object[] methodArgs, PipelineContinuationBehavior doMethodReturnValue, boolean shouldExplodeInDoMethod ) throws InvocationTargetException, IllegalAccessException { // Verify all the various sub-combinations that could affect behavior in this case. // Check with debug logging on or off verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, true, false, false, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, true, false, false, false, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, false, shouldExplodeInDoMethod); // Check with force-dtrace on or off verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, true, false, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, true, false, false, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, false, shouldExplodeInDoMethod); // Check with isDefaultMethodImpl on or off verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, true, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, true, false, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, false, shouldExplodeInDoMethod); // Check with argsEligibleForLinkUnlink on or off verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, true, shouldExplodeInDoMethod); verifyMethodBehaviorDetails(methodName, methodArgs, doMethodReturnValue, false, false, false, false, shouldExplodeInDoMethod); }
@Before public void beforeMethod() { handler = new BaseInboundHandlerWithTracingAndMdcSupport() { }; channelMock = mock(Channel.class); ctxMock = mock(ChannelHandlerContext.class); //noinspection unchecked stateAttributeMock = mock(Attribute.class); state = new HttpProcessingState(); doReturn(channelMock).when(ctxMock).channel(); doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY); doReturn(state).when(stateAttributeMock).get(); resetTracingAndMdc(); }
@Test public void channelWritabilityChanged_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelWritabilityChanged", ctxMock); }
@Test public void channelRegistered_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelRegistered", ctxMock); }
@Test public void userEventTriggered_should_perform_as_expected() throws Exception { verifyMethodBehavior("userEventTriggered", ctxMock, null); }
@Test public void channelInactive_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelInactive", ctxMock); }
@Test public void channelRead_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelRead", ctxMock, null); }
@Test public void exceptionCaught_should_perform_as_expected() throws Exception { verifyMethodBehavior("exceptionCaught", ctxMock, null); }
@Test public void channelUnregistered_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelUnregistered", ctxMock); }
@Test public void channelReadComplete_should_perform_as_expected() throws Exception { verifyMethodBehavior("channelReadComplete", ctxMock); }
@Test public void handlerAdded_should_perform_as_expected() throws Exception { verifyMethodBehavior("handlerAdded", ctxMock); }
@Test public void handlerRemoved_should_perform_as_expected() throws Exception { verifyMethodBehavior("handlerRemoved", ctxMock); }