diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-02-10 15:43:57 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-02-10 15:43:57 -0800 |
commit | b7926325a1c1a370c84c81db80372f59af240a53 (patch) | |
tree | b1d0214be443ea674d0ded8c502a8e074e50bdd2 /sql/src/test/java/tests/SQLite/StmtTest.java | |
parent | 687f18b91f4a0a728a027579110953ee729adcb8 (diff) | |
download | libcore-b7926325a1c1a370c84c81db80372f59af240a53.zip libcore-b7926325a1c1a370c84c81db80372f59af240a53.tar.gz libcore-b7926325a1c1a370c84c81db80372f59af240a53.tar.bz2 |
auto import from //branches/cupcake/...@130745
Diffstat (limited to 'sql/src/test/java/tests/SQLite/StmtTest.java')
-rw-r--r-- | sql/src/test/java/tests/SQLite/StmtTest.java | 407 |
1 files changed, 358 insertions, 49 deletions
diff --git a/sql/src/test/java/tests/SQLite/StmtTest.java b/sql/src/test/java/tests/SQLite/StmtTest.java index fc611db..20f8221 100644 --- a/sql/src/test/java/tests/SQLite/StmtTest.java +++ b/sql/src/test/java/tests/SQLite/StmtTest.java @@ -16,21 +16,20 @@ package tests.SQLite; +import SQLite.Constants; import SQLite.Database; import SQLite.Exception; import SQLite.Stmt; import SQLite.TableResult; -import dalvik.annotation.TestTargets; +import dalvik.annotation.AndroidOnly; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargetClass; -import junit.framework.TestCase; import tests.support.DatabaseCreator; import tests.support.Support_SQL; -import java.io.File; import java.sql.Connection; import java.sql.SQLException; @@ -41,21 +40,71 @@ public class StmtTest extends SQLiteTest { private static Stmt st = null; -// private final File dbFile = new File("sqliteTest.db"); + private static final String createAllTypes = + "create table type (" + - protected void setUp() throws java.lang.Exception { + " BoolVal BOOLEAN," + " IntVal INT," + " LongVal LONG," + + " Bint BIGINT," + " Tint TINYINT," + " Sint SMALLINT," + + " Mint MEDIUMINT, " + + + " IntegerVal INTEGER, " + " RealVal REAL, " + + " DoubleVal DOUBLE, " + " FloatVal FLOAT, " + + " DecVal DECIMAL, " + + + " NumVal NUMERIC, " + " charStr CHAR(20), " + + " dateVal DATE, " + " timeVal TIME, " + " TS TIMESTAMP, " + + + + " DT DATETIME, " + " TBlob TINYBLOB, " + " BlobVal BLOB, " + + " MBlob MEDIUMBLOB, " + " LBlob LONGBLOB, " + + + " TText TINYTEXT, " + " TextVal TEXT, " + + " MText MEDIUMTEXT, " + " LText LONGTEXT, " + + + " MaxLongVal BIGINT, MinLongVal BIGINT, "+ + + " validURL URL, invalidURL URL "+ + + ");"; + + static final String insertAllTypes = + "insert into type (BoolVal, IntVal, LongVal, Bint, Tint, Sint, Mint," + + "IntegerVal, RealVal, DoubleVal, FloatVal, DecVal," + + "NumVal, charStr, dateVal, timeVal, TS," + + "DT, TBlob, BlobVal, MBlob, LBlob," + + "TText, TextVal, MText, LText, MaxLongVal, MinLongVal," + + " validURL, invalidURL" + + ") " + + "values (1, -1, 22, 2, 33," + + "3, 1, 2, 3.9, 23.2, 33.3, 44," + + "5, 'test string', '1799-05-26', '12:35:45', '2007-10-09 14:28:02.0'," + + "'1221-09-22 10:11:55', 1, 2, 3, 4," + + "'Test text message tiny', 'Test text'," + + " 'Test text message medium', 'Test text message long', " + + Long.MAX_VALUE+", "+Long.MIN_VALUE+", " + + "null, null "+ + ");"; + + static final String allTypesTable = "type"; + + public void setUp() throws java.lang.Exception { super.setUp(); Support_SQL.loadDriver(); db = new Database(); - db.open(dbFile.getName(), 0); + db.open(dbFile.getPath(), 0); db.exec(DatabaseCreator.CREATE_TABLE_SIMPLE1, null); - DatabaseCreator.fillSimpleTable1(Support_SQL.getConnection()); - - st = new Stmt(); + DatabaseCreator.fillSimpleTable1(conn); + } public void tearDown() { - super.tearDown(); + if (st != null) { + try { + st.close(); + } catch (Exception e) { + + } + } try { db.close(); Connection con = Support_SQL.getConnection(); @@ -66,7 +115,7 @@ public class StmtTest extends SQLiteTest { } catch (SQLException e) { fail("SQLException in tearDown: "+e.getMessage()); } - + super.tearDown(); } /** @@ -78,7 +127,7 @@ public class StmtTest extends SQLiteTest { method = "Stmt", args = {} ) - public void _testStmt() { + public void testStmt() { Stmt st = new Stmt(); assertNotNull(st); try { @@ -107,8 +156,8 @@ public class StmtTest extends SQLiteTest { method = "finalize", args = {} ) - public void _testFinalize() { - fail("Not yet implemented"); + public void testFinalize() { + } /** @@ -215,16 +264,40 @@ public class StmtTest extends SQLiteTest { } /** + * @throws Exception * @tests {@link Stmt#reset()} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.TODO, notes = "method test", method = "reset", args = {} ) - public void _testReset() { - fail("Not yet implemented"); + @AndroidOnly("Tableresult is not cleared when resetting statement: "+ + "Either complete spec or change implementation accordingly.") + public void testReset() throws Exception { + db.exec("create table TEST (res integer not null)", null); + + st = db.prepare("insert into TEST values (:one);"); + st.bind(1, 1); + st.step(); + + // verify that parameter is still bound + st.reset(); + assertEquals(1,st.bind_parameter_count()); + st.step(); + + TableResult count = db.get_table("select count(*) from TEST where res=1", null); + + String[] row0 = (String[]) count.rows.elementAt(0); + assertEquals(2, Integer.parseInt(row0[0])); + + //Verify that rest (tableResult) is cleared + st = db.prepare("select * from TEST;"); + st.step(); + assertEquals(1, st.column_count()); + st.reset(); + assertEquals(0,st.column_count()); } /** @@ -578,7 +651,7 @@ public class StmtTest extends SQLiteTest { * @tests {@link Stmt#bind_zeroblob(int, int)} */ @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.NOT_FEASIBLE, notes = "not supported", method = "bind_zeroblob", args = {int.class, int.class} @@ -720,16 +793,47 @@ public class StmtTest extends SQLiteTest { } /** + * @throws Exception * @tests {@link Stmt#column_int(int)} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.COMPLETE, notes = "method test", method = "column_int", args = {int.class} ) - public void _testColumn_int() { - fail("Not yet implemented"); + public void testColumn_int() throws Exception { + db.exec(createAllTypes, null); + db.exec(insertAllTypes, null); + + int columnObjectCastFromLong; + Object columnObject = null; + int intColumn = 0; + String selectStmt = "select * from "+DatabaseCreator.SIMPLE_TABLE1; + + st = db.prepare(selectStmt); + st.step(); + // select 'speed' value + columnObject = st.column(1); + intColumn = st.column_int(1); + assertNotNull(intColumn); + + assertTrue("Integer".equalsIgnoreCase(st.column_decltype(1))); + int stSpeed = Integer.parseInt(columnObject.toString()); + assertNotNull(stSpeed); + assertEquals( intColumn, stSpeed); + assertEquals(10,stSpeed); + + selectStmt = "select TextVal from "+allTypesTable; + + st = db.prepare(selectStmt); + st.step(); + // select double value + try { + st.column_int(0); + } catch (Exception e) { + //ok + } } /** @@ -747,7 +851,6 @@ public class StmtTest extends SQLiteTest { long longColumn = 0; try { String selectStmt = "select * from "+DatabaseCreator.SIMPLE_TABLE1; - TableResult res = db.get_table(selectStmt); st = db.prepare(selectStmt); st.step(); columnObject = st.column(1); @@ -779,75 +882,219 @@ public class StmtTest extends SQLiteTest { } /** + * @throws Exception * @tests {@link Stmt#column_double(int)} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.COMPLETE, notes = "method test", method = "column_double", args = {int.class} ) - public void _testColumn_double() { - fail("Not yet implemented"); + public void testColumn_double() throws Exception { + db.exec(createAllTypes, null); + db.exec(insertAllTypes, null); + + Object columnObject = null; + double doubleColumn = 0; + double actualVal = 23.2; + String selectStmt = "select DoubleVal from "+allTypesTable; + + st = db.prepare(selectStmt); + st.step(); + // select double value + doubleColumn = st.column_double(0); + assertNotNull(doubleColumn); + + assertTrue("DOUBLE".equalsIgnoreCase(st.column_decltype(0))); + assertNotNull(doubleColumn); + assertEquals( actualVal, doubleColumn); + + // Exception test + selectStmt = "select dateVal from "+allTypesTable; + + st = db.prepare(selectStmt); + st.step(); + // select double value + try { + st.column_double(0); + } catch (Exception e) { + //ok + } + + } /** + * @throws Exception * @tests {@link Stmt#column_bytes(int)} */ @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.NOT_FEASIBLE, notes = "not supported", method = "column_bytes", args = {int.class} ) - public void testColumn_bytes() { + public void testColumn_bytes() throws Exception { + + db.exec("create table B(id integer primary key, val blob)",null); + db.exec("insert into B values(1, zeroblob(128))", null); + st = db.prepare("select val from B where id = 1"); + assertTrue(st.step()); try { - st.column_bytes(1); + st.column_bytes(0); } catch (Exception e) { assertEquals("unsupported", e.getMessage()); } } /** + * @throws Exception * @tests {@link Stmt#column_string(int)} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.COMPLETE, notes = "method test", method = "column_string", args = {int.class} ) - public void _testColumn_string() { - fail("Not yet implemented"); + public void testColumn_string() throws Exception { + db.exec(createAllTypes, null); + db.exec(insertAllTypes, null); + + Object columnObject = null; + String stringColumn = ""; + String actualVal = "test string"; + String selectStmt = "select charStr from "+allTypesTable; + + st = db.prepare(selectStmt); + st.step(); + // select string value + stringColumn = st.column_string(0); + assertNotNull(stringColumn); + + assertTrue("CHAR(20)".equalsIgnoreCase(st.column_decltype(0))); + assertNotNull(stringColumn); + assertEquals( actualVal, stringColumn); + + // Exception test + selectStmt = "select DoubleVal from "+allTypesTable; + + st = db.prepare(selectStmt); + st.step(); + // select double value + try { + st.column_string(0); + } catch (Exception e) { + //ok + } } /** + * @throws Exception * @tests {@link Stmt#column_type(int)} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.SUFFICIENT, notes = "method test", method = "column_type", args = {int.class} ) - public void _testColumn_type() { - fail("Not yet implemented"); + @AndroidOnly("For numeric, float and blob wrong type is returned") + public void testColumn_type() throws Exception { + db.exec(createAllTypes, null); + db.exec(insertAllTypes, null); + st = db.prepare("select * from " + allTypesTable); + st.step(); + + // Exception test + try { + st.column_type(100); + } catch (Exception e) { + // ok + } + + /* + Dictionary + + public static final int SQLITE_INTEGER = 1; + public static final int SQLITE_FLOAT = 2; + public static final int SQLITE_BLOB = 4; + public static final int SQLITE_NULL = 5; + public static final int SQLITE3_TEXT = 3; + public static final int SQLITE_NUMERIC = -1; + */ + + assertEquals(Constants.SQLITE3_TEXT, st.column_type(23)); // ok TEXT + assertEquals(Constants.SQLITE3_TEXT, st.column_type(13)); // CHAR(20) + + assertEquals(Constants.SQLITE_FLOAT, st.column_type(8)); + assertEquals(Constants.SQLITE_FLOAT, st.column_type(9)); + assertEquals(Constants.SQLITE_FLOAT, st.column_type(10)); // FLOAT + + for (int i = 0; i < 8; i++) { + assertEquals("Expected Integer at position " + i, + Constants.SQLITE_INTEGER, st.column_type(i)); + } + + assertEquals(Constants.SQLITE_NULL, st.column_type(28)); + assertEquals(Constants.SQLITE_NULL, st.column_type(29)); + + // Failing tests + assertTrue("NUMERIC".equalsIgnoreCase(st.column_decltype(12))); + assertEquals(Constants.SQLITE_NUMERIC, st.column_type(12)); // NUMERIC + // -> got + // INTEGER + + assertTrue("FLOAT".equalsIgnoreCase(st.column_decltype(11))); + assertEquals(Constants.SQLITE_FLOAT, st.column_type(11)); // FLOAT -> + // got INTEGER + assertTrue("BLOB".equalsIgnoreCase(st.column_decltype(19))); + assertEquals(Constants.SQLITE_BLOB, st.column_type(19)); // Blob got + // INTEGER + } /** + * @throws Exception * @tests {@link Stmt#column_count() )} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.COMPLETE, notes = "method test", method = "column_count", args = {} ) - public void _testColumn_count() { - fail("Not yet implemented"); + @AndroidOnly("Wrong value is returned in case of a prepared statment to "+ + "which a '*' bound ") + public void testColumn_count() throws Exception { + + String selectStmt = "select * from "+DatabaseCreator.SIMPLE_TABLE1; + st = db.prepare(selectStmt); + + assertEquals(3, st.column_count()); + + st.step(); + int columnCount = st.column_count(); + assertNotNull(columnCount); + assertEquals( 3, columnCount); + + // actual prepared statement + selectStmt = "select ? from "+DatabaseCreator.SIMPLE_TABLE1; + st = db.prepare(selectStmt); + + assertEquals(3, st.column_count()); + + st.bind(1, "*"); + st.step(); + columnCount = st.column_count(); + assertNotNull(columnCount); + assertEquals( 3, columnCount); + } /** + * @throws Exception * @tests {@link Stmt#column(int) )} */ @TestTargetNew( @@ -856,7 +1103,7 @@ public class StmtTest extends SQLiteTest { method = "column", args = {int.class} ) - public void testColumn() { + public void testColumn() throws Exception { Object columnObject = null; int columnObjectCastFromLong; int intColumn = 0; @@ -905,19 +1152,25 @@ public class StmtTest extends SQLiteTest { */ @TestTargetNew( level = TestLevel.NOT_FEASIBLE, - notes = "method test", + notes = "not supported", method = "column_table_name", args = {int.class} ) - public void _testColumn_table_name() { - fail("Not yet implemented"); + public void testColumn_table_name() { + try { + st = db.prepare("select * from " + DatabaseCreator.SIMPLE_TABLE1); + String name = st.column_table_name(1); + fail("Function is now supported."); + } catch (Exception e) { + assertEquals("unsupported", e.getMessage()); + } } /** * @tests {@link Stmt#column_database_name(int)} */ @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.NOT_FEASIBLE, notes = "not supported", method = "column_database_name", args = {int.class} @@ -927,7 +1180,7 @@ public class StmtTest extends SQLiteTest { st = db.prepare("insert into " + DatabaseCreator.SIMPLE_TABLE1 + " values (:one,:two,:three)"); String name = st.column_database_name(1); - fail("test fails"); + fail("Function is now supported."); } catch (Exception e) { assertEquals("unsupported", e.getMessage()); } @@ -935,16 +1188,66 @@ public class StmtTest extends SQLiteTest { } /** + * @throws Exception * @tests {@link Stmt#column_decltype(int)} */ @TestTargetNew( - level = TestLevel.NOT_FEASIBLE, + level = TestLevel.SUFFICIENT, notes = "method test", method = "column_decltype", args = {int.class} ) - public void _testColumn_decltype() { - fail("Not yet implemented"); + public void testColumn_decltype() throws Exception { + db.exec(createAllTypes, null); + db.exec(insertAllTypes, null); + st = db.prepare("select * from " + allTypesTable); + st.step(); + + // Exception test + try { + st.column_decltype(100); + } catch (Exception e) { + // ok + } + + assertTrue(st.column_decltype(0), "BOOLEAN".equalsIgnoreCase(st + .column_decltype(0))); + assertTrue(st.column_decltype(1), "INT".equalsIgnoreCase(st + .column_decltype(1))); + assertTrue(st.column_decltype(2), "LONG".equalsIgnoreCase(st + .column_decltype(2))); + assertTrue(st.column_decltype(3), "BIGINT".equalsIgnoreCase(st + .column_decltype(3))); + assertTrue(st.column_decltype(4), "TINYINT".equalsIgnoreCase(st + .column_decltype(4))); + assertTrue(st.column_decltype(5), "SMALLINT".equalsIgnoreCase(st + .column_decltype(5))); + assertTrue(st.column_decltype(6), "MEDIUMINT".equalsIgnoreCase(st + .column_decltype(6))); + assertTrue(st.column_decltype(7), "INTEGER".equalsIgnoreCase(st + .column_decltype(7))); + assertTrue(st.column_decltype(8), "REAL".equalsIgnoreCase(st + .column_decltype(8))); + assertTrue(st.column_decltype(9), "DOUBLE".equalsIgnoreCase(st + .column_decltype(9))); + assertTrue(st.column_decltype(10), "FLOAT".equalsIgnoreCase(st + .column_decltype(10))); + assertTrue(st.column_decltype(11), "DECIMAL".equalsIgnoreCase(st + .column_decltype(11))); + assertTrue(st.column_decltype(12), "NUMERIC".equalsIgnoreCase(st + .column_decltype(12))); + assertTrue(st.column_decltype(13), "CHAR(20)".equalsIgnoreCase(st + .column_decltype(13))); + + assertTrue(st.column_decltype(19), "BLOB".equalsIgnoreCase(st + .column_decltype(19))); + + assertTrue(st.column_decltype(23), "TEXT".equalsIgnoreCase(st + .column_decltype(23))); + assertTrue(st.column_decltype(28), "URL".equalsIgnoreCase(st + .column_decltype(28))); + assertTrue(st.column_decltype(29), "URL".equalsIgnoreCase(st + .column_decltype(29))); } /** @@ -952,11 +1255,17 @@ public class StmtTest extends SQLiteTest { */ @TestTargetNew( level = TestLevel.NOT_FEASIBLE, - notes = "method test", + notes = "not supported", method = "column_origin_name", args = {int.class} ) - public void _testColumn_origin_name() { - fail("Not yet implemented"); + public void testColumn_origin_name() { + try { + st = db.prepare("select * from " + DatabaseCreator.SIMPLE_TABLE1); + String name = st.column_origin_name(1); + fail("Function is now supported."); + } catch (Exception e) { + assertEquals("unsupported", e.getMessage()); + } } } |