View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * This test makes the assumption that {@link JndiLocator} is tested elsewhere and only makes an attempt to test the
29   * functionality added by {@link JndiObjectFactory}.
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  }