Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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.sqlite.cts;
     18 
     19 import android.content.ContentValues;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.database.Cursor;
     23 import android.database.sqlite.SQLiteDatabase;
     24 import android.test.AndroidTestCase;
     25 
     26 import java.io.File;
     27 
     28 /**
     29  * Tests to verify FTS3/4 SQLite support.
     30  */
     31 public class SQLiteFtsTest extends AndroidTestCase {
     32 
     33     private static final String TEST_TABLE = "cts_fts";
     34 
     35     private static final String[] TEST_CONTENT = {
     36             "Any sufficiently advanced TECHnology is indistinguishable from magic.",
     37             "Those who would give up Essential Liberty to purchase a little Temporary Safety, deserve neither Liberty nor Safety.",
     38             "It is poor civic hygiene to install technologies that could someday facilitate a police state.",
     39     };
     40 
     41     private SQLiteDatabase mDatabase;
     42 
     43     @Override
     44     public void setUp() throws Exception {
     45         super.setUp();
     46         mDatabase = getContext().openOrCreateDatabase("CTS_FTS", Context.MODE_PRIVATE, null);
     47     }
     48 
     49     @Override
     50     public void tearDown() throws Exception {
     51         try {
     52             final String path = mDatabase.getPath();
     53             mDatabase.close();
     54             SQLiteDatabase.deleteDatabase(new File(path));
     55         } finally {
     56             super.tearDown();
     57         }
     58     }
     59 
     60     public void testFts3Porter() throws Exception {
     61         prepareFtsTable(TEST_TABLE, "fts3", "tokenize=porter");
     62 
     63         // Porter should include stemmed words
     64         final Cursor cursor = queryFtsTable(TEST_TABLE, "technology");
     65         try {
     66             assertEquals(2, cursor.getCount());
     67             cursor.moveToPosition(0);
     68             assertTrue(cursor.getString(0).contains(">TECHnology<"));
     69             cursor.moveToPosition(1);
     70             assertTrue(cursor.getString(0).contains(">technologies<"));
     71         } finally {
     72             cursor.close();
     73         }
     74     }
     75 
     76     public void testFts3Simple() throws Exception {
     77         prepareFtsTable(TEST_TABLE, "fts3", "tokenize=simple");
     78 
     79         // Simple shouldn't include stemmed words
     80         final Cursor cursor = queryFtsTable(TEST_TABLE, "technology");
     81         try {
     82             assertEquals(1, cursor.getCount());
     83             cursor.moveToPosition(0);
     84             assertTrue(cursor.getString(0).contains(">TECHnology<"));
     85         } finally {
     86             cursor.close();
     87         }
     88     }
     89 
     90     public void testFts4Simple() throws Exception {
     91         prepareFtsTable(TEST_TABLE, "fts4", "tokenize=simple");
     92 
     93         // Simple shouldn't include stemmed words
     94         final Cursor cursor = queryFtsTable(TEST_TABLE, "technology");
     95         try {
     96             assertEquals(1, cursor.getCount());
     97             cursor.moveToPosition(0);
     98             assertTrue(cursor.getString(0).contains(">TECHnology<"));
     99         } finally {
    100             cursor.close();
    101         }
    102     }
    103 
    104     private void prepareFtsTable(String table, String ftsType, String options)
    105             throws Exception {
    106         mDatabase.execSQL(
    107                 "CREATE VIRTUAL TABLE " + table + " USING " + ftsType
    108                 + "(content TEXT, " + options + ");");
    109 
    110         final Resources res = getContext().getResources();
    111         final ContentValues values = new ContentValues();
    112         for (String content : TEST_CONTENT) {
    113             values.clear();
    114             values.put("content", content);
    115             mDatabase.insert(table, null, values);
    116         }
    117     }
    118 
    119     private Cursor queryFtsTable(String table, String match) {
    120         return mDatabase.query(table, new String[] { "snippet(" + table + ")" },
    121                 "content MATCH ?", new String[] { match },
    122                 null, null, "rowid ASC");
    123     }
    124 }
    125