|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package org.javaee7.concurrency.manageablethread; |
| 7 | + |
| 8 | +import java.util.logging.Level; |
| 9 | +import java.util.logging.Logger; |
| 10 | +import javax.annotation.Resource; |
| 11 | +import javax.enterprise.concurrent.ManagedThreadFactory; |
| 12 | +import javax.naming.InitialContext; |
| 13 | +import javax.naming.NamingException; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | +import static org.junit.Assert.*; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Arun Gupta |
| 20 | + */ |
| 21 | +public class MyTaskTest { |
| 22 | + |
| 23 | + @Resource |
| 24 | + ManagedThreadFactory factory; |
| 25 | + |
| 26 | + @Resource(name = "DefaultManagedThreadFactory") |
| 27 | + ManagedThreadFactory factory2; |
| 28 | + |
| 29 | + /** |
| 30 | + * Test of run method, of class MyTask. |
| 31 | + * |
| 32 | + * using JNDI lookup |
| 33 | + */ |
| 34 | + @Test |
| 35 | + public void testJNDILookup() { |
| 36 | + try { |
| 37 | + InitialContext ctx = new InitialContext(); |
| 38 | + |
| 39 | +// ManagedExecutorService executor = (ManagedExecutorService) ctx.lookup("concurrent/myExecutor"); |
| 40 | + ManagedThreadFactory myFactory = (ManagedThreadFactory) ctx.lookup("java:comp/DefaultManagedThreadFactory"); |
| 41 | + assertNotNull(myFactory); |
| 42 | + Thread thread = myFactory.newThread(new MyTask(1)); |
| 43 | + assertNotNull(thread); |
| 44 | + thread.start(); |
| 45 | + } catch (NamingException ex) { |
| 46 | + Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test of run method, of class MyTask. |
| 52 | + * |
| 53 | + * using @Resource, with no name |
| 54 | + */ |
| 55 | + @Test |
| 56 | + public void testResourceNoName() { |
| 57 | + Thread thread = factory.newThread(new MyTask(1)); |
| 58 | + assertNotNull(thread); |
| 59 | + thread.start(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test of run method, of class MyTask. |
| 64 | + * |
| 65 | + * using @Resource, with no name |
| 66 | + */ |
| 67 | + @Test |
| 68 | + public void testResourceWithName() { |
| 69 | + Thread thread = factory2.newThread(new MyTask(1)); |
| 70 | + assertNotNull(thread); |
| 71 | + thread.start(); |
| 72 | + } |
| 73 | +} |
0 commit comments