1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
63 final EntityManagerFactory result = sut.doJndiLookup( EntityManagerFactory.class, JNDI_NAME );
64
65 assertThat( result, sameInstance( emf ) );
66 }
67
68 @Test( expected = NullPointerException.class )
69 public void shouldThrowExceptionIfContextReturnsNull()
70 throws Exception
71 {
72
73 final Context context = mock( Context.class );
74 doReturn( null ).when( context ).lookup( JNDI_NAME );
75 InitialContextFactoryStub.registerContext( context );
76
77 sut.doJndiLookup( EntityManagerFactory.class, JNDI_NAME );
78 }
79
80 @Test( expected = RuntimeException.class )
81 public void shouldWrapNamingException()
82 throws Exception
83 {
84
85 final Context context = mock( Context.class );
86 doThrow( new NamingException() ).when( context ).lookup( JNDI_NAME );
87 InitialContextFactoryStub.registerContext( context );
88
89 sut.doJndiLookup( EntityManagerFactory.class, JNDI_NAME );
90 }
91
92 }