01: /*
02: * $Id: MockInitialContextFactory.java,v 1.3 2004/10/05 20:13:04 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/test/java/mock/MockInitialContextFactory.java,v $
04: * SQLUnit - a test harness for unit testing database stored procedures.
05: * Copyright (C) 2003 The SQLUnit Team
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21: package net.sourceforge.sqlunit.test.mock;
22:
23: import com.mockrunner.mock.jdbc.MockDataSource;
24:
25: import org.apache.log4j.Logger;
26:
27: import java.sql.Connection;
28: import java.sql.DriverManager;
29: import java.util.Hashtable;
30:
31: import javax.naming.Context;
32: import javax.naming.NamingException;
33: import javax.naming.spi.InitialContextFactory;
34: import javax.sql.DataSource;
35:
36: /**
37: * Builds a MockInitialContext using an environment Hashtable.
38: * @author Sujit Pal (spal@users.sourceforge.net)
39: * @version $Revision: 1.3 $
40: */
41: public class MockInitialContextFactory implements InitialContextFactory {
42:
43: private static final Logger LOG = Logger
44: .getLogger(MockInitialContextFactory.class);
45:
46: // hardcoded values to set up the Connection
47: private static final String DRIVER = "net.sourceforge.sqlunit.test.mock.SQLUnitMockDriver";
48: private static final String URL = "jdbc:mock:net.sourceforge.sqlunit.test.mock.SQLUnitMockDatabase";
49: private static final String DSN = "jdbc/mockDSN";
50:
51: /**
52: * Instantiates a MockInitialContextFactory.
53: */
54: public MockInitialContextFactory() {
55: LOG.debug("Instantiated MockInitialContextFactory");
56: }
57:
58: /**
59: * Builds and returns a MockInitialContext object pre-filled with a
60: * named DataSource object.
61: * @param env a Hashtable of properies to instantiate the InitialContext.
62: * @return a MockInitialContext object.
63: * @exception NamingException if an error occurs building an InitialContext
64: */
65: public final Context getInitialContext(final Hashtable env)
66: throws NamingException {
67: try {
68: MockInitialContext ctx = new MockInitialContext();
69: ctx.bind(DSN, getMockDataSource());
70: return ctx;
71: } catch (Exception e) {
72: throw new NamingException(e.getMessage());
73: }
74: }
75:
76: /**
77: * Uses JDBC to build up the MockDataSource containing a Connection
78: * to our MockDatabase.
79: * @return a MockDataSource object.
80: * @exception Exception if an error occurs.
81: */
82: private DataSource getMockDataSource() throws Exception {
83: MockDataSource mds = new MockDataSource();
84: Class.forName(DRIVER);
85: Connection conn = DriverManager.getConnection(URL);
86: mds.setupConnection(conn);
87: return mds;
88: }
89: }
|