Skip to content

Commit 4873b4b

Browse files
authoredSep 10, 2024
Core, Kafka, Spark: Use AssertJ instead of JUnit assertions (apache#11102)
1 parent 41d00ae commit 4873b4b

File tree

4 files changed

+95
-120
lines changed

4 files changed

+95
-120
lines changed
 

‎.baseline/checkstyle/checkstyle.xml

+5
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@
427427
<property name="illegalPkgs" value="org.hamcrest"/>
428428
<message key="import.illegal" value="Prefer using org.assertj.core.api.Assertions instead."/>
429429
</module>
430+
<module name="IllegalImport">
431+
<property name="id" value="BanJUnit5Assertions"/>
432+
<property name="illegalPkgs" value="org.junit.jupiter.api.Assertions"/>
433+
<message key="import.illegal" value="Prefer using org.assertj.core.api.Assertions instead."/>
434+
</module>
430435
<module name="RegexpSinglelineJava">
431436
<property name="ignoreComments" value="true"/>
432437
<property name="format" value="@Json(S|Des)erialize"/>

‎core/src/test/java/org/apache/iceberg/TestTableMetadata.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.apache.iceberg.transforms.Transforms;
6565
import org.apache.iceberg.types.Types;
6666
import org.apache.iceberg.util.JsonUtil;
67-
import org.junit.jupiter.api.Assertions;
6867
import org.junit.jupiter.api.Test;
6968
import org.junit.jupiter.api.io.TempDir;
7069
import org.junit.jupiter.params.ParameterizedTest;
@@ -1662,19 +1661,19 @@ public void testV3TimestampNanoTypeSupport() {
16621661
6, "ts_nanos", Types.TimestampNanoType.withZone()))));
16631662

16641663
for (int unsupportedFormatVersion : ImmutableList.of(1, 2)) {
1665-
Assertions.assertThrows(
1666-
IllegalStateException.class,
1667-
() ->
1668-
TableMetadata.newTableMetadata(
1669-
v3Schema,
1670-
PartitionSpec.unpartitioned(),
1671-
SortOrder.unsorted(),
1672-
TEST_LOCATION,
1673-
ImmutableMap.of(),
1674-
unsupportedFormatVersion),
1675-
String.format(
1664+
assertThatThrownBy(
1665+
() ->
1666+
TableMetadata.newTableMetadata(
1667+
v3Schema,
1668+
PartitionSpec.unpartitioned(),
1669+
SortOrder.unsorted(),
1670+
TEST_LOCATION,
1671+
ImmutableMap.of(),
1672+
unsupportedFormatVersion))
1673+
.isInstanceOf(IllegalStateException.class)
1674+
.hasMessage(
16761675
"Invalid type in v%s schema: struct.ts_nanos timestamptz_ns is not supported until v3",
1677-
unsupportedFormatVersion));
1676+
unsupportedFormatVersion);
16781677
}
16791678

16801679
// should be allowed in v3

‎kafka-connect/kafka-connect/src/test/java/org/apache/iceberg/connect/channel/CoordinatorTest.java

+21-25
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.time.OffsetDateTime;
2626
import java.util.List;
27-
import java.util.Map;
2827
import java.util.UUID;
2928
import org.apache.iceberg.DataFile;
3029
import org.apache.iceberg.DataFiles;
@@ -47,14 +46,13 @@
4746
import org.apache.iceberg.types.Types.StructType;
4847
import org.apache.kafka.clients.consumer.ConsumerRecord;
4948
import org.apache.kafka.connect.sink.SinkTaskContext;
50-
import org.junit.jupiter.api.Assertions;
5149
import org.junit.jupiter.api.Test;
5250

