Tester测试模板

测试模板来源于Java 编程思想 P565。测试案例我自己编的,稍微改了架构,变得更加通用些。


package cvfaner.JavaIODemo;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class MappedFileIO {
	private static int length = 0x8FFFFFF; 
	private MappedByteBuffer fileHandler = null;
	public MappedFileIO(String filePath) throws Exception
	{
		//!SHEN_TODO need shrink to origin file size
		fileHandler = new RandomAccessFile(filePath, "rw").getChannel()
				.map(FileChannel.MapMode.READ_WRITE, 0, length);		
	}
	
	public void search(byte[] buf)
	{
		
	}
	
	public void insert(int pos, byte[] buf)
	{
		
	}
	
	public void remove(int pos, int size)
	{
		
	}
	
	public void write(int pos, String buf)
	{
		write(pos, buf.getBytes());
	}
	
	public void write(int pos, byte[] buf)
	{
		fileHandler.position(pos);
		fileHandler.put(buf);
	}


}


public abstract class Tester {
	private String name;
	public Tester(String name) { this.name = name; }
	public void runTest() {
		System.out.print(name + ": ");
		try {
			test();
		}catch(IOException e) {
			throw new RuntimeException(e);
		}
	}
	public abstract void test() throws IOException;
		

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Tester[] tests = {
			new Tester("file write") {
				public void test() throws IOException
				{
					try {
						MappedFileIO mappedFileIO  = new MappedFileIO("test.dat");
						mappedFileIO.write(0, "Hello moov, I love you.");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		for(Tester test : tests)
			test.runTest();
	}

}

好久没发博了,之后要井喷了。最近两个月一直用Java开发Android程序,Android和Java文章会多些。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值