This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch feature/fix-flaky-windows-tests in repository https://gitbox.apache.org/repos/asf/sling-ide-tooling.git
commit ff191eba712255adb245e579412b7b4631a755ea Author: Konrad Windszus <[email protected]> AuthorDate: Fri May 15 09:38:21 2020 +0200 SLING-7596 switch no File NIO for testing use JUnit5 ignore non-relevant (platform-specific) events switch to newest parent --- cli/cli/pom.xml | 41 ++-- .../org/apache/sling/ide/cli/impl/DirWatcher.java | 1 - .../apache/sling/ide/cli/impl/DirWatcherTest.java | 213 +++++++-------------- 3 files changed, 93 insertions(+), 162 deletions(-) diff --git a/cli/cli/pom.xml b/cli/cli/pom.xml index f6b83c0..9044825 100644 --- a/cli/cli/pom.xml +++ b/cli/cli/pom.xml @@ -16,14 +16,21 @@ <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.apache.sling</groupId> - <artifactId>sling</artifactId> - <version>33</version> + <artifactId>sling-bundle-parent</artifactId> + <version>39</version> + <relativePath/> </parent> + <groupId>org.apache.sling.ide</groupId> <artifactId>org.apache.sling.ide.cli</artifactId> <name>Apache Sling IDE Tools CLI</name> <version>1.2.3-SNAPSHOT</version> + <properties> + <sling.java.version>8</sling.java.version> + <junit.jupiter.version>5.6.2</junit.jupiter.version> + </properties> + <scm> <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-ide-tooling.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-ide-tooling.git</developerConnection> @@ -36,6 +43,19 @@ <groupId>biz.aQute.bnd</groupId> <artifactId>bnd-maven-plugin</artifactId> </plugin> + <!-- only overwrite config for surefire/failsafe --> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <trimStackTrace>false</trimStackTrace> + </configuration> + </plugin> + <plugin> + <artifactId>maven-failsafe-plugin</artifactId> + <configuration> + <trimStackTrace>false</trimStackTrace> + </configuration> + </plugin> </plugins> </build> @@ -73,17 +93,13 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </dependency> - <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-library</artifactId> - <version>1.3</version> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> - <!-- Note that OSGi annotations are OK since they are not retained at compile time --> + <!-- Note that OSGi annotations are OK since they are not retained at compile time --> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.annotation.versioning</artifactId> @@ -100,8 +116,5 @@ <scope>provided</scope> </dependency> </dependencies> - <properties> - <sling.java.version>8</sling.java.version> - </properties> - <groupId>org.apache.sling.ide</groupId> + </project> diff --git a/cli/cli/src/main/java/org/apache/sling/ide/cli/impl/DirWatcher.java b/cli/cli/src/main/java/org/apache/sling/ide/cli/impl/DirWatcher.java index e5537f7..0477bee 100644 --- a/cli/cli/src/main/java/org/apache/sling/ide/cli/impl/DirWatcher.java +++ b/cli/cli/src/main/java/org/apache/sling/ide/cli/impl/DirWatcher.java @@ -45,7 +45,6 @@ import java.util.stream.Stream; * * <p>This implementation works within the same constraints as the {@link WatchService} so * consumers are advised to read the documentation, particularly those related to platform limitations. - * It is recommended to allow for minimal */ public class DirWatcher implements AutoCloseable { diff --git a/cli/cli/src/test/java/org/apache/sling/ide/cli/impl/DirWatcherTest.java b/cli/cli/src/test/java/org/apache/sling/ide/cli/impl/DirWatcherTest.java index be17894..1347d0d 100644 --- a/cli/cli/src/test/java/org/apache/sling/ide/cli/impl/DirWatcherTest.java +++ b/cli/cli/src/test/java/org/apache/sling/ide/cli/impl/DirWatcherTest.java @@ -16,199 +16,118 @@ */ package org.apache.sling.ide.cli.impl; -import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertThat; -import static org.junit.Assume.assumeThat; - -import java.io.File; + import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; +import java.nio.file.Path; +import java.nio.file.WatchEvent.Kind; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.io.TempDir; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +// due to https://bugs.openjdk.java.net/browse/JDK-7133447 we need a long timeout on Mac OS +@Timeout(value = 30, unit=TimeUnit.SECONDS) public class DirWatcherTest { - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test(timeout = 3000) + @TempDir + Path watchRoot; + + @Test public void addedFileInRoot() throws IOException, InterruptedException { - - File watchRoot = folder.newFolder(); - - try ( DirWatcher w = new DirWatcher(watchRoot.toPath()) ) { + try ( DirWatcher w = new DirWatcher(watchRoot) ) { + Path newFile = Files.createFile(watchRoot.resolve("README")); + DirWatcher.Event event = pollTillEventKind(w, ENTRY_CREATE); - final File created = new File(watchRoot, "README"); - created.createNewFile(); - - DirWatcher.Event event = w.poll(); - - assertThat("event.kind", event.getKind(), equalTo(ENTRY_CREATE)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(created.getName()))); - - assertThat("queue.size", w.queueSize(), equalTo(0)); + Assertions.assertEquals(watchRoot.relativize(newFile), event.getPath(), "event.path"); + Assertions.assertEquals(0, w.queueSize(), "queue.size"); } } - @Test(timeout = 3000) + @Test public void addedFileInSubdir() throws IOException, InterruptedException { + Path subDir = Files.createDirectory(watchRoot.resolve("subDir")); - File watchRoot = folder.newFolder(); - File subDir = new File(watchRoot, "subDir"); - subDir.mkdir(); - - try ( DirWatcher w = new DirWatcher(watchRoot.toPath()) ) { - - File created = new File(subDir, "README"); - created.createNewFile(); + try ( DirWatcher w = new DirWatcher(watchRoot) ) { + Path newFile = Files.createFile(subDir.resolve("README")); - DirWatcher.Event event = w.poll(); - - assertThat("event.kind", event.getKind(), equalTo(ENTRY_CREATE)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(subDir.getName(), created.getName()))); - - assertThat("queue.size", w.queueSize(), equalTo(0)); + DirWatcher.Event event = pollTillEventKind(w, ENTRY_CREATE); + Assertions.assertEquals(watchRoot.relativize(newFile), event.getPath(), "event.path"); } } - - @Test(timeout = 3000) + + @Test public void addedFileInNewSubdir() throws IOException, InterruptedException { - File watchRoot = folder.newFolder(); - - try ( DirWatcher w = new DirWatcher(watchRoot.toPath()) ) { + try ( DirWatcher w = new DirWatcher(watchRoot) ) { - File subDir = new File(watchRoot, "subDir"); - subDir.mkdir(); + Path subDir = Files.createDirectory(watchRoot.resolve("subDir")); - DirWatcher.Event event = w.poll(); + DirWatcher.Event event = pollTillEventKind(w, ENTRY_CREATE); + Assertions.assertEquals(watchRoot.relativize(subDir), event.getPath(), "event.path"); - assertThat("event.kind", event.getKind(), equalTo(ENTRY_CREATE)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(subDir.getName()))); + Path newFile = Files.createFile(subDir.resolve("README")); - File created = new File(subDir, "README"); - created.createNewFile(); - - event = w.poll(); - assertThat("event.kind", event.getKind(), equalTo(ENTRY_CREATE)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(subDir.getName(), created.getName()))); - - assertThat("queue.size", w.queueSize(), equalTo(0)); + event = pollTillEventKind(w, ENTRY_CREATE); + Assertions.assertEquals(watchRoot.relativize(newFile), event.getPath(), "event.path"); } } - - @Test(timeout = 3000) - public void deletedFile() throws IOException, InterruptedException { - - assumeThat(System.getProperty("os.name"), not(containsString("Windows"))); // TODO - SLING-7596 - - File watchRoot = folder.newFolder(); - File subDir = new File(watchRoot, "subDir"); - subDir.mkdir(); - - File created = new File(subDir, "README"); - created.createNewFile(); - try ( DirWatcher w = new DirWatcher(watchRoot.toPath()) ) { + @Test + public void deletedFile() throws IOException, InterruptedException { + Path subDir = Files.createDirectory(watchRoot.resolve("subDir")); + Path newFile = Files.createFile(subDir.resolve("README")); - created.delete(); + try ( DirWatcher w = new DirWatcher(watchRoot) ) { + Files.delete(newFile); - DirWatcher.Event event = w.poll(); - - assertThat("event.kind", event.getKind(), equalTo(ENTRY_DELETE)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(subDir.getName(), created.getName()))); - - assertThat("queue.size", w.queueSize(), equalTo(0)); + DirWatcher.Event event = pollTillEventKind(w, ENTRY_DELETE); + Assertions.assertEquals(watchRoot.relativize(newFile), event.getPath(), "event.path"); } } - - @Test(timeout = 300000) - public void deleteDir() throws IOException, InterruptedException { - - File watchRoot = folder.newFolder(); - File subDir = new File(watchRoot, "subDir"); - subDir.mkdir(); + @Test + public void deleteDir() throws IOException, InterruptedException { + Path subDir = Files.createDirectory(watchRoot.resolve("subDir")); - try ( DirWatcher w = new DirWatcher(watchRoot.toPath()) ) { + try ( DirWatcher w = new DirWatcher(watchRoot) ) { - Files.delete(subDir.toPath()); + Files.delete(subDir); - DirWatcher.Event event = w.poll(); + DirWatcher.Event event = pollTillEventKind(w, ENTRY_DELETE); - assertThat("event.kind", event.getKind(), equalTo(ENTRY_DELETE)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(subDir.getName()))); - - assertThat("queue.size", w.queueSize(), equalTo(0)); + Assertions.assertEquals(watchRoot.relativize(subDir), event.getPath(), "event.path"); } } - @Test(timeout = 3000) public void modifyFile() throws IOException, InterruptedException { + Path newFile = Files.createFile(watchRoot.resolve("README")); - File watchRoot = folder.newFolder(); - final File created = new File(watchRoot, "README"); - created.createNewFile(); - - try ( DirWatcher w = new DirWatcher(watchRoot.toPath()) ) { + try ( DirWatcher w = new DirWatcher(watchRoot) ) { - Files.write(created.toPath(), "hello, world".getBytes(UTF_8)); + Files.write(newFile, "hello, world".getBytes(StandardCharsets.UTF_8)); + DirWatcher.Event event = pollTillEventKind(w, ENTRY_MODIFY); + Assertions.assertEquals(watchRoot.relativize(newFile), event.getPath(), "event.path"); - drainAndCheck(w, (events) -> { - assertThat("events.size", events.size(), greaterThanOrEqualTo(1)); - for ( DirWatcher.Event event : events ) { - assertThat("event.kind", event.getKind(), equalTo(ENTRY_MODIFY)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(created.getName()))); - } - }); - - Files.write(created.toPath(), "hello, again".getBytes(UTF_8)); - - drainAndCheck(w, (events) -> { - assertThat("events.size", events.size(), greaterThanOrEqualTo(1)); - for ( DirWatcher.Event event : events ) { - assertThat("event.kind", event.getKind(), equalTo(ENTRY_MODIFY)); - assertThat("event.path", event.getPath(), equalTo(Paths.get(created.getName()))); - } - }); - - List<DirWatcher.Event> unexpected = new ArrayList<>(); - while( w.queueSize() != 0 ) - unexpected.add(w.poll()); - - // don't use size comparison to print out unexpected events in case of an assertion failure - assertThat("unexpected events", unexpected, equalTo(new ArrayList<>())); + Files.write(newFile, "hello, world".getBytes(StandardCharsets.UTF_8)); + event = pollTillEventKind(w, ENTRY_MODIFY); + Assertions.assertEquals(watchRoot.relativize(newFile), event.getPath(), "event.path"); } } - - private void drainAndCheck(DirWatcher w, Consumer<List<DirWatcher.Event>> check) throws InterruptedException { - - long start = System.currentTimeMillis(); - long delay = 500l; - - List<DirWatcher.Event> events = new ArrayList<>(); - while( System.currentTimeMillis() < start + delay) { - if ( w.queueSize() == 0 ) { - Thread.sleep(50); - continue; - } - events.add(w.poll()); - } - - check.accept(events); + + private DirWatcher.Event pollTillEventKind(DirWatcher watcher, Kind<?> kind) throws InterruptedException { + DirWatcher.Event event; + do { + event = watcher.poll(); + } while(event.getKind() != kind); + return event; } }
