PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/camel-itest/src/test/java/org/apache/camel/itest/ftp/FtpInitialConnectTimeoutTest.java

https://gitlab.com/matticala/apache-camel
Java | 171 lines | 123 code | 23 blank | 25 comment | 6 complexity | bacd6ec6be2225bb91aa3adc66cfd1b6 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.itest.ftp;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.Socket;
  21. import java.net.SocketException;
  22. import java.net.SocketTimeoutException;
  23. import java.util.concurrent.atomic.AtomicBoolean;
  24. import javax.net.SocketFactory;
  25. import org.apache.camel.builder.RouteBuilder;
  26. import org.apache.camel.impl.JndiRegistry;
  27. import org.apache.camel.test.junit4.CamelTestSupport;
  28. import org.apache.commons.net.ftp.FTPClient;
  29. import org.junit.After;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import org.mockftpserver.fake.FakeFtpServer;
  33. import org.mockito.invocation.InvocationOnMock;
  34. import org.mockito.stubbing.Answer;
  35. import static org.mockito.ArgumentMatchers.anyInt;
  36. import static org.mockito.Mockito.doAnswer;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.spy;
  39. import static org.mockito.Mockito.when;
  40. public class FtpInitialConnectTimeoutTest extends CamelTestSupport {
  41. private static final int CONNECT_TIMEOUT = 11223;
  42. /**
  43. * Create the answer for the socket factory that causes a SocketTimeoutException to occur in connect.
  44. */
  45. private static class SocketAnswer implements Answer<Socket> {
  46. @Override
  47. public Socket answer(InvocationOnMock invocation) throws Throwable {
  48. final Socket socket = spy(new Socket());
  49. final AtomicBoolean timeout = new AtomicBoolean();
  50. try {
  51. doAnswer(new Answer<InputStream>() {
  52. @Override
  53. public InputStream answer(InvocationOnMock invocation) throws Throwable {
  54. final InputStream stream = (InputStream) invocation.callRealMethod();
  55. InputStream inputStream = new InputStream() {
  56. @Override
  57. public int read() throws IOException {
  58. if (timeout.get()) {
  59. // emulate a timeout occurring in _getReply()
  60. throw new SocketTimeoutException();
  61. }
  62. return stream.read();
  63. }
  64. };
  65. return inputStream;
  66. }
  67. }).when(socket).getInputStream();
  68. } catch (IOException ignored) {
  69. }
  70. try {
  71. doAnswer(new Answer<Object>() {
  72. @Override
  73. public Object answer(InvocationOnMock invocation) throws Throwable {
  74. if ((Integer) invocation.getArguments()[0] == CONNECT_TIMEOUT) {
  75. // setting of connect timeout
  76. timeout.set(true);
  77. } else {
  78. // non-connect timeout
  79. timeout.set(false);
  80. }
  81. return invocation.callRealMethod();
  82. }
  83. }).when(socket).setSoTimeout(anyInt());
  84. } catch (SocketException e) {
  85. throw new RuntimeException(e);
  86. }
  87. return socket;
  88. }
  89. }
  90. private FakeFtpServer fakeFtpServer;
  91. @Override
  92. @Before
  93. public void setUp() throws Exception {
  94. fakeFtpServer = new FakeFtpServer();
  95. fakeFtpServer.setServerControlPort(0);
  96. fakeFtpServer.start();
  97. super.setUp();
  98. }
  99. @Override
  100. @After
  101. public void tearDown() throws Exception {
  102. super.tearDown();
  103. if (fakeFtpServer != null) {
  104. fakeFtpServer.stop();
  105. }
  106. }
  107. private FTPClient mockedClient() throws IOException {
  108. FTPClient client = new FTPClient();
  109. client.setSocketFactory(createSocketFactory());
  110. return client;
  111. }
  112. private SocketFactory createSocketFactory() throws IOException {
  113. SocketFactory socketFactory = mock(SocketFactory.class);
  114. when(socketFactory.createSocket()).thenAnswer(new SocketAnswer());
  115. return socketFactory;
  116. }
  117. @Override
  118. protected JndiRegistry createRegistry() throws Exception {
  119. JndiRegistry registry = super.createRegistry();
  120. registry.bind("mocked", mockedClient());
  121. return registry;
  122. }
  123. @Test
  124. public void testReConnect() throws Exception {
  125. // we should fail, but we are testing that we are not in a deadlock which could potentially happen
  126. getMockEndpoint("mock:done").expectedMessageCount(0);
  127. getMockEndpoint("mock:dead").expectedMessageCount(1);
  128. sendBody("direct:start", "test");
  129. assertMockEndpointsSatisfied();
  130. }
  131. @Override
  132. protected RouteBuilder createRouteBuilder() throws Exception {
  133. return new RouteBuilder() {
  134. @Override
  135. public void configure() throws Exception {
  136. errorHandler(deadLetterChannel("mock:dead"));
  137. // using soTimeout=0 could potentially cause the ftp producer to dead-lock doing endless reconnection attempts
  138. // this is a test to ensure we have fixed that
  139. from("direct:start")
  140. .to("ftp://localhost:" + fakeFtpServer.getServerControlPort()
  141. + "?ftpClient=#mocked"
  142. + "&soTimeout=0&"
  143. + "connectTimeout=" + CONNECT_TIMEOUT)
  144. .to("mock:done");
  145. }
  146. };
  147. }
  148. }