1 /* 2 * Copyright (C) 2007 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 android.database; 18 19 import android.database.sqlite.SQLiteDatabase; 20 import android.database.Cursor; 21 import android.test.suitebuilder.annotation.MediumTest; 22 import android.test.suitebuilder.annotation.SmallTest; 23 import android.util.Log; 24 import android.test.MoreAsserts; 25 26 import java.util.ArrayList; 27 import java.util.Locale; 28 29 import junit.framework.TestCase; 30 31 public class DatabaseLocaleTest extends TestCase { 32 33 private SQLiteDatabase mDatabase; 34 35 private static final String[] STRINGS = { 36 "c\u00f4t\u00e9", 37 "cote", 38 "c\u00f4te", 39 "cot\u00e9", 40 "boy", 41 "dog", 42 "COTE", 43 }; 44 45 @Override 46 protected void setUp() throws Exception { 47 super.setUp(); 48 mDatabase = SQLiteDatabase.create(null); 49 mDatabase.execSQL( 50 "CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT COLLATE LOCALIZED);"); 51 } 52 53 private void insertStrings() { 54 for (String s : STRINGS) { 55 mDatabase.execSQL("INSERT INTO test (data) VALUES('" + s + "');"); 56 } 57 } 58 59 @Override 60 protected void tearDown() throws Exception { 61 mDatabase.close(); 62 super.tearDown(); 63 } 64 65 private String[] query(String sql) { 66 Log.i("LocaleTest", "Querying: " + sql); 67 Cursor c = mDatabase.rawQuery(sql, null); 68 assertNotNull(c); 69 ArrayList<String> items = new ArrayList<String>(); 70 while (c.moveToNext()) { 71 items.add(c.getString(0)); 72 Log.i("LocaleTest", "...." + c.getString(0)); 73 } 74 String[] result = items.toArray(new String[items.size()]); 75 assertEquals(STRINGS.length, result.length); 76 c.close(); 77 return result; 78 } 79 80 @MediumTest 81 public void testLocaleInsertOrder() throws Exception { 82 insertStrings(); 83 String[] results = query("SELECT data FROM test"); 84 MoreAsserts.assertEquals(STRINGS, results); 85 } 86 87 @MediumTest 88 public void testLocaleenUS() throws Exception { 89 insertStrings(); 90 Log.i("LocaleTest", "about to call setLocale en_US"); 91 mDatabase.setLocale(new Locale("en", "US")); 92 String[] results; 93 results = query("SELECT data FROM test ORDER BY data COLLATE LOCALIZED ASC"); 94 95 // The database code currently uses PRIMARY collation strength, 96 // meaning that all versions of a character compare equal (regardless 97 // of case or accents), leaving the "cote" flavors in database order. 98 MoreAsserts.assertEquals(results, new String[] { 99 STRINGS[4], // "boy" 100 STRINGS[0], // sundry forms of "cote" 101 STRINGS[1], 102 STRINGS[2], 103 STRINGS[3], 104 STRINGS[6], // "COTE" 105 STRINGS[5], // "dog" 106 }); 107 } 108 109 @SmallTest 110 public void testHoge() throws Exception { 111 Cursor cursor = null; 112 try { 113 String expectedString = new String(new int[] {0xFE000}, 0, 1); 114 mDatabase.execSQL("INSERT INTO test(id, data) VALUES(1, '" + expectedString + "')"); 115 cursor = mDatabase.rawQuery("SELECT data FROM test WHERE id = 1", null); 116 117 assertNotNull(cursor); 118 assertTrue(cursor.moveToFirst()); 119 String actualString = cursor.getString(0); 120 assertEquals(expectedString.length(), actualString.length()); 121 for (int i = 0; i < expectedString.length(); i++) { 122 assertEquals((int)expectedString.charAt(i), (int)actualString.charAt(i)); 123 } 124 assertEquals(expectedString, actualString); 125 } finally { 126 if (cursor != null) cursor.close(); 127 } 128 } 129 } 130