1/* 2 * Copyright 2018 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 androidx.room.integration.testapp.test; 18 19import static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.MatcherAssert.assertThat; 21 22import android.support.test.InstrumentationRegistry; 23import android.support.test.filters.SmallTest; 24import android.support.test.runner.AndroidJUnit4; 25 26import androidx.annotation.NonNull; 27import androidx.room.Dao; 28import androidx.room.Database; 29import androidx.room.Entity; 30import androidx.room.Insert; 31import androidx.room.PrimaryKey; 32import androidx.room.Query; 33import androidx.room.Room; 34import androidx.room.RoomDatabase; 35 36import org.junit.After; 37import org.junit.Before; 38import org.junit.Test; 39import org.junit.runner.RunWith; 40 41@RunWith(AndroidJUnit4.class) 42@SmallTest 43public class GenericEntityTest { 44 private GenericDb mDb; 45 private GenericDao mDao; 46 47 @Before 48 public void init() { 49 mDb = Room.inMemoryDatabaseBuilder( 50 InstrumentationRegistry.getTargetContext(), 51 GenericDb.class 52 ).build(); 53 mDao = mDb.getDao(); 54 } 55 56 @After 57 public void close() { 58 mDb.close(); 59 } 60 61 @Test 62 public void readWriteEntity() { 63 EntityItem item = new EntityItem("abc", "def"); 64 mDao.insert(item); 65 EntityItem received = mDao.get("abc"); 66 assertThat(received, is(item)); 67 } 68 69 @Test 70 public void readPojo() { 71 EntityItem item = new EntityItem("abc", "def"); 72 mDao.insert(item); 73 PojoItem received = mDao.getPojo("abc"); 74 assertThat(received.id, is("abc")); 75 } 76 77 static class Item<P, F> { 78 @NonNull 79 @PrimaryKey 80 public final P id; 81 private F mField; 82 83 Item(@NonNull P id) { 84 this.id = id; 85 } 86 87 public F getField() { 88 return mField; 89 } 90 91 public void setField(F field) { 92 mField = field; 93 } 94 95 @Override 96 public boolean equals(Object o) { 97 if (this == o) return true; 98 if (o == null || getClass() != o.getClass()) return false; 99 Item<?, ?> item = (Item<?, ?>) o; 100 //noinspection SimplifiableIfStatement 101 if (!id.equals(item.id)) return false; 102 return mField != null ? mField.equals(item.mField) : item.mField == null; 103 } 104 105 @Override 106 public int hashCode() { 107 int result = id.hashCode(); 108 result = 31 * result + (mField != null ? mField.hashCode() : 0); 109 return result; 110 } 111 } 112 113 static class PojoItem extends Item<String, Integer> { 114 PojoItem(String id) { 115 super(id); 116 } 117 } 118 119 @Entity 120 static class EntityItem extends Item<String, Integer> { 121 public final String name; 122 123 EntityItem(String id, String name) { 124 super(id); 125 this.name = name; 126 } 127 128 @Override 129 public boolean equals(Object o) { 130 if (this == o) return true; 131 if (o == null || getClass() != o.getClass()) return false; 132 if (!super.equals(o)) return false; 133 EntityItem that = (EntityItem) o; 134 return name != null ? name.equals(that.name) : that.name == null; 135 } 136 137 @Override 138 public int hashCode() { 139 int result = super.hashCode(); 140 result = 31 * result + (name != null ? name.hashCode() : 0); 141 return result; 142 } 143 } 144 145 @Dao 146 public interface GenericDao { 147 @Insert 148 void insert(EntityItem... items); 149 150 @Query("SELECT * FROM EntityItem WHERE id = :id") 151 EntityItem get(String id); 152 153 @Query("SELECT * FROM EntityItem WHERE id = :id") 154 PojoItem getPojo(String id); 155 } 156 157 @Database(version = 1, entities = {EntityItem.class}, exportSchema = false) 158 public abstract static class GenericDb extends RoomDatabase { 159 abstract GenericDao getDao(); 160 } 161} 162