use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.
the class FileWritingMessageHandlerSpec method fileNameExpression.
/**
* Set the {@link DefaultFileNameGenerator} based on the provided SpEL expression.
* @param fileNameExpression the SpEL expression for file names generation.
* @return the current Spec
*/
public FileWritingMessageHandlerSpec fileNameExpression(String fileNameExpression) {
Assert.isNull(this.fileNameGenerator, "'fileNameGenerator' and 'fileNameGeneratorExpression' are mutually exclusive.");
this.defaultFileNameGenerator = new DefaultFileNameGenerator();
this.defaultFileNameGenerator.setExpression(fileNameExpression);
return fileNameGenerator(this.defaultFileNameGenerator);
}
use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.
the class FileOutboundChannelAdapterParserTests method simpleAdapter.
@Test
public void simpleAdapter() {
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(simpleAdapter);
FileWritingMessageHandler handler = (FileWritingMessageHandler) adapterAccessor.getPropertyValue("handler");
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
File expected = new File(System.getProperty("java.io.tmpdir"));
Expression destinationDirectoryExpression = (Expression) handlerAccessor.getPropertyValue("destinationDirectoryExpression");
File actual = new File(destinationDirectoryExpression.getExpressionString());
assertEquals(".foo", TestUtils.getPropertyValue(handler, "temporaryFileSuffix", String.class));
assertThat(actual, is(expected));
DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
assertNotNull(fileNameGenerator);
Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class);
assertNotNull(expression);
assertEquals("'foo.txt'", expression.getExpressionString());
assertEquals(Boolean.FALSE, handlerAccessor.getPropertyValue("deleteSourceFiles"));
assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("flushWhenIdle"));
if (FileUtils.IS_POSIX) {
assertThat(TestUtils.getPropertyValue(handler, "permissions", Set.class).size(), equalTo(9));
}
assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("preserveTimestamp"));
}
use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.
the class SftpOutboundTests method testHandleFileMessage.
@Test
public void testHandleFileMessage() throws Exception {
File targetDir = new File("remote-target-dir");
assertTrue("target directory does not exist: " + targetDir.getName(), targetDir.exists());
SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
fGenerator.setBeanFactory(mock(BeanFactory.class));
fGenerator.setExpression("payload + '.test'");
handler.setFileNameGenerator(fGenerator);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
File srcFile = File.createTempFile("testHandleFileMessage", ".tmp", new File("."));
srcFile.deleteOnExit();
File destFile = new File(targetDir, srcFile.getName() + ".test");
destFile.deleteOnExit();
handler.handleMessage(new GenericMessage<>(srcFile));
assertTrue("destination file was not created", destFile.exists());
}
use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.
the class FtpRemoteFileTemplateTests method testINT3412AppendStatRmdir.
@Test
public void testINT3412AppendStatRmdir() throws IOException {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setExpression("'foobar.txt'");
template.setFileNameGenerator(fileNameGenerator);
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setUseTemporaryFileName(false);
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((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((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"));
}
use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.
the class SftpRemoteFileTemplateTests method testINT3412AppendStatRmdir.
@Test
public void testINT3412AppendStatRmdir() {
SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sessionFactory);
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setExpression("'foobar.txt'");
template.setFileNameGenerator(fileNameGenerator);
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setUseTemporaryFileName(false);
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((ClientCallbackWithoutResult<ChannelSftp>) client -> {
try {
SftpATTRS file = client.lstat("foo/foobar.txt");
assertEquals(6, file.getSize());
} catch (SftpException e) {
throw new RuntimeException(e);
}
});
template.execute((SessionCallbackWithoutResult<LsEntry>) session -> {
LsEntry[] files = session.list("foo/");
assertEquals(4, files.length);
assertTrue(session.remove("foo/foobar.txt"));
assertTrue(session.rmdir("foo/bar/"));
files = session.list("foo/");
assertEquals(2, files.length);
List<LsEntry> list = Arrays.asList(files);
assertThat(list.stream().map(l -> l.getFilename()).collect(Collectors.toList()), containsInAnyOrder(".", ".."));
assertTrue(session.rmdir("foo/"));
});
assertFalse(template.exists("foo"));
}
Aggregations