View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import javax.naming.Context;
26  import javax.naming.NamingException;
27  import javax.persistence.EntityManagerFactory;
28  
29  import static org.hamcrest.CoreMatchers.sameInstance;
30  import static org.junit.Assert.assertThat;
31  import static org.mockito.Mockito.doReturn;
32  import static org.mockito.Mockito.doThrow;
33  import static org.mockito.Mockito.mock;
34  
35  /**
36   * Test for {@link JndiLookupHelper}.
37   */
38  public class JndiLookupHelperTest
39  {
40  
41      public static final String JNDI_NAME = "jndiName";
42  
43      private JndiLookupHelper sut;
44  
45      @Before
46      public void setUp()
47          throws Exception
48      {
49          sut = new JndiLookupHelper();
50      }
51  
52  
53      @Test
54      public void shouldLookupEmfByJndiName()
55          throws Exception
56      {
57          // given
58          final Context context = mock( Context.class );
59          final EntityManagerFactory emf = mock( EntityManagerFactory.class );
60          doReturn( emf ).when( context ).lookup( JNDI_NAME );
61          InitialContextFactoryStub.registerContext( context );
62          // when
63          final EntityManagerFactory result = sut.doJndiLookup( EntityManagerFactory.class, JNDI_NAME );
64          // then
65          assertThat( result, sameInstance( emf ) );
66      }
67  
68      @Test( expected = NullPointerException.class )
69      public void shouldThrowExceptionIfContextReturnsNull()
70          throws Exception
71      {
72          // given
73          final Context context = mock( Context.class );
74          doReturn( null ).when( context ).lookup( JNDI_NAME );
75          InitialContextFactoryStub.registerContext( context );
76          // when
77          sut.doJndiLookup( EntityManagerFactory.class, JNDI_NAME );
78      }
79  
80      @Test( expected = RuntimeException.class )
81      public void shouldWrapNamingException()
82          throws Exception
83      {
84          // given
85          final Context context = mock( Context.class );
86          doThrow( new NamingException() ).when( context ).lookup( JNDI_NAME );
87          InitialContextFactoryStub.registerContext( context );
88          // when
89          sut.doJndiLookup( EntityManagerFactory.class, JNDI_NAME );
90      }
91  
92  }