storage/test/test_StatementCache.cpp

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

mercurial