无法使用Singleton模式访问File

时间:2017-09-10 17:26:05

标签: java file junit singleton

我编写了使用文件模拟硬盘的java类。 硬盘calss是Singleton。我现在使用Junit测试项目中的其他类,需要使用硬盘calss。 每次我使用方法HardDisk.getInstance()时出于某种原因; 在Junit中我得到错误IOException。如果我删除文件错误更改为fileNotFound,所以我知道该类有点工作。

Class Junit

import com.hit.algorithm.IAlgoCache;
import com.hit.algorithm.LRUAlgoCacheImpl;
import com.hit.memoryunits.*;

import junit.framework.Assert;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.Test;

public class MemoryManagementUnitTest {
    @Test
    public void testGetPages() throws IOException {
    HardDisk.getInstance();

    }

}

类硬盘

package com.hit.memoryunits;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map.Entry;


public class HardDisk{

    static final String DEFAULT_FILE_NAME="src\\main\\test\\hdPages.txt";
    static final int _SIZE=1000;
    //private static final HardDisk instance=new HardDisk();
    private static  HardDisk instance=new HardDisk();
    LinkedHashMap<Long,Page<byte[]>> harDiskMap;


    @SuppressWarnings({ "unchecked"})
    private HardDisk(){
        harDiskMap = new LinkedHashMap<>(_SIZE);
        try{
            FileInputStream fis = new FileInputStream(DEFAULT_FILE_NAME);
            ObjectInputStream in = new ObjectInputStream(fis);
            try{
                while(true){
                    Page<byte[]> temp = (Page<byte[]>)in.readObject();
                    harDiskMap.put(temp.getPageId(), temp);
                }
            }
            finally{
                    in.close();
            }
        }
        catch (ClassNotFoundException e){
            System.out.println("ClassNotFoundException");
        }
        catch (FileNotFoundException e1){
            System.out.println("FileNotFoundException");
        }
        catch(IOException e2){
            System.out.println("IOException");
        }
    }

    public static HardDisk getInstance(){
        return HardDisk.instance;
    }


    private void writeHd() throws FileNotFoundException, IOException{
        ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(DEFAULT_FILE_NAME));
        for(Entry<Long, Page<byte[]>> entry : harDiskMap.entrySet()) {  
            Long key = entry.getKey();
            out.writeObject(harDiskMap.get(key));
        }
        out.close();        
    }

    public Page<byte[]> pageFault(Long pageId)throws FileNotFoundException,IOException{

        if(!this.harDiskMap.containsKey(pageId))
            this.harDiskMap.put(pageId, new Page<byte[]>( new byte[]{}, pageId));

        Page<byte[]> temp=this.harDiskMap.get(pageId);
        return temp;
    }

    public Page<byte[]> pageReplacement(Page<byte[]> moveToHdPage,Long moveToRamId) throws java.io.FileNotFoundException,java.io.IOException{
        harDiskMap.replace(moveToHdPage.getPageId(), moveToHdPage);
        writeHd();
        return pageFault(moveToRamId);
    }                  
}

这是输出

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at com.hit.memoryunits.HardDisk.<init>(HardDisk.java:26)
    at com.hit.memoryunits.HardDisk.<clinit>(HardDisk.java:17)
    at com.hit.memoryunits.MemoryManagementUnitTest.testGetPages(MemoryManagementUnitTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

0 个答案:

没有答案