use of org.evosuite.runtime.mock.java.io.MockFile in project evosuite by EvoSuite.
the class MockRandomAccessFileTest method testNoWritePermission.
@Test
public void testNoWritePermission() {
MockFramework.enable();
VirtualFileSystem.getInstance().resetSingleton();
VirtualFileSystem.getInstance().init();
String fileName = "foo_random_access.txt";
RandomAccessFile ra = null;
try {
ra = new MockRandomAccessFile(fileName, "r");
Assert.fail();
} catch (FileNotFoundException e1) {
// expected as file does not exist
}
File file = new MockFile(fileName);
try {
file.createNewFile();
} catch (IOException e1) {
// we should be able to create it
Assert.fail();
}
try {
ra = new MockRandomAccessFile(fileName, "r");
} catch (FileNotFoundException e1) {
// we should be able to open the stream
Assert.fail();
}
final int LENGTH = 10;
try {
ra.setLength(LENGTH);
Assert.fail();
} catch (IOException e) {
// expected, as we do now have write permissions;
}
long size = -1;
try {
ra.close();
ra = new MockRandomAccessFile(fileName, "rw");
ra.setLength(LENGTH);
size = ra.length();
ra.close();
} catch (IOException e) {
Assert.fail();
}
Assert.assertEquals(LENGTH, size);
}
use of org.evosuite.runtime.mock.java.io.MockFile in project evosuite by EvoSuite.
the class MockFileTest method testAssumptionOnConstructors_Mock.
@Test
public void testAssumptionOnConstructors_Mock() {
String name = "base";
MockFile base = new MockFile(name);
MockFile emptyParent = new MockFile("", name);
MockFile nullParent = new MockFile((String) null, name);
assertEquals(base.getAbsolutePath(), nullParent.getAbsolutePath());
assertTrue(base.getAbsolutePath().length() > (name.length() + 1));
if (emptyParent.getAbsolutePath().startsWith("/")) {
// Mac/Linux
assertEquals("/" + name, emptyParent.getAbsolutePath());
}
}
use of org.evosuite.runtime.mock.java.io.MockFile in project evosuite by EvoSuite.
the class MockFileTest method testSamePath.
@Test
public void testSamePath() {
String name = "foo.txt";
File real = new File(name);
MockFile mock = new MockFile(name);
assertEquals(real.toString(), mock.toString());
assertEquals(real.getPath(), mock.getPath());
}
use of org.evosuite.runtime.mock.java.io.MockFile in project evosuite by EvoSuite.
the class VirtualFileSystemTest method testReadingNonExistingFile.
@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);
// real file does not exist
Assert.fail();
} catch (FileNotFoundException e) {
}
File mockFile = new MockFile(fileName);
Assert.assertFalse(mockFile.exists());
try {
MockFileInputStream in = new MockFileInputStream(mockFile);
// also the mock file does not exist (yet)
Assert.fail();
} catch (FileNotFoundException e) {
}
boolean created = mockFile.createNewFile();
Assert.assertTrue(created);
Assert.assertTrue(mockFile.exists());
// real file shouldn's have been created
Assert.assertFalse(realFile.exists());
// following should work even if real file does not exist
MockFileInputStream in = new MockFileInputStream(mockFile);
}
use of org.evosuite.runtime.mock.java.io.MockFile in project evosuite by EvoSuite.
the class VirtualFileSystemTest method testWriteToFile.
@Test
public void testWriteToFile() throws IOException {
String fileName = "foo_written_with_FOS";
File realFile = new File(fileName);
// be sure to get it deleted in case we accidently create it
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);
// writing to such file should create it
Assert.assertTrue(file.exists());
out.close();
try {
out.write(data);
Assert.fail();
} catch (Exception e) {
// this is expected, as the stream is closed
}
// be sure that no real file was created
Assert.assertFalse(realFile.exists());
}
Aggregations