<commit_before>package es.tid.cosmos.platform.injection.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.URI;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.AccessControlException;
import org.apache.sshd.server.SshFile;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;
import org.mockito.Matchers;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import es.tid.cosmos.base.util.Logger;

/**
 * @author logc
 */
public class HadoopSshFileTest {
    private static final org.apache.log4j.Logger LOG =
            Logger.get(HadoopSshFile.class);
    private HadoopSshFile hadoopSshFile;
    private HadoopSshFile hadoopSshDir;
    private FileSystem hadoopFS;
    private FileSystem mockedFileSystem;
    private HadoopSshFile neverExists;

    @Before
    public void setUp() throws IOException, InterruptedException{
        Configuration configuration = new Configuration();
        String foodir = "/tmp/user01";
        String foofile = "/tmp/user01/file01";
        this.hadoopFS = FileSystem.get(
                URI.create(configuration.get("fs.default.name")),
                configuration, "user01");
        this.hadoopSshFile = new HadoopSshFile(foofile, "user01", this.hadoopFS);
        this.hadoopSshDir = new HadoopSshFile(foodir, "user01", this.hadoopFS);

        this.mockedFileSystem = mock(FileSystem.class);


        this.neverExists = new HadoopSshFile("/in/fantasy", "whatever_user",
                this.mockedFileSystem);
    }

    @After
    public void tearDown() throws IOException {
        if (this.hadoopSshFile.doesExist()) {
            this.hadoopSshFile.delete();
        }
        if (this.hadoopSshDir.doesExist()) {
            this.hadoopSshDir.delete();
        }
        this.hadoopFS.close();
        this.mockedFileSystem.close();
    }

    @Test
    public void testGetAbsolutePath() throws Exception {
        assertEquals("/tmp/user01/file01", this.hadoopSshFile.getAbsolutePath());
    }

    @Test
    public void testGetName() throws Exception {
        assertEquals("file01", this.hadoopSshFile.getName());
    }

    /**
     * Show that the owner is the logged in user as long as the file does not
     * exist. This complies with clients that request information about a path
     * before creating it.
     *
     * The owner is the username of the Java process after creation, since these
     * tests use the native filesystem underneath.
     *
     * @throws Exception
     */
    @Test
    public void testGetOwner() throws Exception {
        assertEquals("user01", this.hadoopSshFile.getOwner());
        assertFalse(this.hadoopSshFile.doesExist());
        this.hadoopSshFile.create();
        assertNotSame("user01", this.hadoopSshFile.getOwner());
    }

    @Test
    public void testIsDirectory() throws Exception {
        this.hadoopSshDir.mkdir();
        assertTrue(this.hadoopSshDir.isDirectory());
    }

    @Test
    public void testIsDirectoryDoesNotThrowException() throws Exception {
        assertFalse(this.hadoopSshDir.doesExist());
        assertFalse(this.hadoopSshDir.isDirectory());
    }

