View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.component;
8   
9   import org.mule.api.lifecycle.InitialisationException;
10  import org.mule.api.object.ObjectFactory;
11  import org.mule.api.service.Service;
12  import org.mule.lifecycle.LifecycleTrackerComponent;
13  import org.mule.model.seda.SedaService;
14  import org.mule.object.PrototypeObjectFactory;
15  import org.mule.tck.testmodels.fruit.Orange;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNotSame;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  public class DefaultJavaComponentTestCase extends AbstractComponentTestCase
26  {
27  
28      protected ObjectFactory createObjectFactory() throws InitialisationException
29      {
30          PrototypeObjectFactory objectFactory = new PrototypeObjectFactory(Orange.class);
31          objectFactory.initialise();
32          return objectFactory;
33      }
34  
35      @Test
36      public void testComponentCreation() throws Exception
37      {
38          ObjectFactory objectFactory = createObjectFactory();
39          DefaultJavaComponent component = new DefaultJavaComponent(objectFactory);
40  
41          assertNotNull(component.getObjectFactory());
42          assertEquals(objectFactory, component.getObjectFactory());
43          assertEquals(Orange.class, component.getObjectFactory().getObjectClass());
44          assertEquals(Orange.class, component.getObjectType());
45      }
46  
47      @Test
48      public void testLifecycle() throws Exception
49      {
50          DefaultJavaComponent component = new DefaultJavaComponent(createObjectFactory());
51          component.setFlowConstruct(getTestService());
52          component.setMuleContext(muleContext);
53          component.initialise();
54          component.start();
55  
56          assertNotSame(component.borrowComponentLifecycleAdaptor(),
57              component.borrowComponentLifecycleAdaptor());
58  
59          Object obj = component.getObjectFactory().getInstance(muleContext);
60          assertNotNull(obj);
61  
62          component.stop();
63          component.start();
64  
65          assertNotSame(
66              ((DefaultComponentLifecycleAdapter) component.borrowComponentLifecycleAdaptor()).componentObject,
67              ((DefaultComponentLifecycleAdapter) component.borrowComponentLifecycleAdaptor()).componentObject);
68  
69      }
70  
71      @Test
72      public void testComponentDisposal() throws Exception
73      {
74          DefaultJavaComponent component = new DefaultJavaComponent(
75              createObjectFactory());
76          
77          component.setFlowConstruct(getTestService());
78          component.setMuleContext(muleContext);
79          component.initialise();
80          component.start();
81  
82          DefaultComponentLifecycleAdapter lifecycleAdapter = (DefaultComponentLifecycleAdapter) 
83              component.borrowComponentLifecycleAdaptor();
84          component.returnComponentLifecycleAdaptor(lifecycleAdapter);
85          component.stop();
86          component.dispose();
87  
88          assertNull(lifecycleAdapter.componentObject);
89      }
90      
91      @Test
92      public void testServicePropagatedLifecycle() throws InitialisationException
93      {
94          Service service = new SedaService(muleContext);
95          service.setName("service");
96          service.setModel(muleContext.getRegistry().lookupSystemModel());
97          LifecycleTrackerComponent component = new LifecycleTrackerComponent();
98          service.setComponent(component);
99          
100         service.initialise();
101         
102         assertTrue(component.getTracker().contains("initialise"));
103     }
104 
105 }