View Javadoc

1   /*
2    * $Id: SftpInvalidInboundEndpointTestCase.java 22475 2011-07-20 14:30:04Z justin.calleja $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.sftp.dataintegrity;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.fail;
16  
17  import java.io.IOException;
18  import java.util.Arrays;
19  import java.util.Collection;
20  
21  import org.junit.Test;
22  import org.junit.runners.Parameterized.Parameters;
23  import org.mule.api.endpoint.EndpointBuilder;
24  import org.mule.api.endpoint.InboundEndpoint;
25  import org.mule.api.registry.MuleRegistry;
26  import org.mule.transport.sftp.SftpConnector;
27  
28  public class SftpInvalidInboundEndpointTestCase extends AbstractSftpDataIntegrityTestCase
29  {
30      private static final int NO_OF_INVALID_ATTEMPTS = 50;
31  
32  
33      public SftpInvalidInboundEndpointTestCase(ConfigVariant variant, String configResources)
34      {
35          super(variant, configResources);
36      }
37      
38      @Parameters
39      public static Collection<Object[]> parameters()
40      {
41          return Arrays.asList(new Object[][]{
42              {ConfigVariant.SERVICE, "dataintegrity/sftp-invalid-inbound-endpoint-config.xml"}            
43          });
44      }
45  
46      @Test
47      public void testInvalidInboundEndpoint() throws Exception
48      {
49  
50          String expectedStartOfErrorMessage = "Error 'No such file' occurred when trying to CDW";
51  
52          MuleRegistry registry = muleContext.getRegistry();
53  
54          SftpConnector c = (SftpConnector) registry.lookupConnector("sftp");
55          assertNotNull(c);
56  
57          EndpointBuilder epb = registry.lookupEndpointBuilder("InvalidEndpoint");
58          InboundEndpoint ep = epb.buildInboundEndpoint();
59  
60          // Verify that failed creations of sftp-clients don't leak resources (e.g.
61          // ssh-servers)
62          // In v2.2.1-RC2 this tests fails after 132 attempts on a Mac OSX 10.6
63          // machine
64          for (int i = 0; i < NO_OF_INVALID_ATTEMPTS; i++)
65          {
66              if (logger.isDebugEnabled())
67                  logger.debug("CreateSftpClient invalid atempt #" + i + " of " + NO_OF_INVALID_ATTEMPTS);
68              try
69              {
70                  c.createSftpClient(ep);
71                  fail("Should have received an exception here!!!");
72              }
73              catch (IOException ioe)
74              {
75                  String actualStartOfErrorMessage = ioe.getMessage().substring(0,
76                      expectedStartOfErrorMessage.length());
77                  assertEquals(expectedStartOfErrorMessage, actualStartOfErrorMessage);
78              }
79          }
80      }
81  }