天天看点

113.s1-Test测试实现黑名单的增删该查

eclipse可以使用Test实现代码的局部测试

首先代码清单文件中需要添加途中文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ldw.safe"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    
<instrumentation  
        android:name="android.test.InstrumentationTestRunner"  
        android:targetPackage="com.ldw.safe" />  
    <application
        android:allowBackup="true"
        android:icon="@drawable/tb"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner" />  
        <activity
            android:name="com.ldw.safe.Activity.SplashActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.ldw.safe.Activity.HomeActivity"></activity>
        <activity android:name="com.ldw.safe.Activity.SettingActivity"></activity>
        <activity android:name="com.ldw.safe.Activity.LostAndFind"></activity>
        <activity android:name="com.ldw.safe.Activity.Setup1Activity"></activity>
        <activity android:name="com.ldw.safe.Activity.Setup2Activity"></activity>
        <activity android:name="com.ldw.safe.Activity.Setup3Activity"></activity>
        <activity android:name="com.ldw.safe.Activity.Setup4Activity"></activity>
        <activity android:name="com.ldw.safe.Activity.ContactActivity"></activity>
        <activity android:name="com.ldw.safe.Activity.AdvancedToolsActivity"></activity>
        <activity android:name="com.ldw.safe.Activity.AddressActivity"></activity>
        <activity android:name="com.ldw.safe.Activity.DragViewActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            ></activity>
        <activity android:name="com.ldw.safe.Activity.CallSafeActivity"></activity>
        
        
        <receiver android:name="com.ldw.safe.receiver.BootCompletedReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.ldw.safe.receiver.SmsReceiver">
            <intent-filter 
                android:priority="1000"
                >
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
        <!-- 
        <receiver android:name="com.ldw.safe.receiver.OutCallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>
         -->
        <service android:name="com.ldw.safe.service.LocationService"></service>
        <service android:name="com.ldw.safe.service.AddressService"></service>
        
    </application>

</manifest>                
TestBlackNumberDao.java这个文件里面是测试文件,测试增删该查
      
package com.ldw.safe.test;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.test.AndroidTestCase;

import com.ldw.safe.bean.BlackNumberInfo;
import com.ldw.safe.db.dao.BlackNumberDao;

/**
 *
 */
public class TestBlackNumberDao extends AndroidTestCase {

    public Context mContext;
    @Override
    protected void setUp() throws Exception {
        this.mContext = getContext();
        super.setUp();
    }

    public void testAdd(){
        BlackNumberDao dao = new BlackNumberDao(mContext);
        Random random = new Random();
        for (int i = 0; i < 200; i++) {
            Long number = 13300000000l +i;
            dao.add(number +"",String.valueOf(random.nextInt(3) + 1));
        }
    }
    public void testDelete(){
        BlackNumberDao dao = new BlackNumberDao(mContext);
        boolean delete = dao.delete("13300000000");
        assertEquals(true,delete);
    }

    public void testFind(){
        BlackNumberDao dao = new BlackNumberDao(mContext);
        String number = dao.findNumber("13300000004");
        System.out.println(number);
    }

    public void testFindAll(){
        BlackNumberDao dao = new BlackNumberDao(mContext);
        List<BlackNumberInfo> blackNumberInfos = dao.findAll();
        for (BlackNumberInfo blackNumberInfo : blackNumberInfos) {
            System.out.println(blackNumberInfo.getMode() + "" + blackNumberInfo.getNumber());
        }
    }
}
           

黑名单的bean文件BlackNumberInfo.java

package com.ldw.safe.bean;

/*
 * 黑名单的javaBean
 */
public class BlackNumberInfo {

	//number黑名单的号码,
	//mode黑名单的拦截模式1表示全部拦截2表示电话拦截3表示全部拦截
	private String number;
	private String mode;
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getMode() {
		return mode;
	}
	public void setMode(String mode) {
		this.mode = mode;
	}
	
}
           

创建数据库表BlackNumberOpenHelper.java

package com.ldw.safe.db.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/*
 * 黑名单的SQLiteOpenHelper,创建黑名单数据库的表
 */
public class BlackNumberOpenHelper extends SQLiteOpenHelper{

	public BlackNumberOpenHelper(Context context) {
		super(context, "safe.db", null, 1);

	}

	//创建数据库的表
	//blacknumber表名,_id逐渐自动增长,number电话号码,mode拦截模式,电话拦截,短信拦截
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

}
           

数据库相关的逻辑文件BlackNumberDao.java实现数据库的增删改查

package com.ldw.safe.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ldw.safe.bean.BlackNumberInfo;

/*
 * 黑名单数据库的逻辑文件
 */
public class BlackNumberDao {

	private BlackNumberOpenHelper helper;

	public BlackNumberDao(Context context){
		
		helper = new BlackNumberOpenHelper(context);
	}
	
	//添加黑名单
	//number黑名单号码,mode拦截模式,"blacknumber"是表名
	public boolean add(String number, String mode){
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("number", number);
		contentValues.put("mode", mode);
		long blacknumber = db.insert("blacknumber", null, contentValues);
		if(blacknumber == -1){
			//添加失败
			return false;
		}else{
			return true;
		}
		
	}
	
	//通过电话号码删除黑命单
	//number电话号码,"blacknumber"是表名
	public boolean delete(String number){
		SQLiteDatabase db = helper.getWritableDatabase();
		int rowNumber = db.delete("blacknumber", "number=?", new String[]{number});
		if(rowNumber == 0){
			//删除失败
			return false;
		}else{
			return true;
		}
	}
	
	//修改黑名单的模式
	public boolean changeNumberMode(String number, String mode){
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("mode", mode);
		int rowNumber = db.update("blacknumber", contentValues, "number=?", new String[]{number});
		if(rowNumber == 0){
			//修改失败
			return false;
		}else{
			return true;
		}
	}
	
	//通过电话号码查找黑名单拦截模式
	public String findNumber(String number){
		String mode = "";
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.query("blacknumber", new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);
		//只需要查询一个
		if(cursor.moveToNext()){
			mode = cursor.getString(0);
		}
		cursor.close();
		db.close();
		return mode;
	}
	
	//查找出所有的黑名单
	public List<BlackNumberInfo> findAll(){
		SQLiteDatabase db = helper.getWritableDatabase();
		List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		Cursor cursor = db.query("blacknumber", new String[]{"number", "mode"}, null, null, null, null, null);
		//查询所有的所有用while
		while(cursor.moveToNext()){
			BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
			blackNumberInfo.setNumber(cursor.getString(0));
			blackNumberInfo.setMode(cursor.getString(1));
			blackNumberInfos.add(blackNumberInfo);
		}
		cursor.close();
		db.close();
		return blackNumberInfos;
	}
}