1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.jndi;
20
21 import org.junit.Test;
22
23 import javax.naming.NamingException;
24
25 import static org.junit.Assert.assertEquals;
26
27
28
29
30
31 public class JndiObjectFactoryTest {
32 @Test
33 public void testGetInstanceWithType() throws Exception {
34 final String name = "my/jndi/resource";
35 final String returnValue = "jndiString";
36 JndiObjectFactory<String> underTest = new JndiObjectFactory<String>() {
37 @Override
38 protected Object lookup(String jndiName, Class requiredType) throws NamingException {
39 assertEquals(name, jndiName);
40 assertEquals(String.class, requiredType);
41 return new String(returnValue);
42 }
43 };
44
45 underTest.setRequiredType(String.class);
46 underTest.setResourceName(name);
47
48 assertEquals(returnValue, underTest.getInstance());
49 }
50
51 @Test
52 public void testGetInstanceNoType() throws Exception {
53 final String name = "my/jndi/resource";
54 final String returnValue = "jndiString";
55 JndiObjectFactory<String> underTest = new JndiObjectFactory<String>() {
56 @Override
57 protected Object lookup(String jndiName) throws NamingException {
58 assertEquals(name, jndiName);
59 return new String(returnValue);
60 }
61 };
62
63 underTest.setResourceName(name);
64
65 assertEquals(returnValue, underTest.getInstance());
66 }
67
68 @Test(expected = IllegalStateException.class)
69 public void testJndiLookupFailsWithType() throws Exception {
70 final String name = "my/jndi/resource";
71 JndiObjectFactory<String> underTest = new JndiObjectFactory<String>() {
72 @Override
73 protected Object lookup(String jndiName, Class requiredType) throws NamingException {
74 throw new NamingException("No resource named " + jndiName);
75 }
76 };
77
78 underTest.setResourceName(name);
79 underTest.setRequiredType(String.class);
80
81 underTest.getInstance();
82 }
83
84 @Test(expected = IllegalStateException.class)
85 public void testJndiLookupFailsNoType() throws Exception {
86 final String name = "my/jndi/resource";
87 JndiObjectFactory<String> underTest = new JndiObjectFactory<String>() {
88 @Override
89 protected Object lookup(String jndiName) throws NamingException {
90 throw new NamingException("No resource named " + jndiName);
91 }
92 };
93
94 underTest.setResourceName(name);
95
96 underTest.getInstance();
97 }
98 }