diff --git a/amoro-common/pom.xml b/amoro-common/pom.xml index 2b21ff9997..f7d1f2b1e2 100644 --- a/amoro-common/pom.xml +++ b/amoro-common/pom.xml @@ -1,21 +1,21 @@ +~ Licensed to the Apache Software Foundation (ASF) under one +~ or more contributor license agreements. See the NOTICE file +~ distributed with this work for additional information +~ regarding copyright ownership. The ASF licenses this file +~ to you under the Apache License, Version 2.0 (the +~ "License"); you may not use this file except in compliance +~ with the License. You may obtain a copy of the License at +~ +~ http://www.apache.org/licenses/LICENSE-2.0 +~ +~ Unless required by applicable law or agreed to in writing, software +~ distributed under the License is distributed on an "AS IS" BASIS, +~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +~ See the License for the specific language governing permissions and +~ limitations under the License. +--> 4.0.0 @@ -159,6 +159,12 @@ core test + + + org.junit.platform + junit-platform-launcher + test + diff --git a/amoro-common/src/test/java/org/apache/amoro/MockZookeeperServer.java b/amoro-common/src/test/java/org/apache/amoro/MockZookeeperServer.java index 95189c018e..347d24d505 100644 --- a/amoro-common/src/test/java/org/apache/amoro/MockZookeeperServer.java +++ b/amoro-common/src/test/java/org/apache/amoro/MockZookeeperServer.java @@ -22,7 +22,7 @@ import org.apache.amoro.shade.zookeeper3.org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.amoro.shade.zookeeper3.org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.util.Random; @@ -64,7 +64,7 @@ private static void init() throws Exception { } @Test - public void testFoobar() throws Exception { + void testFoobar() throws Exception { System.out.println("client: " + client); getClient().create().forPath("/test", "test-data".getBytes()); diff --git a/amoro-common/src/test/java/org/apache/amoro/TestAms.java b/amoro-common/src/test/java/org/apache/amoro/TestAms.java index c1767a56ea..2d42a89cda 100644 --- a/amoro-common/src/test/java/org/apache/amoro/TestAms.java +++ b/amoro-common/src/test/java/org/apache/amoro/TestAms.java @@ -18,11 +18,10 @@ package org.apache.amoro; -import org.junit.rules.ExternalResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class TestAms extends ExternalResource { +public class TestAms { private static final Logger LOG = LoggerFactory.getLogger(TestAms.class); private static MockAmoroManagementServer SINGLETON; @@ -54,7 +53,6 @@ public MockAmoroManagementServer.OptimizerManagerHandler getOptimizerHandler() { return mockAms.optimizerHandler(); } - @Override public void before() throws Exception { if (SingletonResourceUtil.isUseSingletonResource()) { if (!mockAms.isStarted()) { @@ -74,7 +72,6 @@ public void before() throws Exception { } } - @Override public void after() { if (!SingletonResourceUtil.isUseSingletonResource()) { mockAms.stopAndCleanUp(); diff --git a/amoro-common/src/test/java/org/apache/amoro/client/TestPoolConfig.java b/amoro-common/src/test/java/org/apache/amoro/client/TestPoolConfig.java index a36f9ea793..62af0d06d4 100644 --- a/amoro-common/src/test/java/org/apache/amoro/client/TestPoolConfig.java +++ b/amoro-common/src/test/java/org/apache/amoro/client/TestPoolConfig.java @@ -18,40 +18,40 @@ package org.apache.amoro.client; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class TestPoolConfig { @Test - public void testConstructFromUrl() { + void testConstructFromUrl() { String url = "thrift://127.0.0.1:1261?connectTimeout=5000&socketTimeout=6000&maxMessageSize=100&autoReconnect=false" + "&maxReconnects=3&minIdle=10&maxIdle=10&maxWaitMillis=500"; PoolConfig poolConfig = PoolConfig.forUrl(url); - Assert.assertEquals(5000, poolConfig.getConnectTimeout()); - Assert.assertEquals(6000, poolConfig.getSocketTimeout()); - Assert.assertEquals(100, poolConfig.getMaxMessageSize()); - Assert.assertFalse(poolConfig.isAutoReconnect()); - Assert.assertEquals(3, poolConfig.getMaxReconnects()); - Assert.assertEquals(10, poolConfig.getMinIdle()); - Assert.assertEquals(10, poolConfig.getMaxIdle()); - Assert.assertEquals(500, poolConfig.getMaxWaitMillis()); + Assertions.assertEquals(5000, poolConfig.getConnectTimeout()); + Assertions.assertEquals(6000, poolConfig.getSocketTimeout()); + Assertions.assertEquals(100, poolConfig.getMaxMessageSize()); + Assertions.assertFalse(poolConfig.isAutoReconnect()); + Assertions.assertEquals(3, poolConfig.getMaxReconnects()); + Assertions.assertEquals(10, poolConfig.getMinIdle()); + Assertions.assertEquals(10, poolConfig.getMaxIdle()); + Assertions.assertEquals(500, poolConfig.getMaxWaitMillis()); } @Test - public void tetUrlParameterNameError() { + void tetUrlParameterNameError() { // We will ignore parameters with unknown name String url = "thrift://127.0.0.1:1261?connectTimeouts=300"; PoolConfig poolConfig = PoolConfig.forUrl(url); - Assert.assertEquals(0, poolConfig.getConnectTimeout()); + Assertions.assertEquals(0, poolConfig.getConnectTimeout()); } @Test - public void testUrlFormatError() { + void testUrlFormatError() { String url = "thrift://127.0.0.1:1261?connectTimeout=5000& "; - Assert.assertThrows( + Assertions.assertThrows( IllegalArgumentException.class, () -> { PoolConfig.forUrl(url); diff --git a/amoro-common/src/test/java/org/apache/amoro/formats/AmoroCatalogTestBase.java b/amoro-common/src/test/java/org/apache/amoro/formats/AmoroCatalogTestBase.java index 2027044348..77910a9833 100644 --- a/amoro-common/src/test/java/org/apache/amoro/formats/AmoroCatalogTestBase.java +++ b/amoro-common/src/test/java/org/apache/amoro/formats/AmoroCatalogTestBase.java @@ -19,16 +19,16 @@ package org.apache.amoro.formats; import org.apache.amoro.AmoroCatalog; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; import java.io.IOException; +import java.nio.file.Path; public abstract class AmoroCatalogTestBase { - @Rule public TemporaryFolder temp = new TemporaryFolder(); + @TempDir Path tempDir; protected AmoroCatalogTestHelper catalogTestHelper; @@ -36,20 +36,37 @@ public abstract class AmoroCatalogTestBase { protected Object originalCatalog; + public AmoroCatalogTestBase() { + this.catalogTestHelper = null; + } + public AmoroCatalogTestBase(AmoroCatalogTestHelper catalogTestHelper) { this.catalogTestHelper = catalogTestHelper; } - @Before - public void setupCatalog() throws IOException { - String path = temp.newFolder().getPath(); + protected AmoroCatalogTestHelper createHelper() { + return null; + } + + @BeforeEach + protected void setupCatalog() throws IOException { + if (catalogTestHelper == null) { + catalogTestHelper = createHelper(); + if (catalogTestHelper == null) { + throw new IllegalStateException( + "catalogTestHelper must be set either via constructor or createHelper() method"); + } + } + String path = tempDir.toFile().getAbsolutePath(); catalogTestHelper.initWarehouse(path); this.amoroCatalog = catalogTestHelper.amoroCatalog(); this.originalCatalog = catalogTestHelper.originalCatalog(); } - @After - public void cleanCatalog() { - catalogTestHelper.clean(); + @AfterEach + void cleanCatalog() { + if (catalogTestHelper != null) { + catalogTestHelper.clean(); + } } } diff --git a/amoro-common/src/test/java/org/apache/amoro/formats/TestAmoroCatalogBase.java b/amoro-common/src/test/java/org/apache/amoro/formats/TestAmoroCatalogBase.java index 5d295e9220..f7be049abe 100644 --- a/amoro-common/src/test/java/org/apache/amoro/formats/TestAmoroCatalogBase.java +++ b/amoro-common/src/test/java/org/apache/amoro/formats/TestAmoroCatalogBase.java @@ -21,8 +21,8 @@ import org.apache.amoro.AmoroTable; import org.apache.amoro.shade.guava32.com.google.common.collect.Sets; import org.apache.amoro.table.TableIdentifier; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.HashSet; @@ -37,6 +37,10 @@ public abstract class TestAmoroCatalogBase extends AmoroCatalogTestBase { private static final String TABLE = "table"; + public TestAmoroCatalogBase() { + super(); + } + public TestAmoroCatalogBase(AmoroCatalogTestHelper catalogTestHelper) { super(catalogTestHelper); } @@ -54,9 +58,9 @@ public void testListDatabases() { createDatabase(DB2); createDatabase(DB3); HashSet databases = Sets.newHashSet(amoroCatalog.listDatabases()); - Assert.assertTrue(databases.contains(DB1)); - Assert.assertTrue(databases.contains(DB2)); - Assert.assertTrue(databases.contains(DB3)); + Assertions.assertTrue(databases.contains(DB1)); + Assertions.assertTrue(databases.contains(DB2)); + Assertions.assertTrue(databases.contains(DB3)); } @Test @@ -64,26 +68,26 @@ public void testDropDatabases() { createDatabase(DB1); amoroCatalog.dropDatabase(DB1); - Assert.assertFalse(amoroCatalog.listDatabases().contains(DB1)); + Assertions.assertFalse(amoroCatalog.listDatabases().contains(DB1)); } @Test public void testCreateDatabases() { amoroCatalog.createDatabase(DB1); - Assert.assertTrue(listDatabases().contains(DB1)); + Assertions.assertTrue(listDatabases().contains(DB1)); } @Test public void testExistsDatabase() { createDatabase(DB1); - Assert.assertTrue(amoroCatalog.databaseExists(DB1)); + Assertions.assertTrue(amoroCatalog.databaseExists(DB1)); } @Test public void testExistsTable() { createDatabase(DB1); createTable(DB1, TABLE, new HashMap<>()); - Assert.assertTrue(amoroCatalog.tableExists(DB1, TABLE)); + Assertions.assertTrue(amoroCatalog.tableExists(DB1, TABLE)); } @Test @@ -93,10 +97,10 @@ public void testLoadTable() { properties.put("key1", "value1"); createTable(DB1, TABLE, properties); AmoroTable amoroTable = amoroCatalog.loadTable(DB1, TABLE); - Assert.assertEquals(amoroTable.properties().get("key1"), "value1"); - Assert.assertEquals( - amoroTable.name(), catalogTestHelper.catalogName() + "." + DB1 + "." + TABLE); - Assert.assertEquals( - amoroTable.id(), TableIdentifier.of(catalogTestHelper.catalogName(), DB1, TABLE)); + Assertions.assertEquals("value1", amoroTable.properties().get("key1")); + Assertions.assertEquals( + catalogTestHelper.catalogName() + "." + DB1 + "." + TABLE, amoroTable.name()); + Assertions.assertEquals( + TableIdentifier.of(catalogTestHelper.catalogName(), DB1, TABLE), amoroTable.id()); } } diff --git a/amoro-common/src/test/java/org/apache/amoro/hive/TestHMS.java b/amoro-common/src/test/java/org/apache/amoro/hive/TestHMS.java index b7f2efaf80..3bee27aea9 100644 --- a/amoro-common/src/test/java/org/apache/amoro/hive/TestHMS.java +++ b/amoro-common/src/test/java/org/apache/amoro/hive/TestHMS.java @@ -21,28 +21,27 @@ import org.apache.amoro.SingletonResourceUtil; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; -import org.junit.rules.ExternalResource; -import org.junit.rules.TemporaryFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; -public class TestHMS extends ExternalResource { +public class TestHMS { private static final Logger LOG = LoggerFactory.getLogger(TestHMS.class); private static HMSMockServer SINGLETON; - private static TemporaryFolder SINGLETON_FOLDER; + private static Path SINGLETON_TEMP_DIR; private final HMSMockServer mockHms; - private TemporaryFolder hmsFolder; + private Path tempDir; static { try { if (SingletonResourceUtil.isUseSingletonResource()) { - SINGLETON_FOLDER = new TemporaryFolder(); - SINGLETON_FOLDER.create(); - SINGLETON = new HMSMockServer(SINGLETON_FOLDER.newFile()); + SINGLETON_TEMP_DIR = Files.createTempDirectory("amoro-test-hms-"); + SINGLETON = new HMSMockServer(SINGLETON_TEMP_DIR.resolve("hms").toFile()); } } catch (IOException e) { throw new UncheckedIOException(e); @@ -50,13 +49,19 @@ public class TestHMS extends ExternalResource { } public TestHMS() { + this(null); + } + + public TestHMS(Path tempDir) { + this.tempDir = tempDir; if (SingletonResourceUtil.isUseSingletonResource()) { mockHms = SINGLETON; } else { try { - hmsFolder = new TemporaryFolder(); - hmsFolder.create(); - mockHms = new HMSMockServer(hmsFolder.newFile()); + if (tempDir == null) { + this.tempDir = Files.createTempDirectory("amoro-test-hms-"); + } + mockHms = new HMSMockServer(this.tempDir.resolve("hms").toFile()); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -79,7 +84,6 @@ public String getWareHouseLocation() { return mockHms.getWareHouseLocation(); } - @Override public void before() throws Exception { if (SingletonResourceUtil.isUseSingletonResource()) { if (!mockHms.isStarted()) { @@ -89,7 +93,11 @@ public void before() throws Exception { new Thread( () -> { SINGLETON.stop(); - SINGLETON_FOLDER.delete(); + try { + deleteDirectory(SINGLETON_TEMP_DIR.toFile()); + } catch (IOException e) { + // ignore + } LOG.info("Stop singleton mock HMS after testing."); })); LOG.info("Start singleton mock HMS before testing."); @@ -100,12 +108,33 @@ public void before() throws Exception { } } - @Override public void after() { if (!SingletonResourceUtil.isUseSingletonResource()) { mockHms.stop(); - hmsFolder.delete(); + if (tempDir != null) { + try { + deleteDirectory(tempDir.toFile()); + } catch (IOException e) { + // ignore + } + } LOG.info("Stop mock HMS after testing."); } } + + private void deleteDirectory(java.io.File directory) throws IOException { + if (directory.exists()) { + java.io.File[] files = directory.listFiles(); + if (files != null) { + for (java.io.File file : files) { + if (file.isDirectory()) { + deleteDirectory(file); + } else { + file.delete(); + } + } + } + directory.delete(); + } + } } diff --git a/amoro-common/src/test/java/org/apache/amoro/listener/AmoroRunListener.java b/amoro-common/src/test/java/org/apache/amoro/listener/AmoroRunListener.java index 543b0dc4f3..3d8c6ed908 100644 --- a/amoro-common/src/test/java/org/apache/amoro/listener/AmoroRunListener.java +++ b/amoro-common/src/test/java/org/apache/amoro/listener/AmoroRunListener.java @@ -18,18 +18,18 @@ package org.apache.amoro.listener; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.runner.Description; -import org.junit.runner.Result; -import org.junit.runner.notification.Failure; -import org.junit.runner.notification.RunListener; +import org.junit.jupiter.api.Assertions; +import org.junit.platform.engine.TestExecutionResult; +import org.junit.platform.launcher.TestExecutionListener; +import org.junit.platform.launcher.TestIdentifier; +import org.junit.platform.launcher.TestPlan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Optional; import java.util.PriorityQueue; -public class AmoroRunListener extends RunListener { +public class AmoroRunListener implements TestExecutionListener { private static final Logger LOG = LoggerFactory.getLogger(AmoroRunListener.class); private long startTime; private long singleTestStartTime; @@ -37,55 +37,63 @@ public class AmoroRunListener extends RunListener { private final PriorityQueue testCaseQueue = new PriorityQueue<>(); @Override - public void testRunStarted(Description description) { + public void testPlanExecutionStarted(TestPlan testPlan) { startTime = System.currentTimeMillis(); - LOG.info( - "{} Tests started! Number of Test case: {}", - description == null ? "Unknown" : description.getClassName(), - description == null ? 0 : description.testCount()); + int testCount = (int) testPlan.countTestIdentifiers(TestIdentifier::isTest); + LOG.info("Tests started! Number of Test case: {}", testCount); } @Override - public void testRunFinished(Result result) { + public void testPlanExecutionFinished(TestPlan testPlan) { long endTime = System.currentTimeMillis(); - LOG.info("Tests finished! Number of test case: {}", result.getRunCount()); + int testCount = (int) testPlan.countTestIdentifiers(TestIdentifier::isTest); + LOG.info("Tests finished! Number of test case: {}", testCount); long elapsedSeconds = (endTime - startTime) / 1000; LOG.info("Elapsed time of tests execution: {} seconds", elapsedSeconds); int printNum = Math.min(testCaseQueue.size(), 50); LOG.info("Print the top cost test case method name:"); for (int i = 0; i < printNum; i++) { TestCase testCase = testCaseQueue.poll(); - Assert.assertNotNull(testCase); + Assertions.assertNotNull(testCase); LOG.info("NO-{}, cost: {}ms, methodName:{}", i + 1, testCase.cost, testCase.methodName); } } @Override - public void testStarted(Description description) { - singleTestStartTime = System.currentTimeMillis(); - LOG.info("{} test is starting...", description.getMethodName()); + public void executionStarted(TestIdentifier testIdentifier) { + if (testIdentifier.isTest()) { + singleTestStartTime = System.currentTimeMillis(); + LOG.info("{} test is starting...", testIdentifier.getDisplayName()); + } } @Override - public void testFinished(Description description) { - long cost = System.currentTimeMillis() - singleTestStartTime; - testCaseQueue.add(TestCase.of(cost, description.getMethodName())); - LOG.info("{} test is finished, cost {}ms...\n", description.getMethodName(), cost); - } + public void executionFinished( + TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { + if (testIdentifier.isTest()) { + long cost = System.currentTimeMillis() - singleTestStartTime; + testCaseQueue.add(TestCase.of(cost, testIdentifier.getDisplayName())); + LOG.info("{} test is finished, cost {}ms...\n", testIdentifier.getDisplayName(), cost); - @Override - public void testFailure(Failure failure) { - LOG.info("{} test FAILED!!!", failure.getDescription().getMethodName()); + if (testExecutionResult.getStatus() == TestExecutionResult.Status.FAILED) { + LOG.info("{} test FAILED!!!", testIdentifier.getDisplayName()); + Optional throwable = testExecutionResult.getThrowable(); + if (throwable.isPresent()) { + LOG.info("Failure reason: {}", throwable.get().getMessage()); + } + } + } } @Override - public void testIgnored(Description description) throws Exception { - super.testIgnored(description); - Ignore ignore = description.getAnnotation(Ignore.class); - LOG.info( - "@Ignore test method '{}', ignored reason '{}'.", - description.getMethodName(), - ignore.value()); + public void executionSkipped(TestIdentifier testIdentifier, String reason) { + if (testIdentifier.isTest()) { + String ignoredReason = reason != null && !reason.isEmpty() ? reason : "No reason provided"; + LOG.info( + "@Disabled method '{}', ignored reason '{}'.", + testIdentifier.getDisplayName(), + ignoredReason); + } } private static class TestCase implements Comparable { @@ -103,7 +111,7 @@ public static TestCase of(long cost, String methodName) { @Override public int compareTo(AmoroRunListener.TestCase that) { - Assert.assertNotNull(that); + Assertions.assertNotNull(that); return that.cost.compareTo(cost); } } diff --git a/amoro-common/src/test/java/org/apache/amoro/process/TestSimpleFuture.java b/amoro-common/src/test/java/org/apache/amoro/process/TestSimpleFuture.java index 7f5c354e30..5a85fc9f4b 100644 --- a/amoro-common/src/test/java/org/apache/amoro/process/TestSimpleFuture.java +++ b/amoro-common/src/test/java/org/apache/amoro/process/TestSimpleFuture.java @@ -23,9 +23,9 @@ import static org.mockito.Mockito.mock; import org.apache.amoro.exception.AmoroRuntimeException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.List; @@ -43,8 +43,8 @@ private void resetCallbackData() { calledFlag = new boolean[] {false, false, false, false, false}; } - @Before - public void setUp() { + @BeforeEach + void setUp() { resetCallbackData(); simpleFuture = new SimpleFuture(); long threadId = Thread.currentThread().getId(); @@ -53,34 +53,35 @@ public void setUp() { simpleFuture.whenCompleted( () -> { for (int j = num; j < calledFlag.length; j++) { - Assert.assertFalse( - "callback " + j + " should not be called before " + num, calledFlag[j]); + Assertions.assertFalse( + calledFlag[j], "callback " + j + " should not be called before " + num); } // Trigger error if callbackNum[num] == 0 if (callbackNum[num] == 0) { throw new RuntimeException("Callback error"); } callbackNum[num] = num; - Assert.assertEquals( - "Callback should be run in the same thread", + Assertions.assertEquals( threadId, - Thread.currentThread().getId()); + Thread.currentThread().getId(), + "Callback should be run in the same thread"); }); } } @Test - public void testComplete() { + void testComplete() { simpleFuture.complete(); for (int i = 0; i < 5; i++) { - Assert.assertEquals("Current callback num: " + i, i, callbackNum[i]); + Assertions.assertEquals(i, callbackNum[i], "Current callback num: " + i); } - Assert.assertTrue("SimpleFuture should complete if callback no error", simpleFuture.isDone()); + Assertions.assertTrue( + simpleFuture.isDone(), "SimpleFuture should complete if callback no error"); } @Test - public void testCompleteTwiceButNotReset() { + void testCompleteTwiceButNotReset() { simpleFuture.complete(); // The second call will not trigger any callback for (int i = 0; i < 5; i++) { @@ -88,151 +89,160 @@ public void testCompleteTwiceButNotReset() { } simpleFuture.complete(); for (int i = 0; i < 5; i++) { - Assert.assertEquals("Current callback num: " + i, -1, callbackNum[i]); + Assertions.assertEquals(-1, callbackNum[i], "Current callback num: " + i); } - Assert.assertTrue("SimpleFuture should not complete if callback error", simpleFuture.isDone()); + Assertions.assertTrue( + simpleFuture.isDone(), "SimpleFuture should not complete if callback error"); } // Additional tests for edge cases and error conditions @Test - public void testCallbackError() { + void testCallbackError() { callbackNum[2] = 0; // Trigger error in callbackNum[2] try { simpleFuture.complete(); } catch (Throwable throwable) { - Assert.assertTrue("Should catch the error", throwable instanceof AmoroRuntimeException); - Assert.assertTrue("Should catch the error", throwable.getCause() instanceof RuntimeException); - Assert.assertEquals( - "Should catch the error", "Callback error", throwable.getCause().getMessage()); + Assertions.assertTrue(throwable instanceof AmoroRuntimeException, "Should catch the error"); + Assertions.assertTrue( + throwable.getCause() instanceof RuntimeException, "Should catch the error"); + Assertions.assertEquals( + "Callback error", throwable.getCause().getMessage(), "Should catch the error"); } for (int i = 0; i < 5; i++) { if (i < 2) { - Assert.assertEquals("Current callback num: " + i, i, callbackNum[i]); + Assertions.assertEquals(i, callbackNum[i], "Current callback num: " + i); } else if (i == 2) { - Assert.assertEquals("Current callback num: " + i, 0, callbackNum[i]); + Assertions.assertEquals(0, callbackNum[i], "Current callback num: " + i); } else { - Assert.assertEquals("Current callback num: " + i, -1, callbackNum[i]); + Assertions.assertEquals(-1, callbackNum[i], "Current callback num: " + i); } } - Assert.assertFalse("SimpleFuture should not complete if callback error", simpleFuture.isDone()); + Assertions.assertFalse( + simpleFuture.isDone(), "SimpleFuture should not complete if callback error"); } @Test - public void testReset() { + void testReset() { callbackNum[2] = 0; // Trigger error in callbackNum[2] try { simpleFuture.complete(); } catch (Throwable throwable) { - Assert.assertTrue("Should catch the error", throwable instanceof AmoroRuntimeException); + Assertions.assertTrue(throwable instanceof AmoroRuntimeException, "Should catch the error"); } - Assert.assertFalse("SimpleFuture should not complete if callback error", simpleFuture.isDone()); + Assertions.assertFalse( + simpleFuture.isDone(), "SimpleFuture should not complete if callback error"); resetCallbackData(); // Trigger normal callback simpleFuture.reset(); simpleFuture.complete(); for (int i = 0; i < 5; i++) { - Assert.assertEquals("Current callback num: " + i, i, callbackNum[i]); + Assertions.assertEquals(i, callbackNum[i], "Current callback num: " + i); } - Assert.assertTrue("SimpleFuture should not complete if callback error", simpleFuture.isDone()); + Assertions.assertTrue( + simpleFuture.isDone(), "SimpleFuture should not complete if callback error"); } @Test - public void testIsDone() { + void testIsDone() { simpleFuture.complete(); - Assert.assertTrue("Future should be completed", simpleFuture.isDone()); + Assertions.assertTrue(simpleFuture.isDone(), "Future should be completed"); } - @Test(expected = AmoroRuntimeException.class) - public void testCompleteException() throws ExecutionException, InterruptedException { + @Test + void testCompleteException() throws ExecutionException, InterruptedException { CompletableFuture future = mock(CompletableFuture.class); doReturn(true).when(future).complete(null); doThrow(new RuntimeException()).when(future).get(); SimpleFuture simpleFuture = new SimpleFuture(future); - simpleFuture.complete(); + Assertions.assertThrows( + AmoroRuntimeException.class, + () -> { + simpleFuture.complete(); + }); } @Test - public void testJoin() { + void testJoin() { simpleFuture.complete(); simpleFuture.join(); - Assert.assertTrue("Future should be completed", simpleFuture.isDone()); + Assertions.assertTrue(simpleFuture.isDone(), "Future should be completed"); } @Test - public void testJoinException() throws ExecutionException, InterruptedException { + void testJoinException() throws ExecutionException, InterruptedException { CompletableFuture future = mock(CompletableFuture.class); doThrow(new RuntimeException()).when(future).get(); SimpleFuture simpleFuture = new SimpleFuture(future); simpleFuture.reset(); simpleFuture.complete(); simpleFuture.join(); - Assert.assertTrue("Future should be completed", simpleFuture.isDone()); + Assertions.assertTrue(simpleFuture.isDone(), "Future should be completed"); } @Test - public void testOr() { + void testOr() { SimpleFuture anotherFuture = new SimpleFuture(); SimpleFuture combinedFuture = simpleFuture.or(anotherFuture); simpleFuture.complete(); - Assert.assertTrue( - "Combined future should be completed when either future completes", - combinedFuture.isDone()); + Assertions.assertTrue( + combinedFuture.isDone(), + "Combined future should be completed when either future completes"); } @Test - public void testAnd() { + void testAnd() { SimpleFuture anotherFuture = new SimpleFuture(); SimpleFuture combinedFuture = simpleFuture.and(anotherFuture); simpleFuture.complete(); anotherFuture.complete(); - Assert.assertTrue( - "Combined future should be completed when both futures complete", combinedFuture.isDone()); + Assertions.assertTrue( + combinedFuture.isDone(), "Combined future should be completed when both futures complete"); } @Test - public void testAllOf() { + void testAllOf() { List futures = Arrays.asList(new SimpleFuture(), new SimpleFuture(), new SimpleFuture()); SimpleFuture combinedFuture = SimpleFuture.allOf(futures); futures.forEach(SimpleFuture::complete); - Assert.assertTrue( - "Combined future should be completed when all futures complete", combinedFuture.isDone()); + Assertions.assertTrue( + combinedFuture.isDone(), "Combined future should be completed when all futures complete"); } @Test - public void testAnyOf() { + void testAnyOf() { List futures = Arrays.asList(new SimpleFuture(), new SimpleFuture()); SimpleFuture combinedFuture = SimpleFuture.anyOf(futures); futures.get(0).complete(); - Assert.assertTrue( - "Combined future should be completed when any future completes", combinedFuture.isDone()); + Assertions.assertTrue( + combinedFuture.isDone(), "Combined future should be completed when any future completes"); } // Test for when the future is already completed before calling complete() @Test - public void testCompleteAlreadyCompleted() { + void testCompleteAlreadyCompleted() { simpleFuture.complete(); try { simpleFuture.complete(); } catch (Throwable throwable) { - Assert.fail(throwable.getMessage()); + Assertions.fail(throwable.getMessage()); } } // Test for when the future is already completed before calling join() @Test - public void testJoinAlreadyCompleted() { + void testJoinAlreadyCompleted() { simpleFuture.complete(); try { simpleFuture.join(); } catch (Throwable throwable) { - Assert.fail(throwable.getMessage()); + Assertions.fail(throwable.getMessage()); } } } diff --git a/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java b/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java index ef6f4d2628..9fbfa28e37 100644 --- a/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java +++ b/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java @@ -18,19 +18,22 @@ package org.apache.amoro.table.descriptor; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import org.apache.amoro.AmoroCatalog; import org.apache.amoro.AmoroTable; import org.apache.amoro.formats.AmoroCatalogTestHelper; import org.apache.amoro.hive.TestHMS; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.ClassRule; -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.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.IOException; +import java.nio.file.Path; import java.util.List; import java.util.concurrent.Executors; @@ -40,9 +43,9 @@ public abstract class TestServerTableDescriptor { protected static final String TEST_TABLE = "test_table"; - @Rule public TemporaryFolder temp = new TemporaryFolder(); + @TempDir Path tempDir; - @ClassRule public static TestHMS TEST_HMS = new TestHMS(); + protected static TestHMS TEST_HMS; private final AmoroCatalogTestHelper amoroCatalogTestHelper; private AmoroCatalog amoroCatalog; @@ -52,9 +55,22 @@ public TestServerTableDescriptor(AmoroCatalogTestHelper amoroCatalogTestHelpe this.amoroCatalogTestHelper = amoroCatalogTestHelper; } - @Before - public void before() throws IOException { - String path = temp.newFolder().getPath(); + @BeforeAll + static void setupTestHMS() throws Exception { + TEST_HMS = new TestHMS(); + TEST_HMS.before(); + } + + @AfterAll + static void teardownTestHMS() { + if (TEST_HMS != null) { + TEST_HMS.after(); + } + } + + @BeforeEach + void before() throws IOException { + String path = tempDir.toFile().getAbsolutePath(); amoroCatalogTestHelper.initWarehouse(path); amoroCatalogTestHelper.initHiveConf(TEST_HMS.getHiveConf()); this.amoroCatalog = amoroCatalogTestHelper.amoroCatalog(); @@ -68,8 +84,8 @@ public void before() throws IOException { } } - @After - public void after() throws IOException { + @AfterEach + void after() throws IOException { try { this.amoroCatalogTestHelper.amoroCatalog().dropTable(TEST_DB, TEST_TABLE, true); this.amoroCatalogTestHelper.amoroCatalog().dropDatabase(TEST_DB); @@ -79,7 +95,7 @@ public void after() throws IOException { } @Test - public void tableOperations() throws Exception { + void tableOperations() throws Exception { FormatTableDescriptor tableDescriptor = getTableDescriptor(); tableDescriptor.withIoExecutor(Executors.newSingleThreadExecutor()); @@ -111,44 +127,44 @@ public void tableOperations() throws Exception { List tableOperations = tableDescriptor.getTableOperations(table); - Assert.assertEquals( - tableOperations.get(0).getDdl(), "ALTER TABLE test_table SET TBLPROPERTIES ('k1' = 'v1')"); + assertEquals( + "ALTER TABLE test_table SET TBLPROPERTIES ('k1' = 'v1')", tableOperations.get(0).getDdl()); - Assert.assertEquals( - tableOperations.get(1).getDdl(), "ALTER TABLE test_table UNSET TBLPROPERTIES ('k1')"); + assertEquals( + "ALTER TABLE test_table UNSET TBLPROPERTIES ('k1')", tableOperations.get(1).getDdl()); - Assert.assertTrue( + assertTrue( tableOperations .get(2) .getDdl() .equalsIgnoreCase("ALTER TABLE test_table ADD COLUMNS (new_col int)")); - Assert.assertTrue( + assertTrue( tableOperations .get(3) .getDdl() .equalsIgnoreCase("ALTER TABLE test_table RENAME COLUMN new_col TO renamed_col")); - Assert.assertTrue( + assertTrue( tableOperations .get(4) .getDdl() .equalsIgnoreCase("ALTER TABLE test_table ALTER COLUMN renamed_col TYPE BIGINT")); - Assert.assertTrue( + assertTrue( tableOperations .get(5) .getDdl() .equalsIgnoreCase( "ALTER TABLE test_table ALTER COLUMN renamed_col COMMENT 'new comment'")); - Assert.assertTrue( + assertTrue( tableOperations .get(6) .getDdl() .equalsIgnoreCase("ALTER TABLE test_table ALTER COLUMN renamed_col DROP NOT NULL")); - Assert.assertTrue( + assertTrue( tableOperations .get(7) .getDdl() diff --git a/amoro-common/src/test/java/org/apache/amoro/utils/JacksonUtilTest.java b/amoro-common/src/test/java/org/apache/amoro/utils/JacksonUtilTest.java index c4c1e30af4..c6c6a92cc4 100644 --- a/amoro-common/src/test/java/org/apache/amoro/utils/JacksonUtilTest.java +++ b/amoro-common/src/test/java/org/apache/amoro/utils/JacksonUtilTest.java @@ -18,13 +18,13 @@ package org.apache.amoro.utils; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.JsonNode; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Collections; @@ -36,7 +36,7 @@ public class JacksonUtilTest { @Test - public void jacksonTest() { + void jacksonTest() { JsonTestBean testObject = new JsonTestBean(); testObject.setBoolValue(true); diff --git a/amoro-common/src/test/java/org/apache/amoro/utils/MemorySizeTest.java b/amoro-common/src/test/java/org/apache/amoro/utils/MemorySizeTest.java index f65454e81c..1f9c6db4d3 100644 --- a/amoro-common/src/test/java/org/apache/amoro/utils/MemorySizeTest.java +++ b/amoro-common/src/test/java/org/apache/amoro/utils/MemorySizeTest.java @@ -19,13 +19,11 @@ package org.apache.amoro.utils; import static org.apache.amoro.utils.MemorySize.MemoryUnit.MEGA_BYTES; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.CsvSource; @@ -68,9 +66,13 @@ void testUnitConversion( assertEquals(expectedTebiBytes, memorySize.getTebiBytes()); } - @Test(expected = IllegalArgumentException.class) - public void testInvalid() { - new MemorySize(-1); + @Test + void testInvalid() { + assertThrows( + IllegalArgumentException.class, + () -> { + new MemorySize(-1); + }); } @ParameterizedTest @@ -81,7 +83,7 @@ public void testInvalid() { "'1234bytes', 1234", "'1234 bytes', 1234" }) - public void testParseBytes(String input, long expected) { + void testParseBytes(String input, long expected) { assertEquals(expected, MemorySize.parseBytes(input)); } @@ -94,7 +96,7 @@ public void testParseBytes(String input, long expected) { "667766kibibytes, 667766", "667766 kibibytes, 667766" }) - public void testParseKibiBytes(String input, int expected) { + void testParseKibiBytes(String input, int expected) { assertEquals(expected, MemorySize.parse(input).getKibiBytes()); } @@ -107,7 +109,7 @@ public void testParseKibiBytes(String input, int expected) { "7657623mebibytes, 7657623", "7657623 mebibytes, 7657623" }) - public void testParseMebiBytes(String input, int expected) { + void testParseMebiBytes(String input, int expected) { assertEquals(expected, MemorySize.parse(input).getMebiBytes()); } @@ -120,7 +122,7 @@ public void testParseMebiBytes(String input, int expected) { "987654gibibytes, 987654", "987654 gibibytes, 987654" }) - public void testParseGibiBytes(String input, int expected) { + void testParseGibiBytes(String input, int expected) { assertEquals(expected, MemorySize.parse(input).getGibiBytes()); } @@ -133,12 +135,12 @@ public void testParseGibiBytes(String input, int expected) { "1234567tebibytes, 1234567", "1234567 tebibytes, 1234567" }) - public void testParseTebiBytes(String input, int expected) { + void testParseTebiBytes(String input, int expected) { assertEquals(expected, MemorySize.parse(input).getTebiBytes()); } @Test - public void testUpperCase() { + void testUpperCase() { assertEquals(1L, MemorySize.parse("1 B").getBytes()); assertEquals(1L, MemorySize.parse("1 K").getKibiBytes()); assertEquals(1L, MemorySize.parse("1 M").getMebiBytes()); @@ -147,75 +149,83 @@ public void testUpperCase() { } @Test - public void testTrimBeforeParse() { + void testTrimBeforeParse() { assertEquals(155L, MemorySize.parseBytes(" 155 ")); assertEquals(155L, MemorySize.parseBytes(" 155 bytes ")); } @Test - public void testParseInvalid() { + void testParseInvalid() { // null - try { - MemorySize.parseBytes(null); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes(null); + }); // empty - try { - MemorySize.parseBytes(""); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes(""); + }); // blank - try { - MemorySize.parseBytes(" "); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes(" "); + }); // no number - try { - MemorySize.parseBytes("foobar or fubar or foo bazz"); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes("foobar or fubar or foo bazz"); + }); // wrong unit - try { - MemorySize.parseBytes("16 gjah"); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes("16 gjah"); + }); // multiple numbers - try { - MemorySize.parseBytes("16 16 17 18 bytes"); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes("16 16 17 18 bytes"); + }); // negative number - try { - MemorySize.parseBytes("-100 bytes"); - fail("exception expected"); - } catch (IllegalArgumentException ignored) { - } + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes("-100 bytes"); + }); } - @Test(expected = IllegalArgumentException.class) - public void testParseNumberOverflow() { - MemorySize.parseBytes("100000000000000000000000000000000 bytes"); + @Test + void testParseNumberOverflow() { + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes("100000000000000000000000000000000 bytes"); + }); } - @Test(expected = IllegalArgumentException.class) - public void testParseNumberTimeUnitOverflow() { - MemorySize.parseBytes("100000000000000 tb"); + @Test + void testParseNumberTimeUnitOverflow() { + assertThrows( + IllegalArgumentException.class, + () -> { + MemorySize.parseBytes("100000000000000 tb"); + }); } @Test - public void testParseWithDefaultUnit() { + void testParseWithDefaultUnit() { assertEquals(7, MemorySize.parse("7", MEGA_BYTES).getMebiBytes()); assertNotEquals(7, MemorySize.parse("7340032", MEGA_BYTES)); assertEquals(7, MemorySize.parse("7m", MEGA_BYTES).getMebiBytes()); @@ -229,15 +239,19 @@ public void testParseWithDefaultUnit() { } @Test - public void testDivideByLong() { + void testDivideByLong() { final MemorySize memory = new MemorySize(100L); - assertThat(memory.divide(23), is(new MemorySize(4L))); + assertEquals(new MemorySize(4L), memory.divide(23)); } - @Test(expected = IllegalArgumentException.class) - public void testDivideByNegativeLong() { + @Test + void testDivideByNegativeLong() { final MemorySize memory = new MemorySize(100L); - memory.divide(-23L); + assertThrows( + IllegalArgumentException.class, + () -> { + memory.divide(-23L); + }); } static Stream testToHumanReadableStringProvider() { @@ -255,7 +269,7 @@ static Stream testToHumanReadableStringProvider() { @ParameterizedTest @MethodSource("testToHumanReadableStringProvider") - public void testToHumanReadableString(long bytes, String expected) { - assertThat(new MemorySize(bytes).toHumanReadableString(), is(expected)); + void testToHumanReadableString(long bytes, String expected) { + assertEquals(expected, new MemorySize(bytes).toHumanReadableString()); } } diff --git a/pom.xml b/pom.xml index 641407b88e..57972aca60 100644 --- a/pom.xml +++ b/pom.xml @@ -1,21 +1,21 @@ +~ Licensed to the Apache Software Foundation (ASF) under one +~ or more contributor license agreements. See the NOTICE file +~ distributed with this work for additional information +~ regarding copyright ownership. The ASF licenses this file +~ to you under the Apache License, Version 2.0 (the +~ "License"); you may not use this file except in compliance +~ with the License. You may obtain a copy of the License at +~ +~ http://www.apache.org/licenses/LICENSE-2.0 +~ +~ Unless required by applicable law or agreed to in writing, software +~ distributed under the License is distributed on an "AS IS" BASIS, +~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +~ See the License for the specific language governing permissions and +~ limitations under the License. +--> 4.0.0 @@ -1280,6 +1280,14 @@ ${mockito.version} test + + + org.junit.vintage + junit-vintage-engine + ${junit5.version} + test + + @@ -1334,6 +1342,12 @@ test + + org.junit.vintage + junit-vintage-engine + test + + org.apache.logging.log4j @@ -1686,7 +1700,7 @@ log4j-test.properties log4j2-test.properties + Otherwise, this might cause SecurityExceptions when using the JAR. --> META-INF/*.SF META-INF/*.DSA META-INF/*.RSA