Update ThreadFactoryBuilderTest

Methods are covered by multiple unit test cases .
Console printing in English
pull/1341/head
时间邮递员 2 years ago committed by GitHub
parent 853bddd5a3
commit 86116715f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,27 +3,52 @@ package cn.hippo4j.common.design.builder;
import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.common.design.builder.ThreadFactoryBuilder;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
public class ThreadFactoryBuilderTest { public class ThreadFactoryBuilderTest {
@Test ThreadFactoryBuilder builder;
public void testBuild() { TestUncaughtExceptionHandler uncaughtExceptionHandler;
ThreadFactoryBuilder builder = ThreadFactoryBuilder.builder(); Thread thread;
TestUncaughtExceptionHandler uncaughtExceptionHandler = new TestUncaughtExceptionHandler();
public void buildThread() {
builder = ThreadFactoryBuilder.builder();
uncaughtExceptionHandler = new TestUncaughtExceptionHandler();
builder.uncaughtExceptionHandler(uncaughtExceptionHandler); builder.uncaughtExceptionHandler(uncaughtExceptionHandler);
builder.prefix("my-thread-factory"); builder.prefix("my-thread-factory");
builder.daemon(true); builder.daemon(true);
builder.priority(Thread.MAX_PRIORITY); builder.priority(Thread.MAX_PRIORITY);
ThreadFactory threadFactory = builder.build(); ThreadFactory threadFactory = builder.build();
Thread thread = threadFactory.newThread(() -> { thread = threadFactory.newThread(() -> {
System.out.println("创建新线程"); System.out.println("Create a new thread.");
}); });
thread.start(); thread.start();
}
@Test
public void testName() {
buildThread();
Assert.assertEquals("my-thread-factory_0", thread.getName()); Assert.assertEquals("my-thread-factory_0", thread.getName());
}
@Test
public void testIsDaemon() {
buildThread();
Assert.assertTrue(thread.isDaemon()); Assert.assertTrue(thread.isDaemon());
}
@Test
public void testExceptionHandler() {
buildThread();
Assert.assertEquals(uncaughtExceptionHandler, thread.getUncaughtExceptionHandler()); Assert.assertEquals(uncaughtExceptionHandler, thread.getUncaughtExceptionHandler());
}
@Test
public void testPriority() {
buildThread();
Assert.assertEquals(Thread.MAX_PRIORITY, thread.getPriority()); Assert.assertEquals(Thread.MAX_PRIORITY, thread.getPriority());
} }
@ -43,3 +68,4 @@ class TestUncaughtExceptionHandler implements UncaughtExceptionHandler {
return exceptionCaught; return exceptionCaught;
} }
} }

Loading…
Cancel
Save