Skip to content

Commit

Permalink
GH-42030: [Java] Update Unit Tests for Adapter Module (#42038)
Browse files Browse the repository at this point in the history
### Rationale for this change

Update package from JUnit 4(`org.junit`) to JUnit 5(`org.junit.jupiter`).

### What changes are included in this PR?

- `avro` and `jdbc` module
  - [x] Replacing `org.junit` with `org.junit.jupiter.api`.
  - [x] Updating `Assertions.assertXXX` to `assertXXX` using static imports.
  - [x] Updating annotations such as `@ Before`, `@ After`.
    - `@ Before` -> `@ BeforeEach`
    - `@ After` -> `@ AfterEach`
    - `@ Test` -> `@ Test` with `org.junit.jupiter`
    - `@ ClassRule` -> `@ TempDir` and `@ BeforeAll`
  - [x] Updating `Parameterized` test
  - [x] Doing self review for avro
  - [x] Dealing with `java.io.IOException: Failed to delete temp directory` on Windows with JDK 11
  - [x] Exploring a more effective structure for `ParameterizedTest` in JDBC tests.
  - [x] Doing self review for jdbc
- `orc` module
  - [x] Reviewing the build method
  - [x] Updating annotations such as `@ BeforeAll`, `@ Rule`, `@ TemporaryFolder`
  - [x] Doing self review

### Are these changes tested?

Yes, existing tests have passed.

### Are there any user-facing changes?

No.

* GitHub Issue: #42030

Authored-by: Hyunseok Seo <[email protected]>
Signed-off-by: David Li <[email protected]>
  • Loading branch information
llama90 authored Jun 14, 2024
1 parent 377506c commit 870b315
Show file tree
Hide file tree
Showing 26 changed files with 373 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.avro.Conversions;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericFixed;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class AvroLogicalTypesTest extends AvroTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.arrow.adapter.avro;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand All @@ -31,7 +31,7 @@
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class AvroSkipFieldTest extends AvroTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.arrow.adapter.avro;

import static org.junit.Assert.assertEquals;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -43,17 +43,16 @@
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;

public class AvroTestBase {

@ClassRule public static final TemporaryFolder TMP = new TemporaryFolder();
@TempDir public File TMP;

protected AvroToArrowConfig config;

@Before
@BeforeEach
public void init() {
BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
config = new AvroToArrowConfigBuilder(allocator).build();
Expand Down Expand Up @@ -82,19 +81,21 @@ public static Schema getSchema(String schemaName) throws Exception {
}

protected VectorSchemaRoot writeAndRead(Schema schema, List data) throws Exception {
File dataFile = TMP.newFile();
File dataFile = new File(TMP, "test.avro");

BinaryEncoder encoder =
new EncoderFactory().directBinaryEncoder(new FileOutputStream(dataFile), null);
DatumWriter writer = new GenericDatumWriter(schema);
BinaryDecoder decoder =
new DecoderFactory().directBinaryDecoder(new FileInputStream(dataFile), null);
try (FileOutputStream fos = new FileOutputStream(dataFile);
FileInputStream fis = new FileInputStream(dataFile)) {

for (Object value : data) {
writer.write(value, encoder);
}
BinaryEncoder encoder = new EncoderFactory().directBinaryEncoder(fos, null);
DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
BinaryDecoder decoder = new DecoderFactory().directBinaryDecoder(fis, null);

for (Object value : data) {
writer.write(value, encoder);
}

return AvroToArrow.avroToArrow(schema, decoder, config);
return AvroToArrow.avroToArrow(schema, decoder, config);
}
}

protected void checkArrayResult(List<List<?>> expected, ListVector vector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.arrow.adapter.avro;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.EOFException;
import java.io.File;
Expand Down Expand Up @@ -44,29 +44,31 @@
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.util.Utf8;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class AvroToArrowIteratorTest extends AvroTestBase {

@Override
@BeforeEach
public void init() {
final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
this.config = new AvroToArrowConfigBuilder(allocator).setTargetBatchSize(3).build();
}

private AvroToArrowVectorIterator convert(Schema schema, List data) throws Exception {
File dataFile = TMP.newFile();
private void writeDataToFile(Schema schema, List<?> data, File dataFile) throws Exception {
try (FileOutputStream fos = new FileOutputStream(dataFile)) {
BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(fos, null);
DatumWriter<Object> writer = new GenericDatumWriter<>(schema);

BinaryEncoder encoder =
new EncoderFactory().directBinaryEncoder(new FileOutputStream(dataFile), null);
DatumWriter writer = new GenericDatumWriter(schema);
BinaryDecoder decoder =
new DecoderFactory().directBinaryDecoder(new FileInputStream(dataFile), null);

for (Object value : data) {
writer.write(value, encoder);
for (Object value : data) {
writer.write(value, encoder);
}
encoder.flush();
}
}

private AvroToArrowVectorIterator convert(Schema schema, FileInputStream fis) throws Exception {
BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(fis, null);
return AvroToArrow.avroToArrowIterator(schema, decoder, config);
}

Expand All @@ -75,9 +77,13 @@ public void testStringType() throws Exception {
Schema schema = getSchema("test_primitive_string.avsc");
List<String> data = Arrays.asList("v1", "v2", "v3", "v4", "v5");

File dataFile = new File(TMP, "test.avro");
writeDataToFile(schema, data, dataFile);

List<VectorSchemaRoot> roots = new ArrayList<>();
List<FieldVector> vectors = new ArrayList<>();
try (AvroToArrowVectorIterator iterator = convert(schema, data)) {
try (FileInputStream fis = new FileInputStream(dataFile);
AvroToArrowVectorIterator iterator = convert(schema, fis)) {
while (iterator.hasNext()) {
VectorSchemaRoot root = iterator.next();
FieldVector vector = root.getFieldVectors().get(0);
Expand All @@ -103,9 +109,13 @@ public void testNullableStringType() throws Exception {
data.add(record);
}

File dataFile = new File(TMP, "test.avro");
writeDataToFile(schema, data, dataFile);

List<VectorSchemaRoot> roots = new ArrayList<>();
List<FieldVector> vectors = new ArrayList<>();
try (AvroToArrowVectorIterator iterator = convert(schema, data); ) {
try (FileInputStream fis = new FileInputStream(dataFile);
AvroToArrowVectorIterator iterator = convert(schema, fis)) {
while (iterator.hasNext()) {
VectorSchemaRoot root = iterator.next();
FieldVector vector = root.getFieldVectors().get(0);
Expand All @@ -129,8 +139,12 @@ public void testRecordType() throws Exception {
data.add(record);
}

File dataFile = new File(TMP, "test.avro");
writeDataToFile(schema, data, dataFile);

List<VectorSchemaRoot> roots = new ArrayList<>();
try (AvroToArrowVectorIterator iterator = convert(schema, data)) {
try (FileInputStream fis = new FileInputStream(dataFile);
AvroToArrowVectorIterator iterator = convert(schema, fis)) {
while (iterator.hasNext()) {
roots.add(iterator.next());
}
Expand All @@ -150,9 +164,13 @@ public void testArrayType() throws Exception {
Arrays.asList("1vvv", "2bbb"),
Arrays.asList("1fff", "2"));

File dataFile = new File(TMP, "test.avro");
writeDataToFile(schema, data, dataFile);

List<VectorSchemaRoot> roots = new ArrayList<>();
List<ListVector> vectors = new ArrayList<>();
try (AvroToArrowVectorIterator iterator = convert(schema, data)) {
try (FileInputStream fis = new FileInputStream(dataFile);
AvroToArrowVectorIterator iterator = convert(schema, fis)) {
while (iterator.hasNext()) {
VectorSchemaRoot root = iterator.next();
roots.add(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.arrow.adapter.avro;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand All @@ -34,7 +34,7 @@
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class AvroToArrowTest extends AvroTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.arrow.adapter.avro;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -30,18 +30,16 @@
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class TestWriteReadAvroRecord {

@ClassRule public static final TemporaryFolder TMP = new TemporaryFolder();
@TempDir public static File TMP;

@Test
public void testWriteAndRead() throws Exception {

File dataFile = TMP.newFile();
File dataFile = new File(TMP, "test.avro");
Schema schema = AvroTestBase.getSchema("test.avsc");

// write data to disk
Expand All @@ -55,20 +53,22 @@ public void testWriteAndRead() throws Exception {
user2.put("favorite_color", "red");

DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema);
DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter);
dataFileWriter.create(schema, dataFile);
dataFileWriter.append(user1);
dataFileWriter.append(user2);
dataFileWriter.close();
try (DataFileWriter<GenericRecord> dataFileWriter =
new DataFileWriter<GenericRecord>(datumWriter)) {
dataFileWriter.create(schema, dataFile);
dataFileWriter.append(user1);
dataFileWriter.append(user2);
}

// read data from disk
DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema);
DataFileReader<GenericRecord> dataFileReader =
new DataFileReader<GenericRecord>(dataFile, datumReader);
List<GenericRecord> result = new ArrayList<>();
while (dataFileReader.hasNext()) {
GenericRecord user = dataFileReader.next();
result.add(user);
try (DataFileReader<GenericRecord> dataFileReader =
new DataFileReader<GenericRecord>(dataFile, datumReader)) {
while (dataFileReader.hasNext()) {
GenericRecord user = dataFileReader.next();
result.add(user);
}
}

assertEquals(2, result.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.util.ValueVectorUtility;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;

/** Class to abstract out some common test functionality for testing JDBC to Arrow. */
public abstract class AbstractJdbcToArrowTest {
Expand Down Expand Up @@ -94,8 +92,9 @@ protected static Table getTable(String ymlFilePath, @SuppressWarnings("rawtypes"
* @throws SQLException on error
* @throws ClassNotFoundException on error
*/
@Before
public void setUp() throws SQLException, ClassNotFoundException {
protected void initializeDatabase(Table table) throws SQLException, ClassNotFoundException {
this.table = table;

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
String url = "jdbc:h2:mem:JdbcToArrowTest";
String driver = "org.h2.Driver";
Expand All @@ -114,7 +113,7 @@ public void setUp() throws SQLException, ClassNotFoundException {
*
* @throws SQLException on error
*/
@After
@AfterEach
public void destroy() throws SQLException {
if (conn != null) {
conn.close();
Expand Down Expand Up @@ -146,11 +145,12 @@ public static Object[][] prepareTestData(
/**
* Abstract method to implement test Functionality to test JdbcToArrow methods.
*
* @param table Table object
* @throws SQLException on error
* @throws IOException on error
*/
@Test
public abstract void testJdbcToArrowValues() throws SQLException, IOException;
public abstract void testJdbcToArrowValues(Table table)
throws SQLException, IOException, ClassNotFoundException;

/**
* Abstract method to implement logic to assert test various datatype values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
package org.apache.arrow.adapter.jdbc;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.sql.Types;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class JdbcFieldInfoTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class JdbcToArrowCommentMetadataTest {

Expand All @@ -53,7 +53,7 @@ public class JdbcToArrowCommentMetadataTest {
* @throws SQLException on error
* @throws ClassNotFoundException on error
*/
@Before
@BeforeEach
public void setUp() throws SQLException, ClassNotFoundException {
String url =
"jdbc:h2:mem:JdbcToArrowTest?characterEncoding=UTF-8;INIT=runscript from 'classpath:/h2/comment.sql'";
Expand All @@ -62,7 +62,7 @@ public void setUp() throws SQLException, ClassNotFoundException {
conn = DriverManager.getConnection(url);
}

@After
@AfterEach
public void tearDown() throws SQLException {
if (conn != null) {
conn.close();
Expand Down
Loading

0 comments on commit 870b315

Please sign in to comment.