INT-832: Add File Relative Path to Message Headers
JIRA: https://jira.spring.io/browse/INT-832 When we scan directory recursively for files (e.g. `WatchServiceDirectoryScanner`), it can be useful to get access to the relative path from the `Message`, e.g. on the `FileWritingMessageHandler` side to restore the original structure * Add a `FileHeaders.FILENAME` into the outbound `Message` from the `FileReadingMessageSource` * The result of that header is like a removal of leading `this.directory.getAbsolutePath()` in the target `File.getAbsolutePath()`. In case of not recursion we get just only regular file name. * Introduce `FileHeaders.RELATIVE_PATH` * Populated `FileHeaders.RELATIVE_PATH`, `FileHeaders.FILENAME`, `FileHeaders.ORIGINAL_FILE` in the `FileReadingMessageSource` * File `FileTailingMessageProducerSupport` to populate `FileHeaders` properly * Introduce ctor for the `LastModifiedFileListFilter` for better Java configuration experience * Add docs for changes Doc Polishing
This commit is contained in:
committed by
Gary Russell
parent
7d9c65a108
commit
8f0fa3468f
@@ -134,7 +134,12 @@ public class FileReadingMessageSourceIntegrationTests {
|
||||
public void inputDirExhausted() throws Exception {
|
||||
assertNotNull(pollableFileSource.receive());
|
||||
assertNotNull(pollableFileSource.receive());
|
||||
assertNotNull(pollableFileSource.receive());
|
||||
Message<File> receive = pollableFileSource.receive();
|
||||
assertNotNull(receive);
|
||||
File payload = receive.getPayload();
|
||||
assertEquals(payload, receive.getHeaders().get(FileHeaders.ORIGINAL_FILE));
|
||||
assertEquals(payload.getName(), receive.getHeaders().get(FileHeaders.FILENAME));
|
||||
assertEquals(payload.getName(), receive.getHeaders().get(FileHeaders.RELATIVE_PATH));
|
||||
assertNull(pollableFileSource.receive());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -44,6 +44,7 @@ import org.springframework.messaging.Message;
|
||||
* @author Iwein Fuld
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
* @author Gary Russell
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FileReadingMessageSourceTests {
|
||||
@@ -66,6 +67,8 @@ public class FileReadingMessageSourceTests {
|
||||
when(inputDirectoryMock.isDirectory()).thenReturn(true);
|
||||
when(inputDirectoryMock.exists()).thenReturn(true);
|
||||
when(inputDirectoryMock.canRead()).thenReturn(true);
|
||||
when(inputDirectoryMock.getAbsolutePath()).thenReturn("foo/bar");
|
||||
when(fileMock.getAbsolutePath()).thenReturn("foo/bar/fileMock");
|
||||
when(locker.lock(isA(File.class))).thenReturn(true);
|
||||
}
|
||||
|
||||
@@ -80,13 +83,13 @@ public class FileReadingMessageSourceTests {
|
||||
|
||||
@Test
|
||||
public void straightProcess() throws Exception {
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[]{fileMock});
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[] { fileMock });
|
||||
assertThat(source.receive().getPayload(), is(fileMock));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requeueOnFailure() throws Exception {
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[]{fileMock});
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[] { fileMock });
|
||||
Message<File> received = source.receive();
|
||||
assertNotNull(received);
|
||||
source.onFailure(received);
|
||||
@@ -97,7 +100,8 @@ public class FileReadingMessageSourceTests {
|
||||
@Test
|
||||
public void scanEachPoll() throws Exception {
|
||||
File anotherFileMock = mock(File.class);
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[]{fileMock, anotherFileMock});
|
||||
when(anotherFileMock.getAbsolutePath()).thenReturn("foo/bar/anotherFileMock");
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[] { fileMock, anotherFileMock });
|
||||
source.setScanEachPoll(true);
|
||||
assertNotNull(source.receive());
|
||||
assertNotNull(source.receive());
|
||||
@@ -107,7 +111,7 @@ public class FileReadingMessageSourceTests {
|
||||
|
||||
@Test
|
||||
public void noDuplication() throws Exception {
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[]{fileMock});
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[] { fileMock });
|
||||
Message<File> received = source.receive();
|
||||
assertNotNull(received);
|
||||
assertEquals(fileMock, received.getPayload());
|
||||
@@ -122,7 +126,7 @@ public class FileReadingMessageSourceTests {
|
||||
|
||||
@Test
|
||||
public void lockIsAcquired() throws IOException {
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[]{fileMock});
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[] { fileMock });
|
||||
Message<File> received = source.receive();
|
||||
assertNotNull(received);
|
||||
assertEquals(fileMock, received.getPayload());
|
||||
@@ -131,7 +135,7 @@ public class FileReadingMessageSourceTests {
|
||||
|
||||
@Test
|
||||
public void lockedFilesAreIgnored() throws IOException {
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[]{fileMock});
|
||||
when(inputDirectoryMock.listFiles()).thenReturn(new File[] { fileMock });
|
||||
when(locker.lock(fileMock)).thenReturn(false);
|
||||
Message<File> received = source.receive();
|
||||
assertNull(received);
|
||||
@@ -141,8 +145,11 @@ public class FileReadingMessageSourceTests {
|
||||
@Test
|
||||
public void orderedReception() throws Exception {
|
||||
File file1 = mock(File.class);
|
||||
when(file1.getAbsolutePath()).thenReturn("foo/bar/file1");
|
||||
File file2 = mock(File.class);
|
||||
when(file2.getAbsolutePath()).thenReturn("foo/bar/file2");
|
||||
File file3 = mock(File.class);
|
||||
when(file3.getAbsolutePath()).thenReturn("foo/bar/file3");
|
||||
|
||||
// record the comparator to reverse order the files
|
||||
when(comparator.compare(file1, file2)).thenReturn(1);
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
package org.springframework.integration.file;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -39,6 +41,8 @@ import org.junit.rules.TemporaryFolder;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter;
|
||||
import org.springframework.integration.metadata.SimpleMetadataStore;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
@@ -71,7 +75,7 @@ public class WatchServiceDirectoryScannerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialAndAddMoreThenRemove() throws Exception {
|
||||
public void testWatchServiceDirectoryScanner() throws Exception {
|
||||
FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
|
||||
fileReadingMessageSource.setDirectory(folder.getRoot());
|
||||
fileReadingMessageSource.setUseWatchService(true);
|
||||
@@ -188,6 +192,19 @@ public class WatchServiceDirectoryScannerTests {
|
||||
|
||||
assertTrue(removeFileLatch.await(10, TimeUnit.SECONDS));
|
||||
|
||||
File baz3 = File.createTempFile("baz3", ".txt", baz);
|
||||
|
||||
n = 0;
|
||||
Message<File> fileMessage = null;
|
||||
while (n++ < 300 && (fileMessage = fileReadingMessageSource.receive()) == null) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
|
||||
assertNotNull(fileMessage);
|
||||
assertEquals(baz3, fileMessage.getPayload());
|
||||
assertThat(fileMessage.getHeaders().get(FileHeaders.RELATIVE_PATH, String.class),
|
||||
startsWith(TestUtils.applySystemFileSeparator("foo/baz/")));
|
||||
|
||||
fileReadingMessageSource.stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,12 +37,14 @@ import org.junit.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.file.FileHeaders;
|
||||
import org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @author Gavin Gray
|
||||
* @author Artem Bilan
|
||||
* @since 3.0
|
||||
*
|
||||
*/
|
||||
@@ -169,6 +171,8 @@ public class FileTailingMessageProducerTests {
|
||||
Message<?> message = outputChannel.receive(10000);
|
||||
assertNotNull("expected a non-null message", message);
|
||||
assertEquals("hello" + i, message.getPayload());
|
||||
assertEquals(file, message.getHeaders().get(FileHeaders.ORIGINAL_FILE));
|
||||
assertEquals(file.getName(), message.getHeaders().get(FileHeaders.FILENAME));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user