Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何模拟getApplicationInfo().dataDir_Android_Unit Testing_Powermockito - Fatal编程技术网

Android 如何模拟getApplicationInfo().dataDir

Android 如何模拟getApplicationInfo().dataDir,android,unit-testing,powermockito,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 + "/da

我们如何模拟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");
        }
    }

你可以看到我的代码,我过去是怎么做的:

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();
}
公共类数据库配置扩展了SQLiteOpenHelper{
公共静态字符串DB_路径;
公共静态字符串DB_NAME;
公共静态SQLiteDatabase\u数据库;
私有最终上下文myContext;
/**
*@param生成器
*@author-Wild-Coder
*@说明
*@daJun 242015
*/
公共数据库配置(生成器){
super(builder.CONTEXT,builder.DB_NAME,null,builder.VERSION);
this.myContext=builder.CONTEXT;
DB_PATH=builder.DB_PATH;
DB_NAME=builder.DB_NAME;
}
公共静态类生成器{
公共字符串DB_路径;
公共字符串DB_名称;
公共语境;
公共int版本;
/**
*@param上下文
*@description-Set-DBConfiguration
*@author-Wild-Coder
*/
公共生成器(上下文){
this.CONTEXT=上下文;
DB_PATH=“/data/data/”+context.getPackageName()+“/”;
}
/**
*@description-Set-DBConfiguration
*@author-Wild-Coder
*/
公共生成器集合名(字符串名){
DB_NAME=名称;
归还这个;
}
/**
*@description-Set-DBConfiguration
*@author-Wild-Coder
*/
公共生成器集合名(int版本){
版本=版本;
归还这个;
}
/**
*构建存储工具的配置。
*
*@return-DBConfiguration
*@author-Wild-Coder
*/
公共数据库配置生成(){
返回新的数据库配置(此配置);
}
}
/**
*在系统上创建一个空数据库,并用自己的数据库重写它
*数据库。
*/
公共数据库(){
布尔值dbExist=checkDataBase();
如果(!dbExist){
//通过调用此方法,空数据库将被创建到
//默认系统路径
//你的应用程序,所以我们可以覆盖它
//数据库与我们的数据库。
试一试{
copyDataBase();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
/**
*检查数据库是否已存在,以避免每次重新复制文件
*打开应用程序的时间。
*
*@如果存在则返回true,如果不存在则返回false
*/
私有布尔校验数据库(){
SQLiteDatabase checkDB=null;
试一试{
字符串myPath=DB_PATH+DB_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath,null,
SQLiteDatabase.OPEN_READWRITE);
}catch(sqlitee异常){
//数据库还不存在。
}
if(checkDB!=null){
checkDB.close();
}
return checkDB!=null?true:false;
}
/**
*@author-Wild-Coder
*@说明
*@dJun 242015
*@void
*@使用野生编码器
*/
public void copyDataBase()引发IOException{
//打开本地数据库作为输入流
InputStream myInput=myContext.getAssets().open(DB_NAME);
//刚创建的空数据库的路径
字符串outFileName=DB_路径+DB_名称;
//打开空数据库作为输出流
OutputStream myOutput=新文件OutputStream(outFileName);
//将字节从输入文件传输到输出文件
字节[]缓冲区=新字节[2048];
整数长度;
而((长度=myInput.read(缓冲区))>0){
写入(缓冲区,0,长度);
}
//关闭溪流
myOutput.flush();
myOutput.close();
myInput.close();
}
/**
*@author-Wild-Coder
*@说明
*@dJun 242015
*@SQLiteDatabase
*@使用野生编码器
*/
公共静态SQLiteDatabase openDataBase()引发SQLException{
//打开数据库
如果(_database==null){
_database=SQLiteDatabase.openDatabase(DB\u路径+DB\u名称,null,
SQLiteDatabase.OPEN_READWRITE
|SQLiteDatabase.CREATE_(如果需要);
}else if(!\u database.isOpen()){
_database=SQLiteDatabase.openDatabase(DB\u路径+DB\u名称,null,
SQLiteDatabase.OPEN_READWRITE
|SQLiteDatabase.CREATE_(如果需要);
}
返回数据库;
}
/**
*@author-Wild-Coder
*@说明
*@dJun 242015
*@void
*@使用野生编码器
*/
公共静态数据库(){
if(_database!=null&&_database.isOpen())
_close()数据库;
}
@凌驾
public void onCreate(SQLiteDatabase db){
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
}
/**
*@author-Wild-Coder
*@说明
*@dJun 242015
*@boolean
*@使用野生编码器
*/
公共静态布尔deleteDir(文件目录){
if(dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i

}

您可以看到我的代码,我以前是如何执行的:

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();
}
公共类数据库配置扩展了SQLiteOpenHelper{
公共静态字符串DB_路径;
公共静态字符串DB_NAME;
公共静态SQLiteDatabase\u数据库;
私有最终上下文myContext;
/**
*@param生成器
*@author-Wild-Coder
*@说明
*@daJun 242015
*/
公共数据库配置(生成器){
super(builder.CONTEXT,builder.DB_NAME,null,builder.VERSION);
this.myContext=builder.CONTEXT;
DB_PATH=builder.DB_PATH;
DB_NAME=builder.DB_NAME;
}
公共静态类生成器{
公共字符串DB_路径;
公共字符串DB_名称;
公共语境;
公共int版本;
/**
*@param上下文
*@description-Set-DBConfiguration
*@author-Wild-Coder
*/
公共生成器(上下文){
this.CONTEXT=上下文;
DB_PATH=“/data/data/”+context.getPackageName()+“/”;
}
/**
*@description S