1   /*
2    * $Id: ThreadingProfileTestCase.java 9513 2007-10-31 14:16:22Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.test.config;
12  
13  import org.mule.config.ThreadingProfile;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.util.concurrent.NamedThreadFactory;
16  
17  import edu.emory.mathcs.backport.java.util.concurrent.Executors;
18  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
19  import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
20  import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
21  
22  public class ThreadingProfileTestCase extends AbstractMuleTestCase
23  {
24  
25      public void testCustomRejectedExecutionHandler()
26      {
27          RejectedExecutionHandler handler = new RejectedExecutionHandler()
28          {
29              public void rejectedExecution(Runnable arg0, ThreadPoolExecutor arg1)
30              {
31                  // nothing to do here
32              }
33          };
34  
35          ThreadingProfile profile = new ThreadingProfile();
36          profile.setRejectedExecutionHandler(handler);
37          ThreadPoolExecutor pool = profile.createPool("testPool");
38          assertSame(handler, pool.getRejectedExecutionHandler());
39      }
40  
41      public void testPoolExhaustedActionStrings()
42      {
43          ThreadingProfile tp = new ThreadingProfile();
44  
45          tp.setPoolExhaustedActionString(null);
46          assertEquals(ThreadingProfile.DEFAULT_POOL_EXHAUST_ACTION, tp.getPoolExhaustedAction());
47  
48          tp.setPoolExhaustedActionString("BAZ");
49          assertEquals(ThreadingProfile.DEFAULT_POOL_EXHAUST_ACTION, tp.getPoolExhaustedAction());
50  
51          tp.setPoolExhaustedActionString("WHEN_EXHAUSTED_WAIT");
52          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_WAIT, tp.getPoolExhaustedAction());
53          tp.setPoolExhaustedActionString("WAIT");
54          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_WAIT, tp.getPoolExhaustedAction());
55  
56          tp.setPoolExhaustedActionString("WHEN_EXHAUSTED_DISCARD");
57          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_DISCARD, tp.getPoolExhaustedAction());
58          tp.setPoolExhaustedActionString("DISCARD");
59          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_DISCARD, tp.getPoolExhaustedAction());
60  
61          tp.setPoolExhaustedActionString("WHEN_EXHAUSTED_DISCARD_OLDEST");
62          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_DISCARD_OLDEST, tp.getPoolExhaustedAction());
63          tp.setPoolExhaustedActionString("DISCARD_OLDEST");
64          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_DISCARD_OLDEST, tp.getPoolExhaustedAction());
65  
66          tp.setPoolExhaustedActionString("WHEN_EXHAUSTED_ABORT");
67          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_ABORT, tp.getPoolExhaustedAction());
68          tp.setPoolExhaustedActionString("ABORT");
69          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_ABORT, tp.getPoolExhaustedAction());
70  
71          tp.setPoolExhaustedActionString("WHEN_EXHAUSTED_RUN");
72          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_RUN, tp.getPoolExhaustedAction());
73          tp.setPoolExhaustedActionString("RUN");
74          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_RUN, tp.getPoolExhaustedAction());
75      }
76  
77      public void testDefaultThreadFactory()
78      {
79          ThreadingProfile profile = new ThreadingProfile();
80          ThreadPoolExecutor pool = profile.createPool();
81          ThreadFactory returnedFactory = pool.getThreadFactory();
82          assertTrue(returnedFactory.getClass().isInstance(Executors.defaultThreadFactory()));
83      }
84  
85      public void testDefaultNamedThreadFactory()
86      {
87          ThreadingProfile profile = new ThreadingProfile();
88          ThreadPoolExecutor pool = profile.createPool("myThreadPool");
89          ThreadFactory returnedFactory = pool.getThreadFactory();
90          assertTrue(returnedFactory instanceof NamedThreadFactory);
91      }
92  
93      public void testCustomThreadFactory()
94      {
95          ThreadingProfile profile = new ThreadingProfile();
96  
97          ThreadFactory configuredFactory = new ThreadFactory()
98          {
99              public Thread newThread(Runnable r)
100             {
101                 return null;
102             }
103         };
104 
105         profile.setThreadFactory(configuredFactory);
106         ThreadPoolExecutor pool = profile.createPool();
107         ThreadFactory returnedFactory = pool.getThreadFactory();
108         assertSame(configuredFactory, returnedFactory);
109     }
110 
111 }