KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > db > TestRunscript


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.db;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Statement JavaDoc;
11 import java.util.ArrayList JavaDoc;
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 JavaDoc {
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 JavaDoc {
28
29         deleteDb("runscript");
30         Connection JavaDoc conn1, conn2;
31         Statement JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc stat1, Statement JavaDoc stat2) throws Exception JavaDoc {
86         ResultSet JavaDoc rs1 = stat1.executeQuery("SCRIPT NOPASSWORDS");
87         ResultSet JavaDoc rs2 = stat2.executeQuery("SCRIPT NOPASSWORDS");
88         ArrayList JavaDoc list1 = new ArrayList JavaDoc();
89         ArrayList JavaDoc list2 = new ArrayList JavaDoc();
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 JavaDoc s = (String JavaDoc)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 JavaDoc conn, String JavaDoc schemaName, String JavaDoc triggerName, String JavaDoc tableName) {
106     }
107
108     public void fire(Connection JavaDoc conn, Object JavaDoc[] oldRow, Object JavaDoc[] newRow) throws SQLException JavaDoc {
109     }
110
111 }
112
Popular Tags