DeserializerProcessor extends MonitoredProcess { public DeserializedEvent deserialize(String eventString) { DeserializedEvent dEvent = null; this.getRuntimeStat().start(); try { dEvent = this.deser.deserialize(eventString); this.getSuccessCountStat().increment(); } catch (DeserializationException e) { logger.warn("failed to deserialize", e); this.getErrorCountStat().increment(); } finally { this.getRuntimeStat().stop(); } return dEvent; } DeserializerProcessor(Deserializer deserializer); DeserializedEvent deserialize(String eventString); Deserializer getDeserializer(); void setDeserializer(Deserializer deserializer); String toString();  }
@Test public void testStatsLogging() throws InstantiationException, IllegalAccessException { DeserializerProcessor deser = new DeserializerProcessor(new DummyDeserializer()); Stat runtimeStat = mock(Stat.class); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); deser.setRuntimeStat(runtimeStat); deser.setSuccessCountStat(successStat); deser.setErrorCountStat(errorStat); deser.deserialize("foo"); verify(runtimeStat, times(1)).start(); verify(runtimeStat, times(1)).stop(); verify(successStat, times(1)).increment(); verify(errorStat, never()).increment(); }
@Test public void testStatsLoggingOnError() { DummyDeserializer mockDeser = mock(DummyDeserializer.class); when(mockDeser.deserialize("foo")) .thenThrow(new DeserializationException(new RuntimeException("expected"))); DeserializerProcessor deser = new DeserializerProcessor(mockDeser); Stat runtimeStat = mock(Stat.class); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); deser.setRuntimeStat(runtimeStat); deser.setSuccessCountStat(successStat); deser.setErrorCountStat(errorStat); try { deser.deserialize("foo"); } catch (DeserializationException e) { } verify(runtimeStat, times(1)).start(); verify(runtimeStat, times(1)).stop(); verify(successStat, never()).increment(); verify(errorStat, times(1)).increment(); }
@Test(expected = RuntimeException.class) public void testOnUnexpectedError() { DummyDeserializer mockDeser = mock(DummyDeserializer.class); when(mockDeser.deserialize("foo")).thenThrow(new RuntimeException("unexpected")); DeserializerProcessor deser = new DeserializerProcessor(mockDeser); Stat runtimeStat = mock(Stat.class); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); deser.setRuntimeStat(runtimeStat); deser.setSuccessCountStat(successStat); deser.setErrorCountStat(errorStat); try { deser.deserialize("foo"); } catch (DeserializationException e) { } }