1 package org.objectweb.celtix.application; 2 3 import junit.framework.TestCase; 4 5 import org.objectweb.celtix.plugins.PluginException; 6 import org.objectweb.celtix.plugins.PluginManager; 7 8 public class ApplicationPluginManagerTest extends TestCase { 9 10 public void testGetPluginClassNotFound() { 11 ApplicationPluginManager apm = new ApplicationPluginManager(); 12 try { 13 apm.getPlugin("org.objectweb.celtix.application.test.Greater"); 14 fail("Expected PluginException not thrown"); 15 } catch (PluginException ex) { 16 assertEquals("LOAD_FAILED_EXC", ex.getCode()); 17 assertTrue(ex.getCause() instanceof ClassNotFoundException ); 18 } 19 } 20 21 public void testGetPluginIllegalAccess() { 22 ApplicationPluginManager apm = new ApplicationPluginManager(); 23 try { 24 apm.getPlugin("org.objectweb.celtix.application.test.ProtectedConstructorGreeter"); 25 fail("Expected PluginException not thrown"); 26 } catch (PluginException ex) { 27 assertEquals("LOAD_FAILED_EXC", ex.getCode()); 28 assertTrue("Unexepcted cause: " + ex.getCause().getClass().getName(), 29 ex.getCause() instanceof IllegalAccessException ); 30 } 31 } 32 33 public void testGetPluginNoDefaultConstructor() { 34 ApplicationPluginManager apm = new ApplicationPluginManager(); 35 try { 36 apm.getPlugin("org.objectweb.celtix.application.test.PersonalGreeter"); 37 fail("Expected PluginException not thrown"); 38 } catch (PluginException ex) { 39 assertEquals("LOAD_FAILED_EXC", ex.getCode()); 40 assertTrue(ex.getCause() instanceof InstantiationException ); 41 } 42 } 43 44 public void testGetPlugin() throws PluginException { 45 ApplicationPluginManager apm = new ApplicationPluginManager(); 46 Object object = apm.getPlugin("org.objectweb.celtix.application.test.Greeter"); 47 Object otherObject = apm.createPlugin("org.objectweb.celtix.application.test.Greeter"); 48 assertTrue(object != otherObject); 49 otherObject = apm.getPlugin("org.objectweb.celtix.application.test.Greeter"); 50 assertTrue(object == otherObject); 51 } 52 53 public void testConcurrentLoad() throws PluginException { 54 String className = "org.objectweb.celtix.application.test.GreeterWithConstructorDelay"; 55 56 GetPluginThread t1 = new GetPluginThread(className); 57 GetPluginThread t2 = new GetPluginThread(className); 58 t1.start(); 59 try { 60 Thread.sleep(100); 61 } catch (InterruptedException ex) { 62 } 64 t2.start(); 65 66 try { 67 t1.join(); 68 t2.join(); 69 } catch (InterruptedException ex) { 70 } 72 73 assertTrue(t1.getPlugin() == t2.getPlugin()); 74 } 75 76 class GetPluginThread extends Thread { 77 78 String className; 79 Object plugin; 80 81 GetPluginThread(String cn) { 82 className = cn; 83 } 84 85 public Object getPlugin() { 86 return plugin; 87 } 88 89 public void run() { 90 PluginManager pm = Application.getInstance().getPluginManager(); 91 try { 92 plugin = pm.getPlugin(className); 93 } catch (PluginException ex) { 94 } 96 } 97 } 98 99 } 100 | Popular Tags |