1 24 25 package org.objectweb.cjdbc.scenario.templates; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.util.Properties ; 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 43 public abstract class Template extends NoTemplate 44 { 45 48 protected abstract void setUp(); 49 50 53 protected abstract void tearDown(); 54 55 62 public static Connection getHypersonicConnection(int port) throws Exception 63 { 64 Properties props = new Properties (); 65 props.put("user", "test"); 66 props.put("password", ""); 67 Connection con = DriverManager.getConnection( 68 "jdbc:hsqldb:hsql://localhost:" + port, props); 69 return con; 70 } 71 72 79 public static Connection getCJDBCConnection() throws Exception 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 94 public static Connection getCJDBCConnection(String port) throws Exception 95 { 96 return getCJDBCConnection(port, ScenarioConstants.DEFAULT_VDB_NAME, 97 ScenarioConstants.DEFAULT_VDB_USER_NAME, 98 ScenarioConstants.DEFAULT_VDB_USER_PASSWORD); 99 } 100 101 112 public static Connection getCJDBCConnection(String port, String database, 113 String user, String password) throws Exception 114 { 115 Properties props = new Properties (); 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 132 public static Connection getCJDBCConnection(String port, String database) 133 throws Exception 134 { 135 return getCJDBCConnection(port, database, 136 ScenarioConstants.DEFAULT_VDB_USER_NAME, 137 ScenarioConstants.DEFAULT_VDB_USER_PASSWORD); 138 } 139 140 149 public static Connection getCJDBCConnection(ControllerInfo[] controllersList, 150 String database, Properties props) throws Exception 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 controllers = controllersList[0].toString(); 161 for (int i = 1; i < controllersList.length; i++) 162 controllers += "," + controllersList[i].toString(); 163 Connection 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 174 public static Connection getCJDBCConnection(ControllerInfo[] controllersList, 175 String database) throws Exception 176 { 177 Properties props = new Properties (); 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 186 public static Connection getCJDBCConnection(ControllerInfo[] controllersList) 187 throws Exception 188 { 189 return getCJDBCConnection(controllersList, 190 ScenarioConstants.DEFAULT_VDB_NAME); 191 } 192 } 193 | Popular Tags |