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.DefaultMuleException;
10  import org.mule.object.PrototypeObjectFactory;
11  import org.mule.tck.testmodels.fruit.Apple;
12  import org.mule.tck.testmodels.fruit.Orange;
13  
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertNull;
19  import static org.junit.Assert.assertSame;
20  
21  public class SimpleCallableJavaComponentTestCase extends AbstractComponentTestCase
22  {
23  
24      @Test
25      public void testComponentCreationWithObjectFactory() throws Exception
26      {
27          PrototypeObjectFactory objectFactory = new PrototypeObjectFactory(
28              Apple.class);
29          objectFactory.setObjectClass(Apple.class);
30          objectFactory.initialise();
31  
32          SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(
33              objectFactory);
34  
35          assertNotNull(component.getObjectFactory());
36          assertEquals(objectFactory, component.getObjectFactory());
37          assertEquals(Apple.class, component.getObjectFactory().getObjectClass());
38          assertEquals(Apple.class, component.getObjectType());
39  
40          objectFactory = new PrototypeObjectFactory(Orange.class);
41          objectFactory.setObjectClass(Orange.class);
42          objectFactory.initialise();
43  
44          try
45          {
46              component = new SimpleCallableJavaComponent(objectFactory);
47          }
48          catch (Exception e)
49          {
50              assertSame(DefaultMuleException.class, e.getClass());
51          }
52      }
53  
54      @Test
55      public void testDirectComponentCreation() throws Exception
56      {
57          SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(Apple.class);
58  
59          assertNotNull(component.getObjectFactory());
60          assertEquals(Apple.class, component.getObjectFactory().getObjectClass());
61          assertEquals(Apple.class, component.getObjectType());
62  
63          try
64          {
65              component = new SimpleCallableJavaComponent(Orange.class);
66          }
67          catch (Exception e)
68          {
69              assertSame(DefaultMuleException.class, e.getClass());
70          }
71      }
72  
73      @Test
74      public void testSimpleComponentCreation() throws Exception
75      {
76          SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(
77              new Apple());
78  
79          assertNotNull(component.getObjectFactory());
80          assertEquals(Apple.class, component.getObjectFactory().getObjectClass());
81          assertEquals(Apple.class, component.getObjectType());
82  
83          try
84          {
85              component = new SimpleCallableJavaComponent(new Orange());
86          }
87          catch (Exception e)
88          {
89              assertSame(DefaultMuleException.class, e.getClass());
90          }
91      }
92  
93      @Test
94      public void testLifecycle() throws Exception
95      {
96          SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(
97              new Apple());
98          component.setFlowConstruct(getTestService());
99          component.setMuleContext(muleContext);
100         component.initialise();
101         component.start();
102 
103         assertNull(component.borrowComponentLifecycleAdaptor());
104 
105         Object obj = component.getObjectFactory().getInstance(muleContext);
106         assertNotNull(obj);
107 
108         component.stop();
109         component.dispose();
110 //        try
111 //        {
112 //            component.checkDisposed();
113 //        }
114 //        catch (Exception e)
115 //        {
116 //            assertSame(DisposeException.class, e.getClass());
117 //        }
118 
119     }
120 
121 }