KickJava   Java API By Example, From Geeks To Geeks.

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


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.type.InterfaceType;
27 import org.objectweb.fractal.api.type.TypeFactory;
28 import org.objectweb.fractal.api.type.ComponentType;
29 import org.objectweb.fractal.api.Component;
30 import org.objectweb.fractal.api.control.ContentController;
31 import org.objectweb.fractal.api.control.IllegalContentException;
32 import org.objectweb.fractal.api.factory.GenericFactory;
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 import java.util.Arrays JavaDoc;
40
41 public class TestContentController extends Test {
42
43   protected Component boot;
44   protected TypeFactory tf;
45   protected GenericFactory gf;
46
47   protected ComponentType t;
48   protected Component c, d, e;
49
50   // -------------------------------------------------------------------------
51
// Constructor ans setup
52
// -------------------------------------------------------------------------
53

54   public TestContentController (final String JavaDoc name) {
55     super(name);
56   }
57
58   protected void setUp () throws Exception JavaDoc {
59     boot = Fractal.getBootstrapComponent();
60     tf = Fractal.getTypeFactory(boot);
61     gf = Fractal.getGenericFactory(boot);
62     t = tf.createFcType(new InterfaceType[] {
63       tf.createFcItfType("server", I.class.getName(), false, false, false),
64       tf.createFcItfType("client", I.class.getName(), true, true, false)
65     });
66     setUpComponents();
67   }
68
69   protected void setUpComponents () throws Exception JavaDoc {
70     c = gf.newFcInstance(t, "composite", null);
71     d = gf.newFcInstance(t, "composite", null);
72     e = gf.newFcInstance(t, "primitive", C.class.getName());
73   }
74
75   // -------------------------------------------------------------------------
76
// Test add and remove
77
// -------------------------------------------------------------------------
78

79   public void testAddAndRemove () throws Exception JavaDoc {
80     ContentController cc = Fractal.getContentController(c);
81     cc.addFcSubComponent(e);
82     assertTrue(Arrays.asList(cc.getFcSubComponents()).contains(e));
83     cc.removeFcSubComponent(e);
84     assertTrue(!Arrays.asList(cc.getFcSubComponents()).contains(e));
85   }
86
87   // -------------------------------------------------------------------------
88
// Test add errors
89
// -------------------------------------------------------------------------
90

91   public void testAlreadySubComponent () throws Exception JavaDoc {
92     ContentController cc = Fractal.getContentController(c);
93     cc.addFcSubComponent(e);
94     try {
95       cc.addFcSubComponent(e);
96       fail();
97     } catch (IllegalContentException e) {
98     }
99   }
100
101   public void testWouldCreateCycle1 () throws Exception JavaDoc {
102     ContentController cc = Fractal.getContentController(c);
103     try {
104       cc.addFcSubComponent(c);
105       fail();
106     } catch (IllegalContentException e) {
107     }
108   }
109
110   public void testWouldCreateCycle2 () throws Exception JavaDoc {
111     ContentController cc = Fractal.getContentController(c);
112     ContentController cd = Fractal.getContentController(d);
113     cc.addFcSubComponent(d);
114     try {
115       cd.addFcSubComponent(c);
116       fail();
117     } catch (IllegalContentException e) {
118     }
119   }
120
121   // -------------------------------------------------------------------------
122
// Test remove errors
123
// -------------------------------------------------------------------------
124

125   public void testNotASubComponent () throws Exception JavaDoc {
126     ContentController cc = Fractal.getContentController(c);
127     try {
128       cc.removeFcSubComponent(d);
129       fail();
130     } catch (IllegalContentException e) {
131     }
132   }
133
134   public void testWouldCreateNonLocalExportBinding () throws Exception JavaDoc {
135     ContentController cc = Fractal.getContentController(c);
136     cc.addFcSubComponent(e);
137     Fractal.getBindingController(c).bindFc("server", e.getFcInterface("server"));
138     try {
139       cc.removeFcSubComponent(e);
140       fail();
141     } catch (IllegalContentException e) {
142     }
143   }
144
145   public void testWouldCreateNonLocalImportBinding () throws Exception JavaDoc {
146     ContentController cc = Fractal.getContentController(c);
147     cc.addFcSubComponent(e);
148     Fractal.getBindingController(e).bindFc("client", cc.getFcInternalInterface("client"));
149     try {
150       cc.removeFcSubComponent(e);
151       fail();
152     } catch (IllegalContentException e) {
153     }
154   }
155
156   public void testWouldCreateNonLocalNormalBinding () throws Exception JavaDoc {
157     ContentController cc = Fractal.getContentController(c);
158     cc.addFcSubComponent(d);
159     cc.addFcSubComponent(e);
160     Fractal.getBindingController(d).bindFc("client", e.getFcInterface("server"));
161     try {
162       cc.removeFcSubComponent(e);
163       fail();
164     } catch (IllegalContentException e) {
165     }
166   }
167
168   // ---
169

170   public static class Template extends TestContentController {
171
172     public Template (String JavaDoc name) {
173       super(name);
174     }
175
176     protected void setUpComponents () throws Exception JavaDoc {
177       c = gf.newFcInstance(t, "compositeTemplate", new Object JavaDoc[] { "composite", null });
178       d = gf.newFcInstance(t, "compositeTemplate", new Object JavaDoc[] { "composite", null });
179       e = gf.newFcInstance(t, "primitiveTemplate", new Object JavaDoc[] { "primitive", C.class.getName() });
180     }
181
182     public void testInstanceContent () throws Exception JavaDoc {
183       Component r = gf.newFcInstance(t, "compositeTemplate", new Object JavaDoc[] { "composite", null });
184       Fractal.getContentController(r).addFcSubComponent(c);
185       Fractal.getContentController(r).addFcSubComponent(d);
186       Fractal.getContentController(c).addFcSubComponent(e);
187       Fractal.getContentController(d).addFcSubComponent(e);
188
189       Component root = Fractal.getFactory(r).newFcInstance();
190       Component[] comps = Fractal.getContentController(root).getFcSubComponents();
191       assertEquals(2, comps.length);
192       Component[] cComps = Fractal.getContentController(comps[0]).getFcSubComponents();
193       Component[] dComps = Fractal.getContentController(comps[1]).getFcSubComponents();
194       assertEquals(1, cComps.length);
195       assertEquals(1, dComps.length);
196       assertEquals(cComps[0], dComps[0]);
197     }
198   }
199 }
200
Popular Tags