diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/locking/FileChannelCache.java b/spring-integration-file/src/main/java/org/springframework/integration/file/locking/FileChannelCache.java index 4904c9377d..e7ab63fa22 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/locking/FileChannelCache.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/locking/FileChannelCache.java @@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentMap; */ final class FileChannelCache { - private static ConcurrentMap channelCache = new ConcurrentHashMap(); + private static ConcurrentMap channelCache = new ConcurrentHashMap<>(); private FileChannelCache() { diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/locking/NioFileLocker.java b/spring-integration-file/src/main/java/org/springframework/integration/file/locking/NioFileLocker.java index 2456b04a47..01404a32e3 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/locking/NioFileLocker.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/locking/NioFileLocker.java @@ -39,7 +39,7 @@ import org.springframework.messaging.MessagingException; */ public class NioFileLocker extends AbstractFileLockerFilter { - private final ConcurrentMap lockCache = new ConcurrentHashMap(); + private final ConcurrentMap lockCache = new ConcurrentHashMap<>(); @Override public boolean lock(File fileToLock) { @@ -67,7 +67,7 @@ public class NioFileLocker extends AbstractFileLockerFilter { @Override public void unlock(File fileToUnlock) { - FileLock fileLock = this.lockCache.get(fileToUnlock); + FileLock fileLock = this.lockCache.remove(fileToUnlock); try { if (fileLock != null) { fileLock.release(); diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/locking/NioFileLockerTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/locking/NioFileLockerTests.java index 515a9b7118..e2ef3d0b0e 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/locking/NioFileLockerTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/locking/NioFileLockerTests.java @@ -18,20 +18,24 @@ package org.springframework.integration.file.locking; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.List; +import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.springframework.integration.file.filters.FileListFilter; +import org.springframework.integration.test.util.TestUtils; /** * @author Iwein Fuld + * @author Gary Russell */ public class NioFileLockerTests { @@ -48,13 +52,18 @@ public class NioFileLockerTests { }; @Test - public void fileListedByFirstFilter() throws IOException { + public void fileListedByFirstFilter() throws Exception { NioFileLocker filter = new NioFileLocker(); File testFile = new File(workdir, "test0"); testFile.createNewFile(); assertThat(filter.filterFiles(workdir.listFiles()).get(0), is(testFile)); filter.lock(testFile); assertThat(filter.filterFiles(workdir.listFiles()).get(0), is(testFile)); + filter.unlock(testFile); + Field channelCache = FileChannelCache.class.getDeclaredField("channelCache"); + channelCache.setAccessible(true); + assertTrue(((Map) channelCache.get(null)).isEmpty()); + assertTrue(TestUtils.getPropertyValue(filter, "lockCache", Map.class).isEmpty()); } @Test @@ -65,7 +74,8 @@ public class NioFileLockerTests { testFile.createNewFile(); assertThat(filter1.filterFiles(workdir.listFiles()).get(0), is(testFile)); filter1.lock(testFile); - assertThat(filter2.filterFiles(workdir.listFiles()), is((List) new ArrayList())); + assertThat(filter2.filterFiles(workdir.listFiles()), is(new ArrayList())); + filter1.unlock(testFile); } }