KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > basic > ControllerJavaInstanceTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 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): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.scenario.basic;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.objectweb.cjdbc.common.exceptions.ControllerException;
34 import org.objectweb.cjdbc.common.sql.filters.HexaBlobFilter;
35 import org.objectweb.cjdbc.common.users.DatabaseBackendUser;
36 import org.objectweb.cjdbc.common.users.VirtualDatabaseUser;
37 import org.objectweb.cjdbc.common.util.Constants;
38 import org.objectweb.cjdbc.controller.authentication.AuthenticationManager;
39 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
40 import org.objectweb.cjdbc.controller.cache.result.ResultCacheColumnUnique;
41 import org.objectweb.cjdbc.controller.cache.result.ResultCacheRule;
42 import org.objectweb.cjdbc.controller.cache.result.rules.EagerCaching;
43 import org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager;
44 import org.objectweb.cjdbc.controller.core.Controller;
45 import org.objectweb.cjdbc.controller.core.ControllerConstants;
46 import org.objectweb.cjdbc.controller.loadbalancer.policies.WaitForCompletionPolicy;
47 import org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1_RR;
48 import org.objectweb.cjdbc.controller.loadbalancer.singledb.SingleDB;
49 import org.objectweb.cjdbc.controller.requestmanager.RequestManager;
50 import org.objectweb.cjdbc.controller.requestmanager.distributed.RAIDb1DistributedRequestManager;
51 import org.objectweb.cjdbc.controller.scheduler.raidb1.RAIDb1PessimisticTransactionLevelScheduler;
52 import org.objectweb.cjdbc.controller.scheduler.singledb.SingleDBPassThroughScheduler;
53 import org.objectweb.cjdbc.controller.virtualdatabase.DistributedVirtualDatabase;
54 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
55 import org.objectweb.cjdbc.scenario.templates.DatabaseTemplate;
56
57 /**
58  * This class defines a ControllerJavaInstanceTest. Creates a Controller from
59  * java code (as opposed to load it with an xml configuration file) and check
60  * that it is able to handle requests.
61  *
62  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
63  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
64  * </a>
65  * @version 1.0
66  */

