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.MatcherAssert.assertThat; 20 21 import android.support.test.InstrumentationRegistry; 22 import android.support.test.filters.SdkSuppress; 23 import android.support.test.filters.SmallTest; 24 import android.support.test.runner.AndroidJUnit4; 25 26 import androidx.room.ColumnInfo; 27 import androidx.room.Dao; 28 import androidx.room.Database; 29 import androidx.room.Insert; 30 import androidx.room.PrimaryKey; 31 import androidx.room.Query; 32 import androidx.room.Room; 33 import androidx.room.RoomDatabase; 34 35 import org.hamcrest.CoreMatchers; 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 import java.util.Arrays; 42 import java.util.List; 43 import java.util.Locale; 44 45 @SmallTest 46 @RunWith(AndroidJUnit4.class) 47 public class CollationTest { 48 private CollateDb mDb; 49 private CollateDao mDao; 50 private Locale mDefaultLocale; 51 private final CollateEntity mItem1 = new CollateEntity(1, "ab"); 52 private final CollateEntity mItem2 = new CollateEntity(2, "abi"); 53 private final CollateEntity mItem3 = new CollateEntity(3, "abj"); 54 private final CollateEntity mItem4 = new CollateEntity(4, "ab"); 55 56 @Before 57 public void init() { 58 mDefaultLocale = Locale.getDefault(); 59 } 60 61 private void initDao(Locale systemLocale) { 62 Locale.setDefault(systemLocale); 63 mDb = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getTargetContext(), 64 CollateDb.class).build(); 65 mDao = mDb.dao(); 66 mDao.insert(mItem1); 67 mDao.insert(mItem2); 68 mDao.insert(mItem3); 69 mDao.insert(mItem4); 70 } 71 72 @After 73 public void closeDb() { 74 mDb.close(); 75 Locale.setDefault(mDefaultLocale); 76 } 77 78 @Test 79 public void localized() { 80 initDao(new Locale("tr", "TR")); 81 List<CollateEntity> result = mDao.sortedByLocalized(); 82 assertThat(result, CoreMatchers.is(Arrays.asList( 83 mItem4, mItem1, mItem2, mItem3 84 ))); 85 } 86 87 @Test 88 @SdkSuppress(minSdkVersion = 21) 89 public void localized_asUnicode() { 90 initDao(Locale.getDefault()); 91 List<CollateEntity> result = mDao.sortedByLocalizedAsUnicode(); 92 assertThat(result, CoreMatchers.is(Arrays.asList( 93 mItem4, mItem2, mItem1, mItem3 94 ))); 95 } 96 97 @Test 98 public void unicode_asLocalized() { 99 initDao(new Locale("tr", "TR")); 100 List<CollateEntity> result = mDao.sortedByUnicodeAsLocalized(); 101 assertThat(result, CoreMatchers.is(Arrays.asList( 102 mItem4, mItem1, mItem2, mItem3 103 ))); 104 } 105 106 @Test 107 @SdkSuppress(minSdkVersion = 21) 108 public void unicode() { 109 initDao(Locale.getDefault()); 110 List<CollateEntity> result = mDao.sortedByUnicode(); 111 assertThat(result, CoreMatchers.is(Arrays.asList( 112 mItem4, mItem2, mItem1, mItem3 113 ))); 114 } 115 116 @SuppressWarnings("WeakerAccess") 117 @androidx.room.Entity 118 static class CollateEntity { 119 @PrimaryKey 120 public final int id; 121 @ColumnInfo(collate = ColumnInfo.LOCALIZED) 122 public final String localizedName; 123 @ColumnInfo(collate = ColumnInfo.UNICODE) 124 public final String unicodeName; 125 126 CollateEntity(int id, String name) { 127 this.id = id; 128 this.localizedName = name; 129 this.unicodeName = name; 130 } 131 132 CollateEntity(int id, String localizedName, String unicodeName) { 133 this.id = id; 134 this.localizedName = localizedName; 135 this.unicodeName = unicodeName; 136 } 137 138 @SuppressWarnings("SimplifiableIfStatement") 139 @Override 140 public boolean equals(Object o) { 141 if (this == o) return true; 142 if (o == null || getClass() != o.getClass()) return false; 143 144 CollateEntity that = (CollateEntity) o; 145 146 if (id != that.id) return false; 147 if (!localizedName.equals(that.localizedName)) return false; 148 return unicodeName.equals(that.unicodeName); 149 } 150 151 @Override 152 public int hashCode() { 153 int result = id; 154 result = 31 * result + localizedName.hashCode(); 155 result = 31 * result + unicodeName.hashCode(); 156 return result; 157 } 158 159 @Override 160 public String toString() { 161 return "CollateEntity{" 162 + "id=" + id 163 + ", localizedName='" + localizedName + '\'' 164 + ", unicodeName='" + unicodeName + '\'' 165 + '}'; 166 } 167 } 168 169 @Dao 170 interface CollateDao { 171 @Query("SELECT * FROM CollateEntity ORDER BY localizedName ASC") 172 List<CollateEntity> sortedByLocalized(); 173 174 @Query("SELECT * FROM CollateEntity ORDER BY localizedName COLLATE UNICODE ASC") 175 List<CollateEntity> sortedByLocalizedAsUnicode(); 176 177 @Query("SELECT * FROM CollateEntity ORDER BY unicodeName ASC") 178 List<CollateEntity> sortedByUnicode(); 179 180 @Query("SELECT * FROM CollateEntity ORDER BY unicodeName COLLATE LOCALIZED ASC") 181 List<CollateEntity> sortedByUnicodeAsLocalized(); 182 183 @Insert 184 void insert(CollateEntity... entities); 185 } 186 187 @Database(entities = CollateEntity.class, version = 1, exportSchema = false) 188 abstract static class CollateDb extends RoomDatabase { 189 abstract CollateDao dao(); 190 } 191 } 192