|
| 1 | + |
| 2 | +package com.activeandroid.test.parser; |
| 3 | + |
| 4 | +import android.database.SQLException; |
| 5 | +import android.database.sqlite.SQLiteDatabase; |
| 6 | + |
| 7 | +import com.activeandroid.Configuration; |
| 8 | +import com.activeandroid.DatabaseHelper; |
| 9 | +import com.activeandroid.test.ActiveAndroidTestCase; |
| 10 | + |
| 11 | + |
| 12 | +public class ParserConfigurationTest extends ActiveAndroidTestCase { |
| 13 | + |
| 14 | + /** |
| 15 | + * Should try to use the legacy parser by default, which is be unable to handle the SQL script. |
| 16 | + */ |
| 17 | + public void testLegacyMigration() { |
| 18 | + |
| 19 | + try { |
| 20 | + Configuration configuration = new Configuration.Builder(getContext()) |
| 21 | + .setDatabaseName("migration.db") |
| 22 | + .setDatabaseVersion(2) |
| 23 | + .create(); |
| 24 | + |
| 25 | + DatabaseHelper helper = new DatabaseHelper(configuration); |
| 26 | + SQLiteDatabase db = helper.getWritableDatabase(); |
| 27 | + helper.onUpgrade(db, 1, 2); |
| 28 | + |
| 29 | + fail("Should not be able to parse the SQL script."); |
| 30 | + |
| 31 | + } catch (SQLException e) { |
| 32 | + final String message = e.getMessage(); |
| 33 | + |
| 34 | + assertNotNull(message); |
| 35 | + assertTrue(message.contains("syntax error")); |
| 36 | + assertTrue(message.contains("near \"MockMigration\"")); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Should use the new parser if configured to do so. |
| 42 | + */ |
| 43 | + public void testDelimitedMigration() { |
| 44 | + Configuration configuration = new Configuration.Builder(getContext()) |
| 45 | + .setSqlParser(Configuration.SQL_PARSER_DELIMITED) |
| 46 | + .setDatabaseName("migration.db") |
| 47 | + .setDatabaseVersion(2) |
| 48 | + .create(); |
| 49 | + |
| 50 | + DatabaseHelper helper = new DatabaseHelper(configuration); |
| 51 | + SQLiteDatabase db = helper.getWritableDatabase(); |
| 52 | + helper.onUpgrade(db, 1, 2); |
| 53 | + } |
| 54 | +} |
0 commit comments