5351
public class CoordinatorTest extends ChannelTestBase {
5452

5553
@Test
5654
public void testCommitAppend() {
57-
Assertions.assertEquals(0, ImmutableList.copyOf(table.snapshots().iterator()).size());
55+
assertThat(table.snapshots()).isEmpty();
5856

5957
OffsetDateTime ts = EventTestUtil.now();
6058
UUID commitId =
@@ -66,17 +64,17 @@ public void testCommitAppend() {
6664
assertCommitComplete(2, commitId, ts);
6765

6866
List<Snapshot> snapshots = ImmutableList.copyOf(table.snapshots());
69-
Assertions.assertEquals(1, snapshots.size());
67+
assertThat(snapshots).hasSize(1);
7068

7169
Snapshot snapshot = snapshots.get(0);
72-
Assertions.assertEquals(DataOperations.APPEND, snapshot.operation());
73-
Assertions.assertEquals(1, ImmutableList.copyOf(snapshot.addedDataFiles(table.io())).size());
74-
Assertions.assertEquals(0, ImmutableList.copyOf(snapshot.addedDeleteFiles(table.io())).size());
75-
76-
Map<String, String> summary = snapshot.summary();
77-
Assertions.assertEquals(commitId.toString(), summary.get(COMMIT_ID_SNAPSHOT_PROP));
78-
Assertions.assertEquals("{\"0\":3}", summary.get(OFFSETS_SNAPSHOT_PROP));
79-
Assertions.assertEquals(ts.toString(), summary.get(VALID_THROUGH_TS_SNAPSHOT_PROP));
70+
assertThat(snapshot.operation()).isEqualTo(DataOperations.APPEND);
71+
assertThat(snapshot.addedDataFiles(table.io())).hasSize(1);
72+
assertThat(snapshot.addedDeleteFiles(table.io())).isEmpty();
73+
74+
assertThat(snapshot.summary())
75+
.containsEntry(COMMIT_ID_SNAPSHOT_PROP, commitId.toString())
76+
.containsEntry(OFFSETS_SNAPSHOT_PROP, "{\"0\":3}")
77+
.containsEntry(VALID_THROUGH_TS_SNAPSHOT_PROP, ts.toString());
8078
}
8179

8280
@Test
@@ -93,17 +91,17 @@ public void testCommitDelta() {
9391
assertCommitComplete(2, commitId, ts);
9492

9593
List<Snapshot> snapshots = ImmutableList.copyOf(table.snapshots());
96-
Assertions.assertEquals(1, snapshots.size());
94+
assertThat(snapshots).hasSize(1);
9795

9896
Snapshot snapshot = snapshots.get(0);
99-
Assertions.assertEquals(DataOperations.OVERWRITE, snapshot.operation());
100-
Assertions.assertEquals(1, ImmutableList.copyOf(snapshot.addedDataFiles(table.io())).size());
101-
Assertions.assertEquals(1, ImmutableList.copyOf(snapshot.addedDeleteFiles(table.io())).size());
102-
103-
Map<String, String> summary = snapshot.summary();
104-
Assertions.assertEquals(commitId.toString(), summary.get(COMMIT_ID_SNAPSHOT_PROP));
105-
Assertions.assertEquals("{\"0\":3}", summary.get(OFFSETS_SNAPSHOT_PROP));
106-
Assertions.assertEquals(ts.toString(), summary.get(VALID_THROUGH_TS_SNAPSHOT_PROP));
97+
assertThat(snapshot.operation()).isEqualTo(DataOperations.OVERWRITE);
98+
assertThat(snapshot.addedDataFiles(table.io())).hasSize(1);
99+
assertThat(snapshot.addedDeleteFiles(table.io())).hasSize(1);
100+
101+
assertThat(snapshot.summary())
102+
.containsEntry(COMMIT_ID_SNAPSHOT_PROP, commitId.toString())
103+
.containsEntry(OFFSETS_SNAPSHOT_PROP, "{\"0\":3}")
104+
.containsEntry(VALID_THROUGH_TS_SNAPSHOT_PROP, ts.toString());
107105
}
108106

109107
@Test
@@ -114,8 +112,7 @@ public void testCommitNoFiles() {
114112
assertThat(producer.history()).hasSize(2);
115113
assertCommitComplete(1, commitId, ts);
116114

117-
List<Snapshot> snapshots = ImmutableList.copyOf(table.snapshots());
118-
Assertions.assertEquals(0, snapshots.size());
115+
assertThat(table.snapshots()).isEmpty();
119116
}
120117

121118
@Test
@@ -136,8 +133,7 @@ public void testCommitError() {
136133
// no commit messages sent
137134
assertThat(producer.history()).hasSize(1);
138135

139-
List<Snapshot> snapshots = ImmutableList.copyOf(table.snapshots());
140-
Assertions.assertEquals(0, snapshots.size());
136+
assertThat(table.snapshots()).isEmpty();
141137
}
142138

143139
private void assertCommitTable(int idx, UUID commitId, OffsetDateTime ts) {

‎spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestComputeTableStatsAction.java

+57-82
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
import static org.apache.iceberg.spark.actions.NDVSketchUtil.APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY;
2222
import static org.apache.iceberg.types.Types.NestedField.optional;
2323
import static org.apache.iceberg.types.Types.NestedField.required;
24-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
26-
import static org.junit.jupiter.api.Assertions.assertNotNull;
27-
import static org.junit.jupiter.api.Assertions.assertThrows;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatNoException;
26+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2927

3028
import java.io.IOException;
3129
import java.util.List;
@@ -58,7 +56,6 @@
5856
import org.apache.spark.sql.catalyst.parser.ParseException;
5957
import org.apache.spark.sql.types.StructType;
6058
import org.junit.jupiter.api.AfterEach;
61-
import org.junit.jupiter.api.Assertions;
6259
import org.junit.jupiter.api.TestTemplate;
6360

6461
public class TestComputeTableStatsAction extends CatalogTestBase {
@@ -89,8 +86,8 @@ public void testLoadingTableDirectly() {
8986
SparkActions actions = SparkActions.get();
9087
ComputeTableStats.Result results = actions.computeTableStats(table).execute();
9188
StatisticsFile statisticsFile = results.statisticsFile();
92-
Assertions.assertNotEquals(statisticsFile.fileSizeInBytes(), 0);
93-
Assertions.assertEquals(statisticsFile.blobMetadata().size(), 2);
89+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
90+
assertThat(statisticsFile.blobMetadata()).hasSize(2);
9491
}
9592

9693
@TestTemplate
@@ -115,19 +112,18 @@ public void testComputeTableStatsAction() throws NoSuchTableException, ParseExce
115112
SparkActions actions = SparkActions.get();
116113
ComputeTableStats.Result results =
117114
actions.computeTableStats(table).columns("id", "data").execute();
118-
assertNotNull(results);
115+
assertThat(results).isNotNull();
119116

120117
List<StatisticsFile> statisticsFiles = table.statisticsFiles();
121-
Assertions.assertEquals(statisticsFiles.size(), 1);
118+
assertThat(statisticsFiles).hasSize(1);
122119

123120
StatisticsFile statisticsFile = statisticsFiles.get(0);
124-
assertNotEquals(statisticsFile.fileSizeInBytes(), 0);
125-
Assertions.assertEquals(statisticsFile.blobMetadata().size(), 2);
121+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
122+
assertThat(statisticsFile.blobMetadata()).hasSize(2);
126123

127124
BlobMetadata blobMetadata = statisticsFile.blobMetadata().get(0);
128-
Assertions.assertEquals(
129-
blobMetadata.properties().get(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY),
130-
String.valueOf(4));
125+
assertThat(blobMetadata.properties())
126+
.containsEntry(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY, "4");
131127
}
132128

133129
@TestTemplate
@@ -149,28 +145,16 @@ public void testComputeTableStatsActionWithoutExplicitColumns()
149145
Table table = Spark3Util.loadIcebergTable(spark, tableName);
150146
SparkActions actions = SparkActions.get();
151147
ComputeTableStats.Result results = actions.computeTableStats(table).execute();
152-
assertNotNull(results);
148+
assertThat(results).isNotNull();
153149

154-
Assertions.assertEquals(1, table.statisticsFiles().size());
150+
assertThat(table.statisticsFiles()).hasSize(1);
155151
StatisticsFile statisticsFile = table.statisticsFiles().get(0);
156-
Assertions.assertEquals(2, statisticsFile.blobMetadata().size());
157-
assertNotEquals(0, statisticsFile.fileSizeInBytes());
158-
Assertions.assertEquals(
159-
4,
160-
Long.parseLong(
161-
statisticsFile
162-
.blobMetadata()
163-
.get(0)
164-
.properties()
165-
.get(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY)));
166-
Assertions.assertEquals(
167-
4,
168-
Long.parseLong(
169-
statisticsFile
170-
.blobMetadata()
171-
.get(1)
172-
.properties()
173-
.get(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY)));
152+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
153+
assertThat(statisticsFile.blobMetadata()).hasSize(2);
154+
assertThat(statisticsFile.blobMetadata().get(0).properties())
155+
.containsEntry(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY, "4");
156+
assertThat(statisticsFile.blobMetadata().get(1).properties())
157+
.containsEntry(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY, "4");
174158
}
175159

176160
@TestTemplate
@@ -180,12 +164,9 @@ public void testComputeTableStatsForInvalidColumns() throws NoSuchTableException
180164
sql("INSERT into %s values(1, 'abcd')", tableName);
181165
Table table = Spark3Util.loadIcebergTable(spark, tableName);
182166
SparkActions actions = SparkActions.get();
183-
IllegalArgumentException exception =
184-
assertThrows(
185-
IllegalArgumentException.class,
186-
() -> actions.computeTableStats(table).columns("id1").execute());
187-
String message = exception.getMessage();
188-
assertTrue(message.contains("Can't find column id1 in table"));
167+
assertThatThrownBy(() -> actions.computeTableStats(table).columns("id1").execute())
168+
.isInstanceOf(IllegalArgumentException.class)
169+
.hasMessageStartingWith("Can't find column id1 in table");
189170
}
190171

191172
@TestTemplate
@@ -194,7 +175,7 @@ public void testComputeTableStatsWithNoSnapshots() throws NoSuchTableException,
194175
Table table = Spark3Util.loadIcebergTable(spark, tableName);
195176
SparkActions actions = SparkActions.get();
196177
ComputeTableStats.Result result = actions.computeTableStats(table).columns("id").execute();
197-
Assertions.assertNull(result.statisticsFile());
178+
assertThat(result.statisticsFile()).isNull();
198179
}
199180

