This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-24095 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 08f65c07cb7b5fd82df0e340b06f585c5df55326 Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jul 15 22:43:08 2026 +0200 CAMEL-24095: camel-file - Add missing Move branch to handleExistingTarget for tempFileName with eagerDeleteTargetFile=false Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/component/file/GenericFileProducer.java | 3 +++ .../file/FileProducerMoveExistingTest.java | 21 ++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java index ab50db46a208..03932fb9824d 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java @@ -254,6 +254,9 @@ public class GenericFileProducer<T> extends DefaultAsyncProducer { return doIgnore(target); } else if (endpoint.getFileExist() == GenericFileExist.Fail) { throwFileAlreadyExistException(target); + } else if (endpoint.getFileExist() == GenericFileExist.Move) { + // move any existing file first + this.endpoint.getMoveExistingFileStrategy().moveExistingFile(endpoint, operations, target); } else if (endpoint.getFileExist() == GenericFileExist.Override) { // we override the target so we do this by deleting // it so the temp file can be renamed later diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java index ce76d156154f..4e8228fd12d9 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java @@ -107,14 +107,21 @@ public class FileProducerMoveExistingTest extends ContextTestSupport { template.sendBodyAndHeader( fileUri("?tempFileName=${file:onlyname}.temp&fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}&eagerDeleteTargetFile=false"), "Second File", Exchange.FILE_NAME, filename); - // we should be okay as we will just delete any existing file - template.sendBodyAndHeader( - fileUri("?tempFileName=${file:onlyname}.temp&fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}&eagerDeleteTargetFile=false"), - "Third File", Exchange.FILE_NAME, filename); - // we could write the new file so the old context should be moved - assertFileExists(testFile(filename), "Third File"); - // and the renamed file should not be overridden + // should fail because the move destination already exists and eagerDeleteTargetFile=false + CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> { + template.sendBodyAndHeader( + fileUri("?tempFileName=${file:onlyname}.temp&fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}&eagerDeleteTargetFile=false"), + "Third File", Exchange.FILE_NAME, filename); + }, "Should have thrown an exception"); + + GenericFileOperationFailedException cause + = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause()); + assertTrue(cause.getMessage().startsWith("Cannot move existing file")); + + // we could not write the new file so the previous content should be there + assertFileExists(testFile(filename), "Second File"); + // and the renamed file should be untouched assertFileExists(testFile("renamed-" + filename), "First File"); }
