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.registry;
8   
9   import org.mule.api.registry.RegistrationException;
10  import org.mule.tck.junit4.AbstractMuleContextTestCase;
11  import org.mule.util.StringUtils;
12  
13  import java.util.Collection;
14  
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertTrue;
19  import static org.junit.Assert.fail;
20  
21  public class DuplicateRegistrationTestCase extends AbstractMuleContextTestCase
22  {
23      @Test
24      public void testComponentAlreadyDefinedThrowsException() throws Exception
25      {
26          Collection components = muleContext.getRegistry().lookupServices();
27          assertEquals(0, components.size());
28          
29          final String componentName = "TEST_COMPONENT_1";
30          getTestService(componentName, Object.class);
31  
32          components = muleContext.getRegistry().lookupServices();
33          assertEquals(1, components.size());
34          
35          // register it again with the same name
36          try
37          {
38              getTestService(componentName, Object.class);
39              fail("Trying to register a service with the same name must have thrown an exception.");
40          }
41          catch (RegistrationException e)
42          {
43              // expected
44              assertTrue("Exception message should contain service name", 
45                         StringUtils.contains(e.getMessage(), componentName));
46          }
47  
48          components = muleContext.getRegistry().lookupServices();
49          assertEquals(1, components.size());
50      }
51  }