storage/test/test_StatementCache.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/test_StatementCache.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,162 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "storage_test_harness.h"
    1.11 +
    1.12 +#include "mozilla/storage/StatementCache.h"
    1.13 +using namespace mozilla::storage;
    1.14 +
    1.15 +/**
    1.16 + * This file test our statement cache in StatementCache.h.
    1.17 + */
    1.18 +
    1.19 +////////////////////////////////////////////////////////////////////////////////
    1.20 +//// Helpers
    1.21 +
    1.22 +class SyncCache : public StatementCache<mozIStorageStatement>
    1.23 +{
    1.24 +public:
    1.25 +  SyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
    1.26 +  : StatementCache<mozIStorageStatement>(aConnection)
    1.27 +  {
    1.28 +  }
    1.29 +};
    1.30 +
    1.31 +class AsyncCache : public StatementCache<mozIStorageAsyncStatement>
    1.32 +{
    1.33 +public:
    1.34 +  AsyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
    1.35 +  : StatementCache<mozIStorageAsyncStatement>(aConnection)
    1.36 +  {
    1.37 +  }
    1.38 +};
    1.39 +
    1.40 +/**
    1.41 + * Wraps nsCString so we can not implement the same functions twice for each
    1.42 + * type.
    1.43 + */
    1.44 +class StringWrapper : public nsCString
    1.45 +{
    1.46 +public:
    1.47 +  StringWrapper(const char* aOther)
    1.48 +  {
    1.49 +    this->Assign(aOther);
    1.50 +  }
    1.51 +};
    1.52 +
    1.53 +////////////////////////////////////////////////////////////////////////////////
    1.54 +//// Test Functions
    1.55 +
    1.56 +template<typename StringType>
    1.57 +void
    1.58 +test_GetCachedStatement()
    1.59 +{
    1.60 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.61 +  SyncCache cache(db);
    1.62 +
    1.63 +  StringType sql = "SELECT * FROM sqlite_master";
    1.64 +
    1.65 +  // Make sure we get a statement back with the right state.
    1.66 +  nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
    1.67 +  do_check_true(stmt);
    1.68 +  int32_t state;
    1.69 +  do_check_success(stmt->GetState(&state));
    1.70 +  do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
    1.71 +
    1.72 +  // Check to make sure we get the same copy the second time we ask.
    1.73 +  nsCOMPtr<mozIStorageStatement> stmt2 = cache.GetCachedStatement(sql);
    1.74 +  do_check_true(stmt2);
    1.75 +  do_check_eq(stmt.get(), stmt2.get());
    1.76 +}
    1.77 +
    1.78 +template <typename StringType>
    1.79 +void
    1.80 +test_FinalizeStatements()
    1.81 +{
    1.82 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.83 +  SyncCache cache(db);
    1.84 +
    1.85 +  StringType sql = "SELECT * FROM sqlite_master";
    1.86 +
    1.87 +  // Get a statement, and then tell the cache to finalize.
    1.88 +  nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
    1.89 +  do_check_true(stmt);
    1.90 +
    1.91 +  cache.FinalizeStatements();
    1.92 +
    1.93 +  // We should be in an invalid state at this point.
    1.94 +  int32_t state;
    1.95 +  do_check_success(stmt->GetState(&state));
    1.96 +  do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
    1.97 +
    1.98 +  // Should be able to close the database now too.
    1.99 +  do_check_success(db->Close());
   1.100 +}
   1.101 +
   1.102 +template<typename StringType>
   1.103 +void
   1.104 +test_GetCachedAsyncStatement()
   1.105 +{
   1.106 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   1.107 +  AsyncCache cache(db);
   1.108 +
   1.109 +  StringType sql = "SELECT * FROM sqlite_master";
   1.110 +
   1.111 +  // Make sure we get a statement back with the right state.
   1.112 +  nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
   1.113 +  do_check_true(stmt);
   1.114 +  int32_t state;
   1.115 +  do_check_success(stmt->GetState(&state));
   1.116 +  do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
   1.117 +
   1.118 +  // Check to make sure we get the same copy the second time we ask.
   1.119 +  nsCOMPtr<mozIStorageAsyncStatement> stmt2 = cache.GetCachedStatement(sql);
   1.120 +  do_check_true(stmt2);
   1.121 +  do_check_eq(stmt.get(), stmt2.get());
   1.122 +}
   1.123 +
   1.124 +template <typename StringType>
   1.125 +void
   1.126 +test_FinalizeAsyncStatements()
   1.127 +{
   1.128 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   1.129 +  AsyncCache cache(db);
   1.130 +
   1.131 +  StringType sql = "SELECT * FROM sqlite_master";
   1.132 +
   1.133 +  // Get a statement, and then tell the cache to finalize.
   1.134 +  nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
   1.135 +  do_check_true(stmt);
   1.136 +
   1.137 +  cache.FinalizeStatements();
   1.138 +
   1.139 +  // We should be in an invalid state at this point.
   1.140 +  int32_t state;
   1.141 +  do_check_success(stmt->GetState(&state));
   1.142 +  do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
   1.143 +
   1.144 +  // Should be able to close the database now too.
   1.145 +  do_check_success(db->AsyncClose(nullptr));
   1.146 +}
   1.147 +
   1.148 +////////////////////////////////////////////////////////////////////////////////
   1.149 +//// Test Harness Stuff
   1.150 +
   1.151 +void (*gTests[])(void) = {
   1.152 +  test_GetCachedStatement<const char []>,
   1.153 +  test_GetCachedStatement<StringWrapper>,
   1.154 +  test_FinalizeStatements<const char []>,
   1.155 +  test_FinalizeStatements<StringWrapper>,
   1.156 +  test_GetCachedAsyncStatement<const char []>,
   1.157 +  test_GetCachedAsyncStatement<StringWrapper>,
   1.158 +  test_FinalizeAsyncStatements<const char []>,
   1.159 +  test_FinalizeAsyncStatements<StringWrapper>,
   1.160 +};
   1.161 +
   1.162 +const char *file = __FILE__;
   1.163 +#define TEST_NAME "StatementCache"
   1.164 +#define TEST_FILE file
   1.165 +#include "storage_test_harness_tail.h"

mercurial