1 /* 2 * Copyright 2021 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 androidx.room.integration.testapp.test; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import androidx.room.Dao; 22 import androidx.room.Database; 23 import androidx.room.Entity; 24 import androidx.room.Insert; 25 import androidx.room.OnConflictStrategy; 26 import androidx.room.PrimaryKey; 27 import androidx.room.Query; 28 import androidx.room.Room; 29 import androidx.room.RoomDatabase; 30 import androidx.room.Transaction; 31 import androidx.room.Update; 32 import androidx.test.core.app.ApplicationProvider; 33 import androidx.test.ext.junit.runners.AndroidJUnit4; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 @RunWith(AndroidJUnit4.class) 40 public class DefaultDaoMethodsTest { 41 interface BaseDefaultDao<T> { 42 @Insert(onConflict = OnConflictStrategy.IGNORE) insert(T obj)43 long insert(T obj); 44 @Update update(T obj)45 void update(T obj); 46 47 @Transaction upsert(T obj)48 default void upsert(T obj) { 49 long id = insert(obj); 50 if (id == -1L) { 51 update(obj); 52 } 53 } 54 } 55 56 @Dao 57 abstract static class AbstractEntityDao implements BaseDefaultDao<DefaultEntity> { 58 @Query("SELECT COUNT(*) FROM DefaultEntity") count()59 abstract int count(); 60 @Query("SELECT * FROM DefaultEntity WHERE id = :id") load(long id)61 abstract DefaultEntity load(long id); 62 } 63 64 @Dao 65 interface InterfaceEntityDao extends BaseDefaultDao<DefaultEntity> { 66 @Query("SELECT COUNT(*) FROM DefaultEntity") count()67 int count(); 68 @Query("SELECT * FROM DefaultEntity WHERE id = :id") load(long id)69 DefaultEntity load(long id); 70 } 71 72 @Entity 73 static class DefaultEntity { 74 @PrimaryKey(autoGenerate = true) 75 public long id; 76 public String value; 77 DefaultEntity(long id, String value)78 DefaultEntity(long id, String value) { 79 this.id = id; 80 this.value = value; 81 } 82 83 @Override equals(Object o)84 public boolean equals(Object o) { 85 if (this == o) return true; 86 if (!(o instanceof DefaultEntity)) return false; 87 DefaultEntity that = (DefaultEntity) o; 88 return id == that.id && value.equals(that.value); 89 } 90 91 @Override hashCode()92 public int hashCode() { 93 return (int) id; 94 } 95 } 96 97 @Database( 98 version = 1, 99 exportSchema = false, 100 entities = {DefaultEntity.class} 101 ) 102 abstract static class DefaultsDb extends RoomDatabase { abstractDao()103 abstract AbstractEntityDao abstractDao(); interfaceDao()104 abstract InterfaceEntityDao interfaceDao(); 105 } 106 107 private DefaultsDb mDb; 108 @Before init()109 public void init() { 110 mDb = Room.inMemoryDatabaseBuilder( 111 ApplicationProvider.getApplicationContext(), 112 DefaultsDb.class 113 ).build(); 114 } 115 116 @Test abstractDao()117 public void abstractDao() { 118 DefaultEntity entity = new DefaultEntity(0, "v1"); 119 mDb.abstractDao().insert(entity); 120 entity = mDb.abstractDao().load(1); 121 assertThat(entity).isNotNull(); 122 entity.value = "v2"; 123 mDb.abstractDao().upsert(entity); 124 assertThat(mDb.abstractDao().count()).isEqualTo(1); 125 assertThat( 126 mDb.abstractDao().load(1) 127 ).isEqualTo( 128 new DefaultEntity(1, "v2") 129 ); 130 } 131 132 @Test interfaceDao()133 public void interfaceDao() { 134 DefaultEntity entity = new DefaultEntity(0, "v1"); 135 mDb.interfaceDao().insert(entity); 136 entity = mDb.interfaceDao().load(1); 137 assertThat(entity).isNotNull(); 138 entity.value = "v2"; 139 mDb.interfaceDao().upsert(entity); 140 assertThat(mDb.interfaceDao().count()).isEqualTo(1); 141 assertThat( 142 mDb.interfaceDao().load(1) 143 ).isEqualTo( 144 new DefaultEntity(1, "v2") 145 ); 146 } 147 } 148