View Javadoc

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