void test_obj_propexn() { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<Object> obj = Object::New(); obj->SetAccessor(String::New("myprop"), ReadExn, WriteExn); Local<Object> global = context->Global(); global->Set(String::New("testobj"), obj); Handle<String> source = String::New("var n = 0;" "try { testobj.myprop; } catch (e) { n += e; };" "try { testobj.myprop = (n+9); } catch (e) { n += e; }; n"); // Compile the source code. Handle<Script> script = Script::Compile(source); TryCatch trycatch; // Run the script to get the result. Handle<Value> result = script->Run(); do_check_false(result.IsEmpty()); do_check_true(result->IsInt32()); do_check_false(trycatch.HasCaught()); JSInt32 i = result->Int32Value(); do_check_eq(13, i); context.Dispose(); }
void test_HasTransaction() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // First test that it holds the transaction after it should have gotten one. { mozStorageTransaction transaction(db, PR_FALSE); do_check_true(transaction.HasTransaction()); (void)transaction.Commit(); // And that it does not have a transaction after we have committed. do_check_false(transaction.HasTransaction()); } // Check that no transaction is had after a rollback. { mozStorageTransaction transaction(db, PR_FALSE); do_check_true(transaction.HasTransaction()); (void)transaction.Rollback(); do_check_false(transaction.HasTransaction()); } // Check that we do not have a transaction if one is already obtained. mozStorageTransaction outerTransaction(db, PR_FALSE); do_check_true(outerTransaction.HasTransaction()); { mozStorageTransaction innerTransaction(db, PR_FALSE); do_check_false(innerTransaction.HasTransaction()); } }
void test_SetDefaultAction() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // First we test that rollback happens when we first set it to automatically // commit. { mozStorageTransaction transaction(db, PR_TRUE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test1 (id INTEGER PRIMARY KEY)" )); transaction.SetDefaultAction(PR_FALSE); } PRBool exists = PR_TRUE; (void)db->TableExists(NS_LITERAL_CSTRING("test1"), &exists); do_check_false(exists); // Now we do the opposite and test that a commit happens when we first set it // to automatically rollback. { mozStorageTransaction transaction(db, PR_FALSE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test2 (id INTEGER PRIMARY KEY)" )); transaction.SetDefaultAction(PR_TRUE); } exists = PR_FALSE; (void)db->TableExists(NS_LITERAL_CSTRING("test2"), &exists); do_check_true(exists); }
NS_IMETHOD Run() { // Use an explicit do_GetService instead of getService so that the check in // getService doesn't blow up. nsCOMPtr<mozIStorageService> service = do_GetService("@mozilla.org/storage/service;1"); do_check_false(service); return NS_OK; }
void test_null_database_connection() { // We permit the use of the Transaction helper when passing a null database // in, so we need to make sure this still works without crashing. mozStorageTransaction transaction(nsnull, PR_FALSE); do_check_false(transaction.HasTransaction()); do_check_true(NS_SUCCEEDED(transaction.Commit())); do_check_true(NS_SUCCEEDED(transaction.Rollback())); }
void test_obj_tmplexn() { HandleScope scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<ObjectTemplate> tmpl = ObjectTemplate::New(); tmpl->SetNamedPropertyHandler(NamedGetterThrows, NamedSetterThrows, 0, NamedDeleterThrows); tmpl->SetIndexedPropertyHandler(IndexedGetterThrows, IndexedSetterThrows, 0, IndexedDeleterThrows); Handle<Object> obj = tmpl->NewInstance(); Local<Object> global = context->Global(); global->Set(String::New("testobj"), obj); Handle<String> source = String::New("var n = 0;" "try { testobj.myprop; } catch (e) { n += e; };" "try { testobj.myprop = (n+9); } catch (e) { n += e; }" "try { delete testobj.myprop; } catch (e) { n += e; };" "try { testobj[4]; } catch (e) { n += e; };" "try { testobj[5] = (n+9); } catch (e) { n += e; }" "try { delete testobj[6]; } catch (e) { n += e; }; n"); // Compile the source code. Handle<Script> script = Script::Compile(source); TryCatch trycatch; // Run the script to get the result. Handle<Value> result = script->Run(); do_check_false(result.IsEmpty()); do_check_true(result->IsInt32()); do_check_false(trycatch.HasCaught()); JSInt32 i = result->Int32Value(); do_check_eq(i, 21); context.Dispose(); }
void test_CString() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create table with a single string column. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (str STRING)" )); // Create statements to INSERT and SELECT the string. nsCOMPtr<mozIStorageStatement> insert, select; (void)db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (str) VALUES (?1)" ), getter_AddRefs(insert)); (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT str FROM test" ), getter_AddRefs(select)); // Roundtrip a string through the table, and ensure it comes out as expected. static const char sCharArray[] = "I'm not a \xff\x00\xac\xde\xbb ASCII string!"; nsCAutoString inserted(sCharArray, NS_ARRAY_LENGTH(sCharArray) - 1); do_check_true(inserted.Length() == NS_ARRAY_LENGTH(sCharArray) - 1); { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } { nsCAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); do_check_true(result == inserted); } (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); }
void test_Rollback() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create a table in a transaction, call Rollback, and make sure that it does // not exists after the transaction falls out of scope. { mozStorageTransaction transaction(db, PR_TRUE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (id INTEGER PRIMARY KEY)" )); (void)transaction.Rollback(); } PRBool exists = PR_TRUE; (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); do_check_false(exists); }
void test_ASCIIString() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create table with a single string column. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (str STRING)" )); // Create statements to INSERT and SELECT the string. nsCOMPtr<mozIStorageStatement> insert, select; (void)db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (str) VALUES (?1)" ), getter_AddRefs(insert)); (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT str FROM test" ), getter_AddRefs(select)); // Roundtrip a string through the table, and ensure it comes out as expected. nsCAutoString inserted("I'm an ASCII string"); { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } nsCAutoString result; { mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); } do_check_true(result == inserted); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); }
void test_UTFStrings() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create table with a single string column. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (str STRING)" )); // Create statements to INSERT and SELECT the string. nsCOMPtr<mozIStorageStatement> insert, select; (void)db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (str) VALUES (?1)" ), getter_AddRefs(insert)); (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT str FROM test" ), getter_AddRefs(select)); // Roundtrip a UTF8 string through the table, using UTF8 input and output. static const char sCharArray[] = "I'm a \xc3\xbb\xc3\xbc\xc3\xa2\xc3\xa4\xc3\xa7 UTF8 string!"; nsCAutoString insertedUTF8(sCharArray, NS_ARRAY_LENGTH(sCharArray) - 1); do_check_true(insertedUTF8.Length() == NS_ARRAY_LENGTH(sCharArray) - 1); NS_ConvertUTF8toUTF16 insertedUTF16(insertedUTF8); do_check_true(insertedUTF8 == NS_ConvertUTF16toUTF8(insertedUTF16)); { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, insertedUTF8))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } { nsCAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); do_check_true(result == insertedUTF8); } // Use UTF8 input and UTF16 output. { nsAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetString(0, result))); do_check_true(result == insertedUTF16); } (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); // Roundtrip the same string using UTF16 input and UTF8 output. { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindStringByIndex(0, insertedUTF16))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } { nsCAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); do_check_true(result == insertedUTF8); } // Use UTF16 input and UTF16 output. { nsAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetString(0, result))); do_check_true(result == insertedUTF16); } (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); }