Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: SftpArchiveFunctionalTestCase.java 23812 2012-02-03 13:17:37Z asequeira $
   * --------------------------------------------------------------------------------------
   * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
   *
   * The software in this package is published under the terms of the CPAL v1.0
   * license, a copy of which has been included with this distribution in the
   * LICENSE.txt file.
   */
 
 package org.mule.transport.sftp;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.File;
 
 
 import org.junit.Test;
Test the archive features.
 
 {
 
     private static final long TIMEOUT = 15000;
 
     private static final String FILE1_TXT = "file1.txt";
     private static final String FILE2_TXT = "file2.txt";
     private static final String FILE3_TXT = "file3.txt";
     private static final String FILE4_TXT = "file4.txt";
 
     private static final String FILE2_TMP_REGEXP = "file2_.+";
     private static final String FILE3_TMP_REGEXP = "file3_.+";
 
     private static final String TMP_SENDING = "tmp_sending";
     private static final String TMP_RECEIVING = "tmp_receiving";
 
     private static final String INBOUND_ENDPOINT1 = "inboundEndpoint1";
     private static final String INBOUND_ENDPOINT2 = "inboundEndpoint2";
     private static final String INBOUND_ENDPOINT3 = "inboundEndpoint3";
     private static final String INBOUND_ENDPOINT4 = "inboundEndpoint4";
 
     // Size of the generated stream - 2 Mb
     final static int SEND_SIZE = 1024 * 1024 * 2;
 
     private String archive = null;
     private String archiveCanonicalPath = null;
 
     public SftpArchiveFunctionalTestCase(ConfigVariant variantString configResources)
     {
         super(variantconfigResources);
     }
 
     @Parameters
     public static Collection<Object[]> parameters()
     {
         return Arrays.asList(new Object[][]{
             {."mule-sftp-archive-test-config-service.xml"},
             {."mule-sftp-archive-test-config-flow.xml"}});
     }
 
     @Override
     protected void doSetUp() throws Exception
     {
         super.doSetUp();
 
         // this block moved from the constructor to allow this test to be skipped if
         // the resource isn't found via
         // AbstractMuleTestCase.isDisabledInThisEnvironment
         ResourceBundle rb = ResourceBundle.getBundle("sftp-settings");
          = rb.getString("ARCHIVE");
          = new File().getCanonicalPath();
 
         initEndpointDirectories(new String[]{"receiving1""receiving2""receiving3""receiving4"},
     }

    
Test plain archive functionality with no extra features enabled
 
     @Test
     public void testArchive1() throws Exception
     {
         executeBaseTest("vm://test.upload1""receiving1");
 
         // Assert that the file now exists in the archive
 
        // And that the file is gone from the inbound endpoint
    }

    
Test archive functionality with full usage of temp-dir and creation of unique names of temp-files
    @Test
    public void testArchive2() throws Exception
    {
        executeBaseTest("vm://test.upload2""receiving2");
        // Assert that the file now exists in the archive
        // (with some unknown timestamp in the filename)
        // and that the tmp-archive-folders are empty
        // Assert that the file is gone from the inbound endpoint (including its
        // tmp-folders)
        // Note that directories are not returned in this listing
        MuleClient mc = new MuleClient();
    }

    
Test archive functionality with usage of temp-dir for inbound and outbound endpoints with creation of unique names of temp-files but not for the archive
    @Test
    public void testArchive3() throws Exception
    {
        executeBaseTest("vm://test.upload3""receiving3");
        // Assert that the file now exists in the archive
        // (with some unknown timestamp in the filename)
        // Assert that the file is gone from the inbound endpoint (including its
        // tmp-folders)
        // Note that directories are not returned in this listing
        MuleClient mc = new MuleClient();
    }

    
Test archive functionality with usage of temp-dir for archive but not for inbound and outbound endpoints
    @Test
    public void testArchive4() throws Exception
    {
        executeBaseTest("vm://test.upload4""receiving4");
        // Assert that the file now exists in the archive
        // and that the tmp-archive-folders are empty
        // Assert that the file is gone from the inbound endpoint
        MuleClient mc = new MuleClient();
    }

    
Test error handling with plain archive functionality with no extra features enabled
    @Test
    public void testCantWriteToArchive1() throws Exception
    {
        makeArchiveReadOnly();
        try
        {
            executeBaseTest("vm://test.upload1""receiving1",
                "sftp");
            fail("Expected error");
        }
        catch (Exception e)
        {
            assertNotNull(e);
            assertTrue(e instanceof IOException);
            assertEquals("Destination folder is not writeable: " + e.getMessage());
        }
        // Assert that file still exists in the inbound endpoint after the failure
        // Assert that no files exists in the archive after the error
    }

    
Test error handling with archive functionality with full usage of temp-dir and creation of unique names of temp-files
    @Test
    public void testCantWriteToArchive2() throws Exception
    {
        fail();
        makeArchiveReadOnly();
        try
        {
            executeBaseTest("vm://test.upload2""receiving2",
                "sftp");
            fail("Expected error");
        }
        catch (Exception e)
        {
            assertNotNull(e);
            assertTrue(e instanceof IOException);
            assertEquals("Destination folder is not writeable: " +  + .
                         + e.getMessage());
        }
        // Assert that the file still exists in the inbound endpoint's tmp-folder
        // after the failure
        // (with some unknown timestamp in the filename)
        // Note that directories are not returned in this listing
        MuleClient mc = new MuleClient();
        // Assert that no files exists in the archive after the error except from the
        // temp-folders
    }

    
Test error handling with archive functionality with usage of temp-dir for inbound and outbound endpoints with creation of unique names of temp-files but not for the archive
    @Test
    public void testCantWriteToArchive3() throws Exception
    {
        makeArchiveReadOnly();
        try
        {
            executeBaseTest("vm://test.upload3""receiving3",
                "sftp");
            fail("Expected error");
        }
        catch (Exception e)
        {
            assertNotNull(e);
            assertTrue(e instanceof IOException);
            assertEquals("Destination folder is not writeable: " + e.getMessage());
        }
        // Assert that the file still exists in the inbound endpoint's tmp-folder
        // after the failure
        // (with some unknown timestamp in the filename)
        // Note that directories are not returned in this listing
        MuleClient mc = new MuleClient();
        // Assert that no files exists in the archive after the error
    }

    
Test error handling with archive functionality with usage of temp-dir for archive but not for inbound and outbound endpoints
    @Test
    public void testCantWriteToArchive4() throws Exception
    {
        makeArchiveReadOnly();
        try
        {
            executeBaseTest("vm://test.upload4""receiving4",
                "sftp");
            fail("Expected error");
        }
        catch (Exception e)
        {
            assertNotNull(e);
            assertTrue(e instanceof IOException);
            assertEquals("Destination folder is not writeable: " +  + .
                         + e.getMessage());
        }
        // Assert that file still exists in the inbound endpoint after the failure
        // Assert that no files exists in the archive after the error except from the
        // temp-folders
    }
    private void makeArchiveReadOnly() throws IOException
    {
        makeFolderReadOnly();
    }
    private void makeArchiveTmpFolderReadOnly() throws IOException
    {
    }
    private void makeFolderReadOnly(String folderNamethrows IOException
    {
        File folder = new File(folderName);
        if (!folder.exists())
        {
            if (!folder.mkdirs())
            {
                throw new IOException("Failed to create folder: " + folderName);
            }
        }
        if (!folder.setReadOnly())
        {
            throw new IOException("Failed to make folder readonly: " + folderName);
        }
    }
New to GrepCode? Check out our FAQ X