    @Test
    public void testDoesExist() throws Exception {
        assertFalse(this.hadoopSshFile.doesExist());
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.doesExist());
    }

    /**
     * Show that method doesExist does not send an exception when it encounters
     * an Exception
     */
    @Test
    public void testExceptionDoesExist() throws Exception {
        when(this.mockedFileSystem.exists(Matchers.<Path>any()))
                .thenThrow(new IOException("you have been mocked"));
        assertFalse(this.neverExists.doesExist());
    }

    @Test
    public void testIsReadable() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isReadable());
    }

    @Test
    public void testIsFile() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isFile());
    }

    /**
     * Show that when isFile finds an IOException, e.g. the file is not found,
     * the function returns false instead of re-throwing the exception.
     *
     * @throws Exception
     */
    @Test
    public void testIsFileDoesNotThrowException() throws Exception {
        when(this.mockedFileSystem.isFile(Matchers.<Path>any()))
                .thenThrow(new IOException("you have been mocked"));
        assertFalse(this.neverExists.isFile());
    }

    @Test
    public void testIsWritable() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isWritable());
    }

    /**
     * When a path does not yet exist, the write permission of its parent path
     * is returned.
     *
     * @throws Exception
     */
    @Test
    public void testIsWritableWhenNotYetExisting() throws Exception {
        this.hadoopSshDir.create();
        assertTrue(this.hadoopSshDir.isWritable());
        assertFalse(this.hadoopSshFile.doesExist());
        assertTrue(this.hadoopSshFile.isWritable());
    }

    @Test
    public void testIsExecutable() throws Exception {
        assertFalse(this.hadoopSshFile.isExecutable());
        assertFalse(this.hadoopSshDir.isExecutable());
    }

    @Test
    public void testIsRemovable() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isRemovable());
    }

    @Test
    public void testGetParentFile() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.getParentFile().toString()
                .startsWith("es.tid.cosmos.platform.injection" +
                        ".server.HadoopSshFile@"));
    }

    @Test
    public void testGetLastModified() throws Exception {
        assertEquals(0, this.hadoopSshFile.getLastModified());
    }

    @Test
    public void testSetLastModified() throws Exception {
        this.hadoopSshFile.create();
        long fixedTime = System.currentTimeMillis();
        this.hadoopSshFile.setLastModified(fixedTime);
        // This assertion is not assertEquals because there is a precision
        // mismatch between HDFS and System.currentTimeMillis; we try to
        // write with more decimal places than can be read. What we can say is
        // that this difference has an upper limit.
        assertTrue(fixedTime - this.hadoopSshFile.getLastModified() < 1000);
    }

    @Test
    public void testSetLastModifiedDoesNotThrowException() throws Exception {
        doThrow(new IOException("times could not be set"))
                .when(this.mockedFileSystem).setTimes(
                Matchers.<Path>any(),
                Matchers.anyLong(),
                Matchers.anyLong());
        assertFalse(this.neverExists.setLastModified(123L));
    }

    @Test<commit_msg>Improve tests on exec permission and modification<commit_after>package es.tid.cosmos.platform.injection.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.URI;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.AccessControlException;
import org.apache.sshd.server.SshFile;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;
import org.mockito.Matchers;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import es.tid.cosmos.base.util.Logger;

/**
 * @author logc
 */
public class HadoopSshFileTest {
    private static final org.apache.log4j.Logger LOG =
            Logger.get(HadoopSshFile.class);
    private HadoopSshFile hadoopSshFile;
    private HadoopSshFile hadoopSshDir;
    private FileSystem hadoopFS;
    private FileSystem mockedFileSystem;
    private HadoopSshFile neverExists;

    @Before
    public void setUp() throws IOException, InterruptedException{
        Configuration configuration = new Configuration();
        String foodir = "/tmp/user01";
        String foofile = "/tmp/user01/file01";
        this.hadoopFS = FileSystem.get(
                URI.create(configuration.get("fs.default.name")),
                configuration, "user01");
        this.hadoopSshFile = new HadoopSshFile(foofile, "user01", this.hadoopFS);
        this.hadoopSshDir = new HadoopSshFile(foodir, "user01", this.hadoopFS);

        this.mockedFileSystem = mock(FileSystem.class);


        this.neverExists = new HadoopSshFile("/in/fantasy", "whatever_user",
                this.mockedFileSystem);
    }

    @After
    public void tearDown() throws IOException {
        if (this.hadoopSshFile.doesExist()) {
            this.hadoopSshFile.delete();
        }
        if (this.hadoopSshDir.doesExist()) {
            this.hadoopSshDir.delete();
        }
        this.hadoopFS.close();
        this.mockedFileSystem.close();
    }

    @Test
    public void testGetAbsolutePath() throws Exception {
        assertEquals("/tmp/user01/file01", this.hadoopSshFile.getAbsolutePath());
    }

    @Test
    public void testGetName() throws Exception {
        assertEquals("file01", this.hadoopSshFile.getName());
    }

    /**
     * Show that the owner is the logged in user as long as the file does not
     * exist. This complies with clients that request information about a path
     * before creating it.
     *
     * The owner is the username of the Java process after creation, since these
     * tests use the native filesystem underneath.
     *
     * @throws Exception
     */
    @Test
    public void testGetOwner() throws Exception {
        assertEquals("user01", this.hadoopSshFile.getOwner());
        assertFalse(this.hadoopSshFile.doesExist());
        this.hadoopSshFile.create();
        assertNotSame("user01", this.hadoopSshFile.getOwner());
    }

