1 5 package org.h2.test.unit; 6 7 import java.sql.*; 8 9 import org.h2.test.TestBase; 10 import org.h2.tools.Backup; 11 import org.h2.tools.ChangePassword; 12 import org.h2.tools.DeleteDbFiles; 13 import org.h2.tools.RunScript; 14 import org.h2.tools.Server; 15 import org.h2.util.Resources; 16 17 public class TestTools extends TestBase { 18 19 public void test() throws Exception { 20 deleteDb("utils"); 21 testManagementDb(); 22 testResourceGenerator(); 23 testChangePassword(); 24 testServer(); 25 testBackupRunscript(); 26 } 27 28 private void testManagementDb() throws Exception { 29 int count = getSize(2, 10); 30 for(int i=0; i<count; i++) { 31 Server server = Server.createTcpServer(new String []{}).start(); 32 server.stop(); 33 server = Server.createTcpServer(new String []{"-tcpPassword", "abc"}).start(); 34 server.stop(); 35 } 36 } 37 38 private void testBackupRunscript() throws Exception { 39 Class.forName("org.h2.Driver"); 40 String url = "jdbc:h2:" + BASE_DIR+ "/utils"; 41 String user = "sa", password = "abc"; 42 String fileName = BASE_DIR + "/b2.sql"; 43 Connection conn = DriverManager.getConnection(url, user, password); 44 conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); 45 conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')"); 46 conn.close(); 47 Backup.main(new String []{"-url", url, "-user", user, "-password", password, "-script", fileName, "-options", "nodata", "compression", "lzf", "cipher", "xtea", "password", "'123'"}); 48 DeleteDbFiles.main(new String []{"-dir", BASE_DIR, "-db", "utils", "-quiet"}); 49 RunScript.main(new String []{"-url", url, "-user", user, "-password", password, "-script", fileName, "-options", "compression", "lzf", "cipher", "xtea", "password", "'123'"}); 50 conn = DriverManager.getConnection("jdbc:h2:" + BASE_DIR+ "/utils", "sa", "abc"); 51 ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST"); 52 checkFalse(rs.next()); 53 conn.close(); 54 } 55 56 private void testResourceGenerator() throws Exception { 57 Resources.main(new String []{"."}); 58 } 59 60 private void testChangePassword() throws Exception { 61 Class.forName("org.h2.Driver"); 62 Connection conn = DriverManager.getConnection("jdbc:h2:" + BASE_DIR+ "/utils;CIPHER=XTEA;STORAGE=TEXT", "sa", "abc 123"); 63 Statement stat = conn.createStatement(); 64 stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"); 65 conn.close(); 66 String [] args = new String []{"-dir", BASE_DIR, "-db", "utils", "-cipher", "XTEA", "-decrypt", "abc", "-quiet"}; 67 ChangePassword.main(args); 68 args = new String []{"-dir", BASE_DIR, "-db", "utils", "-cipher", "AES", "-encrypt", "def", "-quiet"}; 69 ChangePassword.main(args); 70 conn = DriverManager.getConnection("jdbc:h2:" + BASE_DIR+ "/utils;CIPHER=AES", "sa", "def 123"); 71 stat = conn.createStatement(); 72 stat.execute("SELECT * FROM TEST"); 73 conn.close(); 74 args = new String []{"-dir", BASE_DIR, "-db", "utils", "-quiet"}; 75 DeleteDbFiles.main(args); 76 } 77 78 private void testServer() throws Exception { 79 Connection conn; 80 Server server = Server.createTcpServer(new String []{"-ifExists", "false", "-baseDir", BASE_DIR}).start(); 81 conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test", "sa", ""); 82 conn.close(); 83 server.stop(); 84 85 server = Server.createTcpServer(new String []{"-ifExists", "true", "-tcpPassword", "abc", "-baseDir", BASE_DIR}).start(); 86 try { 87 conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test2", "sa", ""); 88 error("should not be able to create new db"); 89 } catch(SQLException e) { 90 checkNotGeneralException(e); 91 } 92 conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test", "sa", ""); 93 conn.close(); 94 try { 95 Server.shutdownTcpServer("tcp://localhost", "", true); 96 error("shouldn't work and should throw an exception"); 97 } catch(SQLException e) { 98 } 100 conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test", "sa", ""); 101 conn.close(); 102 Server.shutdownTcpServer("tcp://localhost", "abc", true); 103 try { 104 conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test", "sa", ""); 105 error("server must have been closed"); 106 } catch(SQLException e) { 107 checkNotGeneralException(e); 108 } 109 } 110 111 } 112 | Popular Tags |