SQLException при използване на Google Analytics с Robolectric (или опит за използване на SQLite3 в Robolectric)

Използвам robolectric, за да тествам дейност, която използва Google Analytics. За съжаление, когато се опитам да стартирам дейността, получавам следното изключение

android.database.SQLException
        at com.xtremelabs.robolectric.shadows.ShadowSQLiteDatabase.execSQL(ShadowSQLiteDatabase.java:149)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java)
        at com.google.android.apps.analytics.PersistentEventStore$DataBaseHelper.onCreate(Unknown Source)
        at com.xtremelabs.robolectric.shadows.ShadowSQLiteOpenHelper.getWritableDatabase(ShadowSQLiteOpenHelper.java:52)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java)
        at com.google.android.apps.analytics.PersistentEventStore.<init>(Unknown Source)
        at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
        at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
        ...
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE EVENTS ( 'event_id'[*] BIGINT(19) PRIMARY KEY AUTO_INCREMENT NOT NULL, 'user_id' BIGINT(19) NOT NULL, 'account_id' CHAR(256) NOT NULL, 'random_val' BIGINT(19) NOT NULL, 'timestamp_first' BIGINT(19) NOT NULL, 'timestamp_previous' BIGINT(19) NOT NULL, 'timestamp_current' BIGINT(19) NOT NULL, 'visits' BIGINT(19) NOT NULL, 'category' CHAR(256) NOT NULL, 'action' CHAR(256) NOT NULL, 'label' CHAR(256),  'value' BIGINT(19), 'screen_width' BIGINT(19), 'screen_height' BIGINT(19)); "; expected "identifier"; SQL statement:
CREATE TABLE events ( 'event_id' bigint(19) PRIMARY KEY auto_increment NOT NULL, 'user_id' bigint(19) NOT NULL, 'account_id' CHAR(256) NOT NULL, 'random_val' bigint(19) NOT NULL, 'timestamp_first' bigint(19) NOT NULL, 'timestamp_previous' bigint(19) NOT NULL, 'timestamp_current' bigint(19) NOT NULL, 'visits' bigint(19) NOT NULL, 'category' CHAR(256) NOT NULL, 'action' CHAR(256) NOT NULL, 'label' CHAR(256),  'value' bigint(19), 'screen_width' bigint(19), 'screen_height' bigint(19)); [42001-147]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
        at org.h2.message.DbException.get(DbException.java:167)
        at org.h2.message.DbException.getSyntaxError(DbException.java:192)
        at org.h2.command.Parser.readColumnIdentifier(Parser.java:2694)
        at org.h2.command.Parser.parseCreateTable(Parser.java:4975)
        at org.h2.command.Parser.parseCreate(Parser.java:3705)
        at org.h2.command.Parser.parsePrepared(Parser.java:320)
        at org.h2.command.Parser.parse(Parser.java:275)
        at org.h2.command.Parser.parse(Parser.java:247)
        at org.h2.command.Parser.prepare(Parser.java:201)
        at org.h2.command.Parser.prepareCommand(Parser.java:214)
        at org.h2.engine.Session.prepareLocal(Session.java:425)
        at org.h2.engine.Session.prepareCommand(Session.java:374)
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1056)
        at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:165)
        at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:153)
        at com.xtremelabs.robolectric.shadows.ShadowSQLiteDatabase.execSQL(ShadowSQLiteDatabase.java:147)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:87)
        at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:110)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java)
        at com.google.android.apps.analytics.PersistentEventStore$DataBaseHelper.onCreate(Unknown Source)
        at com.xtremelabs.robolectric.shadows.ShadowSQLiteOpenHelper.getWritableDatabase(ShadowSQLiteOpenHelper.java:52)

Проблемът е, че Android използва бази данни SQLite, но robolectric използва H2, който поддържа малко по-различен вкус на SQL.

Кой е най-лесният начин да заобиколите този проблем?


person emmby    schedule 08.06.2011    source източник
comment
Модифицирането на източника ala github.com/pivotal/robolectric/issues/19 изглежда е един начин да го направя, но бих предпочел начин, който не включва модифициране на източника   -  person emmby    schedule 08.06.2011


Отговори (2)


Robolectric вече предоставя анотацията @UsingDatabaseMap, така че други бази данни могат лесно да се използват с Roboelectric. (В момента това изисква версията SNAPSHOT на Robolectric, Robolectric-1.0-RC5-SNAPSHOT)

