KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > conform > TestLifeCycleController


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

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   // -------------------------------------------------------------------------
49
// Constructor ans setup
50
// -------------------------------------------------------------------------
51

52   public TestLifeCycleController (final String JavaDoc name) {
53     super(name);
54   }
55
56   protected void setUp () throws Exception JavaDoc {
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 JavaDoc {
70     c = gf.newFcInstance(t, "flatPrimitive", C.class.getName());
71     d = gf.newFcInstance(t, "flatPrimitive", C.class.getName());
72   }
73
74   // -------------------------------------------------------------------------
75
// Test started and stopped states
76
// -------------------------------------------------------------------------
77

78   public void testStarted () throws Exception JavaDoc {
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 JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
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 JavaDoc {
93     final I i = (I)c.getFcInterface("server");
94     Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
95       public void run () { i.m(true); }
96     });
97     t.start();
98     t.join(50);
99     assertTrue(t.isAlive());
100   }
101
102   // -------------------------------------------------------------------------
103
// Test errors in start
104
// -------------------------------------------------------------------------
105

106   public void testMandatoryInterfaceNotBound () throws Exception JavaDoc {
107     try {
108       Fractal.getLifeCycleController(c).startFc();
109       fail();
110     } catch (IllegalLifeCycleException e) {
111       assertEquals("STOPPED", Fractal.getLifeCycleController(c).getFcState());
112     }
113   }
114
115   // -------------------------------------------------------------------------
116
// Test invalid operations in started state
117
// -------------------------------------------------------------------------
118

119   public void testUnbindNotStopped () throws Exception JavaDoc {
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   // ---
136

137   public static class Composite extends TestLifeCycleController {
138
139     protected Component r;
140
141     public Composite (String JavaDoc name) {
142       super(name);
143     }
144
145     protected void setUpComponents () throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
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 JavaDoc(new Runnable JavaDoc() {
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 JavaDoc {
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 JavaDoc {
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