Backed out changeset 4b2c67fe7e6b (relanding bug 449168)
[wine-gecko.git] / storage / test / unit / head_storage.js
blobc9823a20345d9f226a3a6c3d585c742f38e9b9d0
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Storage Test Code.
16 * The Initial Developer of the Original Code is
17 * Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 const Ci = Components.interfaces;
39 const Cc = Components.classes;
40 const Cr = Components.results;
42 var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
43 getService(Ci.nsIProperties);
45 function getTestDB()
47 var db = dirSvc.get("CurProcD", Ci.nsIFile);
48 db.append("test_storage.sqlite");
49 return db;
52 /**
53 * Obtains a corrupt database to test against.
55 function getCorruptDB()
57 return do_get_file("storage/test/unit/corruptDB.sqlite");
60 function cleanup()
62 // close the connection
63 print("*** Storage Tests: Trying to close!");
64 getOpenedDatabase().close();
66 // we need to null out the database variable to get a new connection the next
67 // time getOpenedDatabase is called
68 gDBConn = null;
70 // removing test db
71 print("*** Storage Tests: Trying to remove file!");
72 var dbFile = getTestDB();
73 if (dbFile.exists())
74 try { dbFile.remove(false); } catch(e) { /* stupid windows box */ }
77 function getService()
79 return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService);
82 var gDBConn = null;
84 /**
85 * Get a connection to the test database. Creates and caches the connection
86 * if necessary, otherwise reuses the existing cached connection.
88 * @param unshared {boolean}
89 * whether or not to open a connection to the database that doesn't share
90 * its cache; if true, we use mozIStorageService::openUnsharedDatabase
91 * to create the connection; otherwise we use openDatabase.
92 * @returns the mozIStorageConnection for the file.
94 function getOpenedDatabase(unshared)
96 if (!gDBConn) {
97 gDBConn = getService()
98 [unshared ? "openUnsharedDatabase" : "openDatabase"]
99 (getTestDB());
101 return gDBConn;
105 * Obtains a specific database to use.
107 * @param aFile
108 * The nsIFile representing the db file to open.
109 * @returns the mozIStorageConnection for the file.
111 function getDatabase(aFile)
113 return getService().openDatabase(aFile);
116 function createStatement(aSQL)
118 return getOpenedDatabase().createStatement(aSQL);
121 cleanup();