001: package org.objectweb.celtix.application;
002:
003: import junit.framework.TestCase;
004:
005: import org.objectweb.celtix.plugins.PluginException;
006: import org.objectweb.celtix.plugins.PluginManager;
007:
008: public class ApplicationPluginManagerTest extends TestCase {
009:
010: public void testGetPluginClassNotFound() {
011: ApplicationPluginManager apm = new ApplicationPluginManager();
012: try {
013: apm
014: .getPlugin("org.objectweb.celtix.application.test.Greater");
015: fail("Expected PluginException not thrown");
016: } catch (PluginException ex) {
017: assertEquals("LOAD_FAILED_EXC", ex.getCode());
018: assertTrue(ex.getCause() instanceof ClassNotFoundException);
019: }
020: }
021:
022: public void testGetPluginIllegalAccess() {
023: ApplicationPluginManager apm = new ApplicationPluginManager();
024: try {
025: apm
026: .getPlugin("org.objectweb.celtix.application.test.ProtectedConstructorGreeter");
027: fail("Expected PluginException not thrown");
028: } catch (PluginException ex) {
029: assertEquals("LOAD_FAILED_EXC", ex.getCode());
030: assertTrue("Unexepcted cause: "
031: + ex.getCause().getClass().getName(),
032: ex.getCause() instanceof IllegalAccessException);
033: }
034: }
035:
036: public void testGetPluginNoDefaultConstructor() {
037: ApplicationPluginManager apm = new ApplicationPluginManager();
038: try {
039: apm
040: .getPlugin("org.objectweb.celtix.application.test.PersonalGreeter");
041: fail("Expected PluginException not thrown");
042: } catch (PluginException ex) {
043: assertEquals("LOAD_FAILED_EXC", ex.getCode());
044: assertTrue(ex.getCause() instanceof InstantiationException);
045: }
046: }
047:
048: public void testGetPlugin() throws PluginException {
049: ApplicationPluginManager apm = new ApplicationPluginManager();
050: Object object = apm
051: .getPlugin("org.objectweb.celtix.application.test.Greeter");
052: Object otherObject = apm
053: .createPlugin("org.objectweb.celtix.application.test.Greeter");
054: assertTrue(object != otherObject);
055: otherObject = apm
056: .getPlugin("org.objectweb.celtix.application.test.Greeter");
057: assertTrue(object == otherObject);
058: }
059:
060: public void testConcurrentLoad() throws PluginException {
061: String className = "org.objectweb.celtix.application.test.GreeterWithConstructorDelay";
062:
063: GetPluginThread t1 = new GetPluginThread(className);
064: GetPluginThread t2 = new GetPluginThread(className);
065: t1.start();
066: try {
067: Thread.sleep(100);
068: } catch (InterruptedException ex) {
069: // ignore
070: }
071: t2.start();
072:
073: try {
074: t1.join();
075: t2.join();
076: } catch (InterruptedException ex) {
077: // ignore
078: }
079:
080: assertTrue(t1.getPlugin() == t2.getPlugin());
081: }
082:
083: class GetPluginThread extends Thread {
084:
085: String className;
086: Object plugin;
087:
088: GetPluginThread(String cn) {
089: className = cn;
090: }
091:
092: public Object getPlugin() {
093: return plugin;
094: }
095:
096: public void run() {
097: PluginManager pm = Application.getInstance()
098: .getPluginManager();
099: try {
100: plugin = pm.getPlugin(className);
101: } catch (PluginException ex) {
102: // ignore
103: }
104: }
105: }
106:
107: }
|