View Javadoc

1   /*
2    * $Id: NamedThreadFactoryTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.AbstractMuleTestCase;
14  
15  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
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      public void testNameContextClassloader() throws InterruptedException
33      {
34          NamedThreadFactory threadFactory = new NamedThreadFactory(testThreadName, testClassLoader);
35          Thread t = threadFactory.newThread(new Runnable()
36          {
37  
38              public void run()
39              {
40                  assertEquals(testThreadName + ".1", Thread.currentThread().getName());
41                  assertEquals(testClassLoader, Thread.currentThread().getContextClassLoader());
42                  latch.countDown();
43              }
44          });
45          t.start();
46          assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
47      }
48  
49      public void testNameIncrement() throws InterruptedException
50      {
51          NamedThreadFactory threadFactory = new NamedThreadFactory(testThreadName);
52          Thread t = threadFactory.newThread(nullRunnable);
53          assertEquals(testThreadName + ".1", t.getName());
54          t = threadFactory.newThread(nullRunnable);
55          assertEquals(testThreadName + ".2", t.getName());
56          t = threadFactory.newThread(nullRunnable);
57          assertEquals(testThreadName + ".3", t.getName());
58      }
59  
60  }