View Javadoc

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