View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util.concurrent;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
12  import org.junit.Test;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertTrue;
16  
17  public class NamedThreadFactoryTestCase extends AbstractMuleTestCase
18  {
19  
20      protected Latch latch = new Latch();
21      protected String testThreadName = "myThread";
22      protected ClassLoader testClassLoader = new ClassLoader()
23      {
24      };
25      protected Runnable nullRunnable = new Runnable()
26      {
27          public void run()
28          {
29          }
30      };
31  
32      @Test
33      public void testNameContextClassloader() throws InterruptedException
34      {
35          NamedThreadFactory threadFactory = new NamedThreadFactory(testThreadName, testClassLoader);
36          Thread t = threadFactory.newThread(new Runnable()
37          {
38  
39              public void run()
40              {
41                  assertEquals(testThreadName + ".1", Thread.currentThread().getName());
42                  assertEquals(testClassLoader, Thread.currentThread().getContextClassLoader());
43                  latch.countDown();
44              }
45          });
46          t.start();
47          assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
48      }
49  
50      @Test
51      public void testNameIncrement() throws InterruptedException
52      {
53          NamedThreadFactory threadFactory = new NamedThreadFactory(testThreadName);
54          Thread t = threadFactory.newThread(nullRunnable);
55          assertEquals(testThreadName + ".1", t.getName());
56          t = threadFactory.newThread(nullRunnable);
57          assertEquals(testThreadName + ".2", t.getName());
58          t = threadFactory.newThread(nullRunnable);
59          assertEquals(testThreadName + ".3", t.getName());
60      }
61  
62  }