1 5 package org.h2.test.db; 6 7 import java.sql.Connection ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 import java.util.ArrayList ; 12 13 import org.h2.api.Trigger; 14 import org.h2.test.TestBase; 15 16 public class TestRunscript extends TestBase implements Trigger { 17 18 public void test() throws Exception { 19 test(false); 20 test(true); 21 } 22 23 public static int test(int a) { 24 return Math.abs(a); 25 } 26 27 private void test(boolean password) throws Exception { 28 29 deleteDb("runscript"); 30 Connection conn1, conn2; 31 Statement stat1, stat2; 32 conn1 = getConnection("runscript"); 33 stat1 = conn1.createStatement(); 34 stat1.execute("create table test (id identity, name varchar(12))"); 35 stat1.execute("insert into test (name) values ('first'), ('second')"); 36 stat1.execute("create sequence testseq start with 100 increment by 10"); 37 stat1.execute("create alias mytest for \""+getClass().getName()+".test\""); 38 stat1.execute("create trigger mytrigger before insert on test nowait call \""+getClass().getName()+"\""); 39 stat1.execute("create view testview as select * from test where 1=0 union all select * from test where 0=1"); 40 stat1.execute("create user testadmin salt '00' hash '01' admin"); 41 stat1.execute("create schema testschema authorization testadmin"); 42 stat1.execute("create table testschema.parent(id int primary key, name varchar)"); 43 stat1.execute("create index idxname on testschema.parent(name)"); 44 stat1.execute("create table testschema.child(id int primary key, parentId int, name varchar, foreign key(parentId) references parent(id))"); 45 stat1.execute("create user testuser salt '02' hash '03'"); 46 stat1.execute("create role testrole"); 47 stat1.execute("grant all on testschema.child to testuser"); 48 stat1.execute("grant select, insert on testschema.parent to testrole"); 49 stat1.execute("grant testrole to testuser"); 50 51 String sql = "script to '"+BASE_DIR+"/backup.2.sql'"; 52 if(password) { 53 sql += " CIPHER AES PASSWORD 't1e2s3t4'"; 54 } 55 stat1.execute(sql); 56 57 deleteDb("runscriptRestore"); 58 conn2 = getConnection("runscriptRestore"); 59 stat2 = conn2.createStatement(); 60 sql = "runscript from '"+BASE_DIR+"/backup.2.sql'"; 61 if(password) { 62 sql += " CIPHER AES PASSWORD 'wrongpassword'"; 63 } 64 if(password) { 65 try { 66 stat2.execute(sql); 67 error("should fail"); 68 } catch(SQLException e) { 69 checkNotGeneralException(e); 70 } 71 } 72 sql = "runscript from '"+BASE_DIR+"/backup.2.sql'"; 73 if(password) { 74 sql += " CIPHER AES PASSWORD 't1e2s3t4'"; 75 } 76 stat2.execute(sql); 77 stat2.execute("script to '"+BASE_DIR+"/backup.3.sql'"); 78 79 compareDatabases(stat1, stat2); 80 81 conn1.close(); 82 conn2.close(); 83 } 84 85 private void compareDatabases(Statement stat1, Statement stat2) throws Exception { 86 ResultSet rs1 = stat1.executeQuery("SCRIPT NOPASSWORDS"); 87 ResultSet rs2 = stat2.executeQuery("SCRIPT NOPASSWORDS"); 88 ArrayList list1 = new ArrayList (); 89 ArrayList list2 = new ArrayList (); 90 while(rs1.next()) { 91 check(rs2.next()); 92 list1.add(rs1.getString(1)); 93 list2.add(rs2.getString(1)); 94 } 95 for(int i=0; i<list1.size(); i++) { 96 String s = (String )list1.get(i); 97 if(!list2.remove(s)) { 98 error("not found: " + s); 99 } 100 } 101 check(list2.size(), 0); 102 checkFalse(rs2.next()); 103 } 104 105 public void init(Connection conn, String schemaName, String triggerName, String tableName) { 106 } 107 108 public void fire(Connection conn, Object [] oldRow, Object [] newRow) throws SQLException { 109 } 110 111 } 112 | Popular Tags |