200181
@TestTemplate
@@ -215,19 +196,17 @@ public void testComputeTableStatsWithNullValues() throws NoSuchTableException, P
215196
Table table = Spark3Util.loadIcebergTable(spark, tableName);
216197
SparkActions actions = SparkActions.get();
217198
ComputeTableStats.Result results = actions.computeTableStats(table).columns("data").execute();
218-
assertNotNull(results);
199+
assertThat(results).isNotNull();
219200

220201
List<StatisticsFile> statisticsFiles = table.statisticsFiles();
221-
Assertions.assertEquals(statisticsFiles.size(), 1);
202+
assertThat(statisticsFiles).hasSize(1);
222203

223204
StatisticsFile statisticsFile = statisticsFiles.get(0);
224-
assertNotEquals(statisticsFile.fileSizeInBytes(), 0);
225-
Assertions.assertEquals(statisticsFile.blobMetadata().size(), 1);
205+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
206+
assertThat(statisticsFile.blobMetadata()).hasSize(1);
226207

227-
BlobMetadata blobMetadata = statisticsFile.blobMetadata().get(0);
228-
Assertions.assertEquals(
229-
blobMetadata.properties().get(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY),
230-
String.valueOf(4));
208+
assertThat(statisticsFile.blobMetadata().get(0).properties())
209+
.containsEntry(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY, "4");
231210
}
232211

