SerializerProcessor extends MonitoredProcess { public String serialize(Object obj) throws SerializationException { this.getRuntimeStat().start(); String serialized; try { serialized = this.serializer.serialize(obj); } catch (Exception e) { this.getErrorCountStat().increment(); throw new SerializationException(e); } finally { this.getRuntimeStat().stop(); } this.getSuccessCountStat().increment(); return serialized; } SerializerProcessor(Serializer serializer);  SerializerProcessor(Class<? extends SerializerFactory> clazz); String serialize(Object obj); Serializer getSerializer(); void setSerializer(Serializer serializer);  }
@Test public void testStatsLogging() throws SerializationException { DummySerializer serializer = new DummySerializer(); SerializerProcessor processor = new SerializerProcessor(serializer); Stat runtimeStat = mock(Stat.class); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); processor.setRuntimeStat(runtimeStat); processor.setSuccessCountStat(successStat); processor.setErrorCountStat(errorStat); processor.serialize("foo"); verify(runtimeStat, times(1)).start(); verify(runtimeStat, times(1)).stop(); verify(successStat, times(1)).increment(); verify(errorStat, never()).increment(); }
@Test public void testStatsLoggingOnError() { DummySerializer serializer = mock(DummySerializer.class); SerializerProcessor processor = new SerializerProcessor(serializer); doThrow(new RuntimeException()).when(serializer).serialize("foo"); Stat runtimeStat = mock(Stat.class); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); processor.setRuntimeStat(runtimeStat); processor.setSuccessCountStat(successStat); processor.setErrorCountStat(errorStat); try { processor.serialize("foo"); } catch (Exception e) { } verify(runtimeStat, times(1)).start(); verify(runtimeStat, times(1)).stop(); verify(successStat, never()).increment(); verify(errorStat, times(1)).increment(); }
@Test public void testSerializeNull() { DummySerializer serializer = mock(DummySerializer.class); SerializerProcessor processor = new SerializerProcessor(serializer); doThrow(new RuntimeException()).when(serializer).serialize(null); Stat runtimeStat = mock(Stat.class); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); processor.setRuntimeStat(runtimeStat); processor.setSuccessCountStat(successStat); processor.setErrorCountStat(errorStat); try { processor.serialize(null); } catch (Exception e) { } verify(runtimeStat, times(1)).start(); verify(runtimeStat, times(1)).stop(); verify(successStat, never()).increment(); verify(errorStat, times(1)).increment(); }