1 23 24 package org.objectweb.fractal.julia.conform; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 28 import org.objectweb.fractal.api.control.ContentController; 29 import org.objectweb.fractal.api.factory.GenericFactory; 30 import org.objectweb.fractal.api.type.TypeFactory; 31 import org.objectweb.fractal.api.type.ComponentType; 32 import org.objectweb.fractal.api.type.InterfaceType; 33 34 import org.objectweb.fractal.julia.conform.components.I; 35 import org.objectweb.fractal.julia.conform.components.C; 36 37 import org.objectweb.fractal.util.Fractal; 38 39 public class TestLifeCycleController extends Test { 40 41 protected Component boot; 42 protected TypeFactory tf; 43 protected GenericFactory gf; 44 45 protected ComponentType t; 46 protected Component c, d; 47 48 52 public TestLifeCycleController (final String name) { 53 super(name); 54 } 55 56 protected void setUp () throws Exception { 57 boot = Fractal.getBootstrapComponent(); 58 tf = Fractal.getTypeFactory(boot); 59 gf = Fractal.getGenericFactory(boot); 60 t = tf.createFcType(new InterfaceType[] { 61 tf.createFcItfType("server", I.class.getName(), false, false, false), 62 tf.createFcItfType("servers", I.class.getName(), false, false, true), 63 tf.createFcItfType("client", I.class.getName(), true, false, false), 64 tf.createFcItfType("clients", I.class.getName(), true, false, true) 65 }); 66 setUpComponents(); 67 } 68 69 protected void setUpComponents () throws Exception { 70 c = gf.newFcInstance(t, "flatPrimitive", C.class.getName()); 71 d = gf.newFcInstance(t, "flatPrimitive", C.class.getName()); 72 } 73 74 78 public void testStarted () throws Exception { 79 Fractal.getBindingController(c).bindFc("client", d.getFcInterface("server")); 80 assertEquals("STOPPED", Fractal.getLifeCycleController(c).getFcState()); 81 Fractal.getLifeCycleController(c).startFc(); 82 assertEquals("STARTED", Fractal.getLifeCycleController(c).getFcState()); 83 final I i = (I)c.getFcInterface("server"); 84 Thread t = new Thread (new Runnable () { 85 public void run () { i.m(true); } 86 }); 87 t.start(); 88 t.join(50); 89 assertTrue(!t.isAlive()); 90 } 91 92 public void testStopped () throws Exception { 93 final I i = (I)c.getFcInterface("server"); 94 Thread t = new Thread (new Runnable () { 95 public void run () { i.m(true); } 96 }); 97 t.start(); 98 t.join(50); 99 assertTrue(t.isAlive()); 100 } 101 102 106 public void testMandatoryInterfaceNotBound () throws Exception { 107 try { 108 Fractal.getLifeCycleController(c).startFc(); 109 fail(); 110 } catch (IllegalLifeCycleException e) { 111 assertEquals("STOPPED", Fractal.getLifeCycleController(c).getFcState()); 112 } 113 } 114 115 119 public void testUnbindNotStopped () throws Exception { 120 Fractal.getBindingController(c).bindFc("client", d.getFcInterface("server")); 121 Fractal.getBindingController(c).bindFc("clients0", d.getFcInterface("server")); 122 Fractal.getLifeCycleController(c).startFc(); 123 try { 124 Fractal.getBindingController(c).unbindFc("client"); 125 fail(); 126 } catch (IllegalLifeCycleException e) { 127 } 128 try { 129 Fractal.getBindingController(c).unbindFc("clients0"); 130 fail(); 131 } catch (IllegalLifeCycleException e) { 132 } 133 } 134 135 137 public static class Composite extends TestLifeCycleController { 138 139 protected Component r; 140 141 public Composite (String name) { 142 super(name); 143 } 144 145 protected void setUpComponents () throws Exception { 146 Component o = gf.newFcInstance(t, "composite", null); 147 r = gf.newFcInstance(t, "composite", null); 148 c = gf.newFcInstance(t, "primitive", C.class.getName()); 149 d = gf.newFcInstance(t, "primitive", C.class.getName()); 150 Fractal.getContentController(o).addFcSubComponent(r); 151 Fractal.getContentController(r).addFcSubComponent(c); 152 Fractal.getContentController(r).addFcSubComponent(d); 153 } 154 155 public void testRecursiveStartStop () throws Exception { 156 ContentController cc = Fractal.getContentController(r); 157 Fractal.getBindingController(r).bindFc("server", c.getFcInterface("server")); 158 Fractal.getBindingController(c).bindFc("client", d.getFcInterface("server")); 159 Fractal.getBindingController(d).bindFc("client", cc.getFcInternalInterface("client")); 160 Fractal.getBindingController(r).bindFc("client", r.getFcInterface("server")); 161 162 Fractal.getLifeCycleController(r).startFc(); 163 assertEquals("STARTED", Fractal.getLifeCycleController(r).getFcState()); 164 assertEquals("STARTED", Fractal.getLifeCycleController(c).getFcState()); 165 assertEquals("STARTED", Fractal.getLifeCycleController(d).getFcState()); 166 final I i = (I)r.getFcInterface("server"); 167 Thread t = new Thread (new Runnable () { 168 public void run () { i.m(true); } 169 }); 170 t.start(); 171 t.join(50); 172 assertTrue(!t.isAlive()); 173 174 Fractal.getLifeCycleController(r).stopFc(); 175 assertEquals("STOPPED", Fractal.getLifeCycleController(r).getFcState()); 176 assertEquals("STOPPED", Fractal.getLifeCycleController(c).getFcState()); 177 assertEquals("STOPPED", Fractal.getLifeCycleController(d).getFcState()); 178 t = new Thread (new Runnable () { 179 public void run () { i.m(true); } 180 }); 181 t.start(); 182 t.join(50); 183 assertTrue(t.isAlive()); 184 } 185 186 public void testMandatoryInterfaceNotBound () throws Exception { 187 super.testMandatoryInterfaceNotBound(); 188 ContentController cc = Fractal.getContentController(r); 189 cc.removeFcSubComponent(c); 190 cc.removeFcSubComponent(d); 191 Fractal.getBindingController(r).bindFc("client", r.getFcInterface("server")); 192 try { 193 Fractal.getLifeCycleController(c).startFc(); 194 fail(); 195 } catch (IllegalLifeCycleException e) { 196 } 197 } 198 199 public void testRemoveNotStopped () throws Exception { 200 ContentController cc = Fractal.getContentController(r); 201 Fractal.getBindingController(r).bindFc("server", c.getFcInterface("server")); 202 Fractal.getBindingController(c).bindFc("client", cc.getFcInternalInterface("client")); 203 Fractal.getBindingController(r).bindFc("client", r.getFcInterface("server")); 204 cc.removeFcSubComponent(d); 205 Fractal.getLifeCycleController(r).startFc(); 206 cc.addFcSubComponent(d); 207 try { 208 cc.removeFcSubComponent(d); 209 fail(); 210 } catch (IllegalLifeCycleException e) { 211 } 212 } 213 } 214 } 215 | Popular Tags |