### Question:
RefCountResource { public T retain() throws RefCountResourceException { synchronized (this) { if (refCount.getAndIncrement() == 0) { try { log.debug("First Reference. Create Resource."); resource.set(resourceSupplier.get()); } catch (Throwable throwable) { throw new RefCountResourceException(throwable); } } return resource.get(); } } RefCountResource(Supplier<? extends T> resourceSupplier,
                            Consumer<? super T> resourceCleanup);   RefCountResource(Supplier<? extends T> resourceSupplier,
                     Consumer<? super T> resourceCleanup,
                     AtomicInteger refCount,
                     AtomicReference<T> resource); T retain(); void release();  }### Answer:
@Test public void retainShouldCreateCreateResourceOnFirstAccess() throws Throwable { AtomicInteger refCount = new AtomicInteger(0); RefCountResource resource = new RefCountResource(resourceSupplier, resourceCleanup, refCount, new AtomicReference(null)); resource.retain(); verify(resourceSupplier).get(); assertEquals(1, refCount.get()); }
@Test public void retainShouldNotCreateResourceOnSecondCall() throws Throwable { AtomicInteger refCount = new AtomicInteger(1); RefCountResource resource = new RefCountResource(resourceSupplier, resourceCleanup, refCount, new AtomicReference(new Object())); resource.retain(); verify(resourceSupplier, times(0)).get(); assertEquals(2, refCount.get()); }