public void setupApplicationState(final RobolectricConfig robolectricConfig) { ResourceLoader resourceLoader = createResourceLoader(robolectricConfig); PrintStream currentOut = System.out; System.setOut(new PrintStream(new ByteArrayOutputStream())); Robolectric.bindDefaultShadowClasses(); bindShadowClasses(); System.setOut(currentOut); Robolectric.resetStaticState(); resetStaticState(); DatabaseConfig.setDatabaseMap(this.databaseMap);//Set static DatabaseMap in DBConfig Robolectric.application = ShadowApplication.bind(createApplication(), resourceLoader); }
/** * Gets an in memory DB connection. Will load DB Driver if not already loaded. * * @return Connection to In Memory Database. */ public static Connection getMemoryConnection() { if (!isMapLoaded()) LoadSQLiteDriver(); try { return DriverManager.getConnection(dbMap.getConnectionString()); } catch (SQLException e) { throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig, could not retrieve connection to in memory database.", e); } }
/** * Create a SQL INSERT string. Returned values are then bound via * JDBC to facilitate various data types. * * @param table table name * @param values column name/value pairs * @param conflictAlgorithm the conflict algorithm to use * @return insert string */ public static SQLStringAndBindings buildInsertString(String table, ContentValues values, int conflictAlgorithm) throws SQLException { StringBuilder sb = new StringBuilder(); sb.append("INSERT "); sb.append(CONFLICT_VALUES[conflictAlgorithm]); sb.append("INTO "); sb.append(table); sb.append(" "); SQLStringAndBindings columnsValueClause = buildColumnValuesClause(values); sb.append(columnsValueClause.sql); sb.append(";"); String sql = DatabaseConfig.getScrubSQL(sb.toString()); return new SQLStringAndBindings(sql, columnsValueClause.columnValues); }
/** * Makes any edits necessary in the SQL string for it to be compatible with the database in use. * * @return * @throws SQLException */ public static String getScrubSQL(String sql) throws SQLException { if (isMapNull()) throw new NullDatabaseMapException("No database map set!"); return dbMap.getScrubSQL(sql); }
@Implementation public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags) { final SQLiteDatabase db = newInstanceOf(SQLiteDatabase.class); shadowOf(db).connection = DatabaseConfig.getMemoryConnection(); return db; }
@Implementation public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) { String where = selection; if (selection != null && selectionArgs != null) { where = buildWhereClause(selection, selectionArgs); } String sql = SQLiteQueryBuilder.buildQueryString(distinct, table, columns, where, groupBy, having, orderBy, limit); ResultSet resultSet; try { Statement statement = connection.createStatement(DatabaseConfig.getResultSetType(), ResultSet.CONCUR_READ_ONLY); resultSet = statement.executeQuery(sql); } catch (SQLException e) { throw new RuntimeException("SQL exception in query", e); } SQLiteCursor cursor = new SQLiteCursor(null, null, null, null); shadowOf(cursor).setResultSet(resultSet,sql); return cursor; }
public static String getSelectLastInsertIdentity() { if (isMapNull()) throw new NullDatabaseMapException("No database map set!"); return dbMap.getSelectLastInsertIdentity(); }
public void setupApplicationState(final RobolectricConfig robolectricConfig) { setupLogging(); ResourceLoader resourceLoader = createResourceLoader(robolectricConfig ); Robolectric.bindDefaultShadowClasses(); bindShadowClasses(); resourceLoader.setLayoutQualifierSearchPath(); Robolectric.resetStaticState(); resetStaticState(); DatabaseConfig.setDatabaseMap(this.databaseMap);//Set static DatabaseMap in DBConfig Robolectric.application = ShadowApplication.bind(createApplication(), resourceLoader); }
public static int getResultSetType() { if (isMapNull()) throw new NullDatabaseMapException("No database map set!"); return dbMap.getResultSetType(); }
@Implementation public void execSQL(String sql) throws android.database.SQLException { if (!isOpen()) { throw new IllegalStateException("database not open"); } try { String scrubbedSql= DatabaseConfig.getScrubSQL(sql); connection.createStatement().execute(scrubbedSql); } catch (java.sql.SQLException e) { android.database.SQLException ase = new android.database.SQLException(); ase.initCause(e); throw ase; } }
/** * Sets what database will be used and loads the database driver, based on what DBmap is provided. */ private static void LoadSQLiteDriver() { if (isMapNull()) throw new NullDatabaseMapException("Error in DatabaseConfig: DatabaseMap has not been set."); try { Class.forName(dbMap.getDriverClassName()).newInstance(); } catch (InstantiationException e) { throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig: SQLite driver could not be instantiated;", e); } catch (IllegalAccessException e) { throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig: SQLite driver could not be accessed;", e); } catch (ClassNotFoundException e) { throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig: SQLite driver class could not be found;", e); } isLoaded = true; }
@Implementation public SQLiteStatement compileStatement(String sql) throws SQLException { lock(); String scrubbedSql= DatabaseConfig.getScrubSQL(sql); try { SQLiteStatement stmt = Robolectric.newInstanceOf(SQLiteStatement.class); Robolectric.shadowOf(stmt).init(realSQLiteDatabase, scrubbedSql); return stmt; } catch (Exception e){ throw new RuntimeException(e); } finally { unlock(); } }
@Implementation public void execSQL(String sql, Object[] bindArgs) throws SQLException { if (bindArgs == null) { throw new IllegalArgumentException("Empty bindArgs"); } String scrubbedSql= DatabaseConfig.getScrubSQL(sql); SQLiteStatement statement = null; try { statement =compileStatement(scrubbedSql); if (bindArgs != null) { int numArgs = bindArgs.length; for (int i = 0; i < numArgs; i++) { DatabaseUtils.bindObjectToProgram(statement, i + 1, bindArgs[i]); } } statement.execute(); } catch (SQLiteDatabaseCorruptException e) { throw e; } finally { if (statement != null) { statement.close(); } } }