• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.adservices.data.measurement.migration;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.never;
21 import static org.mockito.Mockito.verify;
22 
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.database.Cursor;
26 import android.database.sqlite.SQLiteDatabase;
27 
28 import androidx.test.core.app.ApplicationProvider;
29 
30 import com.android.adservices.data.DbHelper;
31 import com.android.adservices.data.measurement.DbHelperV1;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 
37 import java.io.File;
38 
39 /** @deprecated Use {@link MeasurementDbMigratorTestBase} for migration to V7+ versions. */
40 @Deprecated
41 public abstract class MeasurementDbMigratorTestBaseDeprecated {
42     private static final Context sContext = ApplicationProvider.getApplicationContext();
43     private static final String DATABASE_NAME_FOR_MIGRATION = "adservices_migration.db";
44 
45     private static DbHelper sDbHelper;
46 
47     @Mock private SQLiteDatabase mDb;
48 
49     @Before
setup()50     public void setup() {
51         // To force create a fresh db, delete the existing DB file
52         File databaseFile = sContext.getDatabasePath(DATABASE_NAME_FOR_MIGRATION);
53         if (databaseFile.exists()) {
54             databaseFile.delete();
55         }
56 
57         sDbHelper = null;
58     }
59 
60     @Test
performMigration_alreadyOnHigherVersion_skipMigration()61     public void performMigration_alreadyOnHigherVersion_skipMigration() {
62         // Execution
63         getTestSubject().performMigration(mDb, (getTargetVersion() + 1), (getTargetVersion() + 2));
64 
65         // Verify
66         verify(mDb, never()).execSQL(any());
67     }
68 
69     @Test
performMigration_lowerRequestedVersion_skipMigration()70     public void performMigration_lowerRequestedVersion_skipMigration() {
71         // Execution
72         getTestSubject().performMigration(mDb, (getTargetVersion() - 2), (getTargetVersion() - 1));
73 
74         // Verify
75         verify(mDb, never()).execSQL(any());
76     }
77 
getDbHelper(int version)78     protected DbHelper getDbHelper(int version) {
79         synchronized (DbHelper.class) {
80             if (sDbHelper == null) {
81                 if (version == 1) {
82                     sDbHelper = new DbHelperV1(sContext, DATABASE_NAME_FOR_MIGRATION, version);
83                 }
84             }
85             return sDbHelper;
86         }
87     }
88 
getTargetVersion()89     abstract int getTargetVersion();
90 
getTestSubject()91     abstract AbstractMeasurementDbMigrator getTestSubject();
92 
93     // Create our own method instead of using DatabaseUtils.cursorRowToContentValues because that
94     // one reads every type from the cursor as a String.
cursorRowToContentValues(Cursor cursor)95     public static ContentValues cursorRowToContentValues(Cursor cursor) {
96         String[] columns = cursor.getColumnNames();
97         ContentValues values = new ContentValues();
98         for (int i = 0; i < columns.length; i++) {
99             switch (cursor.getType(i)) {
100                 case Cursor.FIELD_TYPE_FLOAT:
101                     values.put(columns[i], cursor.getDouble(i));
102                     break;
103                 case Cursor.FIELD_TYPE_INTEGER:
104                     values.put(columns[i], cursor.getLong(i));
105                     break;
106                 case Cursor.FIELD_TYPE_STRING:
107                 default:
108                     values.put(columns[i], cursor.getString(i));
109                     break;
110             }
111         }
112         return values;
113     }
114 }
115 
116 
117