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.transport.jms.jndi;
8   
9   import org.mule.api.MuleException;
10  import org.mule.tck.junit4.AbstractMuleTestCase;
11  
12  import java.util.Hashtable;
13  
14  import javax.naming.Context;
15  import javax.naming.NamingException;
16  import javax.naming.spi.InitialContextFactory;
17  
18  import org.junit.Test;
19  import org.mockito.Mockito;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.mockito.Matchers.any;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.times;
25  import static org.mockito.Mockito.when;
26  
27  public class CachedJndiNameResolverTestCase extends AbstractMuleTestCase
28  {
29  
30      private static final String RESOLVED_NAME = "resolvedName";
31      private static final String NAME = "name";
32  
33      @Test
34      public void testResolvesWithCache() throws NamingException, MuleException
35      {
36  
37          Context context = mock(Context.class);
38          when(context.lookup(NAME)).thenReturn(RESOLVED_NAME);
39  
40          InitialContextFactory jndiContextFactory = mock(InitialContextFactory.class);
41          when(jndiContextFactory.getInitialContext(any(Hashtable.class))).thenReturn(context);
42  
43          CachedJndiNameResolver jndiNameResolver = new CachedJndiNameResolver();
44          jndiNameResolver.setContextFactory(jndiContextFactory);
45          jndiNameResolver.setJndiInitialFactory("initialFactory");
46          jndiNameResolver.initialise();
47  
48          // First lookup should use the context, second should use the cache
49          assertEquals(RESOLVED_NAME, jndiNameResolver.lookup(NAME));
50          assertEquals(RESOLVED_NAME, jndiNameResolver.lookup(NAME));
51  
52          Mockito.verify(context, times(1)).lookup(NAME);
53      }
54  }