1 5 package org.h2.test.db; 6 7 import java.io.File ; 8 import java.sql.Connection ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 import java.util.ArrayList ; 12 13 import org.h2.test.TestBase; 14 import org.h2.tools.FileBase; 15 16 public class TestReadOnly extends TestBase { 17 18 public void test() throws Exception { 19 if(config.memory) { 20 return; 21 } 22 deleteDb("readonly"); 23 Connection conn = getConnection("readonly"); 24 Statement stat = conn.createStatement(); 25 stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); 26 stat.execute("INSERT INTO TEST VALUES(1, 'Hello')"); 27 stat.execute("INSERT INTO TEST VALUES(2, 'World')"); 28 conn.close(); 29 30 setReadOnly(); 31 conn = getConnection("readonly"); 32 stat = conn.createStatement(); 33 stat.execute("SELECT * FROM TEST"); 34 try { 35 stat.execute("DELETE FROM TEST"); 36 error("read only delete"); 37 } catch(SQLException e) { 38 checkNotGeneralException(e); 39 } 40 conn.close(); 41 42 conn = getConnection("readonly;DB_CLOSE_DELAY=1"); 43 stat = conn.createStatement(); 44 stat.execute("SELECT * FROM TEST"); 45 try { 46 stat.execute("DELETE FROM TEST"); 47 error("read only delete"); 48 } catch(SQLException e) { 49 checkNotGeneralException(e); 50 } 51 conn.close(); 52 } 53 54 private void setReadOnly() throws SQLException { 55 ArrayList list = FileBase.getDatabaseFiles(TestBase.BASE_DIR, "readonly", true); 56 for(int i=0; i<list.size(); i++) { 57 String fileName = (String ) list.get(i); 58 File file = new File (fileName); 59 file.setReadOnly(); 60 } 61 } 62 63 } 64 | Popular Tags |