KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > GBeanTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.kernel;
19
20 import java.net.URL JavaDoc;
21 import java.net.URLClassLoader JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.extensions.TestSetup;
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 import org.apache.geronimo.gbean.AbstractName;
30 import org.apache.geronimo.gbean.AbstractNameQuery;
31 import org.apache.geronimo.gbean.GBeanData;
32 import org.apache.geronimo.gbean.GBeanInfo;
33 import org.apache.geronimo.gbean.GBeanInfoBuilder;
34 import org.apache.geronimo.kernel.management.State;
35 import org.apache.geronimo.kernel.proxy.ProxyFactory;
36 import org.apache.geronimo.kernel.proxy.ProxyManager;
37 import org.apache.geronimo.kernel.repository.Artifact;
38
39 /**
40  * @version $Rev:386515 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  */

42 public class GBeanTest extends TestCase {
43     private static KernelWraper kernelWraper = new KernelWraper();
44     private Kernel realKernel;
45     private Kernel kernel;
46
47     /**
48      * Test with plain unwrapped kernel
49      */

50     private static class KernelWraper {
51         public Kernel wrap(Kernel kernel) {
52             return kernel;
53         }
54     }
55
56     /**
57      * Test with kernel wrapped in KernelGBean
58      */

59     private static class GBeanKernelLifecycle extends KernelWraper {
60         public Kernel wrap(Kernel kernel) {
61             return new KernelGBean(kernel);
62         }
63     }
64
65     public static Test suite() throws Exception JavaDoc {
66         TestSuite suite = new TestSuite();
67         TestSuite inner = new TestSuite(GBeanTest.class);
68
69         suite.addTest(new TestSetup(inner) {
70             protected void setUp() throws Exception JavaDoc {
71                 super.setUp();
72                 kernelWraper = new KernelWraper();
73             }
74         });
75
76         suite.addTest(new TestSetup(inner) {
77             protected void setUp() throws Exception JavaDoc {
78                 super.setUp();
79                 kernelWraper = new GBeanKernelLifecycle();
80             }
81         });
82
83         return suite;
84     }
85
86     public void testListGBeans() throws Exception JavaDoc {
87         GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
88
89         kernel.loadGBean(gbean, getClass().getClassLoader());
90         kernel.startGBean(gbean.getAbstractName());
91         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean.getAbstractName()));
92
93         Set JavaDoc gbeans = kernel.listGBeans(new AbstractNameQuery(gbean.getAbstractName(), gbean.getGBeanInfo().getInterfaces()));
94         assertEquals(1, gbeans.size());
95         assertEquals(gbean.getAbstractName(), gbeans.iterator().next());
96     }
97
98     public void testLifeCycle() throws Exception JavaDoc {
99         GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
100         gbean.setAttribute("finalInt", new Integer JavaDoc(123));
101
102         // abstract name
103
assertFalse(kernel.isLoaded(gbean.getAbstractName()));
104         assertFalse(kernel.isRunning(gbean.getAbstractName()));
105
106         kernel.loadGBean(gbean, getClass().getClassLoader());
107         assertTrue(kernel.isLoaded(gbean.getAbstractName()));
108         assertFalse(kernel.isRunning(gbean.getAbstractName()));
109
110         kernel.startGBean(gbean.getAbstractName());
111         assertTrue(kernel.isLoaded(gbean.getAbstractName()));
112         assertTrue(kernel.isRunning(gbean.getAbstractName()));
113
114         kernel.stopGBean(gbean.getAbstractName());
115         assertTrue(kernel.isLoaded(gbean.getAbstractName()));
116         assertFalse(kernel.isRunning(gbean.getAbstractName()));
117
118         kernel.unloadGBean(gbean.getAbstractName());
119         assertFalse(kernel.isLoaded(gbean.getAbstractName()));
120         assertFalse(kernel.isRunning(gbean.getAbstractName()));
121
122
123         // short name
124
assertFalse(kernel.isLoaded("name"));
125         assertFalse(kernel.isRunning("name"));
126
127         kernel.loadGBean(gbean, getClass().getClassLoader());
128         assertTrue(kernel.isLoaded("name"));
129         assertFalse(kernel.isRunning("name"));
130
131         kernel.startGBean("name");
132         assertTrue(kernel.isLoaded("name"));
133         assertTrue(kernel.isRunning("name"));
134
135         kernel.stopGBean("name");
136         assertTrue(kernel.isLoaded("name"));
137         assertFalse(kernel.isRunning("name"));
138
139         kernel.unloadGBean("name");
140         assertFalse(kernel.isLoaded("name"));
141         assertFalse(kernel.isRunning("name"));
142
143
144         // type
145
assertFalse(kernel.isLoaded(MockGBean.class));
146         assertFalse(kernel.isRunning(MockGBean.class));
147
148         kernel.loadGBean(gbean, getClass().getClassLoader());
149         assertTrue(kernel.isLoaded(MockGBean.class));
150         assertFalse(kernel.isRunning(MockGBean.class));
151
152         kernel.startGBean(MockGBean.class);
153         assertTrue(kernel.isLoaded(MockGBean.class));
154         assertTrue(kernel.isRunning(MockGBean.class));
155
156         kernel.stopGBean(MockGBean.class);
157         assertTrue(kernel.isLoaded(MockGBean.class));
158         assertFalse(kernel.isRunning(MockGBean.class));
159
160         kernel.unloadGBean(MockGBean.class);
161         assertFalse(kernel.isLoaded(MockGBean.class));
162         assertFalse(kernel.isRunning(MockGBean.class));
163
164
165         // short name and type
166
assertFalse(kernel.isLoaded("name", MockGBean.class));
167         assertFalse(kernel.isRunning("name", MockGBean.class));
168
169         kernel.loadGBean(gbean, getClass().getClassLoader());
170         assertTrue(kernel.isLoaded("name", MockGBean.class));
171         assertFalse(kernel.isRunning("name", MockGBean.class));
172
173         kernel.startGBean("name", MockGBean.class);
174         assertTrue(kernel.isLoaded("name", MockGBean.class));
175         assertTrue(kernel.isRunning("name", MockGBean.class));
176
177         kernel.stopGBean("name", MockGBean.class);
178         assertTrue(kernel.isLoaded("name", MockGBean.class));
179         assertFalse(kernel.isRunning("name", MockGBean.class));
180
181         kernel.unloadGBean("name", MockGBean.class);
182         assertFalse(kernel.isLoaded("name", MockGBean.class));
183         assertFalse(kernel.isRunning("name", MockGBean.class));
184     }
185
186     public void testGetGBean() throws Exception JavaDoc {
187         GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
188         gbean.setAttribute("finalInt", new Integer JavaDoc(123));
189
190         kernel.loadGBean(gbean, getClass().getClassLoader());
191         kernel.startGBean(gbean.getAbstractName());
192         assertTrue(kernel.isRunning(gbean.getAbstractName()));
193
194         // abstract name
195
MockGBean mockGBean = (MockGBean) kernel.getGBean(gbean.getAbstractName());
196         assertEquals(123, mockGBean.getFinalInt());
197         assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean));
198         assertEquals("name", kernel.getShortNameFor(mockGBean));
199
200         // short name
201
mockGBean = (MockGBean) kernel.getGBean("name");
202         assertEquals(123, mockGBean.getFinalInt());
203         assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean));
204         assertEquals("name", kernel.getShortNameFor(mockGBean));
205
206         // type
207
mockGBean = (MockGBean) kernel.getGBean(MockGBean.class);
208         assertEquals(123, mockGBean.getFinalInt());
209         assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean));
210         assertEquals("name", kernel.getShortNameFor(mockGBean));
211
212         // short name and type
213
mockGBean = (MockGBean) kernel.getGBean("name", MockGBean.class);
214         assertEquals(123, mockGBean.getFinalInt());
215         assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean));
216         assertEquals("name", kernel.getShortNameFor(mockGBean));
217     }
218
219     public void testInvoke() throws Exception JavaDoc {
220         GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
221         String JavaDoc testValue = "test-value";
222         String JavaDoc otherValue = "other-value";
223         gbean.setAttribute("value", testValue);
224
225         kernel.loadGBean(gbean, getClass().getClassLoader());
226         kernel.startGBean(gbean.getAbstractName());
227         assertTrue(kernel.isRunning(gbean.getAbstractName()));
228         MockGBean mockGBean = (MockGBean) kernel.getGBean(gbean.getAbstractName());
229
230         //
231
// abstract name
232
//
233

234         // get and set on gbean
235
assertEquals(testValue, mockGBean.getValue());
236         mockGBean.setValue(otherValue);
237         assertEquals(otherValue, mockGBean.getValue());
238         mockGBean.setValue(testValue);
239
240         // get and set with kernel
241
assertEquals(testValue, kernel.getAttribute(gbean.getAbstractName(), "value"));
242         kernel.setAttribute(gbean.getAbstractName(), "value", otherValue);
243         assertEquals(otherValue, kernel.getAttribute(gbean.getAbstractName(), "value"));
244         kernel.setAttribute(gbean.getAbstractName(), "value", testValue);
245         assertEquals(testValue, kernel.getAttribute(gbean.getAbstractName(), "value"));
246
247         // invoke
248
assertEquals(testValue, mockGBean.fetchValue());
249         assertEquals(testValue, kernel.invoke(gbean.getAbstractName(), "fetchValue"));
250         assertEquals(testValue, mockGBean.doSomething(testValue));
251         assertEquals(testValue, kernel.invoke(gbean.getAbstractName(), "doSomething", new Object JavaDoc[] {testValue}, new String JavaDoc[] {String JavaDoc.class.getName()}));
252         assertEquals(testValue, mockGBean.getValue());
253
254         //
255
// short name
256
//
257

258         // get and set on gbean
259
assertEquals(testValue, mockGBean.getValue());
260         mockGBean.setValue(otherValue);
261         assertEquals(otherValue, mockGBean.getValue());
262         mockGBean.setValue(testValue);
263
264         // get and set with kernel
265
assertEquals(testValue, kernel.getAttribute("name", "value"));
266         kernel.setAttribute("name", "value", otherValue);
267         assertEquals(otherValue, kernel.getAttribute("name", "value"));
268         kernel.setAttribute("name", "value", testValue);
269         assertEquals(testValue, kernel.getAttribute("name", "value"));
270
271         // invoke
272
assertEquals(testValue, mockGBean.fetchValue());
273         assertEquals(testValue, kernel.invoke("name", "fetchValue"));
274         assertEquals(testValue, mockGBean.doSomething(testValue));
275         assertEquals(testValue, kernel.invoke("name", "doSomething", new Object JavaDoc[] {testValue}, new String JavaDoc[] {String JavaDoc.class.getName()}));
276         assertEquals(testValue, mockGBean.getValue());
277
278         //
279
// type
280
//
281

282         // get and set on gbean
283
assertEquals(testValue, mockGBean.getValue());
284         mockGBean.setValue(otherValue);
285         assertEquals(otherValue, mockGBean.getValue());
286         mockGBean.setValue(testValue);
287
288         // get and set with kernel
289
assertEquals(testValue, kernel.getAttribute(MockGBean.class, "value"));
290         kernel.setAttribute(MockGBean.class, "value", otherValue);
291         assertEquals(otherValue, kernel.getAttribute(MockGBean.class, "value"));
292         kernel.setAttribute(MockGBean.class, "value", testValue);
293         assertEquals(testValue, kernel.getAttribute(MockGBean.class, "value"));
294
295         // invoke
296
assertEquals(testValue, mockGBean.fetchValue());
297         assertEquals(testValue, kernel.invoke(MockGBean.class, "fetchValue"));
298         assertEquals(testValue, mockGBean.doSomething(testValue));
299         assertEquals(testValue, kernel.invoke(MockGBean.class, "doSomething", new Object JavaDoc[] {testValue}, new String JavaDoc[] {String JavaDoc.class.getName()}));
300         assertEquals(testValue, mockGBean.getValue());
301
302         //
303
// short name and type
304
//
305

306         // get and set on gbean
307
assertEquals(testValue, mockGBean.getValue());
308         mockGBean.setValue(otherValue);
309         assertEquals(otherValue, mockGBean.getValue());
310         mockGBean.setValue(testValue);
311
312         // get and set with kernel
313
assertEquals(testValue, kernel.getAttribute("name", MockGBean.class, "value"));
314         kernel.setAttribute("name", MockGBean.class, "value", otherValue);
315         assertEquals(otherValue, kernel.getAttribute("name", MockGBean.class, "value"));
316         kernel.setAttribute("name", MockGBean.class, "value", testValue);
317         assertEquals(testValue, kernel.getAttribute("name", MockGBean.class, "value"));
318
319         // invoke
320
assertEquals(testValue, mockGBean.fetchValue());
321         assertEquals(testValue, kernel.invoke("name", MockGBean.class, "fetchValue"));
322         assertEquals(testValue, mockGBean.doSomething(testValue));
323         assertEquals(testValue, kernel.invoke("name", MockGBean.class, "doSomething", new Object JavaDoc[] {testValue}, new String JavaDoc[] {String JavaDoc.class.getName()}));
324         assertEquals(testValue, mockGBean.getValue());
325
326     }
327
328     public void testLoad() throws Exception JavaDoc {
329         ClassLoader JavaDoc cl = getClass().getClassLoader();
330         ClassLoader JavaDoc myCl = new URLClassLoader JavaDoc(new URL JavaDoc[0], cl);
331         GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
332         gbean.setAttribute("name", "Test");
333         gbean.setAttribute("finalInt", new Integer JavaDoc(123));
334
335         kernel.loadGBean(gbean, myCl);
336         kernel.startGBean(gbean.getAbstractName());
337         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean.getAbstractName()));
338         assertEquals("Hello", kernel.invoke(gbean.getAbstractName(), "doSomething", new Object JavaDoc[]{"Hello"}, new String JavaDoc[]{String JavaDoc.class.getName()}));
339
340         assertEquals(gbean.getAbstractName().getObjectName().getCanonicalName(), kernel.getAttribute(gbean.getAbstractName(), "objectName"));
341         assertEquals(gbean.getAbstractName().getObjectName().getCanonicalName(), kernel.getAttribute(gbean.getAbstractName(), "actualObjectName"));
342
343         assertSame(myCl, kernel.getAttribute(gbean.getAbstractName(), "actualClassLoader"));
344
345         // the MockGBean implemmentation of getConfigurationClassLoader will throw an exception, but since the GBean architecture
346
// handles this directly the implementation method will never be called
347
kernel.getAttribute(gbean.getAbstractName(), "classLoader");
348
349         assertSame(realKernel, kernel.getAttribute(gbean.getAbstractName(), "kernel"));
350         assertSame(realKernel, kernel.getAttribute(gbean.getAbstractName(), "actualKernel"));
351
352         kernel.stopGBean(gbean.getAbstractName());
353         kernel.unloadGBean(gbean.getAbstractName());
354     }
355
356     public void testEndpoint() throws Exception JavaDoc {
357         ClassLoader JavaDoc cl = MockGBean.class.getClassLoader();
358         GBeanData gbean1 = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
359         gbean1.setAttribute("finalInt", new Integer JavaDoc(123));
360         kernel.loadGBean(gbean1, cl);
361         kernel.startGBean(gbean1.getAbstractName());
362
363         GBeanData gbean2 = buildGBeanData("name", "test2", MockGBean.getGBeanInfo());
364         gbean2.setAttribute("finalInt", new Integer JavaDoc(123));
365         gbean2.setReferencePattern("MockEndpoint", gbean1.getAbstractName());
366         kernel.loadGBean(gbean2, cl);
367         kernel.startGBean(gbean2.getAbstractName());
368
369         assertEquals("endpointCheck", kernel.invoke(gbean2.getAbstractName(), "checkEndpoint", null, null));
370     }
371
372     public void testNoProxyEndpoint() throws Exception JavaDoc {
373         ClassLoader JavaDoc cl = MockGBean.class.getClassLoader();
374         GBeanData gbean1 = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
375         gbean1.setAttribute("finalInt", new Integer JavaDoc(123));
376         kernel.loadGBean(gbean1, cl);
377         kernel.startGBean(gbean1.getAbstractName());
378
379         GBeanData gbean2 = buildGBeanData("name", "test2", MockGBean.getGBeanInfo());
380         gbean2.setAttribute("finalInt", new Integer JavaDoc(123));
381         gbean2.setReferencePattern("MockEndpoint", gbean1.getAbstractName());
382         kernel.loadGBean(gbean2, cl);
383         kernel.startGBean(gbean2.getAbstractName());
384
385
386         MockGBean mockGBean1 = (MockGBean) kernel.getGBean(gbean1.getAbstractName());
387         MockGBean mockGBean2 = (MockGBean) kernel.getGBean(gbean2.getAbstractName());
388         // this can only be tested if no proxy is on
389
// assertSame(mockGBean2.getMockEndpoint(), mockGBean1);
390
}
391
392     public void testProxiesInterfaces() throws Exception JavaDoc {
393         ClassLoader JavaDoc cl = getClass().getClassLoader();
394         ClassLoader JavaDoc myCl = new URLClassLoader JavaDoc(new URL JavaDoc[0], cl);
395         GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo());
396         gbean.setAttribute("name", "Test");
397         gbean.setAttribute("finalInt", new Integer JavaDoc(123));
398         kernel.loadGBean(gbean, myCl);
399         kernel.startGBean(gbean.getAbstractName());
400         ProxyManager mgr = kernel.getProxyManager();
401
402         Object JavaDoc test = mgr.createProxy(gbean.getAbstractName(), myCl);
403         assertTrue(test instanceof MockEndpoint);
404         assertTrue(test instanceof MockParentInterface1);
405         assertTrue(test instanceof MockParentInterface2);
406         assertTrue(test instanceof MockChildInterface1);
407         assertTrue(test instanceof MockChildInterface2);
408         assertFalse(test instanceof Comparable JavaDoc);
409         ((MockEndpoint)test).doNothing();
410         assertEquals("Foo", ((MockEndpoint)test).echo("Foo"));
411         ((MockParentInterface1)test).setValue("Foo");
412         assertEquals("Foo", ((MockParentInterface1)test).getValue());
413         ((MockParentInterface1)test).setMutableInt(6);
414         assertEquals(6, ((MockParentInterface1)test).getMutableInt());
415         ((MockParentInterface2)test).doNothing();
416         assertEquals("Foo", ((MockParentInterface2)test).echo("Foo"));
417         ((MockParentInterface2)test).setValue("Foo");
418         assertEquals("Foo", ((MockParentInterface2)test).getValue());
419         ((MockChildInterface1)test).getFinalInt();
420         ((MockChildInterface2)test).doNothing();
421         assertEquals("Foo", ((MockChildInterface2)test).doSomething("Foo"));
422
423         test = mgr.createProxy(gbean.getAbstractName(), MockEndpoint.class);
424         assertTrue(test instanceof MockEndpoint);
425         assertTrue(test instanceof MockParentInterface1);
426         assertTrue(test instanceof MockParentInterface2);
427         assertTrue(test instanceof MockChildInterface1);
428         assertTrue(test instanceof MockChildInterface2);
429         assertFalse(test instanceof Comparable JavaDoc);
430
431         ProxyFactory proxyFactory;
432         proxyFactory = mgr.createProxyFactory(new Class JavaDoc[]{MockEndpoint.class, MockParentInterface2.class, MockChildInterface2.class}, myCl);
433         test = proxyFactory.createProxy(gbean.getAbstractName());
434         assertTrue(test instanceof MockEndpoint);
435         assertTrue(test instanceof MockParentInterface1);
436         assertTrue(test instanceof MockParentInterface2);
437         assertTrue(test instanceof MockChildInterface1);
438         assertTrue(test instanceof MockChildInterface2);
439         assertFalse(test instanceof Comparable JavaDoc);
440
441         proxyFactory = mgr.createProxyFactory(new Class JavaDoc[]{MockEndpoint.class, MockParentInterface1.class, MockChildInterface1.class}, myCl);
442         test = proxyFactory.createProxy(gbean.getAbstractName());
443         assertTrue(test instanceof MockEndpoint);
444         assertTrue(test instanceof MockParentInterface1);
445         assertFalse(test instanceof MockParentInterface2);
446         assertTrue(test instanceof MockChildInterface1);
447         assertFalse(test instanceof MockChildInterface2);
448         assertFalse(test instanceof Comparable JavaDoc);
449
450         proxyFactory = mgr.createProxyFactory(new Class JavaDoc[]{MockEndpoint.class, MockParentInterface1.class, MockChildInterface1.class, Comparable JavaDoc.class}, myCl);
451         test = proxyFactory.createProxy(gbean.getAbstractName());
452         assertTrue(test instanceof MockEndpoint);
453         assertTrue(test instanceof MockParentInterface1);
454         assertFalse(test instanceof MockParentInterface2);
455         assertTrue(test instanceof MockChildInterface1);
456         assertFalse(test instanceof MockChildInterface2);
457
458         proxyFactory = mgr.createProxyFactory(new Class JavaDoc[]{MockParentInterface1.class, MockChildInterface1.class, Comparable JavaDoc.class}, myCl);
459         test = proxyFactory.createProxy(gbean.getAbstractName());
460         assertFalse(test instanceof MockEndpoint);
461         assertTrue(test instanceof MockParentInterface1);
462         assertFalse(test instanceof MockParentInterface2);
463         assertTrue(test instanceof MockChildInterface1);
464         assertFalse(test instanceof MockChildInterface2);
465
466         proxyFactory = mgr.createProxyFactory(new Class JavaDoc[]{MockEndpoint.class, Comparable JavaDoc.class}, myCl);
467         test = proxyFactory.createProxy(gbean.getAbstractName());
468         assertTrue(test instanceof MockEndpoint);
469         assertFalse(test instanceof MockParentInterface1);
470         assertFalse(test instanceof MockParentInterface2);
471         assertFalse(test instanceof MockChildInterface1);
472         assertFalse(test instanceof MockChildInterface2);
473
474         proxyFactory = mgr.createProxyFactory(new Class JavaDoc[]{Comparable JavaDoc.class}, myCl);
475         test = proxyFactory.createProxy(gbean.getAbstractName());
476
477         try {
478             proxyFactory = mgr.createProxyFactory(null, myCl);
479             fail();
480         } catch (NullPointerException JavaDoc e) {
481         }
482
483         try {
484             proxyFactory = mgr.createProxyFactory(new Class JavaDoc[0], myCl);
485             fail();
486         } catch (IllegalArgumentException JavaDoc e) {
487         }
488
489         try {
490             // two non-assibnable classes
491
test = mgr.createProxyFactory(new Class JavaDoc[]{MockGBean.class, String JavaDoc.class}, cl).createProxy(gbean.getAbstractName());
492             fail();
493         } catch (IllegalArgumentException JavaDoc e) {
494         }
495     }
496
497     public void testProxyClass() throws Exception JavaDoc {
498         ClassLoader JavaDoc cl = getClass().getClassLoader();
499         ClassLoader JavaDoc myCl = new URLClassLoader JavaDoc(new URL JavaDoc[0], cl);
500
501         GBeanData gbean = buildGBeanData("name", "test", ClassGBean.getGBeanInfo());
502         gbean.setAttribute("name", "Test");
503         kernel.loadGBean(gbean, myCl);
504         kernel.startGBean(gbean.getAbstractName());
505         ProxyManager mgr = kernel.getProxyManager();
506
507         Object JavaDoc test = mgr.createProxy(gbean.getAbstractName(), Named.class);
508         assertTrue(test instanceof Named);
509         assertFalse(test instanceof ClassGBean);
510         assertEquals("Test", ((Named)test).getName());
511
512         test = mgr.createProxy(gbean.getAbstractName(), myCl);
513         assertTrue(test instanceof Named);
514         assertFalse(test instanceof ClassGBean);
515         assertEquals("Test", ((Named)test).getName());
516
517         try {
518             mgr.createProxy(gbean.getAbstractName(), ClassGBean.class);
519             fail("expected an IllegalArgumentException");
520         } catch (IllegalArgumentException JavaDoc ignored) {
521             // expected
522
}
523     }
524
525     protected void setUp() throws Exception JavaDoc {
526         super.setUp();
527         realKernel = KernelFactory.newInstance().createKernel("test");
528         realKernel.boot();
529         kernel = kernelWraper.wrap(realKernel);
530     }
531
532     protected void tearDown() throws Exception JavaDoc {
533         realKernel.shutdown();
534         realKernel = null;
535         kernel = null;
536         super.tearDown();
537     }
538
539     private GBeanData buildGBeanData(String JavaDoc name, String JavaDoc type, GBeanInfo info) {
540         AbstractName abstractName = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), name, type);
541         return new GBeanData(abstractName, info);
542     }
543
544     public static interface Named {
545         String JavaDoc getName();
546     }
547
548     public static class ClassGBean implements Named {
549         private final String JavaDoc name;
550
551         public ClassGBean(String JavaDoc name) {
552             this.name = name;
553         }
554
555         public String JavaDoc getName() {
556             return name;
557         }
558
559         private static final GBeanInfo GBEAN_INFO;
560
561         static {
562             GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ClassGBean.class);
563             infoFactory.setConstructor(new String JavaDoc[]{"name"});
564
565             GBEAN_INFO = infoFactory.getBeanInfo();
566         }
567
568         public static GBeanInfo getGBeanInfo() {
569             return GBEAN_INFO;
570         }
571     }
572 }
573
Popular Tags