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.SQLException ; 31 import java.sql.Statement ; 32 import java.util.Properties ; 33 34 import org.objectweb.cjdbc.scenario.templates.Template; 35 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface; 36 import org.objectweb.cjdbc.scenario.tools.components.backend.DatabaseManager; 37 38 44 public class StartStopDatabaseScenario extends Template 45 { 46 47 DatabaseManager hm; 48 ComponentInterface hsql1; 49 String port1 = "9011"; 50 String port2 = "9012"; 51 String port3 = "9013"; 52 53 56 protected void setUp() 57 { 58 59 } 60 61 64 protected void tearDown() 65 { 66 67 } 68 69 72 public void testStart() 73 { 74 hm = new DatabaseManager(); 75 try 76 { 77 hsql1 = hm.start(port1); 78 assertTrue("Hypersonic was not started", hm.isStarted(port1)); 79 } 80 catch (Exception e) 81 { 82 fail("Could not start hsql."); 83 } 84 hm.stop(hsql1); 85 } 86 87 90 public void testStartStop() 91 { 92 hm = new DatabaseManager(); 93 94 try 95 { 96 hsql1 = hm.start(port2); 97 hm.stop(hsql1); 98 } 99 catch (Exception e) 100 { 101 fail("Could not control hsql"); 102 } 103 } 104 105 108 public void testStartStopConnect() 109 { 110 hm = new DatabaseManager(); 111 Connection con = null; 112 Statement statement = null; 113 ResultSet rs = null; 114 115 try 116 { 117 hsql1 = hm.start(port3); 118 hm.loaddatabase(port3); 119 } 120 catch (Exception e) 121 { 122 e.printStackTrace(); 123 hm.stop(hsql1); 124 fail("Could not start hsql."); 125 } 126 try 127 { 128 Class.forName("org.hsqldb.jdbcDriver"); 129 } 130 catch (ClassNotFoundException e) 131 { 132 hm.stop(hsql1); 133 fail("Could not find hsql driver"); 134 } 135 try 136 { 137 Properties props = new Properties (); 138 props.put("user", "test"); 139 props.put("password", ""); 140 con = DriverManager.getConnection( 141 "jdbc:hsqldb:hsql://localhost:" + port3, props); 142 } 143 catch (SQLException e1) 144 { 145 hm.stop(hsql1); 146 e1.printStackTrace(); 147 fail("Could not get connection to hsql"); 148 } 149 try 150 { 151 statement = con.createStatement(); 152 statement.setFetchSize(5); 153 } 154 catch (SQLException e2) 155 { 156 hm.stop(hsql1); 157 fail("could not prepare statement"); 158 } 159 try 160 { 161 162 rs = statement.executeQuery("select * from document"); 163 } 164 catch (SQLException e3) 165 { 166 hm.stop(hsql1); 167 System.out.println(e3.getMessage()); 168 e3.printStackTrace(); 169 fail("Could not get result set"); 170 } 171 catch (Exception e) 172 { 173 e.printStackTrace(); 174 } 175 try 176 { 177 while (rs.next()) 178 { 179 rs.getString("ID"); 180 } 181 } 182 catch (Exception e) 183 { 184 hm.stop(hsql1); 185 e.printStackTrace(); 186 fail("Could not access result set"); 187 } 188 hm.stop(hsql1); 189 } 190 191 196 public void testNewHsqldbRelease() throws Exception 197 { 198 hm = new DatabaseManager(); 199 hsql1 = hm.start(port1); 200 hm.loaddatabase(port1); 201 Connection con = getHypersonicConnection(Integer.parseInt(port1)); 202 Statement stm = con.createStatement(); 203 stm 205 .executeUpdate("create table test (id int,atime timestamp default current_timestamp)"); 206 int row = stm.executeUpdate("insert into test (id) values (1)"); 207 System.out.println(row); 208 hm.stop(hsql1); 209 } 210 211 } 212 | Popular Tags |