233212
@TestTemplate
@@ -241,7 +220,8 @@ public void testComputeTableStatsWithSnapshotHavingDifferentSchemas()
241220
// Snapshot id not specified
242221
Table table = Spark3Util.loadIcebergTable(spark, tableName);
243222

244-
assertDoesNotThrow(() -> actions.computeTableStats(table).columns("data").execute());
223+
assertThatNoException()
224+
.isThrownBy(() -> actions.computeTableStats(table).columns("data").execute());
245225

246226
sql("ALTER TABLE %s DROP COLUMN %s", tableName, "data");
247227
// Append data to create snapshot
@@ -250,15 +230,14 @@ public void testComputeTableStatsWithSnapshotHavingDifferentSchemas()
250230
long snapshotId2 = Spark3Util.loadIcebergTable(spark, tableName).currentSnapshot().snapshotId();
251231

252232
// Snapshot id specified
253-
assertDoesNotThrow(
254-
() -> actions.computeTableStats(table).snapshot(snapshotId1).columns("data").execute());
255-
256-
IllegalArgumentException exception =
257-
assertThrows(
258-
IllegalArgumentException.class,
259-
() -> actions.computeTableStats(table).snapshot(snapshotId2).columns("data").execute());
260-
String message = exception.getMessage();
261-
assertTrue(message.contains("Can't find column data in table"));
233+
assertThatNoException()
234+
.isThrownBy(
235+
() -> actions.computeTableStats(table).snapshot(snapshotId1).columns("data").execute());
236+
237+
assertThatThrownBy(
238+
() -> actions.computeTableStats(table).snapshot(snapshotId2).columns("data").execute())
239+
.isInstanceOf(IllegalArgumentException.class)
240+
.hasMessageStartingWith("Can't find column data in table");
262241
}
263242

