1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.embed

File EmbeddableComponentManagerTest.java

 

Code metrics

2
253
34
9
617
442
36
0.14
7.44
3.78
1.06

Classes

Class Line # Actions
EmbeddableComponentManagerTest 52 243 0% 28 1
0.9963099499.6%
EmbeddableComponentManagerTest.Role 54 0 - 0 0
-1.0 -
EmbeddableComponentManagerTest.RoleImpl 58 0 - 0 0
-1.0 -
EmbeddableComponentManagerTest.OtherRoleImpl 62 0 - 0 0
-1.0 -
EmbeddableComponentManagerTest.InitializableRoleImpl 68 2 0% 2 0
1.0100%
EmbeddableComponentManagerTest.DisposableRoleImpl 84 3 0% 2 0
1.0100%
EmbeddableComponentManagerTest.DisposableWithPriorityRoleImpl 102 3 0% 2 0
1.0100%
EmbeddableComponentManagerTest.LoggingRoleImpl 119 1 0% 1 0
1.0100%
EmbeddableComponentManagerTest.ComponentDescriptorRoleImpl 586 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software 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 software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.embed;
21   
22    import static org.mockito.Mockito.mock;
23    import static org.mockito.Mockito.verify;
24   
25    import java.lang.reflect.Type;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.junit.Assert;
30    import org.junit.Test;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.DisposePriority;
33    import org.xwiki.component.descriptor.ComponentDescriptor;
34    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
35    import org.xwiki.component.descriptor.DefaultComponentDependency;
36    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
37    import org.xwiki.component.manager.ComponentEventManager;
38    import org.xwiki.component.manager.ComponentLifecycleException;
39    import org.xwiki.component.manager.ComponentLookupException;
40    import org.xwiki.component.manager.ComponentManager;
41    import org.xwiki.component.phase.Disposable;
42    import org.xwiki.component.phase.Initializable;
43    import org.xwiki.component.phase.InitializationException;
44    import org.xwiki.component.util.DefaultParameterizedType;
45   
46    /**
47    * Unit tests for {@link EmbeddableComponentManager}.
48    *
49    * @version $Id: 03bb5bd6b5e9ff32e9f04d9681dad9501692dd17 $
50    * @since 2.0M1
51    */
 
