• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.unit_tests;
18 
19 import android.content.Context;
20 import android.database.sqlite.*;
21 import android.util.Log;
22 
23 import android.test.AndroidTestCase;
24 import android.test.suitebuilder.annotation.Suppress;
25 
26 import java.io.File;
27 
28 // This test suite is too desctructive and takes too long to be included in the
29 // automated suite.
30 @Suppress
31 public class DatabaseStressTest extends AndroidTestCase {
32     private static final String TAG = "DatabaseStressTest";
33     private static final int CURRENT_DATABASE_VERSION = 1;
34     private SQLiteDatabase mDatabase;
35     private File mDatabaseFile;
36 
37     @Override
setUp()38     protected void setUp() throws Exception {
39         super.setUp();
40         Context c = getContext();
41 
42         mDatabaseFile = c.getDatabasePath("database_test.db");
43         if (mDatabaseFile.exists()) {
44             mDatabaseFile.delete();
45         }
46 
47         mDatabase = c.openOrCreateDatabase("database_test.db", 0, null);
48 
49         assertNotNull(mDatabase);
50         mDatabase.setVersion(CURRENT_DATABASE_VERSION);
51 
52         mDatabase.execSQL("CREATE TABLE IF NOT EXISTS test (_id INTEGER PRIMARY KEY, data TEXT);");
53 
54     }
55 
56     @Override
tearDown()57     protected void tearDown() throws Exception {
58         mDatabase.close();
59         mDatabaseFile.delete();
60         super.tearDown();
61     }
62 
testSingleThreadInsertDelete()63     public void testSingleThreadInsertDelete() {
64         int i = 0;
65         char[] ch = new char[100000];
66         String str = new String(ch);
67         String[] strArr = new String[1];
68         strArr[0] = str;
69         for (; i < 10000; ++i) {
70             try {
71                 mDatabase.execSQL("INSERT INTO test (data) VALUES (?)", strArr);
72                 mDatabase.execSQL("delete from test;");
73             } catch (Exception e) {
74                 Log.e(TAG, "exception " + e.getMessage());
75             }
76         }
77     }
78 
79     /**
80      * use fillup -p 90 before run the test
81      * and when disk run out
82      * start delete some fillup files
83      * and see if db recover
84      */
testOutOfSpace()85     public void testOutOfSpace() {
86         int i = 0;
87         char[] ch = new char[100000];
88         String str = new String(ch);
89         String[] strArr = new String[1];
90         strArr[0] = str;
91         for (; i < 10000; ++i) {
92             try {
93                 mDatabase.execSQL("INSERT INTO test (data) VALUES (?)", strArr);
94             } catch (Exception e) {
95                 Log.e(TAG, "exception " + e.getMessage());
96             }
97         }
98     }
99 }
100