LoggingThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable runnable) { Runnable commandToRun; if (commandWrapper != null) { commandToRun = () -> commandWrapper.invoke(runnable); } else { commandToRun = runnable; } Thread thread = new LoggingThread(getUniqueName(), isDaemon, commandToRun); if (threadInitializer != null) { threadInitializer.initialize(thread); } return thread; } LoggingThreadFactory(String baseName, ThreadInitializer threadInitializer,
      CommandWrapper commandWrapper, boolean isDaemon);  LoggingThreadFactory(String baseName, ThreadInitializer threadInitializer,
      CommandWrapper commandWrapper);  LoggingThreadFactory(String baseName);  LoggingThreadFactory(String baseName, boolean isDaemon);  LoggingThreadFactory(String baseName, CommandWrapper commandWrapper); @Override Thread newThread(Runnable runnable);  }
@Test public void verifyFirstThreadName() { LoggingThreadFactory factory = new LoggingThreadFactory("baseName"); Thread thread = factory.newThread(null); assertThat(thread.getName()).isEqualTo("baseName" + 1); }
@Test public void verifySecondThreadName() { LoggingThreadFactory factory = new LoggingThreadFactory("baseName"); factory.newThread(null); Thread thread = factory.newThread(null); assertThat(thread.getName()).isEqualTo("baseName" + 2); }
@Test public void verifyThreadsAreDaemons() { LoggingThreadFactory factory = new LoggingThreadFactory("baseName"); Thread thread = factory.newThread(null); assertThat(thread.isDaemon()).isTrue(); }
@Test public void verifyThreadHaveExpectedHandler() { UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance(); LoggingThreadFactory factory = new LoggingThreadFactory("baseName"); Thread thread = factory.newThread(null); assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler); }
@Test public void verifyThreadInitializerCalledCorrectly() { ThreadInitializer threadInitializer = mock(ThreadInitializer.class); LoggingThreadFactory factory = new LoggingThreadFactory("baseName", threadInitializer, null); Thread thread = factory.newThread(null); verify(threadInitializer).initialize(thread); }
@Test public void verifyCommandWrapperNotCalledIfThreadIsNotStarted() { CommandWrapper commandWrapper = mock(CommandWrapper.class); LoggingThreadFactory factory = new LoggingThreadFactory("baseName", commandWrapper); Thread thread = factory.newThread(null); verify(commandWrapper, never()).invoke(any()); }
@Test public void verifyCommandWrapperCalledIfThreadStarted() throws InterruptedException { CommandWrapper commandWrapper = mock(CommandWrapper.class); Runnable command = mock(Runnable.class); LoggingThreadFactory factory = new LoggingThreadFactory("baseName", commandWrapper); Thread thread = factory.newThread(command); thread.start(); thread.join(); verify(commandWrapper).invoke(command); }
@Test public void verifyCommandCalledIfThreadStarted() throws InterruptedException { Runnable command = mock(Runnable.class); LoggingThreadFactory factory = new LoggingThreadFactory("baseName"); Thread thread = factory.newThread(command); thread.start(); thread.join(); verify(command).run(); }