View Javadoc

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