Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
package org.apache.amoro;

import org.apache.amoro.data.DataTreeNode;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class TestTreeNode {

@Test(expected = IllegalArgumentException.class)
@Test
public void testParentNodeIllegal() {
DataTreeNode.of(0, 0).parent();
Assertions.assertThrows(IllegalArgumentException.class, () -> DataTreeNode.of(0, 0).parent());
}

@ParameterizedTest
Expand All @@ -40,12 +40,12 @@ public void testParentNode(int level, int index) {
@ParameterizedTest
@CsvSource({"0, 0, 1", "1, 0, 2", "1, 1, 3", "3, 0, 4", "3, 3, 7", "7, 5, 13", "7, 3, 11"})
public void testNodeId(int mask, int index, int expectedId) {
Assert.assertEquals(expectedId, DataTreeNode.of(mask, index).getId());
Assert.assertEquals(DataTreeNode.of(mask, index), DataTreeNode.ofId(expectedId));
Assertions.assertEquals(expectedId, DataTreeNode.of(mask, index).getId());
Assertions.assertEquals(DataTreeNode.of(mask, index), DataTreeNode.ofId(expectedId));
}

private void assertParentNode(DataTreeNode node) {
Assert.assertEquals(node, node.left().parent());
Assert.assertEquals(node, node.right().parent());
Assertions.assertEquals(node, node.left().parent());
Assertions.assertEquals(node, node.right().parent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,58 @@
import org.apache.amoro.shade.guava32.com.google.common.collect.Lists;
import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
import org.apache.amoro.table.TableMetaStore;
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.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.file.Path;
import java.util.stream.Stream;

@RunWith(Parameterized.class)
public class TestUnifiedCatalog {

@ClassRule public static TestAms testAms = new TestAms();
private static final TestAms testAms = new TestAms();

@Rule public TemporaryFolder warehouse = new TemporaryFolder();
@TempDir public Path warehouse;

@Parameterized.Parameters
public static Object[] parameters() {
return new Object[] {
TestedCatalogs.hadoopCatalog(TableFormat.ICEBERG),
};
@BeforeAll
public static void startTestAms() throws Exception {
testAms.before();
}

private final CatalogTestHelper testedCatalog;
private CatalogMeta meta;
@AfterAll
public static void stopTestAms() {
testAms.after();
}

public TestUnifiedCatalog(CatalogTestHelper testedCatalog) {
this.testedCatalog = testedCatalog;
public static Stream<Arguments> parameters() {
return Stream.of(Arguments.of(TestedCatalogs.hadoopCatalog(TableFormat.ICEBERG)));
}

@Before
public void setupCatalogMeta() {
meta = testedCatalog.buildCatalogMeta(warehouse.getRoot().getAbsolutePath());
private CatalogMeta setupCatalogMeta(CatalogTestHelper testedCatalog) {
CatalogMeta meta = testedCatalog.buildCatalogMeta(warehouse.toFile().getAbsolutePath());
testAms.getAmsHandler().dropCatalog(meta.getCatalogName());
testAms.getAmsHandler().createCatalog(meta);
return meta;
}

@Test
public void testCatalogLoader() {
@ParameterizedTest
@MethodSource("parameters")
public void testCatalogLoader(CatalogTestHelper testedCatalog) {
CatalogMeta meta = setupCatalogMeta(testedCatalog);
UnifiedCatalog unifiedCatalog =
UnifiedCatalogLoader.loadUnifiedCatalog(
testAms.getServerUrl(), meta.getCatalogName(), Maps.newHashMap());
validateUnifiedCatalog(unifiedCatalog);
}

@Test
public void testCreateUnifiedCatalog() {
@ParameterizedTest
@MethodSource("parameters")
public void testCreateUnifiedCatalog(CatalogTestHelper testedCatalog) {
CatalogMeta meta = setupCatalogMeta(testedCatalog);
UnifiedCatalog unifiedCatalog =
new CommonUnifiedCatalog(
meta.getCatalogName(),
Expand All @@ -80,13 +85,14 @@ public void testCreateUnifiedCatalog() {
}

private void validateUnifiedCatalog(UnifiedCatalog unifiedCatalog) {
Assert.assertNotNull(unifiedCatalog);
Assert.assertEquals(CommonUnifiedCatalog.class.getName(), unifiedCatalog.getClass().getName());
Assertions.assertNotNull(unifiedCatalog);
Assertions.assertEquals(
CommonUnifiedCatalog.class.getName(), unifiedCatalog.getClass().getName());

unifiedCatalog.createDatabase(TableTestHelper.TEST_DB_NAME);
Assert.assertEquals(
Assertions.assertEquals(
Lists.newArrayList(TableTestHelper.TEST_DB_NAME), unifiedCatalog.listDatabases());
Assert.assertEquals(0, unifiedCatalog.listTables(TableTestHelper.TEST_DB_NAME).size());
Assertions.assertEquals(0, unifiedCatalog.listTables(TableTestHelper.TEST_DB_NAME).size());
unifiedCatalog.dropDatabase(TableTestHelper.TEST_DB_NAME);
unifiedCatalog.refresh();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,99 @@
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;

/**
* Dual-mode base class supporting both JUnit 4 and JUnit 5 subclasses.
*
* <p>JUnit 4 path (used by cross-module {@code @RunWith(Parameterized.class)} subclasses in
* amoro-format-mixed-hive and amoro-ams that have not yet migrated): the legacy {@link
* #CatalogTestBase(CatalogTestHelper)} constructor is invoked by the parameterized runner, the
* {@code @ClassRule} {@link #TEST_AMS} starts the mock AMS, the {@code @Rule} {@link #temp} manages
* a temporary folder, and the {@code @Before} {@link #setupCatalog()} / {@code @After} {@link
* #dropCatalog()} fire automatically.
*
* <p>JUnit 5 path (used by in-module subclasses migrated as part of #4203): the no-arg constructor
* is used, {@link #startTestAms()} / {@link #stopTestAms()} drive the mock AMS via
* {@code @BeforeAll} / {@code @AfterAll}, and subclasses explicitly call {@link
* #setupCatalog(CatalogTestHelper)} from their {@code @ParameterizedTest} method bodies (or from a
* {@code @BeforeEach} when the helper is fixed). The {@link #temp} field is initialised manually
* inside {@link #setupCatalog(CatalogTestHelper)} when null because JUnit 4 {@code @Rule} does not
* fire under the JUnit Platform engine.
*
* <p>This dual plumbing is removed in the closing PR of #4203 once all cross-module subclasses
* migrate to JUnit 5.
*/
public abstract class CatalogTestBase {

@ClassRule public static TestAms TEST_AMS = new TestAms();
private final CatalogTestHelper testHelper;

@Rule public TemporaryFolder temp = new TemporaryFolder();

protected CatalogTestHelper testHelper;
private UnifiedCatalog unifiedCatalog;
private MixedFormatCatalog mixedFormatCatalog;
private CatalogMeta catalogMeta;
private org.apache.iceberg.catalog.Catalog icebergCatalog;
private boolean tempInitializedByJunit5;

/** JUnit 4 constructor — kept for cross-module {@code @RunWith(Parameterized.class)} callers. */
public CatalogTestBase(CatalogTestHelper testHelper) {
this.testHelper = testHelper;
}

/** JUnit 5 constructor — used by in-module migrated subclasses. */
public CatalogTestBase() {}

@BeforeAll
public static void startTestAms() throws Exception {
TEST_AMS.before();
}

@AfterAll
public static void stopTestAms() {
TEST_AMS.after();
}

public static MockAmoroManagementServer.AmsHandler getAmsHandler() {
return TEST_AMS.getAmsHandler();
}

/** JUnit 4 lifecycle — fires when subclass uses {@code @RunWith(Parameterized)}. */
@Before
public void setupCatalog() throws IOException {
if (testHelper != null) {
doSetupCatalog();
}
}

/**
* JUnit 5 entry point — invoke from {@code @ParameterizedTest} method body or
* {@code @BeforeEach}.
*/
protected void setupCatalog(CatalogTestHelper helper) throws IOException {
this.testHelper = helper;
// JUnit 4 @Rule does not fire under JUnit Platform; manually create the temp folder.
if (!tempInitializedByJunit5) {
if (temp == null) {
temp = new TemporaryFolder();
}
try {
temp.create();
} catch (IOException e) {
throw e;
}
tempInitializedByJunit5 = true;
}
doSetupCatalog();
}

private void doSetupCatalog() throws IOException {
String baseDir = temp.newFolder().getPath();
if (!SystemUtils.IS_OS_UNIX) {
baseDir = "file:/" + temp.newFolder().getPath().replace("\\", "/");
Expand All @@ -65,11 +134,17 @@ public void setupCatalog() throws IOException {
}

@After
@AfterEach
public void dropCatalog() {
if (catalogMeta != null) {
getAmsHandler().dropCatalog(catalogMeta.getCatalogName());
mixedFormatCatalog = null;
}
if (tempInitializedByJunit5 && temp != null) {
temp.delete();
temp = null;
tempInitializedByJunit5 = false;
}
}

protected MixedFormatCatalog getMixedFormatCatalog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,55 @@
import org.apache.amoro.utils.MixedTableUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;

import java.io.IOException;

/**
* Dual-mode base class supporting both JUnit 4 and JUnit 5 subclasses. See {@link CatalogTestBase}
* for the full dual-mode rationale; the same constraints apply here. Cross-module
* {@code @RunWith(Parameterized.class)} subclasses keep the legacy two-arg constructor and rely on
* the {@code @Before} / {@code @After} hooks. In-module JUnit 5 subclasses use the no-arg
* constructor and explicitly call {@link #setupTable(CatalogTestHelper, TableTestHelper)} from
* their {@code @ParameterizedTest} method body or {@code @BeforeEach}.
*/
public abstract class TableTestBase extends CatalogTestBase {

private final TableTestHelper tableTestHelper;
protected TableTestHelper tableTestHelper;
private MixedTable mixedTable;
private TableMetaStore tableMetaStore;

/** JUnit 4 constructor — kept for cross-module {@code @RunWith(Parameterized.class)} callers. */
public TableTestBase(CatalogTestHelper catalogTestHelper, TableTestHelper tableTestHelper) {
super(catalogTestHelper);
this.tableTestHelper = tableTestHelper;
}

/** JUnit 5 constructor — used by in-module migrated subclasses. */
public TableTestBase() {
super();
}

/** JUnit 4 lifecycle — fires when subclass uses {@code @RunWith(Parameterized)}. */
@Before
public void setupTable() {
if (tableTestHelper != null) {
doSetupTable();
}
}

/**
* JUnit 5 entry point — invoke from {@code @ParameterizedTest} method body or
* {@code @BeforeEach}.
*/
protected void setupTable(CatalogTestHelper catalogTestHelper, TableTestHelper tableTestHelper)
throws IOException {
setupCatalog(catalogTestHelper);
this.tableTestHelper = tableTestHelper;
doSetupTable();
}

private void doSetupTable() {
this.tableMetaStore = CatalogUtil.buildMetaStore(getCatalogMeta());

getUnifiedCatalog().createDatabase(TableTestHelper.TEST_DB_NAME);
Expand Down Expand Up @@ -83,7 +118,11 @@ private void createIcebergFormatTable() {
}

@After
@AfterEach
public void dropTable() {
if (tableTestHelper == null) {
return;
}
getUnifiedCatalog()
.dropTable(tableTestHelper.id().getDatabase(), tableTestHelper.id().getTableName(), true);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@
import org.apache.amoro.mixed.MixedFormatCatalog;
import org.apache.amoro.properties.CatalogMetaProperties;
import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Map;

public class TestCatalogLoader {

private static final String TEST_CATALOG_NAME = "test";
@ClassRule public static TestAms TEST_AMS = new TestAms();
private static final TestAms TEST_AMS = new TestAms();

@BeforeAll
public static void startTestAms() throws Exception {
TEST_AMS.before();
}

@AfterAll
public static void stopTestAms() {
TEST_AMS.after();
}

@Test
public void testLoadMixedIcebergCatalog() {
Expand All @@ -49,17 +60,18 @@ public void testLoadMixedIcebergCatalog() {
TableFormat.MIXED_ICEBERG);
TEST_AMS.getAmsHandler().createCatalog(catalogMeta);
MixedFormatCatalog loadCatalog = CatalogLoader.load(getCatalogUrl(TEST_CATALOG_NAME));
Assert.assertEquals(TEST_CATALOG_NAME, loadCatalog.name());
Assert.assertEquals(BasicMixedIcebergCatalog.class.getName(), loadCatalog.getClass().getName());
Assertions.assertEquals(TEST_CATALOG_NAME, loadCatalog.name());
Assertions.assertEquals(
BasicMixedIcebergCatalog.class.getName(), loadCatalog.getClass().getName());
TEST_AMS.getAmsHandler().dropCatalog(TEST_CATALOG_NAME);
}

@Test
public void testLoadNotExistedCatalog() {
Assert.assertThrows(
"catalog not found, please check catalog name",
Assertions.assertThrows(
IllegalArgumentException.class,
() -> CatalogLoader.load(getCatalogUrl(TEST_CATALOG_NAME)));
() -> CatalogLoader.load(getCatalogUrl(TEST_CATALOG_NAME)),
"catalog not found, please check catalog name");
}

private String getCatalogUrl(String catalogName) {
Expand Down
Loading
Loading