1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.support; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.sql.Connection; 23 import java.sql.DriverManager; 24 import java.sql.SQLException; 25 import java.util.Properties; 26 27 public class Support_SQL { 28 29 public static String sqlDriver = null; 30 31 public static String sqlLogin = null; 32 33 public static String sqlCatalog = null; 34 35 public static String sqlHost = null; 36 37 public static String sqlUrl = null; 38 39 public static String sqlPassword = null; 40 41 public static String sqlUser = null; 42 43 public static int sqlMaxConnections = 5; 44 45 public static int sqlMaxTasks = 1; 46 47 private static File dbFile = null; 48 loadDriver()49 public static void loadDriver() { 50 try { 51 InputStream in = Class.forName("tests.support.Support_SQL") 52 .getResourceAsStream("/connection.properties"); 53 loadProperties(in); 54 in.close(); 55 56 String tmp = System.getProperty("java.io.tmpdir"); 57 File tmpDir = new File(tmp); 58 if (tmpDir.isDirectory()) { 59 dbFile = File.createTempFile("sqliteTest", ".db", tmpDir); 60 dbFile.deleteOnExit(); 61 } else { 62 System.err.println("java.io.tmpdir does not exist"); 63 } 64 Class.forName("SQLite.JDBCDriver").newInstance(); 65 66 // overwrite sqlUrl to point to valid directory 67 sqlUrl = "jdbc:sqlite:/" + dbFile.getPath(); 68 69 Class.forName(sqlDriver).newInstance(); 70 } catch (Exception ex) { 71 throw new RuntimeException(ex); 72 } 73 } 74 getConnection()75 public static Connection getConnection() throws SQLException { 76 try { 77 return DriverManager.getConnection(Support_SQL.sqlUrl, 78 Support_SQL.sqlLogin, Support_SQL.sqlPassword); 79 } catch (SQLException e) { 80 throw new SQLException("Failed to connect. url=" + Support_SQL.sqlUrl + ", sqlLogin=" 81 + Support_SQL.sqlLogin + ", sqlPassword=" + Support_SQL.sqlPassword, e); 82 } 83 } 84 getConnection(String url, String login, String password)85 public static Connection getConnection(String url, String login, 86 String password) throws SQLException { 87 88 return DriverManager.getConnection(url, login, password); 89 } 90 isEqual(byte[] b1, int off1, byte[] b2, int off2, int len)91 public static boolean isEqual(byte[] b1, int off1, byte[] b2, int off2, 92 int len) { 93 for (int i = 0; i < len; ++i) 94 if (b1[i + off1] != b2[i + off2]) 95 return false; 96 return true; 97 } 98 loadProperties(InputStream fileName)99 private static void loadProperties(InputStream fileName) throws IOException { 100 Properties properties = new Properties(); 101 properties.load(fileName); 102 sqlDriver = properties.getProperty("sqlDriver"); 103 sqlLogin = properties.getProperty("sqlLogin"); 104 sqlCatalog = properties.getProperty("sqlCatalog"); 105 sqlHost = properties.getProperty("sqlHost"); 106 sqlUrl = properties.getProperty("sqlUrlPrefix") + sqlHost + "/" 107 + sqlCatalog; 108 sqlPassword = properties.getProperty("sqlPassword"); 109 sqlUser = properties.getProperty("sqlUser"); 110 sqlMaxConnections = Integer.parseInt(properties 111 .getProperty("sqlMaxConnections")); 112 sqlMaxTasks = Integer.parseInt(properties.getProperty("sqlMaxTasks")); 113 } 114 getFilename()115 public static String getFilename() { 116 return dbFile.getPath(); 117 } 118 } 119