/luni/src/test/java/libcore/java/sql/OldSQLTest.java

https://bitbucket.org/aways/android_libcore · Java · 103 lines · 76 code · 11 blank · 16 comment · 9 complexity · da3fd4df46e734a160e6af9bf584cb0f MD5 · raw file

  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. package libcore.java.sql;
  17. import java.io.File;
  18. import java.sql.Connection;
  19. import java.sql.DriverManager;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import junit.framework.TestCase;
  24. public abstract class OldSQLTest extends TestCase {
  25. static Connection conn;
  26. @Override public void setUp() throws Exception {
  27. getSQLiteConnection();
  28. createZoo();
  29. }
  30. protected File dbFile;
  31. protected void getSQLiteConnection() throws Exception {
  32. String tmp = System.getProperty("java.io.tmpdir");
  33. File tmpDir = new File(tmp);
  34. if (tmpDir.isDirectory()) {
  35. dbFile = File.createTempFile("sqliteTest", ".db", tmpDir);
  36. dbFile.deleteOnExit();
  37. } else {
  38. System.err.println("java.io.tmpdir does not exist");
  39. }
  40. Class.forName("SQLite.JDBCDriver").newInstance();
  41. conn = DriverManager.getConnection("jdbc:sqlite:/" + dbFile.getPath());
  42. assertNotNull("Connection created ", conn);
  43. }
  44. @Override public void tearDown() throws SQLException {
  45. Statement st = null;
  46. try {
  47. if (! conn.isClosed()) {
  48. st = conn.createStatement();
  49. st.execute("drop table if exists zoo");
  50. }
  51. } finally {
  52. try {
  53. if (st != null) {
  54. st.close();
  55. conn.close();
  56. }
  57. } catch(SQLException ee) {
  58. //ignore
  59. }
  60. }
  61. }
  62. public void createZoo() throws SQLException {
  63. String[] queries = {
  64. "create table zoo(id smallint, name varchar(10), family varchar(10))",
  65. "insert into zoo values (1, 'Kesha', 'parrot')",
  66. "insert into zoo values (2, 'Yasha', 'sparrow')" };
  67. Statement st = null;
  68. try {
  69. st = conn.createStatement();
  70. for (int i = 0; i < queries.length; i++) {
  71. st.execute(queries[i]);
  72. }
  73. } finally {
  74. try {
  75. if (st != null) {
  76. st.close();
  77. }
  78. } catch (SQLException ee) {}
  79. }
  80. }
  81. public int getCount(ResultSet rs) {
  82. int count = 0;
  83. try {
  84. while (rs.next()) {
  85. count++;
  86. }
  87. } catch (SQLException e) {
  88. fail("SQLException is thrown");
  89. }
  90. return count;
  91. }
  92. }