test_StatementCache.cpp (4607B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "storage_test_harness.h" 8 9 #include "mozilla/Attributes.h" 10 #include "mozilla/storage/StatementCache.h" 11 using namespace mozilla::storage; 12 13 /** 14 * This file test our statement cache in StatementCache.h. 15 */ 16 17 //////////////////////////////////////////////////////////////////////////////// 18 //// Helpers 19 20 class SyncCache : public StatementCache<mozIStorageStatement> { 21 public: 22 explicit SyncCache(nsCOMPtr<mozIStorageConnection>& aConnection) 23 : StatementCache<mozIStorageStatement>(aConnection) {} 24 }; 25 26 class AsyncCache : public StatementCache<mozIStorageAsyncStatement> { 27 public: 28 explicit AsyncCache(nsCOMPtr<mozIStorageConnection>& aConnection) 29 : StatementCache<mozIStorageAsyncStatement>(aConnection) {} 30 }; 31 32 /** 33 * Wraps nsCString so we can not implement the same functions twice for each 34 * type. 35 */ 36 class StringWrapper : public nsCString { 37 public: 38 MOZ_IMPLICIT StringWrapper(const char* aOther) { this->Assign(aOther); } 39 }; 40 41 //////////////////////////////////////////////////////////////////////////////// 42 //// Test Functions 43 44 // This is some gtest magic that allows us to parameterize tests by |const 45 // char[]| and |StringWrapper|. 46 template <typename T> 47 class storage_StatementCache : public ::testing::Test {}; 48 typedef ::testing::Types<const char[], StringWrapper> TwoStringTypes; 49 50 TYPED_TEST_SUITE(storage_StatementCache, TwoStringTypes); 51 TYPED_TEST(storage_StatementCache, GetCachedStatement) { 52 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); 53 SyncCache cache(db); 54 55 TypeParam sql = "SELECT * FROM sqlite_master"; 56 57 // Make sure we get a statement back with the right state. 58 nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql); 59 do_check_true(stmt); 60 int32_t state; 61 do_check_success(stmt->GetState(&state)); 62 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state); 63 64 // Check to make sure we get the same copy the second time we ask. 65 nsCOMPtr<mozIStorageStatement> stmt2 = cache.GetCachedStatement(sql); 66 do_check_true(stmt2); 67 do_check_eq(stmt.get(), stmt2.get()); 68 } 69 70 TYPED_TEST_SUITE(storage_StatementCache, TwoStringTypes); 71 TYPED_TEST(storage_StatementCache, FinalizeStatements) { 72 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); 73 SyncCache cache(db); 74 75 TypeParam sql = "SELECT * FROM sqlite_master"; 76 77 // Get a statement, and then tell the cache to finalize. 78 nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql); 79 do_check_true(stmt); 80 81 cache.FinalizeStatements(); 82 83 // We should be in an invalid state at this point. 84 int32_t state; 85 do_check_success(stmt->GetState(&state)); 86 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state); 87 88 // Should be able to close the database now too. 89 do_check_success(db->Close()); 90 } 91 92 TYPED_TEST_SUITE(storage_StatementCache, TwoStringTypes); 93 TYPED_TEST(storage_StatementCache, GetCachedAsyncStatement) { 94 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); 95 AsyncCache cache(db); 96 97 TypeParam sql = "SELECT * FROM sqlite_master"; 98 99 // Make sure we get a statement back with the right state. 100 nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql); 101 do_check_true(stmt); 102 int32_t state; 103 do_check_success(stmt->GetState(&state)); 104 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state); 105 106 // Check to make sure we get the same copy the second time we ask. 107 nsCOMPtr<mozIStorageAsyncStatement> stmt2 = cache.GetCachedStatement(sql); 108 do_check_true(stmt2); 109 do_check_eq(stmt.get(), stmt2.get()); 110 } 111 112 TYPED_TEST_SUITE(storage_StatementCache, TwoStringTypes); 113 TYPED_TEST(storage_StatementCache, FinalizeAsyncStatements) { 114 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); 115 AsyncCache cache(db); 116 117 TypeParam sql = "SELECT * FROM sqlite_master"; 118 119 // Get a statement, and then tell the cache to finalize. 120 nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql); 121 do_check_true(stmt); 122 123 cache.FinalizeStatements(); 124 125 // We should be in an invalid state at this point. 126 int32_t state; 127 do_check_success(stmt->GetState(&state)); 128 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state); 129 130 // Should be able to close the database now too. 131 do_check_success(db->AsyncClose(nullptr)); 132 }