52    public class EmbeddableComponentManagerTest
53    {
 
54    public static interface Role
55    {
56    }
57   
 
58    public static class RoleImpl implements Role
59    {
60    }
61   
 
62    public static class OtherRoleImpl implements Role
63    {
64    }
65   
66    private static String lastDisposedComponent;
67   
 
68    public static class InitializableRoleImpl implements Role, Initializable
69    {
70    private boolean initialized = false;
71   
 
72  1 toggle @Override
73    public void initialize() throws InitializationException
74    {
75  1 this.initialized = true;
76    }
77   
 
78  1 toggle public boolean isInitialized()
79    {
80  1 return this.initialized;
81    }
82    }
83   
 
84    public static class DisposableRoleImpl implements Role, Disposable
85    {
86    private boolean finalized = false;
87   
 
88  4 toggle @Override
89    public void dispose() throws ComponentLifecycleException
90    {
91  4 this.finalized = true;
92  4 lastDisposedComponent = "DisposableRoleImpl";
93    }
94   
 
95  8 toggle public boolean isFinalized()
96    {
97  8 return this.finalized;
98    }
99    }
100   
101    @DisposePriority(2000)
 
102    public static class DisposableWithPriorityRoleImpl implements Role, Disposable
103    {
104    private boolean finalized = false;
105   
 
106  1 toggle @Override
107    public void dispose() throws ComponentLifecycleException
108    {
109  1 this.finalized = true;
110  1 lastDisposedComponent = "DisposableWithPriorityRoleImpl";
111    }
112   
 
113  2 toggle public boolean isFinalized()
114    {
115  2 return this.finalized;
116    }
117    }
118   
 
119    public static class LoggingRoleImpl implements Role
120    {
121    private Logger logger;
122   
 
123  1 toggle public Logger getLogger()
124    {
125  1 return this.logger;
126    }
127    }
128   
 
129  1 toggle @Test
130    public void testLookupThisComponentManager() throws ComponentLookupException
131    {
132  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
133   
134  1 Assert.assertSame(ecm.getInstance(ComponentManager.class), ecm);
135    }
136   
 
137  1 toggle @Test
138    public void testGetComponentDescriptorList() throws Exception
139    {
140  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
141   
142  1 DefaultComponentDescriptor<Role> d1 = new DefaultComponentDescriptor<Role>();
143  1 d1.setRoleType(Role.class);
144  1 d1.setRoleHint("hint1");
145  1 ecm.registerComponent(d1);
146   
147  1 DefaultComponentDescriptor<Role> d2 = new DefaultComponentDescriptor<Role>();
148  1 d2.setRoleType(Role.class);
149  1 d2.setRoleHint("hint2");
150  1 ecm.registerComponent(d2);
151   
152  1 List<ComponentDescriptor<Role>> cds = ecm.getComponentDescriptorList(Role.class);
153  1 Assert.assertEquals(2, cds.size());
154  1 Assert.assertTrue(cds.contains(d1));
155  1 Assert.assertTrue(cds.contains(d2));
156    }
157   
 
158  1 toggle @Test
159    public void getComponentDescriptorListInParent() throws Exception
160    {
161  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
162  1 ecm.setParent(createParentComponentManager());
163   
164  1 List<ComponentDescriptor<Role>> cds = ecm.getComponentDescriptorList((Type) Role.class);
165  1 Assert.assertEquals(1, cds.size());
166    }
167   
 
168  1 toggle @Test
169    public void getComponentDescriptorInParent() throws Exception
170    {
171  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
172  1 ecm.setParent(createParentComponentManager("somehint"));
173   
174  1 ComponentDescriptor<Role> cd = ecm.getComponentDescriptor(Role.class, "somehint");
175  1 Assert.assertNotNull(cd);
176  1 Assert.assertEquals(RoleImpl.class, cd.getImplementation());
177    }
178   
 
179  1 toggle @Test
180    public void getComponentDescriptorWhenSomeComponentsInParent() throws Exception
181    {
182  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
183  1 ecm.setParent(createParentComponentManager());
184   
185    // Register a component with the same Role and Hint as in the parent
186  1 DefaultComponentDescriptor<Role> cd1 = new DefaultComponentDescriptor<Role>();
187  1 cd1.setRoleType(Role.class);
188  1 cd1.setImplementation(RoleImpl.class);
189  1 Role roleImpl = new RoleImpl();
190  1 ecm.registerComponent(cd1, roleImpl);
191   
192    // Register a component with the same Role as in the parent but with a different hint
193  1 DefaultComponentDescriptor<Role> cd2 = new DefaultComponentDescriptor<Role>();
194  1 cd2.setRoleType(Role.class);
195  1 cd2.setRoleHint("hint");
196  1 cd2.setImplementation(RoleImpl.class);
197  1 ecm.registerComponent(cd2);
198   
199    // Verify that the components are found
200    // Note: We find only 2 components since 2 components are registered with the same Role and Hint.
201   
202  1 List<ComponentDescriptor<Role>> descriptors = ecm.getComponentDescriptorList(Role.class);
203  1 Assert.assertEquals(2, descriptors.size());
204    }
205   
 
206  1 toggle @Test
207    public void testRegisterComponentOverExistingOne() throws Exception
208    {
209  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
210   
211  1 DefaultComponentDescriptor<Role> d1 = new DefaultComponentDescriptor<Role>();
212  1 d1.setRoleType(Role.class);
213  1 d1.setImplementation(RoleImpl.class);
214  1 ecm.registerComponent(d1);
215   
216  1 Object instance = ecm.getInstance(Role.class);
217  1 Assert.assertSame(RoleImpl.class, instance.getClass());
218   
219  1 DefaultComponentDescriptor<Role> d2 = new DefaultComponentDescriptor<Role>();
220  1 d2.setRoleType(Role.class);
221  1 d2.setImplementation(OtherRoleImpl.class);
222  1 ecm.registerComponent(d2);
223   
224  1 instance = ecm.getInstance(Role.class);
225  1 Assert.assertSame(OtherRoleImpl.class, instance.getClass());
226    }
227   
 
228  1 toggle @Test
229    public void testRegisterComponentInstance() throws Exception
230    {
231  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
232   
233  1 DefaultComponentDescriptor<Role> d1 = new DefaultComponentDescriptor<Role>();
234  1 d1.setRoleType(Role.class);
235  1 d1.setImplementation(RoleImpl.class);
236  1 Role instance = new RoleImpl();
237  1 ecm.registerComponent(d1, instance);
238   
239  1 Assert.assertSame(instance, ecm.getInstance(Role.class));
240    }
241   
 
242  1 toggle @Test
243    public void testUnregisterComponent() throws Exception
244    {
245  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
246   
247  1 DefaultComponentDescriptor<Role> d1 = new DefaultComponentDescriptor<Role>();
248  1 d1.setRoleType(Role.class);
249  1 d1.setImplementation(RoleImpl.class);
250  1 ecm.registerComponent(d1);
251   
252    // Verify that the component is properly registered
253  1 Assert.assertSame(RoleImpl.class, ecm.getInstance(Role.class).getClass());
254   
255  1 ecm.unregisterComponent(d1.getRoleType(), d1.getRoleHint());
256   
257    // Verify that the component is not registered anymore
258  1 try {
259  1 ecm.getInstance(d1.getRoleType());
260  0 Assert.fail("Should have thrown a ComponentLookupException");
261    } catch (ComponentLookupException expected) {
262    // The exception message doesn't matter. All we need to know is that the component descriptor
263    // doesn't exist anymore.
264    }
265    }
266   
 
267  1 toggle @Test
268    public void testGetInstanceWhenComponentInParent() throws Exception
269    {
270  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
271  1 ecm.setParent(createParentComponentManager());
272   
273  1 Role instance = ecm.getInstance(Role.class);
274  1 Assert.assertNotNull(instance);
275    }
276   
 
277  1 toggle @Test
278    public void testGetInstanceListAndMapWhenSomeComponentsInParent() throws Exception
279    {
280  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
281  1 ecm.setParent(createParentComponentManager());
282   
283    // Register a component with the same Role and Hint as in the parent
284  1 DefaultComponentDescriptor<Role> cd1 = new DefaultComponentDescriptor<Role>();
285  1 cd1.setRoleType(Role.class);
286  1 cd1.setImplementation(RoleImpl.class);
287  1 Role roleImpl = new RoleImpl();
288  1 ecm.registerComponent(cd1, roleImpl);
289   
290    // Register a component with the same Role as in the parent but with a different hint
291  1 DefaultComponentDescriptor<Role> cd2 = new DefaultComponentDescriptor<Role>();
292  1 cd2.setRoleType(Role.class);
293  1 cd2.setRoleHint("hint");
294  1 cd2.setImplementation(RoleImpl.class);
295  1 ecm.registerComponent(cd2);
296   
297    // Verify that the components are found
298    // Note: We find only 2 components since 2 components are registered with the same Role and Hint.
299   
300  1 List<Role> instanceList = ecm.getInstanceList(Role.class);
301  1 Assert.assertEquals(2, instanceList.size());
302  1 Assert.assertSame(roleImpl, instanceList.get(0));
303   
304  1 Map<String, Role> instances = ecm.getInstanceMap(Role.class);
305  1 Assert.assertEquals(2, instances.size());
306  1 Assert.assertSame(roleImpl, instances.get("default"));
307    }
308   
 
309  1 toggle @Test
310    public void testHasComponent() throws Exception
311    {
312  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
313   
314  1 DefaultComponentDescriptor<Role> d1 = new DefaultComponentDescriptor<Role>();
315  1 d1.setRoleType(Role.class);
316  1 d1.setRoleHint("default");
317  1 ecm.registerComponent(d1);
318   
319  1 Assert.assertTrue(ecm.hasComponent(Role.class));
320  1 Assert.assertTrue(ecm.hasComponent(Role.class, "default"));
321    }
322   
 
323  1 toggle @Test
324    public void testHasComponentWhenComponentInParent() throws Exception
325    {
326  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
327  1 ecm.setParent(createParentComponentManager());
328   
329  1 Assert.assertTrue(ecm.hasComponent(Role.class));
330  1 Assert.assertTrue(ecm.hasComponent(Role.class, "default"));
331    }
332   
 
333  1 toggle @Test
334    public void testLoggingInjection() throws Exception
335    {
336  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
337   
338  1 DefaultComponentDescriptor<Role> d = new DefaultComponentDescriptor<Role>();
339  1 d.setRoleType(Role.class);
340  1 d.setImplementation(LoggingRoleImpl.class);
341   
342  1 DefaultComponentDependency dependencyDescriptor = new DefaultComponentDependency();
343  1 dependencyDescriptor.setMappingType(Logger.class);
344  1 dependencyDescriptor.setName("logger");
345   
346  1 d.addComponentDependency(dependencyDescriptor);
347  1 ecm.registerComponent(d);
348   
349  1 LoggingRoleImpl impl = (LoggingRoleImpl) ecm.getInstance(Role.class);
350  1 Assert.assertNotNull(impl.getLogger());
351    }
352   
 
353  5 toggle private ComponentManager createParentComponentManager() throws Exception
354    {
355  5 return createParentComponentManager(null);
356    }
357   
 
358  6 toggle private ComponentManager createParentComponentManager(String hint) throws Exception
359    {
360  6 EmbeddableComponentManager parent = new EmbeddableComponentManager();
361  6 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
362  6 cd.setRoleType(Role.class);
363  6 cd.setImplementation(RoleImpl.class);
364  6 if (hint != null) {
365  1 cd.setRoleHint(hint);
366    }
367  6 parent.registerComponent(cd);
368  6 return parent;
369    }
370   
 
371  1 toggle @Test
372    public void testRegisterInitializableComponent() throws Exception
373    {
374  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
375   
376  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
377  1 cd.setRoleType(Role.class);
378  1 cd.setImplementation(InitializableRoleImpl.class);
379  1 ecm.registerComponent(cd);
380  1 InitializableRoleImpl instance = (InitializableRoleImpl) ecm.getInstance(Role.class);
381   
382  1 Assert.assertTrue(instance.isInitialized());
383    }
384   
 
385  1 toggle @Test
386    public void testUnregisterDisposableSingletonComponent() throws Exception
387    {
388  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
389   
390  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
391  1 cd.setRoleType(Role.class);
392  1 cd.setImplementation(DisposableRoleImpl.class);
393  1 cd.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
394   
395  1 ecm.registerComponent(cd);
396  1 DisposableRoleImpl instance = (DisposableRoleImpl) ecm.getInstance(Role.class);
397   
398  1 Assert.assertFalse(instance.isFinalized());
399   
400  1 ecm.unregisterComponent(cd.getRoleType(), cd.getRoleHint());
401   
402  1 Assert.assertTrue(instance.isFinalized());
403    }
404   
 
405  1 toggle @Test
406    public void testUnregisterDisposableSingletonComponentWithInstance() throws Exception
407    {
408  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
409   
410  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
411  1 cd.setRoleType(Role.class);
412  1 cd.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
413   
414  1 DisposableRoleImpl instance = new DisposableRoleImpl();
415  1 ecm.registerComponent(cd, instance);
416   
417  1 Assert.assertFalse(instance.isFinalized());
418   
419  1 ecm.unregisterComponent(cd.getRoleType(), cd.getRoleHint());
420   
421  1 Assert.assertTrue(instance.isFinalized());
422    }
423   
 
424  1 toggle @Test
425    public void testRelease() throws Exception
426    {
427  1 final EmbeddableComponentManager ecm = new EmbeddableComponentManager();
428   
429  1 final DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
430  1 cd.setRoleType(Role.class);
431  1 cd.setImplementation(RoleImpl.class);
432  1 Role roleImpl = new RoleImpl();
433  1 ecm.registerComponent(cd, roleImpl);
434   
435  1 final ComponentEventManager cem = mock(ComponentEventManager.class);
436  1 ecm.setComponentEventManager(cem);
437   
438  1 ecm.release(roleImpl);
439   
440  1 verify(cem).notifyComponentUnregistered(cd, ecm);
441  1 verify(cem).notifyComponentRegistered(cd, ecm);
442   
443  1 Assert.assertNotNull(ecm.getInstance(Role.class));
444  1 Assert.assertNotSame(roleImpl, ecm.getInstance(Role.class));
445    }
446   
 
447  1 toggle @Test
448    public void testReleaseDisposableComponent() throws Exception
449    {
450  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
451   
452  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
453  1 cd.setRoleType(Role.class);
454  1 cd.setImplementation(DisposableRoleImpl.class);
455  1 cd.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
456   
457  1 ecm.registerComponent(cd);
458  1 DisposableRoleImpl instance = ecm.getInstance(Role.class);
459   
460  1 Assert.assertFalse(instance.isFinalized());
461   
462  1 ecm.release(instance);
463   
464  1 Assert.assertTrue(instance.isFinalized());
465    }
466   
 
467  1 toggle @Test
468    public void testRegisterComponentNotification() throws Exception
469    {
470  1 final EmbeddableComponentManager ecm = new EmbeddableComponentManager();
471   
472  1 final DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
473  1 cd.setRoleType(Role.class);
474  1 cd.setImplementation(RoleImpl.class);
475   
476  1 final ComponentEventManager cem = mock(ComponentEventManager.class);
477  1 ecm.setComponentEventManager(cem);
478   
479  1 ecm.registerComponent(cd);
480   
481  1 verify(cem).notifyComponentRegistered(cd, ecm);
482    }
483   
 
484  1 toggle @Test
485    public void testUnregisterComponentNotification() throws Exception
486    {
487  1 final EmbeddableComponentManager ecm = new EmbeddableComponentManager();
488   
489  1 final DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
490  1 cd.setRoleType(Role.class);
491  1 cd.setImplementation(RoleImpl.class);
492  1 ecm.registerComponent(cd);
493   
494  1 final ComponentEventManager cem = mock(ComponentEventManager.class);
495  1 ecm.setComponentEventManager(cem);
496   
497  1 ecm.unregisterComponent(cd.getRoleType(), cd.getRoleHint());
498   
499  1 verify(cem).notifyComponentUnregistered(cd, ecm);
500    }
501   
 
502  1 toggle @Test
503    public void testRegisterComponentNotificationOnSecondRegistration() throws Exception
504    {
505  1 final EmbeddableComponentManager ecm = new EmbeddableComponentManager();
506   
507  1 final DefaultComponentDescriptor<Role> cd1 = new DefaultComponentDescriptor<Role>();
508  1 cd1.setRoleType(Role.class);
509  1 cd1.setImplementation(RoleImpl.class);
510  1 ecm.registerComponent(cd1);
511   
512  1 final DefaultComponentDescriptor<Role> cd2 = new DefaultComponentDescriptor<Role>();
513  1 cd2.setRoleType(Role.class);
514  1 cd2.setImplementation(OtherRoleImpl.class);
515   
516  1 final ComponentEventManager cem = mock(ComponentEventManager.class);
517  1 ecm.setComponentEventManager(cem);
518   
519  1 ecm.registerComponent(cd2);
520   
521  1 verify(cem).notifyComponentUnregistered(cd1, ecm);
522  1 verify(cem).notifyComponentRegistered(cd2, ecm);
523    }
524   
 
525  1 toggle @Test
526    public void testDispose() throws Exception
527    {
528  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
529   
530    // Register 2 components:
531    // - a first one using a low dispose priority
532    // - a second one using a default dispose priority
533   
534    // First component
535  1 DefaultComponentDescriptor<Role> cd1 = new DefaultComponentDescriptor<>();
536  1 cd1.setRoleType(Role.class);
537  1 cd1.setRoleHint("instance1");
538  1 cd1.setImplementation(DisposableWithPriorityRoleImpl.class);
539  1 cd1.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
540  1 ecm.registerComponent(cd1);
541  1 DisposableWithPriorityRoleImpl instance1 = ecm.getInstance(Role.class, "instance1");
542   
543    // Second component
544  1 DefaultComponentDescriptor<Role> cd2 = new DefaultComponentDescriptor<>();
545  1 cd2.setRoleType(Role.class);
546  1 cd2.setRoleHint("instance2");
547  1 cd2.setImplementation(DisposableRoleImpl.class);
548  1 cd2.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
549  1 ecm.registerComponent(cd2);
550  1 DisposableRoleImpl instance2 = ecm.getInstance(Role.class, "instance2");
551   
552  1 Assert.assertFalse(instance1.isFinalized());
553  1 Assert.assertFalse(instance2.isFinalized());
554   
555  1 ecm.dispose();
556   
557  1 Assert.assertTrue(instance1.isFinalized());
558  1 Assert.assertTrue(instance2.isFinalized());
559   
560  1 Assert.assertNull(ecm.getComponentDescriptor(Role.class, "instance1"));
561  1 Assert.assertNull(ecm.getComponentDescriptor(Role.class, "instance2"));
562  1 Assert.assertNotNull(ecm.getComponentDescriptor(ComponentManager.class, "default"));
563   
564    // Verify that dispose() has been called in the right order.
565    // We check that the last component which had its dispose() called is DisposableWithPriorityRoleImpl since
566    // it has the lowest priority.
567  1 Assert.assertEquals("DisposableWithPriorityRoleImpl", lastDisposedComponent);
568    }
569   
 
570  1 toggle @Test
571    public void diposeWhenImplementationIsECM() throws Exception
572    {
573  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
574   
575  1 DefaultComponentDescriptor<ComponentManager> cd = new DefaultComponentDescriptor<>();
576  1 cd.setRoleType(ComponentManager.class);
577  1 cd.setRoleHint("hint");
578  1 cd.setImplementation(EmbeddableComponentManager.class);
579  1 cd.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
580  1 ecm.registerComponent(cd, ecm);
581   
582    // If the test fails, the following line will generate a StackOverflowException
583  1 ecm.dispose();
584    }
585   
 
586    public static class ComponentDescriptorRoleImpl implements Role
587    {
588    private ComponentDescriptor<ComponentDescriptorRoleImpl> descriptor;
589   
 
590  1 toggle public ComponentDescriptor<ComponentDescriptorRoleImpl> getComponentDescriptor()
591    {
592  1 return this.descriptor;
593    }
594    }
595   
 
596  1 toggle @Test
597    public void testComponentDescriptorInjection() throws Exception
598    {
599  1 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
600   
601  1 DefaultComponentDescriptor<Role> d = new DefaultComponentDescriptor<>();
602  1 d.setRoleType(Role.class);
603  1 d.setImplementation(ComponentDescriptorRoleImpl.class);
604   
605  1 DefaultComponentDependency dependencyDescriptor = new DefaultComponentDependency();
606  1 dependencyDescriptor.setRoleType(
607    new DefaultParameterizedType(null, ComponentDescriptor.class, ComponentDescriptorRoleImpl.class));
608  1 dependencyDescriptor.setName("descriptor");
609   
610  1 d.addComponentDependency(dependencyDescriptor);
611  1 ecm.registerComponent(d);
612   
613  1 ComponentDescriptorRoleImpl impl = ecm.getInstance(Role.class);
614  1 Assert.assertNotNull(impl.getComponentDescriptor());
615    }
616   
617    }