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