VirtualFileSystem { public boolean exists(String rawPath) { return findFSObject(rawPath) != null; } private  VirtualFileSystem(); static VirtualFileSystem getInstance(); void resetSingleton(); void addLeakingResource(LeakingResource resource); File getRealTmpFile(); void throwSimuledIOExceptionIfNeeded(String path); boolean isClassSupposedToThrowIOException(String path); boolean setShouldThrowIOException(EvoSuiteFile file); boolean setShouldAllThrowIOExceptions(); void init(); static String getWorkingDirPath(); static String getDefaultParent(); Set<String> getAccessedFiles(); String createTempFile(String prefix, String suffix, File directory); boolean exists(String rawPath); FSObject findFSObject(String rawPath); boolean deleteFSObject(String rawPath); boolean createFile(String rawPath); boolean rename(String source, String destination); boolean createFolder(String rawPath);  }
@Test public void testReadingNonExistingFile() throws IOException{ String fileName = "this_file_should_not_exist"; File realFile = new File(fileName); Assert.assertFalse(realFile.exists()); try{ MockFileInputStream in = new MockFileInputStream(realFile); Assert.fail(); } catch(FileNotFoundException e){ } File mockFile = new MockFile(fileName); Assert.assertFalse(mockFile.exists()); try{ MockFileInputStream in = new MockFileInputStream(mockFile); Assert.fail(); } catch(FileNotFoundException e){ } boolean created = mockFile.createNewFile(); Assert.assertTrue(created); Assert.assertTrue(mockFile.exists()); Assert.assertFalse(realFile.exists()); MockFileInputStream in = new MockFileInputStream(mockFile); }
@Test public void testWriteToFile() throws IOException{ String fileName = "foo_written_with_FOS"; File realFile = new File(fileName); realFile.deleteOnExit(); Assert.assertFalse(realFile.exists()); File file = new MockFile(fileName); Assert.assertFalse(file.exists()); byte[] data = new byte[]{42}; MockFileOutputStream out = new MockFileOutputStream(file); out.write(data); Assert.assertTrue(file.exists()); out.close(); try{ out.write(data); Assert.fail(); } catch(Exception e){ } Assert.assertFalse(realFile.exists()); }
@Test public void testWorkingDirectoryExists(){ MockFile workingDir = new MockFile(java.lang.System.getProperty("user.dir")); Assert.assertTrue(workingDir.exists()); }
@Test public void testCreateDeleteFileDirectly() throws IOException{ MockFile file = new MockFile("foo"); Assert.assertFalse(file.exists()); boolean created = file.createNewFile(); Assert.assertTrue(created); Assert.assertTrue(file.exists()); boolean deleted = file.delete(); Assert.assertTrue(deleted); Assert.assertFalse(file.exists()); }
@Test public void testCreateDeleteFolderDirectly() throws IOException{ MockFile folder = new MockFile("foo"+File.separator+"hello"); Assert.assertFalse(folder.exists()); boolean created = folder.mkdir(); Assert.assertFalse(created); Assert.assertFalse(folder.exists()); created = folder.mkdirs(); Assert.assertTrue(created); Assert.assertTrue(folder.exists()); MockFile file = new MockFile(folder.getAbsoluteFile()+File.separator+"evo"); created = file.createNewFile(); Assert.assertTrue(created); Assert.assertTrue(file.exists()); boolean deleted = folder.delete(); Assert.assertFalse(deleted); Assert.assertTrue(folder.exists()); deleted = file.delete(); Assert.assertTrue(deleted); Assert.assertFalse(file.exists()); deleted = folder.delete(); Assert.assertTrue(deleted); Assert.assertFalse(folder.exists()); }