### Question:
TypePostProcessorResultHandler implements ResultHandler<Object> { @Override public void handleResult(Object result) { if (result == null) { return; } if (result instanceof Throwable) { THREAD_CONTEXT.set((Throwable) result); } Object obj = result; SshContext ctx = SSH_THREAD_CONTEXT.get(); if (ctx != null && ctx.getPostProcessorsList() != null) { for (PostProcessorObject postProcessorObject : ctx.getPostProcessorsList()) { String name = postProcessorObject.getName(); PostProcessor postProcessor = postProcessorMap.get(name); if (postProcessor == null) { printLogWarn("Unknown post processor [" + name + "]"); continue; } Class<?> cls = ((Class) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]); if (!cls.isAssignableFrom(obj.getClass())) { printLogWarn("Post processor [" + name + "] can only apply to class [" + cls.getName() + "] (current object class is " + obj.getClass().getName() + ")"); } else { LOGGER.debug("Applying post processor [{}] with parameters {}", name, postProcessorObject.getParameters()); try { obj = postProcessor.process(obj, postProcessorObject.getParameters()); } catch (PostProcessorException e) { printError(e.getMessage()); return; } } } } resultHandler.handleResult(obj); } TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<PostProcessor> postProcessorList); @Override void handleResult(Object result); static final ThreadLocal<Throwable> THREAD_CONTEXT; }### Answer:
@Test void handleResultNull() { rh.handleResult(null); assertEquals(0, captor.getAllValues().size()); }
@Test void handleResultThrowable() { IllegalArgumentException ex = new IllegalArgumentException("[TEST]"); rh.handleResult(ex); assertEquals(1, captor.getAllValues().size()); assertEquals(ex, captor.getAllValues().get(0)); }
@Test void handleResultUnknownPostProcessor() { SSH_THREAD_CONTEXT.get().setPostProcessorsList(Collections.singletonList( new PostProcessorObject("unknown") )); rh.handleResult("result"); assertEquals(2, captor.getAllValues().size()); assertTrue(((String) captor.getAllValues().get(0)).contains("Unknown post processor")); assertEquals("result", captor.getAllValues().get(1)); }
@Test void handleResultWrongPostProcessorArgument() { SSH_THREAD_CONTEXT.get().setPostProcessorsList(Collections.singletonList( new PostProcessorObject("grep") )); Object obj = new PostProcessorObject("test"); rh.handleResult(obj); assertEquals(2, captor.getAllValues().size()); assertTrue(((String) captor.getAllValues().get(0)).contains("can only apply to class")); assertEquals(obj, captor.getAllValues().get(1)); }
@Test void handleResultPostProcessorError() { SSH_THREAD_CONTEXT.get().setPostProcessorsList(Collections.singletonList( new PostProcessorObject("save")) ); rh.handleResult("result"); assertEquals(1, captor.getAllValues().size()); assertTrue(((String) captor.getAllValues().get(0)).contains("Cannot save")); }
@Test void handleResultNominal() { SSH_THREAD_CONTEXT.get().setPostProcessorsList(Collections.singletonList( new PostProcessorObject("grep", Collections.singletonList("result"))) ); rh.handleResult("result"); assertEquals(1, captor.getAllValues().size()); assertEquals("result", captor.getAllValues().get(0)); }