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
17package android.test;
18
19import com.google.android.collect.Sets;
20
21import android.database.sqlite.SQLiteDatabase;
22import android.database.Cursor;
23
24import java.util.Set;
25
26/**
27 * A collection of utilities for writing unit tests for database code.
28 * @hide pending API council approval
29 */
30@Deprecated
31public class DatabaseTestUtils {
32
33    /**
34     * Compares the schema of two databases and asserts that they are equal.
35     * @param expectedDb the db that is known to have the correct schema
36     * @param db the db whose schema should be checked
37     */
38    public static void assertSchemaEquals(SQLiteDatabase expectedDb, SQLiteDatabase db) {
39        Set<String> expectedSchema = getSchemaSet(expectedDb);
40        Set<String> schema = getSchemaSet(db);
41        MoreAsserts.assertEquals(expectedSchema, schema);
42    }
43
44    private static Set<String> getSchemaSet(SQLiteDatabase db) {
45        Set<String> schemaSet = Sets.newHashSet();
46
47        Cursor entityCursor = db.rawQuery("SELECT sql FROM sqlite_master", null);
48        try {
49            while (entityCursor.moveToNext()) {
50                String sql = entityCursor.getString(0);
51                schemaSet.add(sql);
52            }
53        } finally {
54            entityCursor.close();
55        }
56        return schemaSet;
57    }
58}
59