1 /*
2  * Copyright 2023 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 package androidx.room
17 
18 import androidx.kruth.assertThat
19 import androidx.room.Room.databaseBuilder
20 import androidx.sqlite.driver.NativeSQLiteDriver
21 import kotlin.test.Test
22 import kotlin.test.assertFailsWith
23 
24 class BuilderTest {
25     @Test
databaseBuilderWithFactorynull26     fun databaseBuilderWithFactory() {
27         val db =
28             databaseBuilder(
29                     name = "TestDatabase",
30                     factory = { TestDatabase::class.instantiateImpl() }
31                 )
32                 .setDriver(NativeSQLiteDriver())
33                 .build()
34 
35         // Assert that the db is built successfully.
36         assertThat(db).isInstanceOf<TestDatabase>()
37     }
38 
39     @Test
missingDrivernull40     fun missingDriver() {
41         assertThat(
42                 assertFailsWith<IllegalArgumentException> {
43                         databaseBuilder(
44                                 name = "TestDatabase",
45                                 factory = { TestDatabase::class.instantiateImpl() }
46                             )
47                             .build()
48                     }
49                     .message
50             )
51             .isEqualTo(
52                 "Cannot create a RoomDatabase without providing a SQLiteDriver via setDriver()."
53             )
54     }
55 
56     internal abstract class TestDatabase : RoomDatabase()
57 }
58