/src/mozilla-central/storage/test/gtest/test_StatementCache.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | { |
22 | | public: |
23 | | explicit SyncCache(nsCOMPtr<mozIStorageConnection>& aConnection) |
24 | | : StatementCache<mozIStorageStatement>(aConnection) |
25 | 0 | { |
26 | 0 | } |
27 | | }; |
28 | | |
29 | | class AsyncCache : public StatementCache<mozIStorageAsyncStatement> |
30 | | { |
31 | | public: |
32 | | explicit AsyncCache(nsCOMPtr<mozIStorageConnection>& aConnection) |
33 | | : StatementCache<mozIStorageAsyncStatement>(aConnection) |
34 | 0 | { |
35 | 0 | } |
36 | | }; |
37 | | |
38 | | /** |
39 | | * Wraps nsCString so we can not implement the same functions twice for each |
40 | | * type. |
41 | | */ |
42 | | class StringWrapper : public nsCString |
43 | | { |
44 | | public: |
45 | | MOZ_IMPLICIT StringWrapper(const char* aOther) |
46 | 0 | { |
47 | 0 | this->Assign(aOther); |
48 | 0 | } |
49 | | }; |
50 | | |
51 | | //////////////////////////////////////////////////////////////////////////////// |
52 | | //// Test Functions |
53 | | |
54 | | // This is some gtest magic that allows us to parameterize tests by |const |
55 | | // char[]| and |StringWrapper|. |
56 | | template <typename T> |
57 | | class storage_StatementCache : public ::testing::Test {}; |
58 | | typedef ::testing::Types<const char[], StringWrapper> TwoStringTypes; |
59 | | |
60 | | TYPED_TEST_CASE(storage_StatementCache, TwoStringTypes); |
61 | | TYPED_TEST(storage_StatementCache, GetCachedStatement) |
62 | 0 | { |
63 | 0 | nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); |
64 | 0 | SyncCache cache(db); |
65 | 0 |
|
66 | 0 | TypeParam sql = "SELECT * FROM sqlite_master"; |
67 | 0 |
|
68 | 0 | // Make sure we get a statement back with the right state. |
69 | 0 | nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql); |
70 | 0 | do_check_true(stmt); |
71 | 0 | int32_t state; |
72 | 0 | do_check_success(stmt->GetState(&state)); |
73 | 0 | do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state); |
74 | 0 |
|
75 | 0 | // Check to make sure we get the same copy the second time we ask. |
76 | 0 | nsCOMPtr<mozIStorageStatement> stmt2 = cache.GetCachedStatement(sql); |
77 | 0 | do_check_true(stmt2); |
78 | 0 | do_check_eq(stmt.get(), stmt2.get()); |
79 | 0 | } Unexecuted instantiation: storage_StatementCache_GetCachedStatement_Test<char const []>::TestBody() Unexecuted instantiation: storage_StatementCache_GetCachedStatement_Test<StringWrapper>::TestBody() |
80 | | |
81 | | TYPED_TEST_CASE(storage_StatementCache, TwoStringTypes); |
82 | | TYPED_TEST(storage_StatementCache, FinalizeStatements) |
83 | 0 | { |
84 | 0 | nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); |
85 | 0 | SyncCache cache(db); |
86 | 0 |
|
87 | 0 | TypeParam sql = "SELECT * FROM sqlite_master"; |
88 | 0 |
|
89 | 0 | // Get a statement, and then tell the cache to finalize. |
90 | 0 | nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql); |
91 | 0 | do_check_true(stmt); |
92 | 0 |
|
93 | 0 | cache.FinalizeStatements(); |
94 | 0 |
|
95 | 0 | // We should be in an invalid state at this point. |
96 | 0 | int32_t state; |
97 | 0 | do_check_success(stmt->GetState(&state)); |
98 | 0 | do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state); |
99 | 0 |
|
100 | 0 | // Should be able to close the database now too. |
101 | 0 | do_check_success(db->Close()); |
102 | 0 | } Unexecuted instantiation: storage_StatementCache_FinalizeStatements_Test<char const []>::TestBody() Unexecuted instantiation: storage_StatementCache_FinalizeStatements_Test<StringWrapper>::TestBody() |
103 | | |
104 | | TYPED_TEST_CASE(storage_StatementCache, TwoStringTypes); |
105 | | TYPED_TEST(storage_StatementCache, GetCachedAsyncStatement) |
106 | 0 | { |
107 | 0 | nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); |
108 | 0 | AsyncCache cache(db); |
109 | 0 |
|
110 | 0 | TypeParam sql = "SELECT * FROM sqlite_master"; |
111 | 0 |
|
112 | 0 | // Make sure we get a statement back with the right state. |
113 | 0 | nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql); |
114 | 0 | do_check_true(stmt); |
115 | 0 | int32_t state; |
116 | 0 | do_check_success(stmt->GetState(&state)); |
117 | 0 | do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state); |
118 | 0 |
|
119 | 0 | // Check to make sure we get the same copy the second time we ask. |
120 | 0 | nsCOMPtr<mozIStorageAsyncStatement> stmt2 = cache.GetCachedStatement(sql); |
121 | 0 | do_check_true(stmt2); |
122 | 0 | do_check_eq(stmt.get(), stmt2.get()); |
123 | 0 | } Unexecuted instantiation: storage_StatementCache_GetCachedAsyncStatement_Test<char const []>::TestBody() Unexecuted instantiation: storage_StatementCache_GetCachedAsyncStatement_Test<StringWrapper>::TestBody() |
124 | | |
125 | | TYPED_TEST_CASE(storage_StatementCache, TwoStringTypes); |
126 | | TYPED_TEST(storage_StatementCache, FinalizeAsyncStatements) |
127 | 0 | { |
128 | 0 | nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); |
129 | 0 | AsyncCache cache(db); |
130 | 0 |
|
131 | 0 | TypeParam sql = "SELECT * FROM sqlite_master"; |
132 | 0 |
|
133 | 0 | // Get a statement, and then tell the cache to finalize. |
134 | 0 | nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql); |
135 | 0 | do_check_true(stmt); |
136 | 0 |
|
137 | 0 | cache.FinalizeStatements(); |
138 | 0 |
|
139 | 0 | // We should be in an invalid state at this point. |
140 | 0 | int32_t state; |
141 | 0 | do_check_success(stmt->GetState(&state)); |
142 | 0 | do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state); |
143 | 0 |
|
144 | 0 | // Should be able to close the database now too. |
145 | 0 | do_check_success(db->AsyncClose(nullptr)); |
146 | 0 | } Unexecuted instantiation: storage_StatementCache_FinalizeAsyncStatements_Test<char const []>::TestBody() Unexecuted instantiation: storage_StatementCache_FinalizeAsyncStatements_Test<StringWrapper>::TestBody() |
147 | | |