1 /*
2 *
3 * The DbUnit Database Testing Framework
4 * Copyright (C)2002-2004, DbUnit.org
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 package org.dbunit;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.util.Properties;
27
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import javax.sql.DataSource;
32
33 import org.dbunit.database.DatabaseConnection;
34 import org.dbunit.database.IDatabaseConnection;
35
36 /**
37 * DatabaseTester that pulls a DataSource from a JNDI location.
38 *
39 * @author Andres Almiray (aalmiray@users.sourceforge.net)
40 * @author Last changed by: $Author: gommma $
41 * @version $Revision: 983 $ $Date: 2009-03-14 13:01:03 +0100 (sab, 14 mar 2009) $
42 * @since 2.2.0
43 */
44 public class JndiDatabaseTester extends AbstractDatabaseTester
45 {
46
47 /**
48 * Logger for this class
49 */
50 private static final Logger logger = LoggerFactory.getLogger(JndiDatabaseTester.class);
51
52 private DataSource dataSource;
53 private Properties environment;
54 private boolean initialized = false;
55 private String lookupName;
56
57 /**
58 * Creates a JndiDatabaseTester with default JNDI properties.
59 *
60 * @param lookupName the name of the resource in the JNDI context
61 */
62 public JndiDatabaseTester(String lookupName)
63 {
64 this(null, lookupName);
65 }
66
67 /**
68 * Creates a JndiDatabaseTester with specific JNDI properties.
69 *
70 * @param environment A Properties object with JNDI properties. Can be null
71 * @param lookupName the name of the resource in the JNDI context
72 */
73 public JndiDatabaseTester(Properties environment, String lookupName)
74 {
75 this(environment, lookupName, null);
76 }
77
78 /**
79 * Creates a JndiDatabaseTester with specific JNDI properties.
80 *
81 * @param environment A Properties object with JNDI properties. Can be <code>null</code>
82 * @param lookupName the name of the resource in the JNDI context
83 * @param schema The schema name to be used for new dbunit connections. Can be <code>null</code>
84 * @since 2.4.5
85 */
86 public JndiDatabaseTester(Properties environment, String lookupName, String schema)
87 {
88 super(schema);
89
90 if (lookupName == null) {
91 throw new NullPointerException(
92 "The parameter 'lookupName' must not be null");
93 }
94 this.lookupName = lookupName;
95 this.environment = environment;
96 }
97
98 public IDatabaseConnection getConnection() throws Exception
99 {
100 logger.trace("getConnection() - start");
101
102 if( !initialized ){
103 initialize();
104 }
105
106 return new DatabaseConnection( dataSource.getConnection(), getSchema() );
107 }
108
109 /**
110 * Verifies the configured properties and locates the Datasource through
111 * JNDI.<br>
112 * This method is called by {@link getConnection} if the tester has not been
113 * initialized yet.
114 */
115 private void initialize() throws NamingException
116 {
117 logger.trace("initialize() - start");
118
119 Context context = new InitialContext( environment );
120 assertNotNullNorEmpty( "lookupName", lookupName );
121 Object obj = context.lookup( lookupName );
122 assertTrue( "JNDI object with [" + lookupName + "] not found", obj!=null );
123 assertTrue( "Object [" + obj + "] at JNDI location [" + lookupName
124 + "] is not of type [" + DataSource.class.getName() + "]", obj instanceof DataSource );
125 dataSource = (DataSource) obj;
126 assertTrue( "DataSource is not set", dataSource!=null );
127 initialized = true;
128 }
129
130 public String toString()
131 {
132 StringBuffer sb = new StringBuffer();
133 sb.append(getClass().getName()).append("[");
134 sb.append("lookupName=").append(this.lookupName);
135 sb.append(", environment=").append(this.environment);
136 sb.append(", initialized=").append(this.initialized);
137 sb.append(", dataSource=").append(this.dataSource);
138 sb.append(", schema=").append(super.getSchema());
139 sb.append("]");
140 return sb.toString();
141 }
142
143 }