67 public class ControllerJavaInstanceTest extends DatabaseTemplate
68 {
69   String JavaDoc cjdbcDriver = "c-jdbc-driver.jar";
70   String JavaDoc driverJar = "hsqldb.jar";
71   String JavaDoc driverClass = "org.hsqldb.jdbcDriver";
72   String JavaDoc driverPath = null;
73   String JavaDoc backendTest = "call now()";
74   String JavaDoc backendUrl = "jdbc:hsqldb:hsql://localhost:9001";
75   String JavaDoc backendUser = "TEST";
76   String JavaDoc backendPassword = "";
77
78   Controller controller;
79
80   /**
81    * @see junit.framework.TestCase#tearDown()
82    */

83   public void tearDown()
84   {
85     if (controller != null)
86     {
87       try
88       {
89         controller.shutdown(Constants.SHUTDOWN_SAFE);
90       }
91       catch (ControllerException e)
92       {
93         e.printStackTrace();
94       }
95     }
96     super.tearDown();
97   }
98
99   /**
100    * Test the test statement on a hypersonic backend
101    *
102    * @throws Exception if fails
103    */

104   public void testBackendTestStatement() throws Exception JavaDoc
105   {
106     Class.forName(driverClass);
107     Properties JavaDoc props = new Properties JavaDoc();
108     props.put("user", backendUser);
109     props.put("password", backendPassword);
110     Connection JavaDoc con = DriverManager.getConnection(backendUrl, props);
111     assertTrue("Statement not executed", con.createStatement().execute(
112         backendTest));
113     con.close();
114   }
115
116   /**
117    * Test the creation of a virtual database and send some requests.
118    *
119    * @throws Exception if fails
120    */

121   public void testCreateVirtualDatabase() throws Exception JavaDoc
122   {
123     // Prepare the controller instance
124
controller = new Controller("127.0.0.1", 25322, 10);
125
126     // Add the driver
127
// TODO: addDriver does not work properly, as some mysterious files need to
128
// be on the class path
129
//cont.addDriver(jar.getAbsolutePath());
130

131     // Prepare the distributed database instance
132
VirtualDatabase vdb = new VirtualDatabase(controller, "blob", 120, true,
133         20, 120, 15000L, 30, new HexaBlobFilter());
134
135     // Set the authentication manager for the virtual database
136
AuthenticationManager am = new AuthenticationManager();
137     am.addVirtualUser(new VirtualDatabaseUser("blob", ""));
138     vdb.setAuthenticationManager(am);
139
140     // Prepare the backend instance
141
DatabaseBackend dbb = new DatabaseBackend("blob", driverPath, driverClass,
142         backendUrl, vdb.getVirtualDatabaseName(), true, backendTest);
143     am.addRealUser("blob", new DatabaseBackendUser(dbb.getName(), backendUser,
144         backendPassword));
145
146     // Prepapre the connection manager for the backend
147
VariablePoolConnectionManager vcpm = new VariablePoolConnectionManager(dbb
148         .getURL(), dbb.getName(), backendUser, backendPassword, driverPath,
149         driverClass, 20, 120, 15, 45);
150     dbb.addConnectionManager("blob", vcpm);
151
152     // Prepare the cache
153
ResultCacheColumnUnique rccu = new ResultCacheColumnUnique(0, 0);
154     ResultCacheRule rcr = new ResultCacheRule("default", false, false, 36000);
155     rcr.setCacheBehavior(new EagerCaching(0));
156     rccu.setDefaultRule(rcr);
157
158     // Set the request manager for the virtual database
159
RequestManager sddrm = new RequestManager(vdb,
160         new SingleDBPassThroughScheduler(), rccu, new SingleDB(vdb), null, 45,
161         45, 45);
162     vdb.setRequestManager(sddrm);
163
164     // add the backend
165
vdb.addBackend(dbb);
166
167     // Add the virtual database to the controller
168
// This cannot be done before, as the backends are going to be enabled
169
// and we need the request manager active
170
controller.addVirtualDatabase(vdb, ControllerConstants.AUTO_ENABLE_TRUE,
171         null);
172
173     // Start the controller thread
174
controller.launch();
175
176     // Test Connection
177
Connection JavaDoc con = getCJDBCConnection("25322", "blob", "blob", "");
178     Statement JavaDoc s = con.createStatement();
179     ResultSet JavaDoc rset = s.executeQuery("select * from document");
180     assertTrue("Failed to move result set to last", rset.last());
181     assertTrue(rset.getRow() == 50);
182
183     rset = s.executeQuery("select * from document");
184     assertTrue("Failed to move result set to last after hit on cache", rset
185         .last());
186     assertTrue(rset.getRow() == 50);
187   }
188
189   /**
190    * Test the creation of a DistributedVirtualDatabase and send some requests
191    *
192    * @throws Exception if fails
193    */

194   public void testCreateDistributedVirtualDatabase() throws Exception JavaDoc
195   {
196
197     // File jar = new File(getClass().getResource("/" + driverJar).getFile());
198

199     // Prepare the controller instance
200
controller = new Controller("127.0.0.1", 25322, 10);
201
202     // Add the driver
203
// TODO: addDriver does not work properly, as some mysterious files need to
204
// be on the class path
205
// cont.addDriver(jar.getAbsolutePath());
206

207     // Prepare the distributed database instance
208
DistributedVirtualDatabase vdb = new DistributedVirtualDatabase(controller,
209         "blob", "blob", 120, true, 20, 120, 15000L, 30, new HexaBlobFilter());
210
211     // Set the authentication manager for the virtual database
212
AuthenticationManager am = new AuthenticationManager();
213     am.addVirtualUser(new VirtualDatabaseUser("blob", ""));
214     vdb.setAuthenticationManager(am);
215
216     // Prepare the backend instance
217
DatabaseBackend dbb = new DatabaseBackend("blob", driverPath, driverClass,
218         backendUrl, vdb.getVirtualDatabaseName(), true, backendTest);
219     am.addRealUser("blob", new DatabaseBackendUser(dbb.getName(), backendUser,
220         backendPassword));
221
222     // Prepapre the connection manager for the backend
223
VariablePoolConnectionManager vcpm = new VariablePoolConnectionManager(dbb
224         .getURL(), dbb.getName(), backendUser, backendPassword, driverPath,
225         driverClass, 20, 120, 15, 45);
226     dbb.addConnectionManager("blob", vcpm);
227
228     // Prepare the cache
229
ResultCacheColumnUnique rccu = new ResultCacheColumnUnique(0, 0);
230     ResultCacheRule rcr = new ResultCacheRule("default", false, false, 36000);
231     rcr.setCacheBehavior(new EagerCaching(0));
232     rccu.setDefaultRule(rcr);
233
234     // Set the request manager for the virtual database
235
RAIDb1DistributedRequestManager sddrm = new RAIDb1DistributedRequestManager(
236         vdb, new RAIDb1PessimisticTransactionLevelScheduler(), rccu,
237         new RAIDb1_RR(vdb, new WaitForCompletionPolicy()), null, 45, 45, 45);
238     vdb.setRequestManager(sddrm);
239
240     // We join the group once the backend has been added
241
vdb.addBackend(dbb);
242     vdb.joinGroup();
243
244     // Add the virtual database to the controller
245
// This cannot be done before, as the backends are going to be enabled
246
// and we need the request manager active, and we need to join the group
247
controller.addVirtualDatabase(vdb, ControllerConstants.AUTO_ENABLE_TRUE,
248         null);
249
250     // Start the controller thread
251
controller.launch();
252
253     // Test Connection
254
Connection JavaDoc con = getCJDBCConnection("25322", "blob", "blob", "");
255     Statement JavaDoc s = con.createStatement();
256     ResultSet JavaDoc rset = s.executeQuery("select * from document");
257     assertTrue("Failed to move result set to last", rset.last());
258     assertTrue(rset.getRow() == 50);
259
260     rset = s.executeQuery("select * from document");
261     assertTrue("Failed to move result set to last after hit on cache", rset
262         .last());
263     assertTrue(rset.getRow() == 50);
264   }
265 }
266
Popular Tags