diff --git a/core/src/test/java/com/linecorp/armeria/client/endpoint/FileWatcherRegistryTest.java b/core/src/test/java/com/linecorp/armeria/client/endpoint/FileWatcherRegistryTest.java index db1e8175f1a..018b76aa53f 100644 --- a/core/src/test/java/com/linecorp/armeria/client/endpoint/FileWatcherRegistryTest.java +++ b/core/src/test/java/com/linecorp/armeria/client/endpoint/FileWatcherRegistryTest.java @@ -19,15 +19,16 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.BufferedReader; -import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.FileSystem; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.WatchKey; import java.nio.file.WatchService; @@ -35,24 +36,25 @@ import java.util.concurrent.atomic.AtomicInteger; import org.awaitility.Awaitility; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; +import org.junit.jupiter.api.io.TempDir; import com.linecorp.armeria.client.endpoint.FileWatcherRegistry.FileWatchRegisterKey; -public class FileWatcherRegistryTest { +class FileWatcherRegistryTest { - @BeforeClass - public static void before() { + @BeforeAll + static void before() { Awaitility.setDefaultTimeout(1, TimeUnit.MINUTES); } - @AfterClass - public static void after() { + @AfterAll + static void after() { Awaitility.setDefaultTimeout(10, TimeUnit.SECONDS); } @@ -62,7 +64,7 @@ private static Path createMockedPath() throws Exception { final FileSystem fileSystem = mock(FileSystem.class); final WatchService watchService = mock(WatchService.class); final WatchKey watchKey = mock(WatchKey.class); - when(path.toRealPath()).thenReturn(path); + lenient().when(path.toRealPath()).thenReturn(path); when(path.getParent()).thenReturn(path); when(path.getFileSystem()).thenReturn(fileSystem); when(fileSystem.newWatchService()).thenReturn(watchService); @@ -73,47 +75,47 @@ private static Path createMockedPath() throws Exception { } return watchKey; }); - when(watchKey.reset()).thenReturn(true); + lenient().when(watchKey.reset()).thenReturn(true); return path; } - @After - public void tearDown() throws Exception { + @AfterEach + void tearDown() throws Exception { PropertiesEndpointGroup.resetRegistry(); } - @Rule - public TemporaryFolder folder = new TemporaryFolder(); + @TempDir + Path folder; @Test - public void emptyGroupStopsBackgroundThread() throws Exception { + void emptyGroupStopsBackgroundThread() throws Exception { - final File file = folder.newFile("temp-file.properties"); - final File file2 = folder.newFile("temp-file2.properties"); + final Path file = Files.createFile(folder.resolve("temp-file.properties")); + final Path file2 = Files.createFile(folder.resolve("temp-file2.properties")); - final FileWatcherRegistry fileWatcherRegistry = - new FileWatcherRegistry(); - final FileWatchRegisterKey key1 = fileWatcherRegistry.register(file.toPath(), () -> {}); - final FileWatchRegisterKey key2 = fileWatcherRegistry.register(file2.toPath(), () -> {}); + try (FileWatcherRegistry fileWatcherRegistry = new FileWatcherRegistry()) { + final FileWatchRegisterKey key1 = fileWatcherRegistry.register(file, () -> {}); + final FileWatchRegisterKey key2 = fileWatcherRegistry.register(file2, () -> {}); - assertThat(fileWatcherRegistry.isRunning()).isTrue(); + assertThat(fileWatcherRegistry.isRunning()).isTrue(); - fileWatcherRegistry.unregister(key1); + fileWatcherRegistry.unregister(key1); - assertThat(fileWatcherRegistry.isRunning()).isTrue(); + assertThat(fileWatcherRegistry.isRunning()).isTrue(); - fileWatcherRegistry.unregister(key2); + fileWatcherRegistry.unregister(key2); - assertThat(fileWatcherRegistry.isRunning()).isFalse(); + assertThat(fileWatcherRegistry.isRunning()).isFalse(); + } } @Test - public void closeEndpointGroupStopsRegistry() throws Exception { + void closeEndpointGroupStopsRegistry() throws Exception { - final File file = folder.newFile("temp-file.properties"); + final Path file = Files.createFile(folder.resolve("temp-file.properties")); final FileWatcherRegistry fileWatcherRegistry = new FileWatcherRegistry(); - fileWatcherRegistry.register(file.toPath(), () -> {}); + fileWatcherRegistry.register(file, () -> {}); assertThat(fileWatcherRegistry.isRunning()).isTrue(); @@ -123,15 +125,16 @@ public void closeEndpointGroupStopsRegistry() throws Exception { } @Test - public void runnableWithExceptionContinuesRun() throws Exception { + @EnabledForJreRange(min = JRE.JAVA_17) // NIO.2 WatchService doesn't work reliably on older Java. + void runnableWithExceptionContinuesRun() throws Exception { - final File file = folder.newFile("temp-file.properties"); + final Path file = Files.createFile(folder.resolve("temp-file.properties")); final FileWatcherRegistry fileWatcherRegistry = new FileWatcherRegistry(); final AtomicInteger val = new AtomicInteger(0); - final FileWatchRegisterKey key = fileWatcherRegistry.register(file.toPath(), () -> { + final FileWatchRegisterKey key = fileWatcherRegistry.register(file, () -> { try { - final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + final BufferedReader bufferedReader = new BufferedReader(new FileReader(file.toFile())); val.set(Integer.valueOf(bufferedReader.readLine())); } catch (IOException e) { // do nothing @@ -139,7 +142,7 @@ public void runnableWithExceptionContinuesRun() throws Exception { throw new RuntimeException(); }); - PrintWriter printWriter = new PrintWriter(file); + PrintWriter printWriter = new PrintWriter(file.toFile()); printWriter.print(1); printWriter.close(); @@ -147,7 +150,7 @@ public void runnableWithExceptionContinuesRun() throws Exception { assertThat(fileWatcherRegistry.isRunning()).isTrue(); - printWriter = new PrintWriter(file); + printWriter = new PrintWriter(file.toFile()); printWriter.print(2); printWriter.close(); @@ -163,21 +166,22 @@ public void runnableWithExceptionContinuesRun() throws Exception { } @Test - public void testMultipleFileSystems() throws Exception { - - final FileWatcherRegistry fileWatcherRegistry = new FileWatcherRegistry(); - - final Path path1 = createMockedPath(); - final Path path2 = createMockedPath(); - - final FileWatchRegisterKey key1 = fileWatcherRegistry.register(path1, () -> {}); - final FileWatchRegisterKey key2 = fileWatcherRegistry.register(path2, () -> {}); - assertThat(fileWatcherRegistry.isRunning()).isTrue(); - - fileWatcherRegistry.unregister(key1); - assertThat(fileWatcherRegistry.isRunning()).isTrue(); - - fileWatcherRegistry.unregister(key2); - assertThat(fileWatcherRegistry.isRunning()).isFalse(); + void testMultipleFileSystems() throws Exception { + try (FileWatcherRegistry fileWatcherRegistry = new FileWatcherRegistry()) { + final Path path1 = createMockedPath(); + final Path path2 = createMockedPath(); + + final FileWatchRegisterKey key1 = fileWatcherRegistry.register(path1, () -> { + }); + final FileWatchRegisterKey key2 = fileWatcherRegistry.register(path2, () -> { + }); + assertThat(fileWatcherRegistry.isRunning()).isTrue(); + + fileWatcherRegistry.unregister(key1); + assertThat(fileWatcherRegistry.isRunning()).isTrue(); + + fileWatcherRegistry.unregister(key2); + assertThat(fileWatcherRegistry.isRunning()).isFalse(); + } } } diff --git a/core/src/test/java/com/linecorp/armeria/client/endpoint/PropertiesEndpointGroupTest.java b/core/src/test/java/com/linecorp/armeria/client/endpoint/PropertiesEndpointGroupTest.java index e9f6e36645e..cfbe4eccc18 100644 --- a/core/src/test/java/com/linecorp/armeria/client/endpoint/PropertiesEndpointGroupTest.java +++ b/core/src/test/java/com/linecorp/armeria/client/endpoint/PropertiesEndpointGroupTest.java @@ -25,41 +25,43 @@ import java.io.PrintWriter; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.awaitility.Awaitility; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; +import org.junit.jupiter.api.io.TempDir; import com.linecorp.armeria.client.Endpoint; -public class PropertiesEndpointGroupTest { +class PropertiesEndpointGroupTest { private static final Properties PROPS = new Properties(); - @BeforeClass - public static void before() { + @BeforeAll + static void before() { Awaitility.setDefaultTimeout(1, TimeUnit.MINUTES); } - @AfterClass - public static void after() { + @AfterAll + static void after() { Awaitility.setDefaultTimeout(10, TimeUnit.SECONDS); } - @After - public void tearDown() throws Exception { + @AfterEach + void tearDown() throws Exception { PropertiesEndpointGroup.resetRegistry(); } - @Rule - public TemporaryFolder folder = new TemporaryFolder(); + @TempDir + Path folder; static { PROPS.setProperty("serverA.hosts.0", "127.0.0.1:8080"); @@ -70,7 +72,7 @@ public void tearDown() throws Exception { } @Test - public void propertiesWithoutDefaultPort() { + void propertiesWithoutDefaultPort() { final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of(PROPS, "serverA.hosts"); assertThat(endpointGroup.endpoints()).containsExactlyInAnyOrder(Endpoint.parse("127.0.0.1:8080"), @@ -79,7 +81,7 @@ public void propertiesWithoutDefaultPort() { } @Test - public void propertiesWithDefaultPort() { + void propertiesWithDefaultPort() { final PropertiesEndpointGroup endpointGroupA = PropertiesEndpointGroup.builder(PROPS, "serverA.hosts") .defaultPort(80) .build(); @@ -95,7 +97,7 @@ public void propertiesWithDefaultPort() { } @Test - public void resourceWithoutDefaultPort() { + void resourceWithoutDefaultPort() { final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of( getClass().getClassLoader(), "server-list.properties", "serverA.hosts"); @@ -105,7 +107,7 @@ public void resourceWithoutDefaultPort() { } @Test - public void resourceWithDefaultPort() { + void resourceWithDefaultPort() { final PropertiesEndpointGroup endpointGroupA = PropertiesEndpointGroup.builder(getClass().getClassLoader(), "server-list.properties", @@ -128,7 +130,7 @@ public void resourceWithDefaultPort() { } @Test - public void pathWithDefaultPort() throws Exception { + void pathWithDefaultPort() throws Exception { final URL resourceUrl = getClass().getClassLoader().getResource("server-list.properties"); assert resourceUrl != null; final Path resourcePath = new File(resourceUrl.toURI().getPath()).toPath(); @@ -141,7 +143,7 @@ public void pathWithDefaultPort() throws Exception { } @Test - public void pathWithoutDefaultPort() throws URISyntaxException { + void pathWithoutDefaultPort() throws URISyntaxException { final URL resourceUrl = getClass().getClassLoader().getResource("server-list.properties"); assert resourceUrl != null; final Path resourcePath = new File(resourceUrl.toURI().getPath()).toPath(); @@ -154,7 +156,7 @@ public void pathWithoutDefaultPort() throws URISyntaxException { } @Test - public void testWithPrefixThatEndsWithDot() { + void testWithPrefixThatEndsWithDot() { final PropertiesEndpointGroup endpointGroup = PropertiesEndpointGroup.of( getClass().getClassLoader(), "server-list.properties", "serverA.hosts."); @@ -164,7 +166,7 @@ public void testWithPrefixThatEndsWithDot() { } @Test - public void containsNoHosts() { + void containsNoHosts() { assertThat(PropertiesEndpointGroup.builder(getClass().getClassLoader(), "server-list.properties", "serverC.hosts") .defaultPort(8080) @@ -173,7 +175,7 @@ public void containsNoHosts() { } @Test - public void illegalDefaultPort() { + void illegalDefaultPort() { assertThatThrownBy(() -> PropertiesEndpointGroup.builder(getClass().getClassLoader(), "server-list.properties", "serverA.hosts") .defaultPort(0)) @@ -182,22 +184,23 @@ public void illegalDefaultPort() { } @Test - public void propertiesFileUpdatesCorrectly() throws Exception { - final File file = folder.newFile("temp-file.properties"); + @EnabledForJreRange(min = JRE.JAVA_17) // NIO.2 WatchService doesn't work reliably on older Java. + void propertiesFileUpdatesCorrectly() throws Exception { + final Path file = folder.resolve("temp-file.properties"); - PrintWriter printWriter = new PrintWriter(file); + PrintWriter printWriter = new PrintWriter(file.toFile()); Properties props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.store(printWriter, ""); printWriter.close(); final PropertiesEndpointGroup endpointGroupA = PropertiesEndpointGroup.of( - file.toPath(), "serverA.hosts"); + file, "serverA.hosts"); await().untilAsserted(() -> assertThat(endpointGroupA.endpoints()).hasSize(1)); // Update resource - printWriter = new PrintWriter(file); + printWriter = new PrintWriter(file.toFile()); props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.setProperty("serverA.hosts.1", "127.0.0.1:8081"); @@ -210,36 +213,37 @@ public void propertiesFileUpdatesCorrectly() throws Exception { } @Test - public void duplicateResourceUrl() throws IOException { - final File file = folder.newFile("temp-file.properties"); + void duplicateResourceUrl() throws IOException { + final Path file = Files.createFile(folder.resolve("temp-file.properties")); final PropertiesEndpointGroup propertiesEndpointGroupA = - PropertiesEndpointGroup.of(file.toPath(), "serverA.hosts"); + PropertiesEndpointGroup.of(file, "serverA.hosts"); final PropertiesEndpointGroup propertiesEndpointGroupB = - PropertiesEndpointGroup.of(file.toPath(), "serverA.hosts"); + PropertiesEndpointGroup.of(file, "serverA.hosts"); propertiesEndpointGroupA.close(); propertiesEndpointGroupB.close(); } @Test - public void propertiesFileRestart() throws Exception { - final File file = folder.newFile("temp-file.properties"); + @EnabledForJreRange(min = JRE.JAVA_17) // NIO.2 WatchService doesn't work reliably on older Java. + void propertiesFileRestart() throws Exception { + final Path file = folder.resolve("temp-file.properties"); - PrintWriter printWriter = new PrintWriter(file); + PrintWriter printWriter = new PrintWriter(file.toFile()); Properties props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.store(printWriter, ""); printWriter.close(); final PropertiesEndpointGroup endpointGroupA = PropertiesEndpointGroup.of( - file.toPath(), "serverA.hosts"); + file, "serverA.hosts"); await().untilAsserted(() -> assertThat(endpointGroupA.endpoints()).hasSize(1)); endpointGroupA.close(); final PropertiesEndpointGroup endpointGroupB = PropertiesEndpointGroup.of( - file.toPath(), "serverB.hosts"); + file, "serverB.hosts"); await().untilAsserted(() -> assertThat(endpointGroupB.endpoints()).isEmpty()); - printWriter = new PrintWriter(file); + printWriter = new PrintWriter(file.toFile()); props = new Properties(); props.setProperty("serverB.hosts.0", "127.0.0.1:8080"); props.setProperty("serverB.hosts.1", "127.0.0.1:8081"); @@ -251,10 +255,11 @@ public void propertiesFileRestart() throws Exception { } @Test - public void endpointChangePropagatesToListeners() throws Exception { - final File file = folder.newFile("temp-file.properties"); + @EnabledForJreRange(min = JRE.JAVA_17) // NIO.2 WatchService doesn't work reliably on older Java. + void endpointChangePropagatesToListeners() throws Exception { + final Path file = folder.resolve("temp-file.properties"); - PrintWriter printWriter = new PrintWriter(file); + PrintWriter printWriter = new PrintWriter(file.toFile()); Properties props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.setProperty("serverA.hosts.1", "127.0.0.1:8081"); @@ -262,20 +267,20 @@ public void endpointChangePropagatesToListeners() throws Exception { printWriter.close(); final PropertiesEndpointGroup propertiesEndpointGroup = PropertiesEndpointGroup.of( - file.toPath(), "serverA.hosts"); + file, "serverA.hosts"); final EndpointGroup fallbackEndpointGroup = Endpoint.of("127.0.0.1", 8081); final EndpointGroup endpointGroup = propertiesEndpointGroup.orElse(fallbackEndpointGroup); await().untilAsserted(() -> assertThat(endpointGroup.endpoints()).hasSize(2)); - printWriter = new PrintWriter(file); + printWriter = new PrintWriter(file.toFile()); props = new Properties(); props.store(printWriter, ""); printWriter.close(); await().untilAsserted(() -> assertThat(endpointGroup.endpoints()).hasSize(1)); - printWriter = new PrintWriter(file); + printWriter = new PrintWriter(file.toFile()); props = new Properties(); props.setProperty("serverA.hosts.0", "127.0.0.1:8080"); props.setProperty("serverA.hosts.1", "127.0.0.1:8081");