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.test.components;
8   
9   import org.mule.api.registry.Registry;
10  import org.mule.tck.junit4.FunctionalTestCase;
11  import org.mule.tck.services.UniqueComponent;
12  
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertNotNull;
18  
19  public class ObjectFactoryTestCase extends FunctionalTestCase
20  {
21  
22      @Override
23      protected String getConfigResources()
24      {
25          return "org/mule/test/components/object-factory-functional-test.xml";
26      }
27  
28      @Test
29      public void testDefaultScope() throws Exception
30      {
31          Registry registry = muleContext.getRegistry();
32          
33          Object bean1 = registry.lookupObject("default");
34          assertNotNull(bean1);
35          String id1 = ((UniqueComponent) bean1).getId();
36          
37          Object bean2 = registry.lookupObject("default");
38          assertNotNull(bean2);
39          String id2 = ((UniqueComponent) bean2).getId();
40          
41          assertEquals(id1, id2);
42      }
43  
44      @Test
45      public void testSingletonScope() throws Exception
46      {
47          Registry registry = muleContext.getRegistry();
48          
49          Object bean1 = registry.lookupObject("singleton");
50          assertNotNull(bean1);
51          String id1 = ((UniqueComponent) bean1).getId();
52          
53          Object bean2 = registry.lookupObject("singleton");
54          assertNotNull(bean2);
55          String id2 = ((UniqueComponent) bean2).getId();
56          
57          assertEquals(id1, id2);
58      }
59  
60      @Test
61      public void testPrototypeScope() throws Exception
62      {
63          Registry registry = muleContext.getRegistry();
64          
65          Object bean1 = registry.lookupObject("prototype");
66          assertNotNull(bean1);
67          String id1 = ((UniqueComponent) bean1).getId();
68          
69          Object bean2 = registry.lookupObject("prototype");
70          assertNotNull(bean2);
71          String id2 = ((UniqueComponent) bean2).getId();
72          
73          assertFalse("IDs " + id1 + " and " + id2 + " should be different", id1.equals(id2));
74      }
75  
76  }
77  
78