Skip to content

Commit 8890816

Browse files
EmmanuelRouxartembilan
authored andcommitted
GH-3076: NioFileLocker: add file existence check
Fixes #3076 * Return `null` instead of throwing an exception * Fix license header * Add missing `@since` * Avoid over-engineered test logic * Fix code style * Test non-existent file does not get created * Fix: remove unused import * Remove Java 8 constructions; revert AssertJ to regular JUnit assertions
1 parent 9c24e16 commit 8890816

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/locking/FileChannelCache.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Iwein Fuld
3232
* @author Gary Russell
33+
* @author Emmanuel Roux
3334
* @since 2.0
3435
*/
3536
final class FileChannelCache {
@@ -51,7 +52,7 @@ private FileChannelCache() {
5152
*/
5253
public static FileLock tryLockFor(File fileToLock) throws IOException {
5354
FileChannel channel = channelCache.get(fileToLock);
54-
if (channel == null) {
55+
if (channel == null && fileToLock.exists()) {
5556
@SuppressWarnings("resource")
5657
FileChannel newChannel = new RandomAccessFile(fileToLock, "rw").getChannel();
5758
FileChannel original = channelCache.putIfAbsent(fileToLock, newChannel);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.file.locking;
18+
19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
import org.junit.rules.TemporaryFolder;
30+
31+
/**
32+
* @author Emmanuel Roux
33+
* @since 4.3.22
34+
*/
35+
public class FileChannelCacheTests {
36+
37+
@Rule
38+
public TemporaryFolder temp = new TemporaryFolder();
39+
40+
@Test
41+
public void noLockWhenFileNotExists() throws IOException {
42+
File testFile = new File(this.temp.getRoot(), "test0");
43+
assertFalse(testFile.exists());
44+
assertNull(FileChannelCache.tryLockFor(testFile));
45+
assertFalse(testFile.exists());
46+
}
47+
48+
@Test
49+
public void fileLocked() throws IOException {
50+
File testFile = this.temp.newFile("test1");
51+
assertTrue(testFile.exists());
52+
assertNotNull(FileChannelCache.tryLockFor(testFile));
53+
FileChannelCache.closeChannelFor(testFile);
54+
}
55+
56+
}

spring-integration-file/src/test/java/org/springframework/integration/file/locking/NioFileLockerTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.file.locking;
1818

1919
import static org.hamcrest.CoreMatchers.is;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertThat;
2122
import static org.junit.Assert.assertTrue;
2223

@@ -36,6 +37,7 @@
3637
/**
3738
* @author Iwein Fuld
3839
* @author Gary Russell
40+
* @author Emmanuel Roux
3941
*/
4042
public class NioFileLockerTests {
4143

@@ -77,4 +79,21 @@ public void fileNotListedWhenLockedByOtherFilter() throws IOException {
7779
filter1.unlock(testFile);
7880
}
7981

82+
@Test
83+
public void fileLockedWhenNotAlreadyLockedAndExists() throws IOException {
84+
NioFileLocker locker = new NioFileLocker();
85+
File testFile = new File(this.workdir, "test2");
86+
testFile.createNewFile();
87+
assertTrue(locker.lock(testFile));
88+
locker.unlock(testFile);
89+
}
90+
91+
@Test
92+
public void fileNotLockedWhenNotExists() {
93+
NioFileLocker locker = new NioFileLocker();
94+
File testFile = new File(this.workdir, "test3");
95+
assertFalse(locker.lock(testFile));
96+
assertFalse(testFile.exists());
97+
}
98+
8099
}

0 commit comments

Comments
 (0)