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 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is storage test code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * the Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2010
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "storage_test_harness.h"
41 :
42 : #include "mozilla/storage/StatementCache.h"
43 : using namespace mozilla::storage;
44 :
45 : /**
46 : * This file test our statement cache in StatementCache.h.
47 : */
48 :
49 : ////////////////////////////////////////////////////////////////////////////////
50 : //// Helpers
51 :
52 : class SyncCache : public StatementCache<mozIStorageStatement>
53 4 : {
54 : public:
55 4 : SyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
56 4 : : StatementCache<mozIStorageStatement>(aConnection)
57 : {
58 4 : }
59 : };
60 :
61 : class AsyncCache : public StatementCache<mozIStorageAsyncStatement>
62 4 : {
63 : public:
64 4 : AsyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
65 4 : : StatementCache<mozIStorageAsyncStatement>(aConnection)
66 : {
67 4 : }
68 : };
69 :
70 : /**
71 : * Wraps nsCString so we can not implement the same functions twice for each
72 : * type.
73 : */
74 : class StringWrapper : public nsCString
75 4 : {
76 : public:
77 4 : StringWrapper(const char* aOther)
78 4 : {
79 4 : this->Assign(aOther);
80 4 : }
81 : };
82 :
83 : ////////////////////////////////////////////////////////////////////////////////
84 : //// Test Functions
85 :
86 : template<typename StringType>
87 : void
88 2 : test_GetCachedStatement()
89 : {
90 4 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
91 4 : SyncCache cache(db);
92 :
93 3 : StringType sql = "SELECT * FROM sqlite_master";
94 :
95 : // Make sure we get a statement back with the right state.
96 4 : nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
97 2 : do_check_true(stmt);
98 : PRInt32 state;
99 2 : do_check_success(stmt->GetState(&state));
100 2 : do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
101 :
102 : // Check to make sure we get the same copy the second time we ask.
103 4 : nsCOMPtr<mozIStorageStatement> stmt2 = cache.GetCachedStatement(sql);
104 2 : do_check_true(stmt2);
105 2 : do_check_eq(stmt.get(), stmt2.get());
106 2 : }
107 :
108 : template <typename StringType>
109 : void
110 2 : test_FinalizeStatements()
111 : {
112 4 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
113 4 : SyncCache cache(db);
114 :
115 3 : StringType sql = "SELECT * FROM sqlite_master";
116 :
117 : // Get a statement, and then tell the cache to finalize.
118 4 : nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
119 2 : do_check_true(stmt);
120 :
121 2 : cache.FinalizeStatements();
122 :
123 : // We should be in an invalid state at this point.
124 : PRInt32 state;
125 2 : do_check_success(stmt->GetState(&state));
126 2 : do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
127 :
128 : // Should be able to close the database now too.
129 2 : do_check_success(db->Close());
130 2 : }
131 :
132 : template<typename StringType>
133 : void
134 2 : test_GetCachedAsyncStatement()
135 : {
136 4 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
137 4 : AsyncCache cache(db);
138 :
139 3 : StringType sql = "SELECT * FROM sqlite_master";
140 :
141 : // Make sure we get a statement back with the right state.
142 4 : nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
143 2 : do_check_true(stmt);
144 : PRInt32 state;
145 2 : do_check_success(stmt->GetState(&state));
146 2 : do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
147 :
148 : // Check to make sure we get the same copy the second time we ask.
149 4 : nsCOMPtr<mozIStorageAsyncStatement> stmt2 = cache.GetCachedStatement(sql);
150 2 : do_check_true(stmt2);
151 2 : do_check_eq(stmt.get(), stmt2.get());
152 2 : }
153 :
154 : template <typename StringType>
155 : void
156 2 : test_FinalizeAsyncStatements()
157 : {
158 4 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
159 4 : AsyncCache cache(db);
160 :
161 3 : StringType sql = "SELECT * FROM sqlite_master";
162 :
163 : // Get a statement, and then tell the cache to finalize.
164 4 : nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
165 2 : do_check_true(stmt);
166 :
167 2 : cache.FinalizeStatements();
168 :
169 : // We should be in an invalid state at this point.
170 : PRInt32 state;
171 2 : do_check_success(stmt->GetState(&state));
172 2 : do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
173 :
174 : // Should be able to close the database now too.
175 2 : do_check_success(db->AsyncClose(nsnull));
176 2 : }
177 :
178 : ////////////////////////////////////////////////////////////////////////////////
179 : //// Test Harness Stuff
180 :
181 : void (*gTests[])(void) = {
182 : test_GetCachedStatement<const char []>,
183 : test_GetCachedStatement<StringWrapper>,
184 : test_FinalizeStatements<const char []>,
185 : test_FinalizeStatements<StringWrapper>,
186 : test_GetCachedAsyncStatement<const char []>,
187 : test_GetCachedAsyncStatement<StringWrapper>,
188 : test_FinalizeAsyncStatements<const char []>,
189 : test_FinalizeAsyncStatements<StringWrapper>,
190 : };
191 :
192 : const char *file = __FILE__;
193 : #define TEST_NAME "StatementCache"
194 : #define TEST_FILE file
195 : #include "storage_test_harness_tail.h"
|