Home | History | Annotate | Download | only in test
      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 
     17 package androidx.room.integration.testapp.test;
     18 
     19 import static org.hamcrest.CoreMatchers.is;
     20 import static org.hamcrest.MatcherAssert.assertThat;
     21 
     22 import static java.util.Collections.singletonList;
     23 
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 
     28 import androidx.room.ColumnInfo;
     29 import androidx.room.Dao;
     30 import androidx.room.Database;
     31 import androidx.room.Embedded;
     32 import androidx.room.Entity;
     33 import androidx.room.ForeignKey;
     34 import androidx.room.Index;
     35 import androidx.room.Insert;
     36 import androidx.room.PrimaryKey;
     37 import androidx.room.Query;
     38 import androidx.room.Relation;
     39 import androidx.room.Room;
     40 import androidx.room.RoomDatabase;
     41 import androidx.room.Transaction;
     42 
     43 import org.junit.Before;
     44 import org.junit.Test;
     45 import org.junit.runner.RunWith;
     46 
     47 import java.util.List;
     48 
     49 @RunWith(AndroidJUnit4.class)
     50 @SmallTest
     51 public class RelationWithReservedKeywordTest {
     52     private MyDatabase mDb;
     53 
     54     @Before
     55     public void initDb() {
     56         mDb = Room.inMemoryDatabaseBuilder(
     57                 InstrumentationRegistry.getTargetContext(),
     58                 MyDatabase.class).build();
     59     }
     60 
     61     @Test
     62     public void loadRelation() {
     63         Category category = new Category(1, "cat1");
     64         mDb.getDao().insert(category);
     65         Topic topic = new Topic(2, 1, "foo");
     66         mDb.getDao().insert(topic);
     67         List<CategoryWithTopics> categoryWithTopics = mDb.getDao().loadAll();
     68         assertThat(categoryWithTopics.size(), is(1));
     69         assertThat(categoryWithTopics.get(0).category, is(category));
     70         assertThat(categoryWithTopics.get(0).topics, is(singletonList(topic)));
     71     }
     72 
     73     @Entity(tableName = "categories")
     74     static class Category {
     75 
     76         @PrimaryKey(autoGenerate = true)
     77         public final long id;
     78 
     79         public final String name;
     80 
     81         Category(long id, String name) {
     82             this.id = id;
     83             this.name = name;
     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             Category category = (Category) o;
     91             //noinspection SimplifiableIfStatement
     92             if (id != category.id) return false;
     93             return name != null ? name.equals(category.name) : category.name == null;
     94         }
     95 
     96         @Override
     97         public int hashCode() {
     98             int result = (int) (id ^ (id >>> 32));
     99             result = 31 * result + (name != null ? name.hashCode() : 0);
    100             return result;
    101         }
    102     }
    103 
    104     @Dao
    105     interface MyDao {
    106         @Transaction
    107         @Query("SELECT * FROM categories")
    108         List<CategoryWithTopics> loadAll();
    109 
    110         @Insert
    111         void insert(Category... categories);
    112 
    113         @Insert
    114         void insert(Topic... topics);
    115     }
    116 
    117     @Database(
    118             entities = {Category.class, Topic.class},
    119             version = 1,
    120             exportSchema = false)
    121     abstract static class MyDatabase extends RoomDatabase {
    122         abstract MyDao getDao();
    123     }
    124 
    125 
    126     @SuppressWarnings("WeakerAccess")
    127     static class CategoryWithTopics {
    128         @Embedded
    129         public Category category;
    130 
    131         @Relation(
    132                 parentColumn = "id",
    133                 entityColumn = "category_id",
    134                 entity = Topic.class)
    135         public List<Topic> topics;
    136     }
    137 
    138     @Entity(
    139             tableName = "topics",
    140             foreignKeys = @ForeignKey(
    141                     entity = Category.class,
    142                     parentColumns = "id",
    143                     childColumns = "category_id",
    144                     onDelete = ForeignKey.CASCADE),
    145             indices = @Index("category_id"))
    146     static class Topic {
    147 
    148         @PrimaryKey(autoGenerate = true)
    149         public final long id;
    150 
    151         @ColumnInfo(name = "category_id")
    152         public final long categoryId;
    153 
    154         public final String to;
    155 
    156         Topic(long id, long categoryId, String to) {
    157             this.id = id;
    158             this.categoryId = categoryId;
    159             this.to = to;
    160         }
    161 
    162         @Override
    163         public boolean equals(Object o) {
    164             if (this == o) return true;
    165             if (o == null || getClass() != o.getClass()) return false;
    166             Topic topic = (Topic) o;
    167             if (id != topic.id) return false;
    168             //noinspection SimplifiableIfStatement
    169             if (categoryId != topic.categoryId) return false;
    170             return to != null ? to.equals(topic.to) : topic.to == null;
    171         }
    172 
    173         @Override
    174         public int hashCode() {
    175             int result = (int) (id ^ (id >>> 32));
    176             result = 31 * result + (int) (categoryId ^ (categoryId >>> 32));
    177             result = 31 * result + (to != null ? to.hashCode() : 0);
    178             return result;
    179         }
    180     }
    181 }
    182