Ако някой желае да използва SQLite с Robolectric, препоръчвам да провери моя проект в github, който предоставя класа SQLiteMap, така че човек да може да използва SQLite с Robolectric.

https://github.com/cessationoftime/robolectric-sqlite/wiki

Резултатът е тестови класове, анотирани да използват базата данни SQLite вместо H2:

@UsingDatabaseMap(SQLiteMap.class)
@RunWith(RobolectricTestRunner.class)
public class DaoTest {
    @Test
    public void RobolectricSQLiteTest() {
    }
}
person cessationoftime    schedule 28.08.2011
comment
Използването на моя проект в github вече не е необходимо. Моят код е напълно интегриран в проекта Robolectric (първоначално го написах като плъгин) и вярвам, че базата данни SQLite поддържа Robolectric сега по подразбиране. - person cessationoftime; 26.12.2012

Успях да извървя по-голямата част от пътя дотук, като внедрих собствения си клас Shadow за SQLiteDatabase и го обвързах в персонализиран тестов инструмент. Вероятно можете да го обвържете в методите за настройка на теста вместо това, ако не използвате персонализиран тестов инструмент.

public class MyRobolectricTestRunner extends RobolectricTestRunner {

    public InjectedTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected void bindShadowClasses() {
        super.bindShadowClasses();
        Robolectric.bindShadowClass(ShadowRealSQLiteDatabase.class);
    }


@Implements(SQLiteDatabase.class)
public static class ShadowRealSQLiteDatabase extends ShadowSQLiteDatabase {
    protected static Connection connection;

    // Specify the SQLite JDBC driver
    @Implementation
    public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags) {
        try {
            Class.forName("org.sqlite.JDBC").newInstance();
            final Field f = ShadowSQLiteDatabase.class.getDeclaredField("connection");
            f.setAccessible(true);
            connection = DriverManager.getConnection("jdbc:sqlite:mem:");
            f.set(null, connection );
        } catch (Exception e) {
            throw new RuntimeException("SQL exception in openDatabase", e);
        }
        return newInstanceOf(SQLiteDatabase.class);
    }


    // Bypass TYPE_FORWARD_ONLY by calling next instead of first: http://stackoverflow.com/questions/2260575/mysql-java-exception-before-start-of-result-set/2260592#2260592
    @Implementation
    public long insert(String table, String nullColumnHack, ContentValues values) {
        SQLite.SQLStringAndBindings sqlInsertString = buildInsertString(table, values);
        try {
            PreparedStatement statement = connection.prepareStatement(sqlInsertString.sql, Statement.RETURN_GENERATED_KEYS);
            Iterator<Object> columns = sqlInsertString.columnValues.iterator();
            int i = 1;
            while (columns.hasNext()) {
                statement.setObject(i++, columns.next());
            }

            statement.executeUpdate();

            ResultSet resultSet = statement.getGeneratedKeys();
            if (resultSet.next()) {
                return resultSet.getLong(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException("SQL exception in insert", e);
        }
        return -1;
    }


    // Bypass ShadowSQLiteDatabase.execSQL auto_increment hack
    @Implementation
    public void execSQL(String sql) throws android.database.SQLException {
        if (!isOpen()) {
            throw new IllegalStateException("database not open");
        }

        try {
            connection.createStatement().execute(sql);
        } catch (java.sql.SQLException e) {
            android.database.SQLException ase = new android.database.SQLException();
            ase.initCause(e);
            throw ase;
        }
    }
}
}

Ще ви е необходим драйвер com.zentus v056 sqlite-jdbc във вашия клас път.

Проверих, че това използва SQLite вместо H2, въпреки че получавам нова грешка с това решение:

Caused by: java.sql.SQLException: NYI
        at org.sqlite.Conn.prepareStatement(Conn.java:217)
        at com.groupon.InjectedTestRunner$ShadowRealSQLiteDatabase.insert(InjectedTestRunner.java:94)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:87)
        at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:110)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java)
        at com.google.android.apps.analytics.PersistentEventStore$DataBaseHelper.createCustomVariableTables(Unknown Source)
        at com.google.android.apps.analytics.PersistentEventStore$DataBaseHelper.onCreate(Unknown Source)
        at com.xtremelabs.robolectric.shadows.ShadowSQLiteOpenHelper.getWritableDatabase(ShadowSQLiteOpenHelper.java:52)
person emmby    schedule 08.06.2011
comment
Еха. Това не изглежда забавно. - person Donn Felker; 09.06.2011