All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.runtime.mock.java.io.MockFileInputStream Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
/**
 * Copyright (C) 2010-2017 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.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.concurrent.atomic.AtomicInteger;

import org.evosuite.runtime.LeakingResource;
import org.evosuite.runtime.mock.MockFramework;
import org.evosuite.runtime.mock.OverrideMock;
import org.evosuite.runtime.mock.java.lang.MockNullPointerException;
import org.evosuite.runtime.vfs.VFile;
import org.evosuite.runtime.vfs.VirtualFileSystem;

public class MockFileInputStream extends FileInputStream implements LeakingResource, OverrideMock{


	private FileChannel channel = null;

	/**
	 * Is this stream closed?
	 */
	private volatile boolean closed = false;

	/**
	 * The path to the file
	 */
	private final String path;

	/**
	 * The position to read in the stream next
	 */
	private final AtomicInteger position = new AtomicInteger(0);

	// ----- constructors -------------

	public MockFileInputStream(String name) throws FileNotFoundException {
		this( name != null ? 
						(!MockFramework.isEnabled() ? new File(name) : new MockFile(name)) : 
							null
				);
	}

	
	public MockFileInputStream(File file) throws FileNotFoundException {
		super(!MockFramework.isEnabled() ?
						file :
							VirtualFileSystem.getInstance().getRealTmpFile()  // just to make compiler happy
				);
		if(!MockFramework.isEnabled()){
			path = null;
			return;
		}
		
		VirtualFileSystem.getInstance().addLeakingResource(this);

		path = (file != null ? file.getAbsolutePath() : null);
		if (path == null) {
			throw new MockNullPointerException();
		}

		VFile vf = NativeMockedIO.getFileForReading(path);
		if(vf==null){
			throw new FileNotFoundException();
		}
	}

	/*
	 * we do not really handle this constructor.
	 * 
	 * TODO: we could, by using StaticReplacementMock and reflection, but anyway it is very rare
	 */
	public MockFileInputStream(FileDescriptor fdObj) {
		super(fdObj);
		path = "";
	}

	// ----  read methods  ----------

	public int read() throws IOException{

		if(!MockFramework.isEnabled()){
			return super.read();
		}
		
		throwExceptionIfClosed();

		return NativeMockedIO.read(path, position); 
	}

	private  int readBytes(byte b[], int off, int len) throws IOException{

		if(!MockFramework.isEnabled()){
			return super.read(b, off, len);
		}
		
		int counter = 0;
		for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy