(S)FTP Lambdas
AMQP Lambdas Event Lambdas Feed/File Lambdas Gemfile/Groovy Lambdas * Ensure that JavaDocs are checked via ` check.dependsOn javadoc` * Add `@return` tag to the `MessageBuilder.readOnlyHeaders()`
This commit is contained in:
committed by
Artem Bilan
parent
3402a86fb5
commit
e5ae7d886d
@@ -80,33 +80,28 @@ public class FtpRemoteFileTemplate extends RemoteFileTemplate<FTPFile> {
|
||||
*/
|
||||
@Override
|
||||
public boolean exists(final String path) {
|
||||
return doExecuteWithClient(new ClientCallback<FTPClient, Boolean>() {
|
||||
return doExecuteWithClient(client -> {
|
||||
try {
|
||||
switch (FtpRemoteFileTemplate.this.existsMode) {
|
||||
|
||||
@Override
|
||||
public Boolean doWithClient(FTPClient client) {
|
||||
try {
|
||||
switch (FtpRemoteFileTemplate.this.existsMode) {
|
||||
case STAT:
|
||||
return client.getStatus(path) != null;
|
||||
|
||||
case STAT:
|
||||
return client.getStatus(path) != null;
|
||||
case NLST:
|
||||
String[] names = client.listNames(path);
|
||||
return !ObjectUtils.isEmpty(names);
|
||||
|
||||
case NLST:
|
||||
String[] names = client.listNames(path);
|
||||
return !ObjectUtils.isEmpty(names);
|
||||
case NLST_AND_DIRS:
|
||||
return getSession().exists(path);
|
||||
|
||||
case NLST_AND_DIRS:
|
||||
return getSession().exists(path);
|
||||
|
||||
default:
|
||||
throw new IllegalStateException("Unsupported 'existsMode': " +
|
||||
FtpRemoteFileTemplate.this.existsMode);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessagingException("Failed to check the remote path for " + path, e);
|
||||
default:
|
||||
throw new IllegalStateException("Unsupported 'existsMode': " +
|
||||
FtpRemoteFileTemplate.this.existsMode);
|
||||
}
|
||||
}
|
||||
|
||||
catch (IOException e) {
|
||||
throw new MessagingException("Failed to check the remote path for " + path, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,6 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.ReflectionUtils.MethodCallback;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -111,16 +110,10 @@ public class FtpInboundChannelAdapterParserTests {
|
||||
FileListFilter<?> acceptAllFilter = context.getBean("acceptAllFilter", FileListFilter.class);
|
||||
assertTrue(TestUtils.getPropertyValue(inbound, "fileSource.scanner.filter.fileFilters", Collection.class).contains(acceptAllFilter));
|
||||
final AtomicReference<Method> genMethod = new AtomicReference<Method>();
|
||||
ReflectionUtils.doWithMethods(AbstractInboundFileSynchronizer.class, new MethodCallback() {
|
||||
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
if ("generateLocalFileName".equals(method.getName())) {
|
||||
method.setAccessible(true);
|
||||
genMethod.set(method);
|
||||
}
|
||||
}
|
||||
});
|
||||
ReflectionUtils.doWithMethods(AbstractInboundFileSynchronizer.class, method -> {
|
||||
method.setAccessible(true);
|
||||
genMethod.set(method);
|
||||
}, method -> "generateLocalFileName".equals(method.getName()));
|
||||
assertEquals("FOO.afoo", genMethod.get().invoke(fisync, "foo"));
|
||||
assertEquals(42, inbound.getMaxFetchSize());
|
||||
}
|
||||
|
||||
@@ -136,16 +136,10 @@ public class FtpOutboundGatewayParserTests {
|
||||
//INT-3129
|
||||
assertNotNull(TestUtils.getPropertyValue(gateway, "localFilenameGeneratorExpression"));
|
||||
final AtomicReference<Method> genMethod = new AtomicReference<Method>();
|
||||
ReflectionUtils.doWithMethods(FtpOutboundGateway.class, new ReflectionUtils.MethodCallback() {
|
||||
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
if ("generateLocalFileName".equals(method.getName())) {
|
||||
method.setAccessible(true);
|
||||
genMethod.set(method);
|
||||
}
|
||||
}
|
||||
});
|
||||
ReflectionUtils.doWithMethods(FtpOutboundGateway.class, method -> {
|
||||
method.setAccessible(true);
|
||||
genMethod.set(method);
|
||||
}, method -> "generateLocalFileName".equals(method.getName()));
|
||||
assertEquals("FOO.afoo", genMethod.get().invoke(gateway, new GenericMessage<String>(""), "foo"));
|
||||
assertThat(TestUtils.getPropertyValue(gateway, "mputFilter"), Matchers.instanceOf(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
@@ -52,7 +52,6 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.file.FileNameGenerator;
|
||||
import org.springframework.integration.file.remote.FileInfo;
|
||||
import org.springframework.integration.file.remote.RemoteFileTemplate;
|
||||
import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler;
|
||||
@@ -95,12 +94,7 @@ public class FtpOutboundTests {
|
||||
assertFalse(file.exists());
|
||||
FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handler.setFileNameGenerator(new FileNameGenerator() {
|
||||
@Override
|
||||
public String generateFileName(Message<?> message) {
|
||||
return "handlerContent.test";
|
||||
}
|
||||
});
|
||||
handler.setFileNameGenerator(message -> "handlerContent.test");
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage<String>("String data"));
|
||||
@@ -119,12 +113,7 @@ public class FtpOutboundTests {
|
||||
assertFalse(file.exists());
|
||||
FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handler.setFileNameGenerator(new FileNameGenerator() {
|
||||
@Override
|
||||
public String generateFileName(Message<?> message) {
|
||||
return "handlerContent.test";
|
||||
}
|
||||
});
|
||||
handler.setFileNameGenerator(message -> "handlerContent.test");
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage<byte[]>("byte[] data".getBytes()));
|
||||
@@ -141,12 +130,7 @@ public class FtpOutboundTests {
|
||||
|
||||
FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
|
||||
handler.setFileNameGenerator(new FileNameGenerator() {
|
||||
@Override
|
||||
public String generateFileName(Message<?> message) {
|
||||
return ((File) message.getPayload()).getName() + ".test";
|
||||
}
|
||||
});
|
||||
handler.setFileNameGenerator(message -> ((File) message.getPayload()).getName() + ".test");
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
@@ -167,12 +151,7 @@ public class FtpOutboundTests {
|
||||
|
||||
FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
|
||||
handler.setFileNameGenerator(new FileNameGenerator() {
|
||||
@Override
|
||||
public String generateFileName(Message<?> message) {
|
||||
return ((File) message.getPayload()).getName() + ".test";
|
||||
}
|
||||
});
|
||||
handler.setFileNameGenerator(message -> ((File) message.getPayload()).getName() + ".test");
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
@@ -181,14 +160,10 @@ public class FtpOutboundTests {
|
||||
Log logger = spy(TestUtils.getPropertyValue(handler, "remoteFileTemplate.logger", Log.class));
|
||||
when(logger.isWarnEnabled()).thenReturn(true);
|
||||
final AtomicReference<String> logged = new AtomicReference<String>();
|
||||
doAnswer(new Answer<Object>() {
|
||||
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
logged.set((String) invocation.getArguments()[0]);
|
||||
invocation.callRealMethod();
|
||||
return null;
|
||||
}
|
||||
doAnswer(invocation -> {
|
||||
logged.set((String) invocation.getArguments()[0]);
|
||||
invocation.callRealMethod();
|
||||
return null;
|
||||
}).when(logger).warn(Mockito.anyString());
|
||||
RemoteFileTemplate<?> template = TestUtils.getPropertyValue(handler, "remoteFileTemplate",
|
||||
RemoteFileTemplate.class);
|
||||
|
||||
@@ -42,7 +42,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -55,8 +54,6 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -70,7 +67,6 @@ import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.file.remote.InputStreamCallback;
|
||||
import org.springframework.integration.file.remote.MessageSessionCallback;
|
||||
import org.springframework.integration.file.remote.RemoteFileTemplate;
|
||||
import org.springframework.integration.file.remote.SessionCallback;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.ftp.FtpTestSupport;
|
||||
@@ -304,23 +300,13 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
template.setBeanFactory(mock(BeanFactory.class));
|
||||
template.afterPropertiesSet();
|
||||
final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
|
||||
assertTrue(template.get(new GenericMessage<String>("ftpSource/ ftpSource1.txt"), new InputStreamCallback() {
|
||||
|
||||
@Override
|
||||
public void doWithInputStream(InputStream stream) throws IOException {
|
||||
FileCopyUtils.copy(stream, baos1);
|
||||
}
|
||||
}));
|
||||
assertTrue(template.get(new GenericMessage<String>("ftpSource/ ftpSource1.txt"),
|
||||
(InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos1)));
|
||||
assertEquals("source1", new String(baos1.toByteArray()));
|
||||
|
||||
final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
|
||||
assertTrue(template.get(new GenericMessage<String>("ftpSource/ftpSource2.txt"), new InputStreamCallback() {
|
||||
|
||||
@Override
|
||||
public void doWithInputStream(InputStream stream) throws IOException {
|
||||
FileCopyUtils.copy(stream, baos2);
|
||||
}
|
||||
}));
|
||||
assertTrue(template.get(new GenericMessage<String>("ftpSource/ftpSource2.txt"),
|
||||
(InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos2)));
|
||||
assertEquals("source2", new String(baos2.toByteArray()));
|
||||
}
|
||||
|
||||
@@ -438,18 +424,14 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
@Test
|
||||
public void testMgetPartial() throws Exception {
|
||||
Session<FTPFile> session = spyOnSession();
|
||||
doAnswer(new Answer<FTPFile[]>() {
|
||||
|
||||
@Override
|
||||
public FTPFile[] answer(InvocationOnMock invocation) throws Throwable {
|
||||
FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
|
||||
// add an extra file where the get will fail
|
||||
files = Arrays.copyOf(files, files.length + 1);
|
||||
FTPFile bogusFile = new FTPFile();
|
||||
bogusFile.setName("bogus.txt");
|
||||
files[files.length - 1] = bogusFile;
|
||||
return files;
|
||||
}
|
||||
doAnswer(invocation -> {
|
||||
FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
|
||||
// add an extra file where the get will fail
|
||||
files = Arrays.copyOf(files, files.length + 1);
|
||||
FTPFile bogusFile = new FTPFile();
|
||||
bogusFile.setName("bogus.txt");
|
||||
files[files.length - 1] = bogusFile;
|
||||
return files;
|
||||
}).when(session).list("ftpSource/subFtpSource/*");
|
||||
String dir = "ftpSource/subFtpSource/";
|
||||
try {
|
||||
@@ -468,19 +450,15 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
@Test
|
||||
public void testMgetRecursivePartial() throws Exception {
|
||||
Session<FTPFile> session = spyOnSession();
|
||||
doAnswer(new Answer<FTPFile[]>() {
|
||||
|
||||
@Override
|
||||
public FTPFile[] answer(InvocationOnMock invocation) throws Throwable {
|
||||
FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
|
||||
// add an extra file where the get will fail
|
||||
files = Arrays.copyOf(files, files.length + 1);
|
||||
FTPFile bogusFile = new FTPFile();
|
||||
bogusFile.setName("bogus.txt");
|
||||
bogusFile.setTimestamp(Calendar.getInstance());
|
||||
files[files.length - 1] = bogusFile;
|
||||
return files;
|
||||
}
|
||||
doAnswer(invocation -> {
|
||||
FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
|
||||
// add an extra file where the get will fail
|
||||
files = Arrays.copyOf(files, files.length + 1);
|
||||
FTPFile bogusFile = new FTPFile();
|
||||
bogusFile.setName("bogus.txt");
|
||||
bogusFile.setTimestamp(Calendar.getInstance());
|
||||
files[files.length - 1] = bogusFile;
|
||||
return files;
|
||||
}).when(session).list("ftpSource/subFtpSource/");
|
||||
String dir = "ftpSource/";
|
||||
try {
|
||||
@@ -498,13 +476,8 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
@Test
|
||||
public void testMputPartial() throws Exception {
|
||||
Session<FTPFile> session = spyOnSession();
|
||||
doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
throw new IOException("Failed to send localSource2");
|
||||
}
|
||||
|
||||
doAnswer(invocation -> {
|
||||
throw new IOException("Failed to send localSource2");
|
||||
}).when(session).write(Mockito.any(InputStream.class), Mockito.contains("localSource2"));
|
||||
try {
|
||||
this.inboundMPut.send(new GenericMessage<File>(getSourceLocalDirectory()));
|
||||
@@ -528,13 +501,8 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
FileOutputStream writer = new FileOutputStream(extra);
|
||||
writer.write("foo".getBytes());
|
||||
writer.close();
|
||||
doAnswer(new Answer<Void>() {
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
throw new IOException("Failed to send subLocalSource2");
|
||||
}
|
||||
|
||||
doAnswer(invocation -> {
|
||||
throw new IOException("Failed to send subLocalSource2");
|
||||
}).when(session).write(Mockito.any(InputStream.class), Mockito.contains("subLocalSource2"));
|
||||
try {
|
||||
this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory()));
|
||||
@@ -569,13 +537,7 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
}
|
||||
|
||||
private void assertLength6(FtpRemoteFileTemplate template) {
|
||||
FTPFile[] files = template.execute(new SessionCallback<FTPFile, FTPFile[]>() {
|
||||
|
||||
@Override
|
||||
public FTPFile[] doInSession(Session<FTPFile> session) throws IOException {
|
||||
return session.list("ftpTarget/appending.txt");
|
||||
}
|
||||
});
|
||||
FTPFile[] files = template.execute(session -> session.list("ftpTarget/appending.txt"));
|
||||
assertEquals(1, files.length);
|
||||
assertEquals(6, files[0].getSize());
|
||||
}
|
||||
@@ -638,21 +600,16 @@ public class FtpServerOutboundTests extends FtpTestSupport {
|
||||
@Override
|
||||
public List<File> filterFiles(File[] files) {
|
||||
File[] sorted = Arrays.copyOf(files, files.length);
|
||||
Arrays.sort(sorted, new Comparator<File>() {
|
||||
|
||||
@Override
|
||||
public int compare(File o1, File o2) {
|
||||
if (o1.isDirectory() && !o2.isDirectory()) {
|
||||
return 1;
|
||||
}
|
||||
else if (!o1.isDirectory() && o2.isDirectory()) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
Arrays.sort(sorted, (o1, o2) -> {
|
||||
if (o1.isDirectory() && !o2.isDirectory()) {
|
||||
return 1;
|
||||
}
|
||||
else if (!o1.isDirectory() && o2.isDirectory()) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
|
||||
});
|
||||
return Arrays.asList(sorted);
|
||||
}
|
||||
|
||||
@@ -39,9 +39,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.remote.ClientCallbackWithoutResult;
|
||||
import org.springframework.integration.file.remote.SessionCallback;
|
||||
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.ftp.FtpTestSupport;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
@@ -69,41 +67,28 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
|
||||
template.setFileNameGenerator(fileNameGenerator);
|
||||
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
|
||||
template.setUseTemporaryFileName(false);
|
||||
template.execute(new SessionCallback<FTPFile, Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean doInSession(Session<FTPFile> session) throws IOException {
|
||||
session.mkdir("foo/");
|
||||
return session.mkdir("foo/bar/");
|
||||
}
|
||||
|
||||
template.execute(session -> {
|
||||
session.mkdir("foo/");
|
||||
return session.mkdir("foo/bar/");
|
||||
});
|
||||
template.append(new GenericMessage<String>("foo"));
|
||||
template.append(new GenericMessage<String>("bar"));
|
||||
assertTrue(template.exists("foo/foobar.txt"));
|
||||
template.executeWithClient(new ClientCallbackWithoutResult<FTPClient>() {
|
||||
|
||||
@Override
|
||||
public void doWithClientWithoutResult(FTPClient client) {
|
||||
try {
|
||||
FTPFile[] files = client.listFiles("foo/foobar.txt");
|
||||
assertEquals(6, files[0].getSize());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
|
||||
try {
|
||||
FTPFile[] files = client.listFiles("foo/foobar.txt");
|
||||
assertEquals(6, files[0].getSize());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
template.execute(new SessionCallbackWithoutResult<FTPFile>() {
|
||||
|
||||
@Override
|
||||
public void doInSessionWithoutResult(Session<FTPFile> session) throws IOException {
|
||||
assertTrue(session.remove("foo/foobar.txt"));
|
||||
assertTrue(session.rmdir("foo/bar/"));
|
||||
FTPFile[] files = session.list("foo/");
|
||||
assertEquals(0, files.length);
|
||||
assertTrue(session.rmdir("foo/"));
|
||||
}
|
||||
template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
|
||||
assertTrue(session.remove("foo/foobar.txt"));
|
||||
assertTrue(session.rmdir("foo/bar/"));
|
||||
FTPFile[] files = session.list("foo/");
|
||||
assertEquals(0, files.length);
|
||||
assertTrue(session.rmdir("foo/"));
|
||||
});
|
||||
assertFalse(template.getSession().exists("foo"));
|
||||
}
|
||||
|
||||
@@ -191,18 +191,15 @@ public class SessionFactoryTests {
|
||||
final Random random = new Random();
|
||||
final AtomicInteger failures = new AtomicInteger();
|
||||
for (int i = 0; i < 30; i++) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Session session = factory.getSession();
|
||||
Thread.sleep(random.nextInt(5000));
|
||||
session.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
failures.incrementAndGet();
|
||||
}
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
Session session = factory.getSession();
|
||||
Thread.sleep(random.nextInt(5000));
|
||||
session.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
failures.incrementAndGet();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user