Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: SftpCheckDuplicateFileHandlingTestCase.java 24840 2012-08-27 16:20:58Z dirk.olmes $
   * --------------------------------------------------------------------------------------
   * 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.dataintegrity;
 
 
 
 import org.junit.Rule;
 import org.junit.Test;
 
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;

Test the three different types of handling when duplicate files (i.e. file names) are being transferred by SftpTransport. Available duplicate handling types are: - SftpConnector.PROPERTY_DUPLICATE_HANDLING_THROW_EXCEPTION = "throwException" - SftpConnectorPROPERTY_DUPLICATE_HANDLING_OVERWRITE = "overwrite" (currently not implemented) - SftpConnector.PROPERTY_DUPLICATE_HANDLING_ASS_SEQ_NO = "addSeqNo"
 
 {
     private static final String DUPLICATED_FILENAME = "file_1.txt";
 
     private static final String FILENAME = "file.txt";
 
     private static final String FILENAME_MESSAGE_PROPERTY = "filename";
 
     private static SftpClient sftpClient;
 
     @Rule
     public DynamicPort port = new DynamicPort("SFTP_PORT");
 
     private static SftpServer sftpServer;
 
     public SftpCheckDuplicateFileHandlingTestCase(ConfigVariant variantString configResources)
     {
         super(variantconfigResources);
     }
 
     private static final HashMap<StringStringMESSAGE_PROPERTIES = new HashMap<StringString>();
     {
     }
 
     private Prober prober = new PollingProber(2000, 100);
 
     private static MuleClient muleClient;
 
     @Parameters
     public static Collection<Object[]> parameters()
     {
         return Arrays.asList(new Object[][]{
             {."dataintegrity/sftp-dataintegrity-duplicate-handling-service.xml"},
             {."dataintegrity/sftp-dataintegrity-duplicate-handling-flow.xml"}});
     }

    
Try to transfer two files with the same name. The second file will be given a new name.
 
     @Test
     public void testDuplicateChangeNameHandling() throws Exception
     {
         String endpointUrl = "sftp://muletest1:muletest1@localhost:" + .getNumber() + "/~/inbound";
         .dispatch(endpointUrl);
         .check(new SftpFilePresentProbe("outbound"));
         .dispatch(endpointUrl);
         .check(new SftpFilePresentProbe("outbound"));
     }

    
Try to transfer two files with the same name. The second dispatch will throw and exception.
    @Test
    public void testDuplicateThrowExceptionHandling() throws Exception
    {
        String endpointUrl = "sftp://muletest1:muletest1@localhost:" + .getNumber() + "/~/inbound2";
        .dispatch(endpointUrl);
        .check(new SftpFilePresentProbe("outbound2"));
        final CountDownLatch latch = new CountDownLatch(1);
        .dispatch(endpointUrl);
        assertTrue(latch.await(3000, .));
    }

    
Returns a SftpClient that is logged in to the sftp server that the endpoint is configured against.
    protected SftpClient getSftpClient(String hostint portString userString password)
        throws IOException
    {
        SftpClient sftpClient = new SftpClient(host);
        sftpClient.setPort(port);
        try
        {
            sftpClient.login(userpassword);
        }
        catch (Exception e)
        {
            fail("Login failed: " + e);
        }
        return sftpClient;
    }
    @Before
    public void before() throws MuleExceptionIOException
    {
         = new SftpServer(.getNumber());
        .start();
         = new MuleClient();
         = getSftpClient("localhost".getNumber(), "muletest1""muletest1");
        .mkdir("inbound");
        .mkdir("outbound");
        .mkdir("inbound2");
        .mkdir("outbound2");
    }
    @After
    public void after() throws IOException
    {
        .recursivelyDeleteDirectory("outbound");
        .recursivelyDeleteDirectory("inbound");
        .recursivelyDeleteDirectory("outbound2");
        .recursivelyDeleteDirectory("inbound2");
        .stop();
    }
New to GrepCode? Check out our FAQ X