View Javadoc

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