DaoNameConflictTest.java revision bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8
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.room.Dao; 27import androidx.room.Database; 28import androidx.room.Entity; 29import androidx.room.Insert; 30import androidx.room.PrimaryKey; 31import androidx.room.Query; 32import androidx.room.Room; 33import androidx.room.RoomDatabase; 34 35import org.junit.After; 36import org.junit.Before; 37import org.junit.Test; 38import org.junit.runner.RunWith; 39 40@RunWith(AndroidJUnit4.class) 41@SmallTest 42public class DaoNameConflictTest { 43 private ConflictDatabase mDb; 44 @Before 45 public void init() { 46 mDb = Room.inMemoryDatabaseBuilder( 47 InstrumentationRegistry.getTargetContext(), 48 ConflictDatabase.class 49 ).build(); 50 } 51 52 @After 53 public void close() { 54 mDb.close(); 55 } 56 57 @Test 58 public void readFromItem1() { 59 Item1 item1 = new Item1(1, "a"); 60 mDb.item1Dao().insert(item1); 61 Item2 item2 = new Item2(2, "b"); 62 mDb.item2Dao().insert(item2); 63 assertThat(mDb.item1Dao().get(), is(item1)); 64 assertThat(mDb.item2Dao().get(), is(item2)); 65 } 66 67 @Entity 68 static class Item1 { 69 @PrimaryKey 70 public int id; 71 public String name; 72 73 Item1(int id, String name) { 74 this.id = id; 75 this.name = name; 76 } 77 78 @Dao 79 public interface Store { 80 @Query("SELECT * FROM Item1 LIMIT 1") 81 Item1 get(); 82 @Insert 83 void insert(Item1... items); 84 } 85 86 @Override 87 public boolean equals(Object o) { 88 if (this == o) return true; 89 if (o == null || getClass() != o.getClass()) return false; 90 Item1 item1 = (Item1) o; 91 92 //noinspection SimplifiableIfStatement 93 if (id != item1.id) return false; 94 return name != null ? name.equals(item1.name) : item1.name == null; 95 } 96 97 @Override 98 public int hashCode() { 99 int result = id; 100 result = 31 * result + (name != null ? name.hashCode() : 0); 101 return result; 102 } 103 } 104 105 @Entity 106 static class Item2 { 107 @PrimaryKey 108 public int id; 109 public String name; 110 111 Item2(int id, String name) { 112 this.id = id; 113 this.name = name; 114 } 115 116 @Dao 117 public interface Store { 118 @Query("SELECT * FROM Item2 LIMIT 1") 119 Item2 get(); 120 @Insert 121 void insert(Item2... items); 122 } 123 124 @Override 125 public boolean equals(Object o) { 126 if (this == o) return true; 127 if (o == null || getClass() != o.getClass()) return false; 128 Item2 item2 = (Item2) o; 129 130 //noinspection SimplifiableIfStatement 131 if (id != item2.id) return false; 132 return name != null ? name.equals(item2.name) : item2.name == null; 133 } 134 135 @Override 136 public int hashCode() { 137 int result = id; 138 result = 31 * result + (name != null ? name.hashCode() : 0); 139 return result; 140 } 141 } 142 143 @Database(version = 1, exportSchema = false, entities = {Item1.class, Item2.class}) 144 public abstract static class ConflictDatabase extends RoomDatabase { 145 public abstract Item1.Store item1Dao(); 146 public abstract Item2.Store item2Dao(); 147 } 148} 149