    @Test
    public void testIsDirectory() throws Exception {
        this.hadoopSshDir.mkdir();
        assertTrue(this.hadoopSshDir.isDirectory());
    }

    @Test
    public void testIsDirectoryDoesNotThrowException() throws Exception {
        assertFalse(this.hadoopSshDir.doesExist());
        assertFalse(this.hadoopSshDir.isDirectory());
    }

    @Test
    public void testDoesExist() throws Exception {
        assertFalse(this.hadoopSshFile.doesExist());
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.doesExist());
    }

    /**
     * Show that method doesExist does not send an exception when it encounters
     * an Exception
     */
    @Test
    public void testExceptionDoesExist() throws Exception {
        when(this.mockedFileSystem.exists(Matchers.<Path>any()))
                .thenThrow(new IOException("you have been mocked"));
        assertFalse(this.neverExists.doesExist());
    }

    @Test
    public void testIsReadable() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isReadable());
    }

    @Test
    public void testIsFile() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isFile());
    }

    /**
     * Show that when isFile finds an IOException, e.g. the file is not found,
     * the function returns false instead of re-throwing the exception.
     *
     * @throws Exception
     */
    @Test
    public void testIsFileDoesNotThrowException() throws Exception {
        when(this.mockedFileSystem.isFile(Matchers.<Path>any()))
                .thenThrow(new IOException("you have been mocked"));
        assertFalse(this.neverExists.isFile());
    }

    @Test
    public void testIsWritable() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isWritable());
    }

    /**
     * When a path does not yet exist, the write permission of its parent path
     * is returned.
     *
     * @throws Exception
     */
    @Test
    public void testIsWritableWhenNotYetExisting() throws Exception {
        this.hadoopSshDir.create();
        assertTrue(this.hadoopSshDir.isWritable());
        assertFalse(this.hadoopSshFile.doesExist());
        assertTrue(this.hadoopSshFile.isWritable());
    }

    /**
     * Show that there is no sense of an executable permission in HDFS. Anything
     * is not executable before creation, and executable afterwards,
     * even directories.
     *
     * @throws Exception
     */
    @Test
    public void testIsExecutable() throws Exception {
        assertFalse(this.hadoopSshFile.isExecutable());
        this.hadoopSshDir.mkdir();
        assertTrue(this.hadoopSshDir.isExecutable());
    }

    @Test
    public void testIsRemovable() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.isRemovable());
    }

    @Test
    public void testGetParentFile() throws Exception {
        this.hadoopSshFile.create();
        assertTrue(this.hadoopSshFile.getParentFile().toString()
                .startsWith("es.tid.cosmos.platform.injection" +
                        ".server.HadoopSshFile@"));
    }

    @Test
    public void testGetLastModified() throws Exception {
        this.hadoopSshFile.create();
        assertNotSame(0, this.hadoopSshFile.getLastModified());
    }

    @Test
    public void testSetLastModified() throws Exception {
        this.hadoopSshFile.create();
        long fixedTime = System.currentTimeMillis();
        this.hadoopSshFile.setLastModified(fixedTime);
        // This assertion is not assertEquals because there is a precision
        // mismatch between HDFS and System.currentTimeMillis; we try to
        // write with more decimal places than can be read. What we can say is
        // that this difference has an upper limit.
        assertTrue(fixedTime - this.hadoopSshFile.getLastModified() < 1000);
    }

    @Test
    public void testSetLastModifiedDoesNotThrowException() throws Exception {
        doThrow(new IOException("times could not be set"))
                .when(this.mockedFileSystem).setTimes(
                Matchers.<Path>any(),
                Matchers.anyLong(),
                Matchers.anyLong());
        assertFalse(this.neverExists.setLastModified(123L));
    }

    @Test
{'fraction_non_alphanumeric': 0.1013240035672635, 'fraction_numerical': 0.004253275708307608, 'mean_word_length': 2.787477266822551, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5634226', 'n_tokens_mistral': 4162, 'n_tokens_neox': 4035, 'n_words': 932}