You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/07/20 06:42:44 UTC

[avro] branch master updated: AVRO-3579 : insert JUnit5 (#1769)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 2a7e85185 AVRO-3579 : insert JUnit5 (#1769)
2a7e85185 is described below

commit 2a7e85185e92f5ccb7f36b7cfab1d96e76b2bd12
Author: clesaec <51...@users.noreply.github.com>
AuthorDate: Wed Jul 20 08:42:40 2022 +0200

    AVRO-3579 : insert JUnit5 (#1769)
    
    Co-authored-by: Martin Grigorov <ma...@users.noreply.github.com>
---
 .../test/java/org/apache/avro/TestDataFile.java    | 104 ++++++++++-----------
 .../java/org/apache/avro/TestDataFileMeta.java     |  22 ++---
 .../avro/generic/TestGenericLogicalTypes.java      |  65 ++++++-------
 lang/java/pom.xml                                  |   7 ++
 4 files changed, 101 insertions(+), 97 deletions(-)

diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestDataFile.java b/lang/java/avro/src/test/java/org/apache/avro/TestDataFile.java
index a5c0dec3e..ef20201a3 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestDataFile.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestDataFile.java
@@ -17,10 +17,10 @@
  */
 package org.apache.avro;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-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.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -28,6 +28,8 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
+import java.util.stream.Stream;
+
 import org.apache.avro.file.CodecFactory;
 import org.apache.avro.file.DataFileReader;
 import org.apache.avro.file.DataFileStream;
@@ -40,31 +42,23 @@ import org.apache.avro.generic.GenericDatumReader;
 import org.apache.avro.generic.GenericDatumWriter;
 import org.apache.avro.io.DatumReader;
 import org.apache.avro.util.RandomData;
-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.runners.Parameterized.Parameters;
+
+import org.junit.jupiter.api.Test;
+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 org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@RunWith(Parameterized.class)
 public class TestDataFile {
   private static final Logger LOG = LoggerFactory.getLogger(TestDataFile.class);
 
-  @Rule
-  public TemporaryFolder DIR = new TemporaryFolder();
+  @TempDir
+  public File DIR;
 
-  private final CodecFactory codec;
-
-  public TestDataFile(CodecFactory codec) {
-    this.codec = codec;
-    LOG.info("Running with codec: " + codec);
-  }
-
-  @Parameters
-  public static List<Object[]> codecs() {
+  public static Stream<Arguments> codecs() {
     List<Object[]> r = new ArrayList<>();
     r.add(new Object[] { null });
     r.add(new Object[] { CodecFactory.deflateCodec(0) });
@@ -81,7 +75,7 @@ public class TestDataFile {
     r.add(new Object[] { CodecFactory.zstandardCodec(18, true) });
     r.add(new Object[] { CodecFactory.zstandardCodec(0, false, false) });
     r.add(new Object[] { CodecFactory.zstandardCodec(0, false, true) });
-    return r;
+    return r.stream().map(Arguments::of);
   }
 
   private static final int COUNT = Integer.parseInt(System.getProperty("test.count", "200"));
@@ -92,28 +86,30 @@ public class TestDataFile {
       + "{\"name\":\"stringField\", \"type\":\"string\"}," + "{\"name\":\"longField\", \"type\":\"long\"}]}";
   private static final Schema SCHEMA = new Schema.Parser().parse(SCHEMA_JSON);
 
-  private File makeFile() {
-    return new File(DIR.getRoot().getPath(), "test-" + codec + ".avro");
+  private File makeFile(CodecFactory codec) {
+    return new File(DIR, "test-" + codec + ".avro");
   }
 
-  @Test
-  public void runTestsInOrder() throws Exception {
-    testGenericWrite();
-    testGenericRead();
-    testSplits();
-    testSyncDiscovery();
-    testGenericAppend();
-    testReadWithHeader();
-    testFSync(false);
-    testFSync(true);
+  @ParameterizedTest
+  @MethodSource("codecs")
+  public void runTestsInOrder(CodecFactory codec) throws Exception {
+    LOG.info("Running with codec: " + codec);
+    testGenericWrite(codec);
+    testGenericRead(codec);
+    testSplits(codec);
+    testSyncDiscovery(codec);
+    testGenericAppend(codec);
+    testReadWithHeader(codec);
+    testFSync(codec, false);
+    testFSync(codec, true);
   }
 
-  private void testGenericWrite() throws IOException {
+  private void testGenericWrite(CodecFactory codec) throws IOException {
     DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>()).setSyncInterval(100);
     if (codec != null) {
       writer.setCodec(codec);
     }
-    writer.create(SCHEMA, makeFile());
+    writer.create(SCHEMA, makeFile(codec));
     try {
       int count = 0;
       for (Object datum : new RandomData(SCHEMA, COUNT, SEED)) {
@@ -132,7 +128,7 @@ public class TestDataFile {
           } catch (DataFileWriter.AppendWriteException e) {
             System.out.println("Ignoring: " + e);
           }
-          assertTrue("failed to throw when expected", threwProperly);
+          assertTrue(threwProperly, "failed to throw when expected");
         }
       }
     } finally {
@@ -148,11 +144,11 @@ public class TestDataFile {
       doubleCloseEx = e;
     }
 
-    assertNull("Double close() threw an unexpected exception", doubleCloseEx);
+    assertNull(doubleCloseEx, "Double close() threw an unexpected exception");
   }
 
-  private void testGenericRead() throws IOException {
-    try (DataFileReader<Object> reader = new DataFileReader<>(makeFile(), new GenericDatumReader<>())) {
+  private void testGenericRead(CodecFactory codec) throws IOException {
+    try (DataFileReader<Object> reader = new DataFileReader<>(makeFile(codec), new GenericDatumReader<>())) {
       Object datum = null;
       if (VALIDATE) {
         for (Object expected : new RandomData(SCHEMA, COUNT, SEED)) {
@@ -167,8 +163,8 @@ public class TestDataFile {
     }
   }
 
-  private void testSplits() throws IOException {
-    File file = makeFile();
+  private void testSplits(CodecFactory codec) throws IOException {
+    File file = makeFile(codec);
     try (DataFileReader<Object> reader = new DataFileReader<>(file, new GenericDatumReader<>())) {
       Random rand = new Random(SEED);
       int splits = 10; // number of splits
@@ -190,8 +186,8 @@ public class TestDataFile {
     }
   }
 
-  private void testSyncDiscovery() throws IOException {
-    File file = makeFile();
+  private void testSyncDiscovery(CodecFactory codec) throws IOException {
+    File file = makeFile(codec);
     try (DataFileReader<Object> reader = new DataFileReader<>(file, new GenericDatumReader<>())) {
       // discover the sync points
       ArrayList<Long> syncs = new ArrayList<>();
@@ -214,8 +210,8 @@ public class TestDataFile {
     }
   }
 
-  private void testGenericAppend() throws IOException {
-    File file = makeFile();
+  private void testGenericAppend(CodecFactory codec) throws IOException {
+    File file = makeFile(codec);
     long start = file.length();
     try (DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>()).appendTo(file)) {
       for (Object datum : new RandomData(SCHEMA, COUNT, SEED + 1)) {
@@ -238,8 +234,8 @@ public class TestDataFile {
     }
   }
 
-  private void testReadWithHeader() throws IOException {
-    File file = makeFile();
+  private void testReadWithHeader(CodecFactory codec) throws IOException {
+    File file = makeFile(codec);
     try (DataFileReader<Object> reader = new DataFileReader<>(file, new GenericDatumReader<>())) {
       // get a header for this file
       DataFileStream.Header header = reader.getHeader();
@@ -249,14 +245,14 @@ public class TestDataFile {
       try (DataFileReader<Object> readerTrue = DataFileReader.openReader(sin, new GenericDatumReader<>(), header,
           true);) {
 
-        assertNotNull("Should be able to reopen from arbitrary point", readerTrue.next());
+        assertNotNull(readerTrue.next(), "Should be able to reopen from arbitrary point");
         long validPos = readerTrue.previousSync();
         // post sync, we know of a valid sync point: re-open with seek (sync == false)
         sin.seek(validPos);
         try (DataFileReader<Object> readerFalse = DataFileReader.openReader(sin, new GenericDatumReader<>(), header,
             false)) {
-          assertEquals("Should not move from sync point on reopen", validPos, sin.tell());
-          assertNotNull("Should be able to reopen at sync point", readerFalse.next());
+          assertEquals(validPos, sin.tell(), "Should not move from sync point on reopen");
+          assertNotNull(readerFalse.next(), "Should be able to reopen at sync point");
         }
 
       }
@@ -310,12 +306,12 @@ public class TestDataFile {
     assertTrue(out.flushCount < currentCount && out.flushCount >= flushCounter);
   }
 
-  private void testFSync(boolean useFile) throws IOException {
+  private void testFSync(CodecFactory codec, boolean useFile) throws IOException {
     try (DataFileWriter<Object> writer = new DataFileWriter<>(new GenericDatumWriter<>())) {
       writer.setFlushOnEveryBlock(false);
       TestingByteArrayOutputStream out = new TestingByteArrayOutputStream();
       if (useFile) {
-        File f = makeFile();
+        File f = makeFile(codec);
         try (SeekableFileInput in = new SeekableFileInput(f)) {
           writer.appendTo(in, out);
         }
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestDataFileMeta.java b/lang/java/avro/src/test/java/org/apache/avro/TestDataFileMeta.java
index 3a70df4a6..5916ffef8 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestDataFileMeta.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestDataFileMeta.java
@@ -30,25 +30,25 @@ import org.apache.avro.file.DataFileStream;
 import org.apache.avro.file.DataFileWriter;
 import org.apache.avro.generic.GenericDatumReader;
 import org.apache.avro.generic.GenericDatumWriter;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 public class TestDataFileMeta {
 
-  @Rule
-  public TemporaryFolder DIR = new TemporaryFolder();
+  @TempDir
+  public File DIR;
 
-  @Test(expected = AvroRuntimeException.class)
+  @Test
   public void testUseReservedMeta() throws IOException {
     try (DataFileWriter<?> w = new DataFileWriter<>(new GenericDatumWriter<>())) {
-      w.setMeta("avro.foo", "bar");
+      Assertions.assertThrows(AvroRuntimeException.class, () -> w.setMeta("avro.foo", "bar"));
     }
   }
 
-  @Test()
+  @Test
   public void testUseMeta() throws IOException {
-    File f = new File(DIR.getRoot().getPath(), "testDataFileMeta.avro");
+    File f = new File(DIR, "testDataFileMeta.avro");
     try (DataFileWriter<?> w = new DataFileWriter<>(new GenericDatumWriter<>())) {
       w.setMeta("hello", "bar");
       w.create(Schema.create(Type.NULL), f);
@@ -62,11 +62,11 @@ public class TestDataFileMeta {
 
   }
 
-  @Test(expected = AvroRuntimeException.class)
+  @Test
   public void testUseMetaAfterCreate() throws IOException {
     try (DataFileWriter<?> w = new DataFileWriter<>(new GenericDatumWriter<>())) {
       w.create(Schema.create(Type.NULL), new ByteArrayOutputStream());
-      w.setMeta("foo", "bar");
+      Assertions.assertThrows(AvroRuntimeException.class, () -> w.setMeta("foo", "bar"));
     }
 
   }
diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
index 7597b3f94..593f759f8 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericLogicalTypes.java
@@ -44,20 +44,20 @@ import org.apache.avro.file.DataFileWriter;
 import org.apache.avro.file.FileReader;
 import org.apache.avro.io.DatumReader;
 import org.apache.avro.io.DatumWriter;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 public class TestGenericLogicalTypes {
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @TempDir
+  public File temp;
 
   public static final GenericData GENERIC = new GenericData();
 
-  @BeforeClass
+  @BeforeAll
   public static void addLogicalTypes() {
     GENERIC.addLogicalTypeConversion(new Conversions.DecimalConversion());
     GENERIC.addLogicalTypeConversion(new Conversions.UUIDConversion());
@@ -75,7 +75,8 @@ public class TestGenericLogicalTypes {
     List<UUID> expected = Arrays.asList(u1, u2);
 
     File test = write(Schema.create(Schema.Type.STRING), u1.toString(), u2.toString());
-    Assert.assertEquals("Should convert Strings to UUIDs", expected, read(GENERIC.createDatumReader(uuidSchema), test));
+    Assertions.assertEquals(expected, read(GENERIC.createDatumReader(uuidSchema), test),
+        "Should convert Strings to UUIDs");
   }
 
   @Test
@@ -90,8 +91,8 @@ public class TestGenericLogicalTypes {
     List<String> expected = Arrays.asList(u1.toString(), u2.toString());
 
     File test = write(GENERIC, uuidSchema, u1, u2);
-    Assert.assertEquals("Should read UUIDs as Strings", expected,
-        read(GenericData.get().createDatumReader(stringSchema), test));
+    Assertions.assertEquals(expected, read(GenericData.get().createDatumReader(stringSchema), test),
+        "Should read UUIDs as Strings");
   }
 
   @Test
@@ -109,8 +110,8 @@ public class TestGenericLogicalTypes {
     List<String> expected = Arrays.asList(u1.toString(), u2.toString());
 
     File test = write(GENERIC, nullableUuidSchema, u1, u2);
-    Assert.assertEquals("Should read UUIDs as Strings", expected,
-        read(GenericData.get().createDatumReader(nullableStringSchema), test));
+    Assertions.assertEquals(expected, read(GenericData.get().createDatumReader(nullableStringSchema), test),
+        "Should read UUIDs as Strings");
   }
 
   @Test
@@ -130,8 +131,8 @@ public class TestGenericLogicalTypes {
     GenericFixed d2fixed = conversion.toFixed(d2, fixedSchema, decimal);
 
     File test = write(fixedSchema, d1fixed, d2fixed);
-    Assert.assertEquals("Should convert fixed to BigDecimals", expected,
-        read(GENERIC.createDatumReader(decimalSchema), test));
+    Assertions.assertEquals(expected, read(GENERIC.createDatumReader(decimalSchema), test),
+        "Should convert fixed to BigDecimals");
   }
 
   @Test
@@ -150,8 +151,8 @@ public class TestGenericLogicalTypes {
     List<GenericFixed> expected = Arrays.asList(d1fixed, d2fixed);
 
     File test = write(GENERIC, decimalSchema, d1, d2);
-    Assert.assertEquals("Should read BigDecimals as fixed", expected,
-        read(GenericData.get().createDatumReader(fixedSchema), test));
+    Assertions.assertEquals(expected, read(GenericData.get().createDatumReader(fixedSchema), test),
+        "Should read BigDecimals as fixed");
   }
 
   @Test
@@ -209,8 +210,8 @@ public class TestGenericLogicalTypes {
     ByteBuffer d2bytes = conversion.toBytes(d2, bytesSchema, decimal);
 
     File test = write(bytesSchema, d1bytes, d2bytes);
-    Assert.assertEquals("Should convert bytes to BigDecimals", expected,
-        read(GENERIC.createDatumReader(decimalSchema), test));
+    Assertions.assertEquals(expected, read(GENERIC.createDatumReader(decimalSchema), test),
+        "Should convert bytes to BigDecimals");
   }
 
   @Test
@@ -230,8 +231,8 @@ public class TestGenericLogicalTypes {
     List<ByteBuffer> expected = Arrays.asList(d1bytes, d2bytes);
 
     File test = write(GENERIC, decimalSchema, d1bytes, d2bytes);
-    Assert.assertEquals("Should read BigDecimals as bytes", expected,
-        read(GenericData.get().createDatumReader(bytesSchema), test));
+    Assertions.assertEquals(expected, read(GenericData.get().createDatumReader(bytesSchema), test),
+        "Should read BigDecimals as bytes");
   }
 
   private <D> List<D> read(DatumReader<D> reader, File file) throws IOException {
@@ -252,7 +253,7 @@ public class TestGenericLogicalTypes {
 
   @SuppressWarnings("unchecked")
   private <D> File write(GenericData model, Schema schema, D... data) throws IOException {
-    File file = temp.newFile();
+    File file = new File(temp, "out.avro");
     DatumWriter<D> writer = model.createDatumWriter(schema);
 
     try (DataFileWriter<D> fileWriter = new DataFileWriter<>(writer)) {
@@ -318,8 +319,8 @@ public class TestGenericLogicalTypes {
 
   private void checkCopy(Object original, Object copy, boolean notSame) {
     if (notSame)
-      Assert.assertNotSame(original, copy);
-    Assert.assertEquals(original, copy);
+      Assertions.assertNotSame(original, copy);
+    Assertions.assertEquals(original, copy);
   }
 
   @Test
@@ -339,8 +340,8 @@ public class TestGenericLogicalTypes {
     Long i2long = 0L;
 
     File test = write(longSchema, i1long, i2long);
-    Assert.assertEquals("Should convert long to LocalDateTime", expected,
-        read(GENERIC.createDatumReader(timestampSchema), test));
+    Assertions.assertEquals(expected, read(GENERIC.createDatumReader(timestampSchema), test),
+        "Should convert long to LocalDateTime");
   }
 
   @Test
@@ -359,8 +360,8 @@ public class TestGenericLogicalTypes {
     List<Long> expected = Arrays.asList(d1long, d2long);
 
     File test = write(GENERIC, timestampSchema, i1, i2);
-    Assert.assertEquals("Should read LocalDateTime as longs", expected,
-        read(GenericData.get().createDatumReader(timestampSchema), test));
+    Assertions.assertEquals(expected, read(GenericData.get().createDatumReader(timestampSchema), test),
+        "Should read LocalDateTime as longs");
   }
 
   @Test
@@ -380,8 +381,8 @@ public class TestGenericLogicalTypes {
     Long i2long = conversion.toLong(i2, longSchema, timestamp);
 
     File test = write(longSchema, i1long, i2long);
-    Assert.assertEquals("Should convert long to LocalDateTime", expected,
-        read(GENERIC.createDatumReader(timestampSchema), test));
+    Assertions.assertEquals(expected, read(GENERIC.createDatumReader(timestampSchema), test),
+        "Should convert long to LocalDateTime");
   }
 
   @Test
@@ -400,7 +401,7 @@ public class TestGenericLogicalTypes {
     List<Long> expected = Arrays.asList(d1long, d2long);
 
     File test = write(GENERIC, timestampSchema, i1, i2);
-    Assert.assertEquals("Should read LocalDateTime as longs", expected,
-        read(GenericData.get().createDatumReader(timestampSchema), test));
+    Assertions.assertEquals(expected, read(GenericData.get().createDatumReader(timestampSchema), test),
+        "Should read LocalDateTime as longs");
   }
 }
diff --git a/lang/java/pom.xml b/lang/java/pom.xml
index 42b7dec0a..cfe31e78a 100644
--- a/lang/java/pom.xml
+++ b/lang/java/pom.xml
@@ -43,6 +43,7 @@
     <jetty.version>9.4.48.v20220622</jetty.version>
     <jopt-simple.version>5.0.4</jopt-simple.version>
     <junit.version>4.13.2</junit.version>
+    <junit5.version>5.8.2</junit5.version>
     <netty.version>4.1.79.Final</netty.version>
     <protobuf.version>3.21.2</protobuf.version>
     <thrift.version>0.16.0</thrift.version>
@@ -597,6 +598,12 @@
       <version>${junit.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter</artifactId>
+      <version>${junit5.version}</version>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>