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文章會多些。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章