KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > templates > Template


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.scenario.templates;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.objectweb.cjdbc.driver.ControllerInfo;
32 import org.objectweb.cjdbc.driver.Driver;
33 import org.objectweb.cjdbc.scenario.tools.ScenarioConstants;
34
35 /**
36  * This class defines a Template for CJDBC.
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
39  * @author <a HREF="mailto:mathieu.peltier@emicnetworks.com">Mathieu Peltier
40  * </a>
41  * @version 1.0
42  */

43 public abstract class Template extends NoTemplate
44 {
45   /**
46    * @see junit.framework.TestCase#setUp()
47    */

48   protected abstract void setUp();
49
50   /**
51    * @see junit.framework.TestCase#tearDown()
52    */

53   protected abstract void tearDown();
54
55   /**
56    * Returns a connection to hypersonic backend.
57    *
58    * @param port the port on which the backend is running.
59    * @return <code>Connection</code> to hsqldb.
60    * @throws Exception if fails.
61    */

62   public static Connection JavaDoc getHypersonicConnection(int port) throws Exception JavaDoc
63   {
64     Properties JavaDoc props = new Properties JavaDoc();
65     props.put("user", "test");
66     props.put("password", "");
67     Connection JavaDoc con = DriverManager.getConnection(
68         "jdbc:hsqldb:hsql://localhost:" + port, props);
69     return con;
70   }
71
72   /**
73    * Returns a connection to C-JDBC controller using default values (controller
74    * hostname, virtaul database name and user).
75    *
76    * @return a connection to the cjdbc myDB database
77    * @throws Exception if fails
78    */

79   public static Connection JavaDoc getCJDBCConnection() throws Exception JavaDoc
80   {
81     return getCJDBCConnection(ScenarioConstants.DEFAULT_CONTROLLER_PORT,
82         ScenarioConstants.DEFAULT_VDB_NAME,
83         ScenarioConstants.DEFAULT_VDB_USER_NAME,
84         ScenarioConstants.DEFAULT_VDB_USER_PASSWORD);
85   }
86
87   /**
88    * Returns a connection to C-JDBC controller on the given port.
89    *
90    * @param port to connect on to
91    * @return a connection to the cjdbc myDB database
92    * @throws Exception if fails
93    */

94   public static Connection JavaDoc getCJDBCConnection(String JavaDoc port) throws Exception JavaDoc
95   {
96     return getCJDBCConnection(port, ScenarioConstants.DEFAULT_VDB_NAME,
97         ScenarioConstants.DEFAULT_VDB_USER_NAME,
98         ScenarioConstants.DEFAULT_VDB_USER_PASSWORD);
99   }
100
101   /**
102    * Returns a connection to C-JDBC controller on the given port and the given
103    * database and the given user authentication.
104    *
105    * @param port to connect to
106    * @param database to connect to
107    * @param user login name
108    * @param password password
109    * @return a connection to the cjdbc given database
110    * @throws Exception if fails
111    */

112   public static Connection JavaDoc getCJDBCConnection(String JavaDoc port, String JavaDoc database,
113       String JavaDoc user, String JavaDoc password) throws Exception JavaDoc
114   {
115     Properties JavaDoc props = new Properties JavaDoc();
116     props.put("user", user);
117     props.put("password", password);
118     ControllerInfo[] controllerList = new ControllerInfo[]{new ControllerInfo(
119         ScenarioConstants.DEFAULT_CONTROLLER_HOSTNAME, Integer.parseInt(port))};
120     return getCJDBCConnection(controllerList, database, props);
121   }
122
123   /**
124    * Returns a connection to cjdbc controller on the given port and the given
125    * database.
126    *
127    * @param port to connect on to
128    * @param database to connect to
129    * @return a connection to the cjdbc given database
130    * @throws Exception if fails
131    */

132   public static Connection JavaDoc getCJDBCConnection(String JavaDoc port, String JavaDoc database)
133       throws Exception JavaDoc
134   {
135     return getCJDBCConnection(port, database,
136         ScenarioConstants.DEFAULT_VDB_USER_NAME,
137         ScenarioConstants.DEFAULT_VDB_USER_PASSWORD);
138   }
139
140   /**
141    * Get connection with list of controllers
142    *
143    * @param controllersList list of ControllerInfo objects
144    * @param database to connect to
145    * @param props containing user and password
146    * @return <code>Connection</code> to cjdbc controller
147    * @throws Exception if fails
148    */

149   public static Connection JavaDoc getCJDBCConnection(ControllerInfo[] controllersList,
150       String JavaDoc database, Properties JavaDoc props) throws Exception JavaDoc
151   {
152     if (controllersList == null || controllersList.length < 1)
153       return getCJDBCConnection(new ControllerInfo[]{new ControllerInfo(
154           ScenarioConstants.DEFAULT_CONTROLLER_HOSTNAME, Integer
155               .parseInt(ScenarioConstants.DEFAULT_CONTROLLER_PORT))}, database,
156           props);
157     else
158     {
159       Class.forName("org.objectweb.cjdbc.driver.Driver");
160       String JavaDoc controllers = controllersList[0].toString();
161       for (int i = 1; i < controllersList.length; i++)
162         controllers += "," + controllersList[i].toString();
163       Connection JavaDoc con = DriverManager.getConnection(Driver.CJDBC_URL_HEADER
164           + controllers + "/" + database, props);
165       assertNotNull("Connection to cjdbc controller was null", con);
166       assertFalse("Connection should not be closed", con.isClosed());
167       return con;
168     }
169   }
170
171   /**
172    * @see #getCJDBCConnection(ControllerInfo[],String,Properties)
173    */

174   public static Connection JavaDoc getCJDBCConnection(ControllerInfo[] controllersList,
175       String JavaDoc database) throws Exception JavaDoc
176   {
177     Properties JavaDoc props = new Properties JavaDoc();
178     props.put("user", ScenarioConstants.DEFAULT_VDB_USER_NAME);
179     props.put("password", ScenarioConstants.DEFAULT_VDB_USER_PASSWORD);
180     return getCJDBCConnection(controllersList, database, props);
181   }
182
183   /**
184    * @see #getCJDBCConnection(ControllerInfo[],String,Properties)
185    */

186   public static Connection JavaDoc getCJDBCConnection(ControllerInfo[] controllersList)
187       throws Exception JavaDoc
188   {
189     return getCJDBCConnection(controllersList,
190         ScenarioConstants.DEFAULT_VDB_NAME);
191   }
192 }
193
Popular Tags