Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. // compile("commons-net:commons-net:3.5")
  2. // testCompile("org.mockftpserver:MockFtpServer:2.7.1")
  3.  
  4. import org.apache.commons.net.ftp.FTPClient;
  5. import org.junit.Before;
  6. import org.junit.Rule;
  7. import org.junit.Test;
  8. import org.junit.rules.TemporaryFolder;
  9.  
  10. import java.io.IOException;
  11.  
  12. import static org.mockito.Matchers.any;
  13. import static org.mockito.Mockito.mock;
  14. import static org.mockito.Mockito.verify;
  15. import static org.mockito.Mockito.when;
  16. import static org.mockito.internal.verification.VerificationModeFactory.times;
  17.  
  18. public class VibesClientTest {
  19. @Rule
  20. public TemporaryFolder folder = new TemporaryFolder();
  21.  
  22. private VibesClient vibesClient;
  23. private VibesCredential vibesCredential;
  24. private FTPClient ftpClient;
  25.  
  26. @Before
  27. public void setUp() throws Exception {
  28. vibesCredential = mock(VibesCredential.class);
  29. ftpClient = mock(FTPClient.class);
  30. vibesClient = new VibesClient(vibesCredential, ftpClient);
  31. }
  32.  
  33. @Test(expected = VibesException.class)
  34. public void shouldThrowExceptionWhenFailedToStore() throws Exception {
  35. when(ftpClient.login(any(), any())).thenReturn(true);
  36. when(ftpClient.storeFileStream(any())).thenThrow(new IOException());
  37.  
  38. vibesClient.sendFile(folder.newFile("testFile"));
  39.  
  40. verify(ftpClient, times(1)).storeFile(any(), any());
  41. verify(ftpClient, times(1)).login(any(), any());
  42. verify(ftpClient, times(1)).logout();
  43. }
  44. }
  45.  
  46. import org.apache.commons.net.ftp.FTP;
  47. import org.apache.commons.net.ftp.FTPClient;
  48. import org.slf4j.Logger;
  49. import org.slf4j.LoggerFactory;
  50.  
  51. import java.io.File;
  52. import java.io.FileInputStream;
  53. import java.io.IOException;
  54. import java.io.InputStream;
  55. import java.io.OutputStream;
  56. import java.time.Duration;
  57. import java.time.LocalDateTime;
  58.  
  59. import static java.lang.String.format;
  60.  
  61. public class VibesClient {
  62. private static final Logger LOGGER = LoggerFactory.getLogger(VibesClient.class);
  63. private static final int ONE_KILOBYTE = 1024;
  64. private static final int ONE_MEGABYTE = 1024*ONE_KILOBYTE;
  65. private LocalDateTime lastLogTime = LocalDateTime.now();
  66.  
  67. private String hostname;
  68. private String username;
  69. private String password;
  70. private int port;
  71. private FTPClient client = new FTPClient();
  72.  
  73. public VibesClient (VibesCredential credential){
  74. this.hostname = credential.getHostname();
  75. this.username = credential.getUsername();
  76. this.password = credential.getPassword();
  77. this.port = credential.getPort();
  78. }
  79.  
  80. public VibesClient(VibesCredential credential, FTPClient client){
  81. this(credential);
  82. this.client = client;
  83. }
  84.  
  85. public void sendFile(File file) throws IOException {
  86. LOGGER.info(format("Sending file %s.", file.getName()));
  87.  
  88. try {
  89. login();
  90. storeFile(file);
  91. } catch (IOException e) {
  92. String errorMessage = format("Error happened on uploading for %s: %s.", username, client.getReplyString());
  93. LOGGER.error(errorMessage);
  94. throw new VibesException(errorMessage);
  95. } finally {
  96. client.logout();
  97. }
  98. }
  99.  
  100. private void storeFile(File file) throws IOException {
  101. LOGGER.info(String.format("Started sending file %.4f MB.", (file.length() / (float) ONE_MEGABYTE)));
  102. LocalDateTime startTime = LocalDateTime.now();
  103. byte[] bytesIn = new byte[ONE_KILOBYTE];
  104. client.setBufferSize(ONE_MEGABYTE);
  105. client.setFileType(FTP.ASCII_FILE_TYPE);
  106. int read;
  107.  
  108. InputStream inputStream = new FileInputStream(file);
  109. OutputStream outputStream = client.storeFileStream(file.getName());
  110.  
  111. while((read = inputStream.read(bytesIn)) != -1) {
  112. outputStream.write(bytesIn, 0, read);
  113. timeTrackingLog(LocalDateTime.now());
  114. }
  115.  
  116. inputStream.close();
  117. outputStream.close();
  118.  
  119. LOGGER.info(String.format("File sent, time elapsed %d seconds.",
  120. Duration.between(startTime, LocalDateTime.now()).getSeconds()));
  121. LOGGER.info(String.format("Server reply: %s.", client.getReplyString()));
  122. }
  123.  
  124. private void timeTrackingLog(LocalDateTime currentTime) {
  125. long seconds = Duration.between(lastLogTime, currentTime).getSeconds();
  126.  
  127. if(seconds > 30){
  128. LOGGER.info("Still sending file...");
  129. lastLogTime = currentTime;
  130. }
  131. }
  132.  
  133. private void login() throws IOException {
  134. client.enterLocalPassiveMode();
  135. client.enterRemotePassiveMode();
  136.  
  137. boolean loggedIn = client.login(username, password);
  138. String serverReply = client.getReplyString();
  139.  
  140. if (!loggedIn){
  141. LOGGER.error(format("Error happened while logging in to %s: %s.", username, serverReply));
  142. throw new VibesException(format("%s: %s.", username, serverReply));
  143. } else {
  144. LOGGER.info(format("Logged in successfully: %s.", serverReply));
  145. }
  146. }
  147.  
  148. public void connect() throws IOException {
  149. LOGGER.info(format("Connecting to %s with %s as username.", hostname, username));
  150. if (port == 0){
  151. client.connect(hostname);
  152. } else {
  153. client.connect(hostname,port);
  154. }
  155. LOGGER.info(format("Server reply: %s.", client.getReplyString()));
  156. }
  157.  
  158. public void disconnect() throws IOException {
  159. client.disconnect();
  160. LOGGER.info(format("Closing connection with %s.", hostname));
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement