1 /* 2 * Replica is published under the terms 3 * of the Apache Software License. 4 */ 5 package replica.database.commands.plsql; 6 7 import java.sql.Connection; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 12 import org.hsqldb.Server; 13 import org.springframework.jdbc.datasource.DriverManagerDataSource; 14 15 import replica.database.commands.DatabaseCommandVO; 16 17 import junit.framework.TestCase; 18 19 /*** 20 * 21 * @author Pedro Costa 22 * @author Helder Silva 23 * @since 31/Jan/2004 24 */ 25 public class PlsqlDbCommandTest extends TestCase { 26 27 PlsqlDbCommand plsqlDbCommand; 28 Server server; 29 DriverManagerDataSource dataSource; 30 31 String testTableName = "replica_plsql_test"; 32 33 /*** 34 * Constructor for PlsqlDbCommandTest. 35 * @param name 36 */ 37 public PlsqlDbCommandTest(String name) { 38 super(name); 39 } 40 41 /* 42 * @see TestCase#setUp() 43 */ 44 protected void setUp() throws Exception { 45 super.setUp(); 46 47 server = new Server(); 48 server.putPropertiesFromString("database.0=plsqldbcomtest"); 49 server.setPort(9002); 50 server.start(); 51 52 try{ Thread.sleep(500); }catch(InterruptedException e){ } 53 54 // BasicDataSource dataSource = new BasicDataSource(); 55 dataSource = new DriverManagerDataSource(); 56 dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); 57 dataSource.setUrl("jdbc:hsqldb:hsql://localhost:9002"); 58 dataSource.setUsername("sa"); 59 60 plsqlDbCommand = new PlsqlDbCommand(); 61 plsqlDbCommand.setDataSource(dataSource); 62 63 try{ dropTestingData(); }catch(Throwable t){ } 64 65 createTestingData(); 66 } 67 68 /* 69 * @see TestCase#tearDown() 70 */ 71 protected void tearDown() throws Exception { 72 super.tearDown(); 73 74 dropTestingData(); 75 76 server.stop(); 77 while( server.getState() != 16 /*ServerConstants.SERVER_STATE_SHUTDOWN*/ ){ 78 try{ Thread.sleep(200); }catch(InterruptedException e){} 79 } 80 } 81 82 void createTestingData(){ 83 84 Connection con = null; 85 try{ 86 87 con = dataSource.getConnection(); 88 89 Statement st = con.createStatement(); 90 91 st.execute("create table " + testTableName + " (id integer primary key, nome varchar);"); 92 93 st.close(); 94 } 95 catch(SQLException e){ 96 throw new RuntimeException(e); 97 } 98 finally{ 99 try{ con.close(); }catch(Throwable t){} 100 } 101 } 102 103 void dropTestingData(){ 104 105 Connection con = null; 106 try{ 107 108 con = dataSource.getConnection(); 109 110 Statement st = con.createStatement(); 111 112 st.execute("drop table " + testTableName + ";"); 113 114 st.close(); 115 } 116 catch(SQLException e){ 117 throw new RuntimeException(e); 118 } 119 finally{ 120 try{ con.close(); }catch(Throwable t){} 121 } 122 } 123 124 /* 125 * Test for Object execute(DatabaseCommandVO) 126 */ 127 public void testExecuteDatabaseCommandVO() throws SQLException{ 128 129 String program = "begin insert into " + testTableName + " values ( 1, 'nome' ); commit; end;"; 130 131 DatabaseCommandVO c = new DatabaseCommandVO(dataSource.getConnection(), program); 132 133 plsqlDbCommand.execute(c); 134 135 assertEquals(1, getTestingTableCount()); 136 } 137 138 int getTestingTableCount(){ 139 140 Connection con = null; 141 try{ 142 143 con = dataSource.getConnection(); 144 145 Statement st = con.createStatement(); 146 147 ResultSet rs = st.executeQuery("select count(*) from " + testTableName + ";"); 148 149 rs.next(); 150 int res = rs.getInt(1); 151 152 st.close(); 153 154 return res; 155 } 156 catch(SQLException e){ 157 throw new RuntimeException(e); 158 } 159 finally{ 160 try{ con.close(); }catch(Throwable t){} 161 } 162 } 163 164 public void testInit() { 165 plsqlDbCommand.init(); 166 167 assertNotNull(plsqlDbCommand.mobisnapInterpreter); 168 } 169 170 }

This page was automatically generated by Maven