### Question:
PooledItemSourceFactory implements ItemSourceFactory { @Override public ItemSource create(Object source, ObjectWriter objectWriter) { ItemSource<ByteBuf> pooled; try { pooled = bufferedItemSourcePool.getPooled(); } catch (PoolResourceException e) { throw new IllegalStateException(e); } try { OutputStream byteBufOutputStream = new ReusableByteBufOutputStream(pooled.getSource()); objectWriter.writeValue(byteBufOutputStream, source); return pooled; } catch (IOException e) { pooled.release(); throw new IllegalArgumentException(e); } } protected  PooledItemSourceFactory(ItemSourcePool bufferedItemSourcePool); @Override final boolean isBuffered(); @Override ItemSource create(Object source, ObjectWriter objectWriter); @Override ItemSource createEmptySource(); @PluginBuilderFactory static PooledItemSourceFactory.Builder newBuilder(); @Override void start(); @Override void stop(); @Override boolean isStarted(); @Override boolean isStopped(); static final String PLUGIN_NAME; }### Answer:
@Test public void throwsWhenCreateCantGetPooledElement() throws PoolResourceException { ItemSourcePool mockedPool = mock(ItemSourcePool.class); String expectedMessage = UUID.randomUUID().toString(); when(mockedPool.getPooled()).thenThrow(new PoolResourceException(expectedMessage)); PooledItemSourceFactory pooledItemSourceFactory = new PooledItemSourceFactory(mockedPool); expectedException.expect(IllegalStateException.class); expectedException.expectMessage(expectedMessage); pooledItemSourceFactory.create(mock(LogEvent.class), new ObjectMapper().writerFor(LogEvent.class)); }
@Test public void createRemovesFromPool() throws PoolResourceException { ItemSourcePool mockedPool = mock(ItemSourcePool.class); ByteBuf byteBuf = createDefaultTestByteBuf(); ItemSource<ByteBuf> bufferedItemSource = new ByteBufItemSource(byteBuf, source -> {}); when(mockedPool.getPooled()).thenReturn(bufferedItemSource); PooledItemSourceFactory pooledItemSourceFactory = new PooledItemSourceFactory(mockedPool); pooledItemSourceFactory.create(mock(LogEvent.class), new ObjectMapper().writerFor(LogEvent.class)); verify(mockedPool).getPooled(); }
@Test public void createWritesItemSource() throws IOException, PoolResourceException { ItemSourcePool mockedPool = mock(ItemSourcePool.class); ByteBuf byteBuf = createDefaultTestByteBuf(); ItemSource<ByteBuf> bufferedItemSource = new ByteBufItemSource(byteBuf, source -> {}); when(mockedPool.getPooled()).thenReturn(bufferedItemSource); PooledItemSourceFactory pooledItemSourceFactory = new PooledItemSourceFactory(mockedPool); LogEvent logEvent = mock(LogEvent.class); ObjectWriter objectWriter = spy(new ObjectMapper().writerFor(LogEvent.class)); pooledItemSourceFactory.create(logEvent, objectWriter); verify(objectWriter).writeValue(any(OutputStream.class), eq(logEvent)); }
@Test public void createExceptionReleasesPooledElement() throws IOException, PoolResourceException { ItemSourcePool mockedPool = mock(ItemSourcePool.class); ByteBuf byteBuf = createDefaultTestByteBuf(); ItemSource<ByteBuf> bufferedItemSource = spy(new ByteBufItemSource(byteBuf, source -> {})); when(mockedPool.getPooled()).thenReturn(bufferedItemSource); PooledItemSourceFactory pooledItemSourceFactory = new PooledItemSourceFactory(mockedPool); LogEvent logEvent = mock(LogEvent.class); ObjectWriter objectWriter = spy(new ObjectMapper().writerFor(LogEvent.class)); doThrow(new IOException("test exception")).when(objectWriter).writeValue(any(OutputStream.class), eq(logEvent)); ItemSource<ByteBuf> result = null; Exception caught = null; try { result = pooledItemSourceFactory.create(logEvent, objectWriter); } catch (Exception e) { caught = e; } assertNull(result); verify(bufferedItemSource).release(); assertEquals(IllegalArgumentException.class, caught.getClass()); }