Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/TestJNDI.java

raw
1 /*
2 * Copyright (C) 2014 Brett Wooldridge
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.zaxxer.hikari.pool;
17
18 import com.zaxxer.hikari.HikariConfig;
19 import com.zaxxer.hikari.HikariDataSource;
20 import com.zaxxer.hikari.HikariJNDIFactory;
21 import com.zaxxer.hikari.mocks.StubDataSource;
22 import org.junit.Test;
23 import org.osjava.sj.jndi.AbstractContext;
24
25 import javax.naming.*;
26 import java.sql.Connection;
27
28 import static com.zaxxer.hikari.pool.TestElf.getUnsealedConfig;
29 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
30 import static org.junit.Assert.*;
31
32 public class TestJNDI
33 {
34 @Test
35 public void testJndiLookup1() throws Exception
36 {
37 HikariJNDIFactory jndi = new HikariJNDIFactory();
38 Reference ref = new Reference("javax.sql.DataSource");
39 ref.add(new BogusRef("driverClassName", "com.zaxxer.hikari.mocks.StubDriver"));
40 ref.add(new BogusRef("jdbcUrl", "jdbc:stub"));
41 ref.add(new BogusRef("username", "foo"));
42 ref.add(new BogusRef("password", "foo"));
43 ref.add(new BogusRef("minimumIdle", "0"));
44 ref.add(new BogusRef("maxLifetime", "30000"));
45 ref.add(new BogusRef("maximumPoolSize", "10"));
46 ref.add(new BogusRef("dataSource.loginTimeout", "10"));
47 Context nameCtx = new BogusContext();
48
49 try (HikariDataSource ds = (HikariDataSource) jndi.getObjectInstance(ref, null, nameCtx, null)) {
50 assertNotNull(ds);
51 assertEquals("foo", getUnsealedConfig(ds).getUsername());
52 }
53 }
54
55 @Test
56 public void testJndiLookup2() throws Exception
57 {
58 HikariJNDIFactory jndi = new HikariJNDIFactory();
59 Reference ref = new Reference("javax.sql.DataSource");
60 ref.add(new BogusRef("dataSourceJNDI", "java:comp/env/HikariDS"));
61 ref.add(new BogusRef("driverClassName", "com.zaxxer.hikari.mocks.StubDriver"));
62 ref.add(new BogusRef("jdbcUrl", "jdbc:stub"));
63 ref.add(new BogusRef("username", "foo"));
64 ref.add(new BogusRef("password", "foo"));
65 ref.add(new BogusRef("minimumIdle", "0"));
66 ref.add(new BogusRef("maxLifetime", "30000"));
67 ref.add(new BogusRef("maximumPoolSize", "10"));
68 ref.add(new BogusRef("dataSource.loginTimeout", "10"));
69 Context nameCtx = new BogusContext2();
70
71 try (HikariDataSource ds = (HikariDataSource) jndi.getObjectInstance(ref, null, nameCtx, null)) {
72 assertNotNull(ds);
73 assertEquals("foo", getUnsealedConfig(ds).getUsername());
74 }
75 }
76
77 @Test
78 public void testJndiLookup3() throws Exception
79 {
80 HikariJNDIFactory jndi = new HikariJNDIFactory();
81
82 Reference ref = new Reference("javax.sql.DataSource");
83 ref.add(new BogusRef("dataSourceJNDI", "java:comp/env/HikariDS"));
84 try {
85 jndi.getObjectInstance(ref, null, null, null);
86 fail();
87 }
88 catch (RuntimeException e) {
89 assertTrue(e.getMessage().contains("JNDI context does not found"));
90 }
91 }
92
93 @Test
94 public void testJndiLookup4() throws Exception
95 {
96 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory");
97 System.setProperty("org.osjava.sj.jndi.shared", "true");
98 InitialContext ic = new InitialContext();
99
100 StubDataSource ds = new StubDataSource();
101
102 Context subcontext = ic.createSubcontext("java:/comp/env/jdbc");
103 subcontext.bind("java:/comp/env/jdbc/myDS", ds);
104
105 HikariConfig config = newHikariConfig();
106 config.setDataSourceJNDI("java:/comp/env/jdbc/myDS");
107
108 try (HikariDataSource hds = new HikariDataSource(config);
109 Connection conn = hds.getConnection()) {
110 assertNotNull(conn);
111 }
112 }
113
114 @SuppressWarnings("unchecked")
115 private class BogusContext extends AbstractContext
116 {
117 @Override
118 public Context createSubcontext(Name name)
119 {
120 return null;
121 }
122
123 @Override
124 public Object lookup(String name)
125 {
126 final HikariDataSource ds = new HikariDataSource();
127 ds.setPoolName("TestJNDI");
128 return ds;
129 }
130 }
131
132 @SuppressWarnings("unchecked")
133 private class BogusContext2 extends AbstractContext
134 {
135 @Override
136 public Context createSubcontext(Name name)
137 {
138 return null;
139 }
140
141 @Override
142 public Object lookup(String name)
143 {
144 return new StubDataSource();
145 }
146 }
147
148 private class BogusRef extends RefAddr
149 {
150 private static final long serialVersionUID = 1L;
151
152 private String content;
153 BogusRef(String type, String content)
154 {
155 super(type);
156 this.content = content;
157 }
158
159 @Override
160 public Object getContent()
161 {
162 return content;
163 }
164 }
165 }