This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit b0c9b171d89027cd4555aac60cc19515395e16ef Author: Guillaume Nodet <[email protected]> AuthorDate: Wed Jun 24 17:26:20 2020 +0200 [CAMEL-11807] Upgrade camel-file-watch to junit5 --- components/camel-file-watch/pom.xml | 2 +- .../file/watch/FileWatchComponentNegativeTest.java | 4 +- .../watch/FileWatchComponentRecursiveTest.java | 2 +- .../file/watch/FileWatchComponentTest.java | 4 +- .../file/watch/FileWatchComponentTestBase.java | 49 ++++++++++++++-------- .../file/watch/SpringFileWatcherTest.java | 8 ++-- 6 files changed, 43 insertions(+), 26 deletions(-) diff --git a/components/camel-file-watch/pom.xml b/components/camel-file-watch/pom.xml index 99c4399..eeffa91 100644 --- a/components/camel-file-watch/pom.xml +++ b/components/camel-file-watch/pom.xml @@ -45,7 +45,7 @@ <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-test-spring</artifactId> + <artifactId>camel-test-spring-junit5</artifactId> <scope>test</scope> </dependency> <dependency> diff --git a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentNegativeTest.java b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentNegativeTest.java index 798a393..7e14bf6 100644 --- a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentNegativeTest.java +++ b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentNegativeTest.java @@ -19,7 +19,9 @@ package org.apache.camel.component.file.watch; import java.nio.file.Paths; import org.apache.camel.builder.RouteBuilder; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.fail; public class FileWatchComponentNegativeTest extends FileWatchComponentTestBase { diff --git a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentRecursiveTest.java b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentRecursiveTest.java index 858111b..021ef75 100644 --- a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentRecursiveTest.java +++ b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentRecursiveTest.java @@ -25,7 +25,7 @@ import java.util.UUID; import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class FileWatchComponentRecursiveTest extends FileWatchComponentTestBase { diff --git a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTest.java b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTest.java index 7f64446..1ee54b1a 100644 --- a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTest.java +++ b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTest.java @@ -26,7 +26,9 @@ import java.util.UUID; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.file.watch.constants.FileEventEnum; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class FileWatchComponentTest extends FileWatchComponentTestBase { diff --git a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java index 15d819f..074c2c3 100644 --- a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java +++ b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java @@ -17,32 +17,37 @@ package org.apache.camel.component.file.watch; import java.io.File; +import java.io.IOError; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import org.apache.camel.Exchange; import org.apache.camel.component.file.watch.constants.FileEventEnum; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Assert; -import org.junit.Assume; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assumptions.assumeTrue; +import static org.springframework.test.util.AssertionErrors.assertTrue; public class FileWatchComponentTestBase extends CamelTestSupport { - @Rule - public TemporaryFolder folder = new TemporaryFolder(Paths.get("target").toAbsolutePath().toFile()); + @TempDir + public Path folder; protected List<Path> testFiles = new ArrayList<>(); + protected String testMethod; + static void assertFileEvent(String expectedFileName, FileEventEnum expectedEventType, Exchange exchange) { - Assert.assertEquals(expectedFileName, exchange.getIn().getBody(File.class).getName()); - Assert.assertEquals(expectedEventType, exchange.getIn().getHeader(FileWatchComponent.EVENT_TYPE_HEADER, FileEventEnum.class)); + assertEquals(expectedFileName, exchange.getIn().getBody(File.class).getName()); + assertEquals(expectedEventType, exchange.getIn().getHeader(FileWatchComponent.EVENT_TYPE_HEADER, FileEventEnum.class)); } static boolean isWindows() { @@ -51,14 +56,18 @@ public class FileWatchComponentTestBase extends CamelTestSupport { return System.getProperty("os.name").toLowerCase().contains("win"); } + public void beforeEach(ExtensionContext context) throws Exception { + super.beforeEach(context); + this.testMethod = context.getTestMethod().map(Method::getName).orElse(""); + } + @Override protected void doPreSetup() throws Exception { - super.doPostSetup(); cleanTestDir(new File(testPath())); new File(testPath()).mkdirs(); for (int i = 0; i < 10; i++) { - File newFile = new File(testPath(), getTestName().getMethodName() + "-" + i); - Assume.assumeTrue(newFile.createNewFile()); + File newFile = new File(testPath(), testMethod + "-" + i); + assumeTrue(newFile.createNewFile()); testFiles.add(newFile.toPath()); } } @@ -89,17 +98,21 @@ public class FileWatchComponentTestBase extends CamelTestSupport { } protected String testPath() { - return folder.getRoot().getAbsolutePath() - + folder.getRoot().toPath().getFileSystem().getSeparator() - + getClass().getSimpleName() + "_" + getTestName().getMethodName() - + folder.getRoot().toPath().getFileSystem().getSeparator(); + try { + return folder.toRealPath() + + folder.getFileSystem().getSeparator() + + getClass().getSimpleName() + "_" + testMethod + + folder.getFileSystem().getSeparator(); + } catch (IOException e) { + throw new IOError(e); + } } protected File createFile(File parent, String child) { try { Files.createDirectories(parent.toPath()); File newFile = new File(parent, child); - Assert.assertTrue("File should be created but already exists", newFile.createNewFile()); + assertTrue("File should be created but already exists", newFile.createNewFile()); return newFile; } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/SpringFileWatcherTest.java b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/SpringFileWatcherTest.java index b2e500a..558511d 100644 --- a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/SpringFileWatcherTest.java +++ b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/SpringFileWatcherTest.java @@ -23,9 +23,9 @@ import java.nio.file.StandardOpenOption; import java.util.UUID; import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.test.spring.CamelSpringTestSupport; -import org.junit.Before; -import org.junit.Test; +import org.apache.camel.test.spring.junit5.CamelSpringTestSupport; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.context.support.AbstractXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -34,7 +34,7 @@ public class SpringFileWatcherTest extends CamelSpringTestSupport { private File springTestFile; private File springTestCustomHasherFile; - @Before + @BeforeEach public void createTestFiles() throws Exception { Files.createDirectories(Paths.get("target/fileWatchSpringTest")); Files.createDirectories(Paths.get("target/fileWatchSpringTestCustomHasher"));
