1 24 25 package org.objectweb.cjdbc.scenario.basic; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.ResultSet ; 30 import java.sql.Statement ; 31 import java.util.Properties ; 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 67 public class ControllerJavaInstanceTest extends DatabaseTemplate 68 { 69 String cjdbcDriver = "c-jdbc-driver.jar"; 70 String driverJar = "hsqldb.jar"; 71 String driverClass = "org.hsqldb.jdbcDriver"; 72 String driverPath = null; 73 String backendTest = "call now()"; 74 String backendUrl = "jdbc:hsqldb:hsql://localhost:9001"; 75 String backendUser = "TEST"; 76 String backendPassword = ""; 77 78 Controller controller; 79 80 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 104 public void testBackendTestStatement() throws Exception 105 { 106 Class.forName(driverClass); 107 Properties props = new Properties (); 108 props.put("user", backendUser); 109 props.put("password", backendPassword); 110 Connection con = DriverManager.getConnection(backendUrl, props); 111 assertTrue("Statement not executed", con.createStatement().execute( 112 backendTest)); 113 con.close(); 114 } 115 116 121 public void testCreateVirtualDatabase() throws Exception 122 { 123 controller = new Controller("127.0.0.1", 25322, 10); 125 126 131 VirtualDatabase vdb = new VirtualDatabase(controller, "blob", 120, true, 133 20, 120, 15000L, 30, new HexaBlobFilter()); 134 135 AuthenticationManager am = new AuthenticationManager(); 137 am.addVirtualUser(new VirtualDatabaseUser("blob", "")); 138 vdb.setAuthenticationManager(am); 139 140 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 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 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 RequestManager sddrm = new RequestManager(vdb, 160 new SingleDBPassThroughScheduler(), rccu, new SingleDB(vdb), null, 45, 161 45, 45); 162 vdb.setRequestManager(sddrm); 163 164 vdb.addBackend(dbb); 166 167 controller.addVirtualDatabase(vdb, ControllerConstants.AUTO_ENABLE_TRUE, 171 null); 172 173 controller.launch(); 175 176 Connection con = getCJDBCConnection("25322", "blob", "blob", ""); 178 Statement s = con.createStatement(); 179 ResultSet 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 194 public void testCreateDistributedVirtualDatabase() throws Exception 195 { 196 197 199 controller = new Controller("127.0.0.1", 25322, 10); 201 202 207 DistributedVirtualDatabase vdb = new DistributedVirtualDatabase(controller, 209 "blob", "blob", 120, true, 20, 120, 15000L, 30, new HexaBlobFilter()); 210 211 AuthenticationManager am = new AuthenticationManager(); 213 am.addVirtualUser(new VirtualDatabaseUser("blob", "")); 214 vdb.setAuthenticationManager(am); 215 216 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 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 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 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 vdb.addBackend(dbb); 242 vdb.joinGroup(); 243 244 controller.addVirtualDatabase(vdb, ControllerConstants.AUTO_ENABLE_TRUE, 248 null); 249 250 controller.launch(); 252 253 Connection con = getCJDBCConnection("25322", "blob", "blob", ""); 255 Statement s = con.createStatement(); 256 ResultSet 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 |