org.evosuite.runtime.mock.java.io.MockFile Maven / Gradle / Ivy
/**
* Copyright (C) 2010-2016 Gordon Fraser, Andrea Arcuri and EvoSuite
* contributors
*
* This file is part of EvoSuite.
*
* EvoSuite is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* EvoSuite is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with EvoSuite. If not, see .
*/
package org.evosuite.runtime.mock.java.io;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import org.evosuite.runtime.RuntimeSettings;
import org.evosuite.runtime.mock.MockFramework;
import org.evosuite.runtime.mock.OverrideMock;
import org.evosuite.runtime.mock.java.lang.MockIllegalArgumentException;
import org.evosuite.runtime.mock.java.net.MockURL;
import org.evosuite.runtime.vfs.FSObject;
import org.evosuite.runtime.vfs.VFile;
import org.evosuite.runtime.vfs.VFolder;
import org.evosuite.runtime.vfs.VirtualFileSystem;
/**
* This class is used in the mocking framework to replace File instances.
*
*
* All files are created in memory, and no access to disk is ever done
*
* @author arcuri
*
*/
public class MockFile extends File implements OverrideMock {
private static final long serialVersionUID = -8217763202925800733L;
/*
* Constructors, with same inputs as in File. Note: it is not possible to inherit JavaDocs for constructors.
*/
public MockFile(String pathname) {
super(pathname);
}
public MockFile(String parent, String child) {
this(combine(parent,child));
}
public MockFile(File parent, String child) {
this(parent == null ? (String) null : parent.getAbsolutePath()
, child);
}
public MockFile(URI uri) {
super(uri);
}
private static String combine(String parent, String child){
if (child == null) {
throw new NullPointerException();
}
if(parent == null){
return child;
}
if(parent.equals("")){
return VirtualFileSystem.getDefaultParent()+child;
}
return makeAbsolute(parent) + "/" + child;
}
private static String makeAbsolute(String path){
String base = VirtualFileSystem.getWorkingDirPath();
if(base.startsWith("/")){
//Mac/Linux
if(path.startsWith("/")){
return path;
} else {
return base + "/" + path;
}
} else {
//Windows
//TODO: tmp, nasty hack, but anyway this class ll need refactoring when fully handling Java 8
String root = base.substring(0, 3); //eg, C:\
if(path.startsWith(root)){
return path;
} else{
return base + "/" + path;
}
}
}
/*
* TODO: Java 7
*
* there is only one method in File that depends on Java 7:
*
* public Path toPath()
*
*
* but if we include it here, we will break compatibility with Java 6.
* Once we drop such backward compatibility, we will need to override
* such method
*/
/*
* --------- static methods ------------------
*
* recall: it is not possible to override static methods.
* In the SUT, all calls to those static methods of File, eg File.foo(),
* will need to be replaced with EvoFile.foo()
*/
public static File[] listRoots() {
if(! MockFramework.isEnabled()){
return File.listRoots();
}
//FIXME: this is not going to work if tests are executed on different machine
File[] roots = File.listRoots();
MockFile[] mocks = new MockFile[roots.length];
for(int i=0; i files = new ArrayList();
for (String s : ss) {
File f = new MockFile(this,s);
if ((filter == null) || filter.accept(f))
files.add(f);
}
return files.toArray(new File[files.size()]);
}
@Override
public String getCanonicalPath() throws IOException {
if(! MockFramework.isEnabled()){
return super.getCanonicalPath();
}
VirtualFileSystem.getInstance().throwSimuledIOExceptionIfNeeded(getAbsolutePath());
return super.getCanonicalPath();
}
@Override
public URL toURL() throws MalformedURLException {
if(! MockFramework.isEnabled() || !RuntimeSettings.useVNET){
return super.toURL();
}
URL url = super.toURL();
return MockURL.URL(url.toString());
}
// -------- unmodified methods --------------
@Override
public String getName(){
return super.getName();
}
@Override
public String getParent() {
return super.getParent();
}
@Override
public String getPath() {
return super.getPath();
}
@Override
public boolean isAbsolute() {
return super.isAbsolute();
}
@Override
public URI toURI() {
return super.toURI(); //no need of VNET here
}
@Override
public String[] list(FilenameFilter filter) {
//no need to mock it, as it uses the mocked list()
return super.list(filter);
}
@Override
public boolean mkdirs() {
//no need to mock it, as all methods it calls are mocked
return super.mkdirs();
}
@Override
public boolean setWritable(boolean writable) {
return super.setWritable(writable); // it calls mocked method
}
@Override
public boolean setReadable(boolean readable) {
return super.setReadable(readable); //it calls mocked method
}
@Override
public boolean setExecutable(boolean executable) {
return super.setExecutable(executable); // it calls mocked method
}
// ------- Object methods -----------
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public String toString() {
return super.toString();
}
}