264243
@TestTemplate
@@ -271,19 +250,17 @@ public void testComputeTableStatsWhenSnapshotIdNotSpecified()
271250
SparkActions actions = SparkActions.get();
272251
ComputeTableStats.Result results = actions.computeTableStats(table).columns("data").execute();
273252

274-
assertNotNull(results);
253+
assertThat(results).isNotNull();
275254

276255
List<StatisticsFile> statisticsFiles = table.statisticsFiles();
277-
Assertions.assertEquals(statisticsFiles.size(), 1);
256+
assertThat(statisticsFiles).hasSize(1);
278257

279258
StatisticsFile statisticsFile = statisticsFiles.get(0);
280-
assertNotEquals(statisticsFile.fileSizeInBytes(), 0);
281-
Assertions.assertEquals(statisticsFile.blobMetadata().size(), 1);
259+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
260+
assertThat(statisticsFile.blobMetadata()).hasSize(1);
282261

283-
BlobMetadata blobMetadata = statisticsFile.blobMetadata().get(0);
284-
Assertions.assertEquals(
285-
blobMetadata.properties().get(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY),
286-
String.valueOf(1));
262+
assertThat(statisticsFile.blobMetadata().get(0).properties())
263+
.containsEntry(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY, "1");
287264
}
288265

289266
@TestTemplate
@@ -305,10 +282,10 @@ public void testComputeTableStatsWithNestedSchema()
305282

306283
tbl.refresh();
307284
List<StatisticsFile> statisticsFiles = tbl.statisticsFiles();
308-
Assertions.assertEquals(statisticsFiles.size(), 1);
285+
assertThat(statisticsFiles).hasSize(1);
309286
StatisticsFile statisticsFile = statisticsFiles.get(0);
310-
assertNotEquals(statisticsFile.fileSizeInBytes(), 0);
311-
Assertions.assertEquals(statisticsFile.blobMetadata().size(), 1);
287+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
288+
assertThat(statisticsFile.blobMetadata()).hasSize(1);
312289
}
313290

314291
@TestTemplate
@@ -322,10 +299,9 @@ public void testComputeTableStatsWithNoComputableColumns() throws IOException {
322299

323300
table.refresh();
324301
SparkActions actions = SparkActions.get();
325-
IllegalArgumentException exception =
326-
assertThrows(
327-
IllegalArgumentException.class, () -> actions.computeTableStats(table).execute());
328-
Assertions.assertEquals(exception.getMessage(), "No columns found to compute stats");
302+
assertThatThrownBy(() -> actions.computeTableStats(table).execute())
303+
.isInstanceOf(IllegalArgumentException.class)
304+
.hasMessage("No columns found to compute stats");
329305
}
330306

331307
@TestTemplate
@@ -386,18 +362,17 @@ public void testComputeTableStats(String columnName, String type)
386362
table.refresh();
387363
ComputeTableStats.Result results =
388364
actions.computeTableStats(table).columns(columnName).execute();
389-
assertNotNull(results);
365+
assertThat(results).isNotNull();
390366

391367
List<StatisticsFile> statisticsFiles = table.statisticsFiles();
392-
Assertions.assertEquals(statisticsFiles.size(), 1);
368+
assertThat(statisticsFiles).hasSize(1);
393369

394370
StatisticsFile statisticsFile = statisticsFiles.get(0);
395-
assertNotEquals(statisticsFile.fileSizeInBytes(), 0);
396-
Assertions.assertEquals(statisticsFile.blobMetadata().size(), 1);
371+
assertThat(statisticsFile.fileSizeInBytes()).isGreaterThan(0);
372+
assertThat(statisticsFile.blobMetadata()).hasSize(1);
397373

398-
BlobMetadata blobMetadata = statisticsFile.blobMetadata().get(0);
399-
Assertions.assertNotNull(
400-
blobMetadata.properties().get(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY));
374+
assertThat(statisticsFile.blobMetadata().get(0).properties())
375+
.containsKey(APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY);
401376
}
402377

403378
private GenericRecord createNestedRecord() {

0 commit comments

Comments
 (0)
Please sign in to comment.