001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.util.Hashtable;
021:
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025: import javax.sql.DataSource;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
030: import org.apache.commons.dbcp.datasources.PerUserPoolDataSource;
031:
032: /**
033: * Tests JNID bind and lookup for DataSource implementations.
034: * Demonstrates problem indicated in BZ #38073.
035: *
036: */
037:
038: public class TestJndi extends TestCase {
039: /**
040: * The subcontext where the data source is bound.
041: */
042: protected static final String JNDI_SUBCONTEXT = "jdbc";
043:
044: /**
045: * the full jndi path to the data source.
046: */
047: protected static final String JNDI_PATH = JNDI_SUBCONTEXT + "/"
048: + "jndiTestDataSource";
049:
050: /** jndi context to use in tests **/
051: protected Context context = null;
052:
053: /**
054: * Creates a new instance.
055: */
056: public TestJndi(String name) {
057: super (name);
058: }
059:
060: /**
061: * Test BasicDatsource bind and lookup
062: *
063: * @throws Exception
064: */
065: public void testBasicDataSourceBind() throws Exception {
066: BasicDataSource dataSource = new BasicDataSource();
067: checkBind(dataSource);
068: }
069:
070: /**
071: * Test SharedPoolDataSource bind and lookup
072: *
073: * @throws Exception
074: */
075: public void testSharedPoolDataSourceBind() throws Exception {
076: SharedPoolDataSource dataSource = new SharedPoolDataSource();
077: checkBind(dataSource);
078: }
079:
080: /**
081: * Test PerUserPoolDataSource bind and lookup
082: *
083: * @throws Exception
084: */
085: public void testPerUserPoolDataSourceBind() throws Exception {
086: PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
087: checkBind(dataSource);
088: }
089:
090: public void setUp() throws Exception {
091: context = getInitialContext();
092: context.createSubcontext(JNDI_SUBCONTEXT);
093: }
094:
095: public void tearDown() throws Exception {
096: context.unbind(JNDI_PATH);
097: context.destroySubcontext(JNDI_SUBCONTEXT);
098: }
099:
100: /**
101: * Binds a DataSource to the jndi and checks that we have successfully
102: * bound it by looking it up again.
103: *
104: * @throws Exception if the bind, lookup or connect fails
105: */
106: protected void checkBind(DataSource dataSource) throws Exception {
107: bindDataSource(dataSource);
108: retrieveDataSource();
109: }
110:
111: /**
112: * Binds a DataSource into jndi.
113: *
114: * @throws Exception if creation or binding fails.
115: */
116: protected void bindDataSource(DataSource dataSource)
117: throws Exception {
118: context.bind(JNDI_PATH, dataSource);
119: }
120:
121: /**
122: * Retrieves a DataSource from jndi.
123: *
124: * @throws Exception if the jndi lookup fails or no DataSource is bound.
125: */
126: protected DataSource retrieveDataSource() throws Exception {
127: Context context = getInitialContext();
128: DataSource dataSource = (DataSource) context.lookup(JNDI_PATH);
129:
130: if (dataSource == null) {
131: fail("DataSource should not be null");
132: }
133: return dataSource;
134: }
135:
136: /**
137: * Retrieves (or creates if it does not exist) an InitialContext.
138: *
139: * @return the InitialContext.
140: * @throws NamingException if the InitialContext cannot be retrieved
141: * or created.
142: */
143: protected InitialContext getInitialContext() throws NamingException {
144: Hashtable environment = new Hashtable();
145: environment.put(Context.INITIAL_CONTEXT_FACTORY,
146: org.apache.naming.java.javaURLContextFactory.class
147: .getName());
148: InitialContext context = new InitialContext(environment);
149: return context;
150: }
151: }
|