'Specifying the runtime user for a JUnit run configuration in eclipse

I'm in the process of writing some tests for a java application. One of the functions I need to test involves copying some files from a remote FTP to a local directory. However, when I try to actually run my test, I'm running into problems with the file permissions. Whatever the default user is that eclipse uses when running the test does not have permission to write files to the target directory.

I could modify the test so it just writes the files into a temp directory, but I would like to avoid that. Part of what I'm testing is the setup of the directory structure, so I want to keep the test as close to the real thing as possible.

Is there some way to tell eclipse to run a test as a specific user rather than using the default? If not, is there some other way to handle this that I'm not seeing?

For reference the method under test looks like this:

public static void copyEyeRepOrders(String localDirectory, String remoteDirectory, LocalDateTime startTime, String server, Integer port, String username, String password) throws SocketException, IOException
     {
         File localDir = new File(localDirectory);
         if( !localDir.exists())
         {
             localDir.mkdirs();
         }
         
         FTPClient ftpClient = getFtpClient();
        
        ftpClient.connect(server, port);
        ftpClient.enterLocalPassiveMode();
        ftpClient.login(username, password);
            
            
        FTPFile[] files = ftpClient.listFiles(remoteDirectory, new DateFileFilter(startTime) );
            
        if(files != null)
        {
            log.info("Found " + files.length + " to tranfer from ftp.");
            for(int i = 0; i < files.length; i++)
            {
                log.debug("transferring " + i +" of " + files.length + " to local");
                    FTPFile f = files[i];
                    if(f != null && f.isFile())
                    {
                        String filePath = remoteDirectory + "/" + f.getName();                              
                        FileOutputStream out = new FileOutputStream(localDirectory + "/" + f.getName());
                        ftpClient.retrieveFile(filePath, out);
                        out.close();
                    }
            }
        }
            
        ftpClient.disconnect();     
        
     }

The junit test looks like:

    @Test
    public void testCopyEyeRepOrders() throws IOException, InterruptedException
    {
                
        String[] filePaths = {"ORDERS_R1028611.txt", "ORDERS_R1031233.txt", "ORDERS_R1055249.txt" };    

        assertNotNull(fakeFtp);
        //setup the mock filesystem
        for(int i = 0; i < filePaths.length; i++)
        {
                Functions.addFileToSystem(-20, TURA_RESOURCES,filePaths[i], "/tura/" + ftpDirectory, fakeFtp.getFileSystem());                  
        }
                
        LocalDateTime startTime = LocalDateTime.now();
        
        log.debug("Start time for testCopyEyeRepOrders was " + startTime);
        //we want to check that only files created before the job started are included.
        //to do that we create a newer file that we expect to be excluded.
        Functions.addFileToSystem(10, TURA_RESOURCES, "ORDERS_R1053926.txt", "/tura/" + ftpDirectory, fakeFtp.getFileSystem());
                
        ExportDataApi.copyEyeRepOrders(newOrdersDirTura, ftpDirectory, startTime, ftpServerName, fakeFtp.getServerControlPort(), ftpUsername, ftpPassword);
        
        assertNotNull(newOrdersDirTura);
        File localDir = new File(newOrdersDirTura);             
        File[] files = localDir.listFiles();
        assertNotNull(files);
            
        assertEquals(filePaths.length, files.length);
            
        List<String> expectedFileNames = Arrays.asList(filePaths);
            
        for(File f : files)
        {
            assertThat(expectedFileNames, hasItem(f.getName()));                                
        }           
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source