storage/test/test_StatementCache.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "storage_test_harness.h"
michael@0 8
michael@0 9 #include "mozilla/storage/StatementCache.h"
michael@0 10 using namespace mozilla::storage;
michael@0 11
michael@0 12 /**
michael@0 13 * This file test our statement cache in StatementCache.h.
michael@0 14 */
michael@0 15
michael@0 16 ////////////////////////////////////////////////////////////////////////////////
michael@0 17 //// Helpers
michael@0 18
michael@0 19 class SyncCache : public StatementCache<mozIStorageStatement>
michael@0 20 {
michael@0 21 public:
michael@0 22 SyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
michael@0 23 : StatementCache<mozIStorageStatement>(aConnection)
michael@0 24 {
michael@0 25 }
michael@0 26 };
michael@0 27
michael@0 28 class AsyncCache : public StatementCache<mozIStorageAsyncStatement>
michael@0 29 {
michael@0 30 public:
michael@0 31 AsyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
michael@0 32 : StatementCache<mozIStorageAsyncStatement>(aConnection)
michael@0 33 {
michael@0 34 }
michael@0 35 };
michael@0 36
michael@0 37 /**
michael@0 38 * Wraps nsCString so we can not implement the same functions twice for each
michael@0 39 * type.
michael@0 40 */
michael@0 41 class StringWrapper : public nsCString
michael@0 42 {
michael@0 43 public:
michael@0 44 StringWrapper(const char* aOther)
michael@0 45 {
michael@0 46 this->Assign(aOther);
michael@0 47 }
michael@0 48 };
michael@0 49
michael@0 50 ////////////////////////////////////////////////////////////////////////////////
michael@0 51 //// Test Functions
michael@0 52
michael@0 53 template<typename StringType>
michael@0 54 void
michael@0 55 test_GetCachedStatement()
michael@0 56 {
michael@0 57 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 58 SyncCache cache(db);
michael@0 59
michael@0 60 StringType sql = "SELECT * FROM sqlite_master";
michael@0 61
michael@0 62 // Make sure we get a statement back with the right state.
michael@0 63 nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
michael@0 64 do_check_true(stmt);
michael@0 65 int32_t state;
michael@0 66 do_check_success(stmt->GetState(&state));
michael@0 67 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
michael@0 68
michael@0 69 // Check to make sure we get the same copy the second time we ask.
michael@0 70 nsCOMPtr<mozIStorageStatement> stmt2 = cache.GetCachedStatement(sql);
michael@0 71 do_check_true(stmt2);
michael@0 72 do_check_eq(stmt.get(), stmt2.get());
michael@0 73 }
michael@0 74
michael@0 75 template <typename StringType>
michael@0 76 void
michael@0 77 test_FinalizeStatements()
michael@0 78 {
michael@0 79 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 80 SyncCache cache(db);
michael@0 81
michael@0 82 StringType sql = "SELECT * FROM sqlite_master";
michael@0 83
michael@0 84 // Get a statement, and then tell the cache to finalize.
michael@0 85 nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
michael@0 86 do_check_true(stmt);
michael@0 87
michael@0 88 cache.FinalizeStatements();
michael@0 89
michael@0 90 // We should be in an invalid state at this point.
michael@0 91 int32_t state;
michael@0 92 do_check_success(stmt->GetState(&state));
michael@0 93 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
michael@0 94
michael@0 95 // Should be able to close the database now too.
michael@0 96 do_check_success(db->Close());
michael@0 97 }
michael@0 98
michael@0 99 template<typename StringType>
michael@0 100 void
michael@0 101 test_GetCachedAsyncStatement()
michael@0 102 {
michael@0 103 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 104 AsyncCache cache(db);
michael@0 105
michael@0 106 StringType sql = "SELECT * FROM sqlite_master";
michael@0 107
michael@0 108 // Make sure we get a statement back with the right state.
michael@0 109 nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
michael@0 110 do_check_true(stmt);
michael@0 111 int32_t state;
michael@0 112 do_check_success(stmt->GetState(&state));
michael@0 113 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
michael@0 114
michael@0 115 // Check to make sure we get the same copy the second time we ask.
michael@0 116 nsCOMPtr<mozIStorageAsyncStatement> stmt2 = cache.GetCachedStatement(sql);
michael@0 117 do_check_true(stmt2);
michael@0 118 do_check_eq(stmt.get(), stmt2.get());
michael@0 119 }
michael@0 120
michael@0 121 template <typename StringType>
michael@0 122 void
michael@0 123 test_FinalizeAsyncStatements()
michael@0 124 {
michael@0 125 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 126 AsyncCache cache(db);
michael@0 127
michael@0 128 StringType sql = "SELECT * FROM sqlite_master";
michael@0 129
michael@0 130 // Get a statement, and then tell the cache to finalize.
michael@0 131 nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
michael@0 132 do_check_true(stmt);
michael@0 133
michael@0 134 cache.FinalizeStatements();
michael@0 135
michael@0 136 // We should be in an invalid state at this point.
michael@0 137 int32_t state;
michael@0 138 do_check_success(stmt->GetState(&state));
michael@0 139 do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
michael@0 140
michael@0 141 // Should be able to close the database now too.
michael@0 142 do_check_success(db->AsyncClose(nullptr));
michael@0 143 }
michael@0 144
michael@0 145 ////////////////////////////////////////////////////////////////////////////////
michael@0 146 //// Test Harness Stuff
michael@0 147
michael@0 148 void (*gTests[])(void) = {
michael@0 149 test_GetCachedStatement<const char []>,
michael@0 150 test_GetCachedStatement<StringWrapper>,
michael@0 151 test_FinalizeStatements<const char []>,
michael@0 152 test_FinalizeStatements<StringWrapper>,
michael@0 153 test_GetCachedAsyncStatement<const char []>,
michael@0 154 test_GetCachedAsyncStatement<StringWrapper>,
michael@0 155 test_FinalizeAsyncStatements<const char []>,
michael@0 156 test_FinalizeAsyncStatements<StringWrapper>,
michael@0 157 };
michael@0 158
michael@0 159 const char *file = __FILE__;
michael@0 160 #define TEST_NAME "StatementCache"
michael@0 161 #define TEST_FILE file
michael@0 162 #include "storage_test_harness_tail.h"

mercurial