View Javadoc

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