001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.db;
007:
008: import java.sql.Connection;
009: import java.sql.SQLException;
010: import java.sql.Statement;
011:
012: import org.h2.api.Trigger;
013: import org.h2.test.TestBase;
014:
015: /**
016: * Tests the RUNSCRIPT SQL statement.
017: */
018: public class TestRunscript extends TestBase implements Trigger {
019:
020: public void test() throws Exception {
021: test(false);
022: test(true);
023: }
024:
025: public static int test(int a) {
026: return Math.abs(a);
027: }
028:
029: private void test(boolean password) throws Exception {
030: deleteDb("runscript");
031: Connection conn1, conn2;
032: Statement stat1, stat2;
033: conn1 = getConnection("runscript");
034: stat1 = conn1.createStatement();
035: stat1
036: .execute("create table test (id identity, name varchar(12))");
037: stat1
038: .execute("insert into test (name) values ('first'), ('second')");
039: stat1
040: .execute("create sequence testSeq start with 100 increment by 10");
041: stat1.execute("create alias myTest for \""
042: + getClass().getName() + ".test\"");
043: stat1
044: .execute("create trigger myTrigger before insert on test nowait call \""
045: + getClass().getName() + "\"");
046: stat1
047: .execute("create view testView as select * from test where 1=0 union all select * from test where 0=1");
048: stat1
049: .execute("create user testAdmin salt '00' hash '01' admin");
050: stat1
051: .execute("create schema testSchema authorization testAdmin");
052: stat1
053: .execute("create table testSchema.parent(id int primary key, name varchar)");
054: stat1
055: .execute("create index idxname on testSchema.parent(name)");
056: stat1
057: .execute("create table testSchema.child(id int primary key, parentId int, name varchar, foreign key(parentId) references parent(id))");
058: stat1.execute("create user testUser salt '02' hash '03'");
059: stat1.execute("create role testRole");
060: stat1.execute("grant all on testSchema.child to testUser");
061: stat1
062: .execute("grant select, insert on testSchema.parent to testRole");
063: stat1.execute("grant testRole to testUser");
064:
065: String sql = "script to '" + baseDir + "/backup.2.sql'";
066: if (password) {
067: sql += " CIPHER AES PASSWORD 't1e2s3t4'";
068: }
069: stat1.execute(sql);
070:
071: deleteDb("runscriptRestore");
072: conn2 = getConnection("runscriptRestore");
073: stat2 = conn2.createStatement();
074: sql = "runscript from '" + baseDir + "/backup.2.sql'";
075: if (password) {
076: sql += " CIPHER AES PASSWORD 'wrongPassword'";
077: }
078: if (password) {
079: try {
080: stat2.execute(sql);
081: error();
082: } catch (SQLException e) {
083: checkNotGeneralException(e);
084: }
085: }
086: sql = "runscript from '" + baseDir + "/backup.2.sql'";
087: if (password) {
088: sql += " CIPHER AES PASSWORD 't1e2s3t4'";
089: }
090: stat2.execute(sql);
091: stat2.execute("script to '" + baseDir + "/backup.3.sql'");
092:
093: compareDatabases(stat1, stat2);
094:
095: conn1.close();
096: conn2.close();
097: }
098:
099: public void init(Connection conn, String schemaName,
100: String triggerName, String tableName, boolean before,
101: int type) {
102: if (!before) {
103: throw new InternalError("before:" + before);
104: }
105: if (type != INSERT) {
106: throw new InternalError("type:" + type);
107: }
108: }
109:
110: public void fire(Connection conn, Object[] oldRow, Object[] newRow)
111: throws SQLException {
112: }
113:
114: }
|