如何模拟getApplicationInfo()。dataDir

时间:2016-05-03 10:41:22

标签: android unit-testing powermockito

我们如何模拟getApplicationInfo.dataDir? 以下是代码代码段:

 private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = context;
        DATABASE_PATH = mContext.getApplicationInfo().dataDir + "/databases/";
        try {
            createDataBase();
            openDataBase();
        } catch (IOException e) {
            throw new Error("ErrorCopyingDataBase");
        }
    }

1 个答案:

答案 0 :(得分:3)

你可以看到我以前的代码:

public class DataBaseConfiguration extends SQLiteOpenHelper {
public static String DB_PATH;
public static String DB_NAME;

public static SQLiteDatabase _database;
private final Context myContext;

/**
 * @param builder
 * @author Wild Coder
 * @description
 * @daJun 24, 2015
 */
public DataBaseConfiguration(Builder builder) {
    super(builder.CONTEXT, builder.DB_NAME, null, builder.VERSION);
    this.myContext = builder.CONTEXT;
    DB_PATH = builder.DB_PATH;
    DB_NAME = builder.DB_NAME;
}

public static class Builder {
    public String DB_PATH;
    public String DB_NAME;
    public Context CONTEXT;
    public int VERSION;

    /**
     * @param context Context
     * @description Set DBConfiguration
     * @author Wild Coder
     */
    public Builder(Context context) {
        this.CONTEXT = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/";
    }

    /**
     * @description Set DBConfiguration
     * @author Wild Coder
     */
    public Builder setName(String name) {
        DB_NAME = name;
        return this;
    }

    /**
     * @description Set DBConfiguration
     * @author Wild Coder
     */
    public Builder setName(int version) {
        VERSION = version;
        return this;
    }

    /**
     * Build the configuration for storage tool.
     *
     * @return DBConfiguration
     * @author Wild Coder
     */
    public DataBaseConfiguration build() {
        return new DataBaseConfiguration(this);
    }
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 */
public void createDataBase() {
    boolean dbExist = checkDataBase();
    if (!dbExist) {
        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        // database does't exist yet.
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @void
 * @use Wild Coder
 */
public void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[2048];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @SQLiteDatabase
 * @use Wild Coder
 */
public static SQLiteDatabase openDataBase() throws SQLException {
    // Open the database
    if (_database == null) {
        _database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
                SQLiteDatabase.OPEN_READWRITE
                        | SQLiteDatabase.CREATE_IF_NECESSARY);
    } else if (!_database.isOpen()) {
        _database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
                SQLiteDatabase.OPEN_READWRITE
                        | SQLiteDatabase.CREATE_IF_NECESSARY);
    }
    return _database;
}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @void
 * @use Wild Coder
 */
public static void closeDatabase() {
    if (_database != null && _database.isOpen())
        _database.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @boolean
 * @use Wild Coder
 */
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    // The directory is now empty so delete it return dir.delete(); }
    return dir.delete();
}

}