From c1e877a567b5b5f4b4895b7ec9b1cf9b449d4bfe Mon Sep 17 00:00:00 2001 From: Song Minseok <99078115+skytin1004@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:41:03 +0900 Subject: [PATCH] Core: Switch tests to JUnit5 in avro, data.avro packages (#8380) --- .../iceberg/avro/TestAvroNameMapping.java | 78 ++--- .../iceberg/avro/TestAvroReadProjection.java | 21 +- .../iceberg/avro/TestBuildAvroProjection.java | 94 +++--- .../iceberg/avro/TestReadProjection.java | 270 +++++++++++------- .../data/avro/TestDecoderResolver.java | 30 +- 5 files changed, 278 insertions(+), 215 deletions(-) diff --git a/core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java b/core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java index 2efb5c8f9af3..f4c7ee883e49 100644 --- a/core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java +++ b/core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java @@ -41,8 +41,7 @@ import org.apache.iceberg.types.Comparators; import org.apache.iceberg.types.Types; import org.assertj.core.api.Assertions; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; @SuppressWarnings("unchecked") public class TestAvroNameMapping extends TestAvroReadProjection { @@ -81,11 +80,13 @@ public void testMapProjections() throws IOException { Record projected = writeAndRead(writeSchema, readSchema, record, nameMapping); // field id 5 comes from read schema - Assert.assertNotNull( - "Field missing from table mapping is renamed", - projected.getSchema().getField("location_r5")); - Assert.assertNull("location field should not be read", projected.get("location_r5")); - Assert.assertEquals(34L, projected.get("id")); + Assertions.assertThat(projected.getSchema().getField("location_r5")) + .as("Field missing from table mapping is renamed") + .isNotNull(); + Assertions.assertThat(projected.get("location_r5")) + .as("location field should not be read") + .isNull(); + Assertions.assertThat(projected.get("id")).isEqualTo(34L); // Table mapping partially project `location` map value nameMapping = @@ -104,9 +105,12 @@ public void testMapProjections() throws IOException { projected = writeAndRead(writeSchema, readSchema, record, nameMapping); Record projectedL1 = ((Map) projected.get("location")).get("l1"); - Assert.assertNotNull( - "Field missing from table mapping is renamed", projectedL1.getSchema().getField("long_r2")); - Assert.assertNull("location.value.long, should not be read", projectedL1.get("long_r2")); + Assertions.assertThat(projectedL1.getSchema().getField("long_r2")) + .as("Field missing from table mapping is renamed") + .isNotNull(); + Assertions.assertThat(projectedL1.get("long_r2")) + .as("location.value.long, should not be read") + .isNull(); } @Test @@ -176,13 +180,15 @@ public void testComplexMapKeys() throws IOException { Map projectedLocation = (Map) projected.get("location"); Record projectedKey = projectedLocation.keySet().iterator().next(); Record projectedValue = projectedLocation.values().iterator().next(); - Assert.assertEquals( - 0, Comparators.charSequences().compare("k1", (CharSequence) projectedKey.get("k1"))); - Assert.assertEquals( - 0, Comparators.charSequences().compare("k2", (CharSequence) projectedKey.get("k2"))); - Assert.assertEquals(52.995143f, projectedValue.get("lat")); - Assert.assertNotNull(projectedValue.getSchema().getField("long_r2")); - Assert.assertNull(projectedValue.get("long_r2")); + Assertions.assertThat( + Comparators.charSequences().compare("k1", (CharSequence) projectedKey.get("k1"))) + .isEqualTo(0); + Assertions.assertThat( + Comparators.charSequences().compare("k2", (CharSequence) projectedKey.get("k2"))) + .isEqualTo(0); + Assertions.assertThat(projectedValue.get("lat")).isEqualTo(52.995143f); + Assertions.assertThat(projectedValue.getSchema().getField("long_r2")).isNotNull(); + Assertions.assertThat(projectedValue.get("long_r2")).isNull(); } @Test @@ -243,11 +249,11 @@ public void testArrayProjections() throws Exception { Schema readSchema = writeSchema; Record projected = writeAndRead(writeSchema, readSchema, record, nameMapping); - Assert.assertNotNull( - "Field missing from table mapping is renamed", projected.getSchema().getField("point_r22")); - Assert.assertNull("point field is not projected", projected.get("point_r22")); - Assert.assertEquals(34L, projected.get("id")); - + Assertions.assertThat(projected.getSchema().getField("point_r22")) + .as("Field missing from table mapping is renamed") + .isNotNull(); + Assertions.assertThat(projected.get("point_r22")).as("point field is not projected").isNull(); + Assertions.assertThat(projected.get("id")).isEqualTo(34L); // point array is partially projected nameMapping = MappingUtil.create( @@ -263,12 +269,12 @@ public void testArrayProjections() throws Exception { projected = writeAndRead(writeSchema, readSchema, record, nameMapping); Record point = ((List) projected.get("point")).get(0); - - Assert.assertNotNull( - "Field missing from table mapping is renamed", point.getSchema().getField("y_r18")); - Assert.assertEquals("point.x is projected", 1, point.get("x")); - Assert.assertNull("point.y is not projected", point.get("y_r18")); - Assert.assertEquals(34L, projected.get("id")); + Assertions.assertThat(point.getSchema().getField("y_r18")) + .as("Field missing from table mapping is renamed") + .isNotNull(); + Assertions.assertThat(point.get("x")).as("point.x is projected").isEqualTo(1); + Assertions.assertThat(point.get("y_r18")).as("point.y is not projected").isNull(); + Assertions.assertThat(projected.get("id")).isEqualTo(34L); } @Test @@ -316,8 +322,9 @@ public void testAliases() throws IOException { Types.NestedField.required(19, "y", Types.IntegerType.get()))))); Record projected = writeAndRead(writeSchema, readSchema, record, nameMapping); - Assert.assertEquals( - "x is read as y", 1, ((List) projected.get("points")).get(0).get("y")); + Assertions.assertThat(((List) projected.get("points")).get(0).get("y")) + .as("x is read as y") + .isEqualTo(1); readSchema = new Schema( @@ -331,8 +338,9 @@ public void testAliases() throws IOException { Types.NestedField.required(19, "z", Types.IntegerType.get()))))); projected = writeAndRead(writeSchema, readSchema, record, nameMapping); - Assert.assertEquals( - "x is read as z", 1, ((List) projected.get("points")).get(0).get("z")); + Assertions.assertThat(((List) projected.get("points")).get(0).get("z")) + .as("x is read as z") + .isEqualTo(1); } @Test @@ -349,7 +357,7 @@ public void testInferredMapping() throws IOException { Schema readSchema = writeSchema; // Pass null for nameMapping so that it is automatically inferred from read schema Record projected = writeAndRead(writeSchema, readSchema, record, null); - Assert.assertEquals(record, projected); + Assertions.assertThat(projected).isEqualTo(record); } @Test @@ -369,7 +377,7 @@ protected Record writeAndRead( Record record = super.writeAndRead(desc, writeSchema, readSchema, inputRecord); Record projectedWithNameMapping = writeAndRead(writeSchema, readSchema, inputRecord, MappingUtil.create(writeSchema)); - Assert.assertEquals(record, projectedWithNameMapping); + Assertions.assertThat(projectedWithNameMapping).isEqualTo(record); return record; } @@ -377,7 +385,7 @@ private Record writeAndRead( Schema writeSchema, Schema readSchema, Record record, NameMapping nameMapping) throws IOException { - File file = temp.newFile(); + File file = temp.resolve("test.avro").toFile(); // Write without file ids org.apache.avro.Schema writeAvroSchema = RemoveIds.removeIds(writeSchema); DatumWriter datumWriter = new GenericDatumWriter<>(writeAvroSchema); diff --git a/core/src/test/java/org/apache/iceberg/avro/TestAvroReadProjection.java b/core/src/test/java/org/apache/iceberg/avro/TestAvroReadProjection.java index 0049357def77..e3aca9baef8d 100644 --- a/core/src/test/java/org/apache/iceberg/avro/TestAvroReadProjection.java +++ b/core/src/test/java/org/apache/iceberg/avro/TestAvroReadProjection.java @@ -30,16 +30,15 @@ import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; import org.apache.iceberg.relocated.com.google.common.collect.Iterables; import org.apache.iceberg.types.Types; -import org.junit.Assert; -import org.junit.Test; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; public class TestAvroReadProjection extends TestReadProjection { @Override protected GenericData.Record writeAndRead( String desc, Schema writeSchema, Schema readSchema, GenericData.Record record) throws IOException { - File file = temp.newFile(desc + ".avro"); - file.delete(); + File file = temp.resolve(desc + ".avro").toFile(); try (FileAppender appender = Avro.write(Files.localOutput(file)).schema(writeSchema).build()) { @@ -73,13 +72,11 @@ public void testAvroArrayAsLogicalMap() throws IOException { GenericData.Record projected = writeAndRead("full_projection", writeSchema, writeSchema, record); - Assert.assertEquals( - "Should contain correct value list", - values1, - ((Map>) projected.get("map")).get(100L)); - Assert.assertEquals( - "Should contain correct value list", - values2, - ((Map>) projected.get("map")).get(200L)); + Assertions.assertThat(((Map>) projected.get("map")).get(100L)) + .as("Should contain correct value list") + .isEqualTo(values1); + Assertions.assertThat(((Map>) projected.get("map")).get(200L)) + .as("Should contain correct value list") + .isEqualTo(values2); } } diff --git a/core/src/test/java/org/apache/iceberg/avro/TestBuildAvroProjection.java b/core/src/test/java/org/apache/iceberg/avro/TestBuildAvroProjection.java index edee46685e32..40c04de050db 100644 --- a/core/src/test/java/org/apache/iceberg/avro/TestBuildAvroProjection.java +++ b/core/src/test/java/org/apache/iceberg/avro/TestBuildAvroProjection.java @@ -19,14 +19,14 @@ package org.apache.iceberg.avro; import static org.apache.iceberg.types.Types.NestedField.optional; -import static org.junit.Assert.assertEquals; import java.util.Collections; import java.util.function.Supplier; import org.apache.avro.SchemaBuilder; import org.apache.iceberg.types.Type; import org.apache.iceberg.types.Types; -import org.junit.Test; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; public class TestBuildAvroProjection { @@ -68,11 +68,13 @@ public void projectArrayWithElementSchemaUnchanged() { final org.apache.avro.Schema actual = testSubject.array(expected, supplier); - assertEquals("Array projection produced undesired array schema", expected, actual); - assertEquals( - "Unexpected element ID discovered on the projected array schema", - 0, - Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()); + Assertions.assertThat(actual) + .as("Array projection produced undesired array schema") + .isEqualTo(expected); + Assertions.assertThat( + Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()) + .as("Unexpected element ID discovered on the projected array schema") + .isEqualTo(0); } @Test @@ -141,11 +143,13 @@ public void projectArrayWithExtraFieldInElementSchema() { final org.apache.avro.Schema actual = testSubject.array(extraField, supplier); - assertEquals("Array projection produced undesired array schema", expected, actual); - assertEquals( - "Unexpected element ID discovered on the projected array schema", - 0, - Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()); + Assertions.assertThat(actual) + .as("Array projection produced undesired array schema") + .isEqualTo(expected); + Assertions.assertThat( + Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()) + .as("Unexpected element ID discovered on the projected array schema") + .isEqualTo(0); } @Test @@ -202,11 +206,13 @@ public void projectArrayWithLessFieldInElementSchema() { final org.apache.avro.Schema actual = testSubject.array(lessField, supplier); - assertEquals("Array projection produced undesired array schema", expected, actual); - assertEquals( - "Unexpected element ID discovered on the projected array schema", - 0, - Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()); + Assertions.assertThat(actual) + .as("Array projection produced undesired array schema") + .isEqualTo(expected); + Assertions.assertThat( + Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()) + .as("Unexpected element ID discovered on the projected array schema") + .isEqualTo(0); } @Test @@ -250,15 +256,15 @@ public void projectMapWithValueSchemaUnchanged() { final org.apache.avro.Schema actual = testSubject.map(expected, supplier); - assertEquals("Map projection produced undesired map schema", expected, actual); - assertEquals( - "Unexpected key ID discovered on the projected map schema", - 0, - Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()); - assertEquals( - "Unexpected value ID discovered on the projected map schema", - 1, - Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()); + Assertions.assertThat(actual) + .as("Map projection produced undesired map schema") + .isEqualTo(expected); + Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()) + .as("Unexpected key ID discovered on the projected map schema") + .isEqualTo(0); + Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()) + .as("Unexpected value ID discovered on the projected map schema") + .isEqualTo(1); } @Test @@ -331,15 +337,15 @@ public void projectMapWithExtraFieldInValueSchema() { final org.apache.avro.Schema actual = testSubject.map(extraField, supplier); - assertEquals("Map projection produced undesired map schema", expected, actual); - assertEquals( - "Unexpected key ID discovered on the projected map schema", - 0, - Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()); - assertEquals( - "Unexpected value ID discovered on the projected map schema", - 1, - Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()); + Assertions.assertThat(actual) + .as("Map projection produced undesired map schema") + .isEqualTo(expected); + Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()) + .as("Unexpected key ID discovered on the projected map schema") + .isEqualTo(0); + Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()) + .as("Unexpected value ID discovered on the projected map schema") + .isEqualTo(1); } @Test @@ -400,14 +406,14 @@ public void projectMapWithLessFieldInValueSchema() { final org.apache.avro.Schema actual = testSubject.map(lessField, supplier); - assertEquals("Map projection produced undesired map schema", expected, actual); - assertEquals( - "Unexpected key ID discovered on the projected map schema", - 0, - Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()); - assertEquals( - "Unexpected value ID discovered on the projected map schema", - 1, - Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()); + Assertions.assertThat(actual) + .as("Map projection produced undesired map schema") + .isEqualTo(expected); + Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()) + .as("Unexpected key ID discovered on the projected map schema") + .isEqualTo(0); + Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()) + .as("Unexpected value ID discovered on the projected map schema") + .isEqualTo(1); } } diff --git a/core/src/test/java/org/apache/iceberg/avro/TestReadProjection.java b/core/src/test/java/org/apache/iceberg/avro/TestReadProjection.java index b6b6c2c967d8..331e427861c6 100644 --- a/core/src/test/java/org/apache/iceberg/avro/TestReadProjection.java +++ b/core/src/test/java/org/apache/iceberg/avro/TestReadProjection.java @@ -19,6 +19,7 @@ package org.apache.iceberg.avro; import java.io.IOException; +import java.nio.file.Path; import java.util.List; import java.util.Map; import org.apache.avro.AvroRuntimeException; @@ -28,20 +29,17 @@ import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; import org.apache.iceberg.relocated.com.google.common.collect.Maps; -import org.apache.iceberg.relocated.com.google.common.collect.Sets; import org.apache.iceberg.types.Comparators; import org.apache.iceberg.types.Types; import org.assertj.core.api.Assertions; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public abstract class TestReadProjection { protected abstract Record writeAndRead( String desc, Schema writeSchema, Schema readSchema, Record record) throws IOException; - @Rule public TemporaryFolder temp = new TemporaryFolder(); + @TempDir Path temp; @Test public void testFullProjection() throws Exception { @@ -56,10 +54,11 @@ public void testFullProjection() throws Exception { Record projected = writeAndRead("full_projection", schema, schema, record); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); - + Assertions.assertThat((Long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); int cmp = Comparators.charSequences().compare("test", (CharSequence) projected.get("data")); - Assert.assertTrue("Should contain the correct data value", cmp == 0); + Assertions.assertThat(cmp).as("Should contain the correct data value").isEqualTo(0); } @Test @@ -79,9 +78,10 @@ public void testReorderedFullProjection() throws Exception { Types.NestedField.required(0, "id", Types.LongType.get())); Record projected = writeAndRead("full_projection", schema, reordered, record); - - Assert.assertEquals("Should contain the correct 0 value", "test", projected.get(0).toString()); - Assert.assertEquals("Should contain the correct 1 value", 34L, projected.get(1)); + Assertions.assertThat(projected.get(0).toString()) + .as("Should contain the correct 0 value") + .isEqualTo("test"); + Assertions.assertThat(projected.get(1)).as("Should contain the correct 1 value").isEqualTo(34L); } @Test @@ -102,10 +102,11 @@ public void testReorderedProjection() throws Exception { Types.NestedField.optional(3, "missing_2", Types.LongType.get())); Record projected = writeAndRead("full_projection", schema, reordered, record); - - Assert.assertNull("Should contain the correct 0 value", projected.get(0)); - Assert.assertEquals("Should contain the correct 1 value", "test", projected.get(1).toString()); - Assert.assertNull("Should contain the correct 2 value", projected.get(2)); + Assertions.assertThat(projected.get(0)).as("Should contain the correct 0 value").isNull(); + Assertions.assertThat(projected.get(1).toString()) + .as("Should contain the correct 1 value") + .isEqualTo("test"); + Assertions.assertThat(projected.get(2)).as("Should contain the correct 2 value").isNull(); } @Test @@ -121,7 +122,7 @@ public void testEmptyProjection() throws Exception { Record projected = writeAndRead("empty_projection", schema, schema.select(), record); - Assert.assertNotNull("Should read a non-null record", projected); + Assertions.assertThat(projected).as("Should read a non-null record").isNotNull(); // this is expected because there are no values Assertions.assertThatThrownBy(() -> projected.get(0)) .isInstanceOf(ArrayIndexOutOfBoundsException.class); @@ -142,7 +143,9 @@ public void testBasicProjection() throws Exception { Record projected = writeAndRead("basic_projection_id", writeSchema, idOnly, record); assertEmptyAvroField(projected, "data"); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((Long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); Schema dataOnly = new Schema(Types.NestedField.optional(1, "data", Types.StringType.get())); @@ -150,7 +153,7 @@ public void testBasicProjection() throws Exception { assertEmptyAvroField(projected, "id"); int cmp = Comparators.charSequences().compare("test", (CharSequence) projected.get("data")); - Assert.assertEquals("Should contain the correct data value", 0, cmp); + Assertions.assertThat(cmp).as("Should contain the correct data value").isEqualTo(0); } @Test @@ -171,9 +174,11 @@ public void testRename() throws Exception { Record projected = writeAndRead("project_and_rename", writeSchema, readSchema, record); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((Long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); int cmp = Comparators.charSequences().compare("test", (CharSequence) projected.get("renamed")); - Assert.assertEquals("Should contain the correct data/renamed value", 0, cmp); + Assertions.assertThat(cmp).as("Should contain the correct data/renamed value").isEqualTo(0); } @Test @@ -200,7 +205,9 @@ public void testNestedStructProjection() throws Exception { Record projected = writeAndRead("id_only", writeSchema, idOnly, record); assertEmptyAvroField(projected, "location"); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); Schema latOnly = new Schema( @@ -212,10 +219,11 @@ public void testNestedStructProjection() throws Exception { projected = writeAndRead("latitude_only", writeSchema, latOnly, record); Record projectedLocation = (Record) projected.get("location"); assertEmptyAvroField(projected, "id"); - Assert.assertNotNull("Should project location", projected.get("location")); + Assertions.assertThat(projected.get("location")).as("Should project location").isNotNull(); assertEmptyAvroField(projectedLocation, "long"); - Assert.assertEquals( - "Should project latitude", 52.995143f, (float) projectedLocation.get("lat"), 0.000001f); + Assertions.assertThat((Float) projectedLocation.get("lat")) + .as("Should project latitude") + .isCloseTo(52.995143f, Assertions.within(0.000001f)); Schema longOnly = new Schema( @@ -227,20 +235,23 @@ public void testNestedStructProjection() throws Exception { projected = writeAndRead("longitude_only", writeSchema, longOnly, record); projectedLocation = (Record) projected.get("location"); assertEmptyAvroField(projected, "id"); - Assert.assertNotNull("Should project location", projected.get("location")); + Assertions.assertThat(projected.get("location")).as("Should project location").isNotNull(); assertEmptyAvroField(projectedLocation, "lat"); - Assert.assertEquals( - "Should project longitude", -1.539054f, (float) projectedLocation.get("long"), 0.000001f); + Assertions.assertThat((Float) projectedLocation.get("long")) + .as("Should project longitude") + .isCloseTo(-1.539054f, Assertions.within(0.000001f)); Schema locationOnly = writeSchema.select("location"); projected = writeAndRead("location_only", writeSchema, locationOnly, record); projectedLocation = (Record) projected.get("location"); assertEmptyAvroField(projected, "id"); - Assert.assertNotNull("Should project location", projected.get("location")); - Assert.assertEquals( - "Should project latitude", 52.995143f, (float) projectedLocation.get("lat"), 0.000001f); - Assert.assertEquals( - "Should project longitude", -1.539054f, (float) projectedLocation.get("long"), 0.000001f); + Assertions.assertThat(projected.get("location")).as("Should project location").isNotNull(); + Assertions.assertThat((Float) projectedLocation.get("lat")) + .as("Should project latitude") + .isCloseTo(52.995143f, Assertions.within(0.000001f)); + Assertions.assertThat((Float) projectedLocation.get("long")) + .as("Should project longitude") + .isCloseTo(-1.539054f, Assertions.within(0.000001f)); } @Test @@ -262,26 +273,31 @@ public void testMapProjection() throws IOException { Schema idOnly = new Schema(Types.NestedField.required(0, "id", Types.LongType.get())); Record projected = writeAndRead("id_only", writeSchema, idOnly, record); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); assertEmptyAvroField(projected, "properties"); Schema keyOnly = writeSchema.select("properties.key"); projected = writeAndRead("key_only", writeSchema, keyOnly, record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals( - "Should project entire map", properties, toStringMap((Map) projected.get("properties"))); + Assertions.assertThat(toStringMap((Map) projected.get("properties"))) + .as("Should project entire map") + .isEqualTo(properties); Schema valueOnly = writeSchema.select("properties.value"); projected = writeAndRead("value_only", writeSchema, valueOnly, record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals( - "Should project entire map", properties, toStringMap((Map) projected.get("properties"))); + Assertions.assertThat(toStringMap((Map) projected.get("properties"))) + .as("Should project entire map") + .isEqualTo(properties); Schema mapOnly = writeSchema.select("properties"); projected = writeAndRead("map_only", writeSchema, mapOnly, record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals( - "Should project entire map", properties, toStringMap((Map) projected.get("properties"))); + Assertions.assertThat(toStringMap((Map) projected.get("properties"))) + .as("Should project entire map") + .isEqualTo(properties); } private Map toStringMap(Map map) { @@ -329,50 +345,57 @@ public void testMapOfStructsProjection() throws IOException { Schema idOnly = new Schema(Types.NestedField.required(0, "id", Types.LongType.get())); Record projected = writeAndRead("id_only", writeSchema, idOnly, record); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); assertEmptyAvroField(projected, "locations"); projected = writeAndRead("all_locations", writeSchema, writeSchema.select("locations"), record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals( - "Should project locations map", - record.get("locations"), - toStringMap((Map) projected.get("locations"))); + Assertions.assertThat(toStringMap((Map) projected.get("locations"))) + .as("Should project locations map") + .isEqualTo(record.get("locations")); projected = writeAndRead("lat_only", writeSchema, writeSchema.select("locations.lat"), record); assertEmptyAvroField(projected, "id"); Map locations = toStringMap((Map) projected.get("locations")); - Assert.assertNotNull("Should project locations map", locations); - Assert.assertEquals( - "Should contain L1 and L2", Sets.newHashSet("L1", "L2"), locations.keySet()); + Assertions.assertThat(locations).as("Should project locations map").isNotNull(); + Assertions.assertThat(locations.keySet()) + .as("Should contain L1 and L2") + .containsExactly("L1", "L2"); Record projectedL1 = (Record) locations.get("L1"); - Assert.assertNotNull("L1 should not be null", projectedL1); - Assert.assertEquals( - "L1 should contain lat", 53.992811f, (float) projectedL1.get("lat"), 0.000001); + Assertions.assertThat(projectedL1).as("L1 should not be null").isNotNull(); + Assertions.assertThat((float) projectedL1.get("lat")) + .as("L1 should contain lat") + .isCloseTo(53.992811f, Assertions.within(0.000001f)); assertEmptyAvroField(projectedL1, "long"); Record projectedL2 = (Record) locations.get("L2"); - Assert.assertNotNull("L2 should not be null", projectedL2); - Assert.assertEquals( - "L2 should contain lat", 52.995143f, (float) projectedL2.get("lat"), 0.000001); + Assertions.assertThat(projectedL2).as("L2 should not be null").isNotNull(); + Assertions.assertThat((float) projectedL2.get("lat")) + .as("L2 should contain lat") + .isCloseTo(52.995143f, Assertions.within(0.000001f)); assertEmptyAvroField(projectedL2, "y"); projected = writeAndRead("long_only", writeSchema, writeSchema.select("locations.long"), record); assertEmptyAvroField(projected, "id"); locations = toStringMap((Map) projected.get("locations")); - Assert.assertNotNull("Should project locations map", locations); - Assert.assertEquals( - "Should contain L1 and L2", Sets.newHashSet("L1", "L2"), locations.keySet()); + Assertions.assertThat(locations).as("Should project locations map").isNotNull(); + Assertions.assertThat(locations.keySet()) + .as("Should contain L1 and L2") + .containsExactly("L1", "L2"); projectedL1 = (Record) locations.get("L1"); - Assert.assertNotNull("L1 should not be null", projectedL1); + Assertions.assertThat(projectedL1).as("L1 should not be null").isNotNull(); assertEmptyAvroField(projectedL1, "lat"); - Assert.assertEquals( - "L1 should contain long", -1.542616f, (float) projectedL1.get("long"), 0.000001); + Assertions.assertThat((float) projectedL1.get("long")) + .as("L1 should contain long") + .isCloseTo(-1.542616f, Assertions.within(0.000001f)); projectedL2 = (Record) locations.get("L2"); - Assert.assertNotNull("L2 should not be null", projectedL2); + Assertions.assertThat(projectedL2).as("L2 should not be null").isNotNull(); assertEmptyAvroField(projectedL2, "lat"); - Assert.assertEquals( - "L2 should contain long", -1.539054f, (float) projectedL2.get("long"), 0.000001); + Assertions.assertThat((float) projectedL2.get("long")) + .as("L2 should contain long") + .isCloseTo(-1.539054f, Assertions.within(0.000001f)); Schema latitiudeRenamed = new Schema( @@ -389,19 +412,22 @@ public void testMapOfStructsProjection() throws IOException { projected = writeAndRead("latitude_renamed", writeSchema, latitiudeRenamed, record); assertEmptyAvroField(projected, "id"); locations = toStringMap((Map) projected.get("locations")); - Assert.assertNotNull("Should project locations map", locations); - Assert.assertEquals( - "Should contain L1 and L2", Sets.newHashSet("L1", "L2"), locations.keySet()); + Assertions.assertThat(locations).as("Should project locations map").isNotNull(); + Assertions.assertThat(locations.keySet()) + .as("Should contain L1 and L2") + .containsExactly("L1", "L2"); projectedL1 = (Record) locations.get("L1"); - Assert.assertNotNull("L1 should not be null", projectedL1); - Assert.assertEquals( - "L1 should contain latitude", 53.992811f, (float) projectedL1.get("latitude"), 0.000001); + Assertions.assertThat(projectedL1).as("L1 should not be null").isNotNull(); + Assertions.assertThat((float) projectedL1.get("latitude")) + .as("L1 should contain latitude") + .isCloseTo(53.992811f, Assertions.within(0.000001f)); assertEmptyAvroField(projectedL1, "lat"); assertEmptyAvroField(projectedL1, "long"); projectedL2 = (Record) locations.get("L2"); - Assert.assertNotNull("L2 should not be null", projectedL2); - Assert.assertEquals( - "L2 should contain latitude", 52.995143f, (float) projectedL2.get("latitude"), 0.000001); + Assertions.assertThat(projectedL2).as("L2 should not be null").isNotNull(); + Assertions.assertThat((float) projectedL2.get("latitude")) + .as("L2 should contain latitude") + .isCloseTo(52.995143f, Assertions.within(0.000001f)); assertEmptyAvroField(projectedL2, "lat"); assertEmptyAvroField(projectedL2, "long"); } @@ -423,18 +449,24 @@ public void testListProjection() throws IOException { Schema idOnly = new Schema(Types.NestedField.required(0, "id", Types.LongType.get())); Record projected = writeAndRead("id_only", writeSchema, idOnly, record); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); assertEmptyAvroField(projected, "values"); Schema elementOnly = writeSchema.select("values.element"); projected = writeAndRead("element_only", writeSchema, elementOnly, record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals("Should project entire list", values, projected.get("values")); + Assertions.assertThat(projected.get("values")) + .as("Should project entire list") + .isEqualTo(values); Schema listOnly = writeSchema.select("values"); projected = writeAndRead("list_only", writeSchema, listOnly, record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals("Should project entire list", values, projected.get("values")); + Assertions.assertThat(projected.get("values")) + .as("Should project entire list") + .isEqualTo(values); } @Test @@ -469,37 +501,40 @@ public void testListOfStructsProjection() throws IOException { Schema idOnly = new Schema(Types.NestedField.required(0, "id", Types.LongType.get())); Record projected = writeAndRead("id_only", writeSchema, idOnly, record); - Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id")); + Assertions.assertThat((long) projected.get("id")) + .as("Should contain the correct id value") + .isEqualTo(34L); assertEmptyAvroField(projected, "points"); projected = writeAndRead("all_points", writeSchema, writeSchema.select("points"), record); assertEmptyAvroField(projected, "id"); - Assert.assertEquals( - "Should project points list", record.get("points"), projected.get("points")); + Assertions.assertThat(projected.get("points")) + .as("Should project points list") + .isEqualTo(record.get("points")); projected = writeAndRead("x_only", writeSchema, writeSchema.select("points.x"), record); assertEmptyAvroField(projected, "id"); - Assert.assertNotNull("Should project points list", projected.get("points")); + Assertions.assertThat(projected.get("points")).as("Should project points list").isNotNull(); List points = (List) projected.get("points"); - Assert.assertEquals("Should read 2 points", 2, points.size()); + Assertions.assertThat(points).as("Should read 2 points").hasSize(2); Record projectedP1 = points.get(0); - Assert.assertEquals("Should project x", 1, (int) projectedP1.get("x")); + Assertions.assertThat((int) projectedP1.get("x")).as("Should project x").isEqualTo(1); assertEmptyAvroField(projectedP1, "y"); Record projectedP2 = points.get(1); - Assert.assertEquals("Should project x", 3, (int) projectedP2.get("x")); + Assertions.assertThat((int) projectedP2.get("x")).as("Should project x").isEqualTo(3); assertEmptyAvroField(projectedP2, "y"); projected = writeAndRead("y_only", writeSchema, writeSchema.select("points.y"), record); assertEmptyAvroField(projected, "id"); - Assert.assertNotNull("Should project points list", projected.get("points")); + Assertions.assertThat(projected.get("points")).as("Should project points list").isNotNull(); points = (List) projected.get("points"); - Assert.assertEquals("Should read 2 points", 2, points.size()); + Assertions.assertThat(points).as("Should read 2 points").hasSize(2); projectedP1 = points.get(0); assertEmptyAvroField(projectedP1, "x"); - Assert.assertEquals("Should project y", 2, (int) projectedP1.get("y")); + Assertions.assertThat((int) projectedP1.get("y")).as("Should project y").isEqualTo(2); projectedP2 = points.get(1); assertEmptyAvroField(projectedP2, "x"); - Assert.assertEquals("Should project null y", null, projectedP2.get("y")); + Assertions.assertThat(projectedP2.get("y")).as("Should project null y").isNull(); Schema yRenamed = new Schema( @@ -513,17 +548,17 @@ public void testListOfStructsProjection() throws IOException { projected = writeAndRead("y_renamed", writeSchema, yRenamed, record); assertEmptyAvroField(projected, "id"); - Assert.assertNotNull("Should project points list", projected.get("points")); + Assertions.assertThat(projected.get("points")).as("Should project points list").isNotNull(); points = (List) projected.get("points"); - Assert.assertEquals("Should read 2 points", 2, points.size()); + Assertions.assertThat(points).as("Should read 2 points").hasSize(2); projectedP1 = points.get(0); assertEmptyAvroField(projectedP1, "x"); assertEmptyAvroField(projectedP1, "y"); - Assert.assertEquals("Should project z", 2, (int) projectedP1.get("z")); + Assertions.assertThat((int) projectedP1.get("z")).as("Should project z").isEqualTo(2); projectedP2 = points.get(1); assertEmptyAvroField(projectedP2, "x"); assertEmptyAvroField(projectedP2, "y"); - Assert.assertNull("Should project null z", projectedP2.get("z")); + Assertions.assertThat(projectedP2.get("z")).as("Should project null z").isNull(); } @Test @@ -553,8 +588,10 @@ public void testEmptyStructProjection() throws Exception { assertEmptyAvroField(projected, "id"); Record result = (Record) projected.get("location"); - Assert.assertEquals("location should be in the 0th position", result, projected.get(0)); - Assert.assertNotNull("Should contain an empty record", result); + Assertions.assertThat(projected.get(0)) + .as("location should be in the 0th position") + .isEqualTo(result); + Assertions.assertThat(result).as("Should contain an empty record").isNotNull(); assertEmptyAvroField(result, "lat"); assertEmptyAvroField(result, "long"); } @@ -584,8 +621,10 @@ public void testEmptyStructRequiredProjection() throws Exception { Record projected = writeAndRead("empty_req_proj", writeSchema, emptyStruct, record); assertEmptyAvroField(projected, "id"); Record result = (Record) projected.get("location"); - Assert.assertEquals("location should be in the 0th position", result, projected.get(0)); - Assert.assertNotNull("Should contain an empty record", result); + Assertions.assertThat(projected.get(0)) + .as("location should be in the 0th position") + .isEqualTo(result); + Assertions.assertThat(result).as("Should contain an empty record").isNotNull(); assertEmptyAvroField(result, "lat"); assertEmptyAvroField(result, "long"); } @@ -620,16 +659,21 @@ public void testRequiredEmptyStructInRequiredStruct() throws Exception { Types.NestedField.required(4, "empty", Types.StructType.of())))); Record projected = writeAndRead("req_empty_req_proj", writeSchema, emptyStruct, record); - Assert.assertEquals("Should project id", 34L, projected.get("id")); + Assertions.assertThat(projected.get("id")).as("Should project id").isEqualTo(34L); Record result = (Record) projected.get("location"); - Assert.assertEquals("location should be in the 1st position", result, projected.get(1)); - Assert.assertNotNull("Should contain an empty record", result); + Assertions.assertThat(projected.get(1)) + .as("location should be in the 1st position") + .isEqualTo(result); + Assertions.assertThat(result).as("Should contain an empty record").isNotNull(); assertEmptyAvroField(result, "lat"); assertEmptyAvroField(result, "long"); - Assert.assertNotNull("Should project empty", result.getSchema().getField("empty")); - Assert.assertNotNull("Empty should not be null", result.get("empty")); - Assert.assertEquals( - "Empty should be empty", 0, ((Record) result.get("empty")).getSchema().getFields().size()); + Assertions.assertThat(result.getSchema().getField("empty")) + .as("Should project empty") + .isNotNull(); + Assertions.assertThat(result.get("empty")).as("Empty should not be null").isNotNull(); + Assertions.assertThat(((Record) result.get("empty")).getSchema().getFields()) + .as("Empty should be empty") + .isEmpty(); } @Test @@ -670,12 +714,16 @@ public void testEmptyNestedStructProjection() throws Exception { Record projected = writeAndRead("nested_empty_proj", writeSchema, emptyStruct, record); assertEmptyAvroField(projected, "id"); Record outerResult = (Record) projected.get("outer"); - Assert.assertEquals("Outer should be in the 0th position", outerResult, projected.get(0)); - Assert.assertNotNull("Should contain the outer record", outerResult); + Assertions.assertThat(projected.get(0)) + .as("Outer should be in the 0th position") + .isEqualTo(outerResult); + Assertions.assertThat(outerResult).as("Should contain the outer record").isNotNull(); assertEmptyAvroField(outerResult, "lat"); Record innerResult = (Record) outerResult.get("inner"); - Assert.assertEquals("Inner should be in the 0th position", innerResult, outerResult.get(0)); - Assert.assertNotNull("Should contain the inner record", innerResult); + Assertions.assertThat(outerResult.get(0)) + .as("Inner should be in the 0th position") + .isEqualTo(innerResult); + Assertions.assertThat(innerResult).as("Should contain the inner record").isNotNull(); assertEmptyAvroField(innerResult, "lon"); } @@ -715,12 +763,16 @@ public void testEmptyNestedStructRequiredProjection() throws Exception { Record projected = writeAndRead("nested_empty_req_proj", writeSchema, emptyStruct, record); assertEmptyAvroField(projected, "id"); Record outerResult = (Record) projected.get("outer"); - Assert.assertEquals("Outer should be in the 0th position", outerResult, projected.get(0)); - Assert.assertNotNull("Should contain the outer record", outerResult); + Assertions.assertThat(projected.get(0)) + .as("Outer should be in the 0th position") + .isEqualTo(outerResult); + Assertions.assertThat(outerResult).as("Should contain the outer record").isNotNull(); assertEmptyAvroField(outerResult, "lat"); Record innerResult = (Record) outerResult.get("inner"); - Assert.assertEquals("Inner should be in the 0th position", innerResult, outerResult.get(0)); - Assert.assertNotNull("Should contain the inner record", innerResult); + Assertions.assertThat(outerResult.get(0)) + .as("Inner should be in the 0th position") + .isEqualTo(innerResult); + Assertions.assertThat(innerResult).as("Should contain the inner record").isNotNull(); assertEmptyAvroField(innerResult, "lon"); } diff --git a/core/src/test/java/org/apache/iceberg/data/avro/TestDecoderResolver.java b/core/src/test/java/org/apache/iceberg/data/avro/TestDecoderResolver.java index 5855e80998d3..9b355464a9d8 100644 --- a/core/src/test/java/org/apache/iceberg/data/avro/TestDecoderResolver.java +++ b/core/src/test/java/org/apache/iceberg/data/avro/TestDecoderResolver.java @@ -30,12 +30,12 @@ import org.apache.iceberg.avro.AvroSchemaUtil; import org.apache.iceberg.relocated.com.google.common.collect.Sets; import org.awaitility.Awaitility; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class TestDecoderResolver { - @Before + @BeforeEach public void before() { DecoderResolver.DECODER_CACHES.get().clear(); } @@ -47,8 +47,8 @@ public void testDecoderCachingReadSchemaSameAsFileSchema() throws Exception { ResolvingDecoder resolvingDecoder = DecoderResolver.resolve(dummyDecoder, fileSchema, fileSchema); - assertThat(DecoderResolver.DECODER_CACHES.get().size()).isEqualTo(1); - assertThat(DecoderResolver.DECODER_CACHES.get().get(fileSchema).size()).isEqualTo(1); + assertThat(DecoderResolver.DECODER_CACHES.get()).hasSize(1); + assertThat(DecoderResolver.DECODER_CACHES.get().get(fileSchema)).hasSize(1); checkCached(fileSchema, fileSchema); // Equal but new one @@ -58,8 +58,8 @@ public void testDecoderCachingReadSchemaSameAsFileSchema() throws Exception { DecoderResolver.resolve(dummyDecoder, fileSchema1, fileSchema1); assertThat(resolvingDecoder1).isNotSameAs(resolvingDecoder); - assertThat(DecoderResolver.DECODER_CACHES.get().size()).isEqualTo(2); - assertThat(DecoderResolver.DECODER_CACHES.get().get(fileSchema1).size()).isEqualTo(1); + assertThat(DecoderResolver.DECODER_CACHES.get()).hasSize(2); + assertThat(DecoderResolver.DECODER_CACHES.get().get(fileSchema1)).hasSize(1); checkCached(fileSchema1, fileSchema1); // New one @@ -68,8 +68,8 @@ public void testDecoderCachingReadSchemaSameAsFileSchema() throws Exception { DecoderResolver.resolve(dummyDecoder, fileSchema2, fileSchema2); assertThat(resolvingDecoder2).isNotSameAs(resolvingDecoder); - assertThat(DecoderResolver.DECODER_CACHES.get().size()).isEqualTo(3); - assertThat(DecoderResolver.DECODER_CACHES.get().get(fileSchema2).size()).isEqualTo(1); + assertThat(DecoderResolver.DECODER_CACHES.get()).hasSize(3); + assertThat(DecoderResolver.DECODER_CACHES.get().get(fileSchema2)).hasSize(1); checkCached(fileSchema2, fileSchema2); checkCachedSize(3); @@ -92,8 +92,8 @@ public void testDecoderCachingReadSchemaNotSameAsFileSchema() throws Exception { ResolvingDecoder resolvingDecoder = DecoderResolver.resolve(dummyDecoder, readSchema, fileSchema); - assertThat(DecoderResolver.DECODER_CACHES.get().size()).isEqualTo(1); - assertThat(DecoderResolver.DECODER_CACHES.get().get(readSchema).size()).isEqualTo(1); + assertThat(DecoderResolver.DECODER_CACHES.get()).hasSize(1); + assertThat(DecoderResolver.DECODER_CACHES.get().get(readSchema)).hasSize(1); checkCached(readSchema, fileSchema); // Equal but new one @@ -105,8 +105,8 @@ public void testDecoderCachingReadSchemaNotSameAsFileSchema() throws Exception { DecoderResolver.resolve(dummyDecoder, readSchema1, fileSchema1); assertThat(resolvingDecoder1).isNotSameAs(resolvingDecoder); - assertThat(DecoderResolver.DECODER_CACHES.get().size()).isEqualTo(2); - assertThat(DecoderResolver.DECODER_CACHES.get().get(readSchema1).size()).isEqualTo(1); + assertThat(DecoderResolver.DECODER_CACHES.get()).hasSize(2); + assertThat(DecoderResolver.DECODER_CACHES.get().get(readSchema1)).hasSize(1); checkCached(readSchema1, fileSchema1); // New read schema @@ -115,8 +115,8 @@ public void testDecoderCachingReadSchemaNotSameAsFileSchema() throws Exception { DecoderResolver.resolve(dummyDecoder, readSchema2, fileSchema); assertThat(resolvingDecoder2).isNotSameAs(resolvingDecoder); - assertThat(DecoderResolver.DECODER_CACHES.get().size()).isEqualTo(3); - assertThat(DecoderResolver.DECODER_CACHES.get().get(readSchema2).size()).isEqualTo(1); + assertThat(DecoderResolver.DECODER_CACHES.get()).hasSize(3); + assertThat(DecoderResolver.DECODER_CACHES.get().get(readSchema2)).hasSize(1); checkCached(readSchema2, fileSchema); checkCachedSize(3);