Skip to content
Permalink
Browse files
[FLINK-27682][tests] Migrate ComparatorTestBase to JUnit5
  • Loading branch information
snuyanzin authored and zentol committed May 19, 2022
1 parent cf4d755 commit df307518ed00d82d55b99fc25c961e5188beb08f
Showing 71 changed files with 161 additions and 171 deletions.
@@ -23,7 +23,7 @@
import org.apache.flink.api.common.typeutils.TypeSerializer;

/** Tests for the {@link WritableComparator}. */
public class WritableComparatorTest extends ComparatorTestBase<StringArrayWritable> {
class WritableComparatorTest extends ComparatorTestBase<StringArrayWritable> {

StringArrayWritable[] data =
new StringArrayWritable[] {
@@ -25,7 +25,7 @@
import java.util.UUID;

/** Tests for the {@link WritableComparator} with {@link WritableID}. */
public class WritableComparatorUUIDTest extends ComparatorTestBase<WritableID> {
class WritableComparatorUUIDTest extends ComparatorTestBase<WritableID> {
@Override
protected TypeComparator<WritableID> createComparator(boolean ascending) {
return new WritableComparator<>(ascending, WritableID.class);
@@ -23,28 +23,27 @@
import org.apache.flink.core.memory.DataOutputView;
import org.apache.flink.core.memory.MemorySegment;
import org.apache.flink.core.memory.MemorySegmentFactory;
import org.apache.flink.util.TestLogger;
import org.apache.flink.util.TestLoggerExtension;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

/**
* Abstract test base for comparators.
*
* @param <T>
*/
public abstract class ComparatorTestBase<T> extends TestLogger {
@ExtendWith(TestLoggerExtension.class)
public abstract class ComparatorTestBase<T> {

// Same as in the NormalizedKeySorter
private static final int DEFAULT_MAX_NORMALIZED_KEY_LEN = 8;
@@ -79,13 +78,14 @@ public void testDuplicate() {
comparator.setReference(data[0]);
clone.setReference(data[1]);

assertTrue(
"Comparator duplication does not work: Altering the reference in a duplicated comparator alters the original comparator's reference.",
comparator.equalToReference(data[0]) && clone.equalToReference(data[1]));
assertThat(comparator.equalToReference(data[0]) && clone.equalToReference(data[1]))
.as(
"Comparator duplication does not work: Altering the reference in a duplicated comparator alters the original comparator's reference.")
.isTrue();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
Assert.fail(e.getMessage());
fail(e.getMessage());
}
}

@@ -121,7 +121,7 @@ protected void testEquals(boolean ascending) {
writeSortedData(d, out1);
in1 = out1.getInputView();

assertTrue(comparator.compareSerialized(in1, in2) == 0);
assertThat(comparator.compareSerialized(in1, in2)).isEqualTo(0);
}
} catch (Exception e) {
System.err.println(e.getMessage());
@@ -144,9 +144,9 @@ public void testEqualityWithReference() {
T copy = serializer.copy(d, serializer.createInstance());

// And then test equalTo and compareToReference method of comparator
assertTrue(comparator.equalToReference(d));
assertThat(comparator.equalToReference(d)).isTrue();
comparator2.setReference(copy);
assertTrue(comparator.compareToReference(comparator2) == 0);
assertThat(comparator.compareToReference(comparator2)).isEqualTo(0);
}
} catch (Exception e) {
System.err.println(e.getMessage());
@@ -188,16 +188,16 @@ protected void testGreatSmallAscDesc(boolean ascending, boolean greater) {
in2 = out2.getInputView();

if (greater && ascending) {
assertTrue(comparator.compareSerialized(in1, in2) < 0);
assertThat(comparator.compareSerialized(in1, in2)).isLessThan(0);
}
if (greater && !ascending) {
assertTrue(comparator.compareSerialized(in1, in2) > 0);
assertThat(comparator.compareSerialized(in1, in2)).isGreaterThan(0);
}
if (!greater && ascending) {
assertTrue(comparator.compareSerialized(in2, in1) > 0);
assertThat(comparator.compareSerialized(in2, in1)).isGreaterThan(0);
}
if (!greater && !ascending) {
assertTrue(comparator.compareSerialized(in2, in1) < 0);
assertThat(comparator.compareSerialized(in2, in1)).isLessThan(0);
}
}
}
@@ -231,16 +231,18 @@ protected void testGreatSmallAscDescWithReference(boolean ascending, boolean gre
comparatorHigh.setReference(data[y]);

if (greater && ascending) {
assertTrue(comparatorLow.compareToReference(comparatorHigh) > 0);
assertThat(comparatorLow.compareToReference(comparatorHigh))
.isGreaterThan(0);
}
if (greater && !ascending) {
assertTrue(comparatorLow.compareToReference(comparatorHigh) < 0);
assertThat(comparatorLow.compareToReference(comparatorHigh)).isLessThan(0);
}
if (!greater && ascending) {
assertTrue(comparatorHigh.compareToReference(comparatorLow) < 0);
assertThat(comparatorHigh.compareToReference(comparatorLow)).isLessThan(0);
}
if (!greater && !ascending) {
assertTrue(comparatorHigh.compareToReference(comparatorLow) > 0);
assertThat(comparatorHigh.compareToReference(comparatorLow))
.isGreaterThan(0);
}
}
}
@@ -274,12 +276,12 @@ private int getNormKeyLen(boolean halfLength, T[] data, TypeComparator<T> compar
// Same as in the NormalizedKeySorter
int keyLen = Math.min(comparator.getNormalizeKeyLen(), DEFAULT_MAX_NORMALIZED_KEY_LEN);
if (keyLen < comparator.getNormalizeKeyLen()) {
assertTrue(comparator.isNormalizedKeyPrefixOnly(keyLen));
assertThat(comparator.isNormalizedKeyPrefixOnly(keyLen)).isTrue();
}

if (halfLength) {
keyLen = keyLen / 2;
assertTrue(comparator.isNormalizedKeyPrefixOnly(keyLen));
assertThat(comparator.isNormalizedKeyPrefixOnly(keyLen)).isTrue();
}
return keyLen;
}
@@ -316,8 +318,8 @@ public void testNormalizedKeysEquals(boolean halfLength) {
MemorySegment memSeg2 = setupNormalizedKeysMemSegment(data, normKeyLen, comparator);

for (int i = 0; i < data.length; i++) {
assertTrue(
memSeg1.compare(memSeg2, i * normKeyLen, i * normKeyLen, normKeyLen) == 0);
assertThat(memSeg1.compare(memSeg2, i * normKeyLen, i * normKeyLen, normKeyLen))
.isEqualTo(0);
}
} catch (Exception e) {
System.err.println(e.getMessage());
@@ -372,18 +374,18 @@ protected void testNormalizedKeysGreatSmall(
memSegLow.compare(
memSegHigh, l * normKeyLen, h * normKeyLen, normKeyLen);
if (fullyDetermines) {
assertTrue(cmp < 0);
assertThat(cmp).isLessThan(0);
} else {
assertTrue(cmp <= 0);
assertThat(cmp).isLessThanOrEqualTo(0);
}
} else {
cmp =
memSegHigh.compare(
memSegLow, h * normKeyLen, l * normKeyLen, normKeyLen);
if (fullyDetermines) {
assertTrue(cmp > 0);
assertThat(cmp).isGreaterThan(0);
} else {
assertTrue(cmp >= 0);
assertThat(cmp).isGreaterThanOrEqualTo(0);
}
}
}
@@ -418,7 +420,7 @@ public void testNormalizedKeyReadWriter() {
in = out.getInputView();
comp1.readWithKeyDenormalization(reuse, in);

assertTrue(comp1.compareToReference(comp2) == 0);
assertThat(comp1.compareToReference(comp2)).isEqualTo(0);
}
} catch (Exception e) {
System.err.println(e.getMessage());
@@ -440,24 +442,24 @@ public void testKeyExtraction() {
TypeComparator[] comparators = comparator.getFlatComparators();
Object[] extractedKeys = new Object[comparators.length];
int insertedKeys = comparator.extractKeys(value, extractedKeys, 0);
assertTrue(insertedKeys == comparators.length);
assertThat(insertedKeys).isEqualTo(comparators.length);

for (int i = 0; i < insertedKeys; i++) {
// check if some keys are null, although this is not supported
if (!supportsNullKeys()) {
assertNotNull(extractedKeys[i]);
assertThat(extractedKeys[i]).isNotNull();
}
// compare the extracted key with itself as a basic check
// if the extracted key corresponds to the comparator
assertTrue(comparators[i].compare(extractedKeys[i], extractedKeys[i]) == 0);
assertThat(comparators[i].compare(extractedKeys[i], extractedKeys[i])).isEqualTo(0);
}
}
}

// --------------------------------------------------------------------------------------------

protected void deepEquals(String message, T should, T is) {
assertEquals(message, should, is);
assertThat(is).as(message).isEqualTo(should);
}

// --------------------------------------------------------------------------------------------
@@ -499,7 +501,7 @@ protected void writeSortedData(T value, TestOutputView out) throws IOException {
// Just look if the data is really there after serialization, before testing comparator on
// it
TestInputView in = out.getInputView();
assertTrue("No data available during deserialization.", in.available() > 0);
assertThat(in.available()).as("No data available during deserialization.").isGreaterThan(0);

T deserialized = serializer.deserialize(serializer.createInstance(), in);
deepEquals("Deserialized value is wrong.", value, deserialized);
@@ -24,7 +24,7 @@

import java.math.BigDecimal;

public class BigDecComparatorTest extends ComparatorTestBase<BigDecimal> {
class BigDecComparatorTest extends ComparatorTestBase<BigDecimal> {

@Override
protected TypeComparator<BigDecimal> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.math.BigInteger;

public class BigIntComparatorTest extends ComparatorTestBase<BigInteger> {
class BigIntComparatorTest extends ComparatorTestBase<BigInteger> {

@Override
protected TypeComparator<BigInteger> createComparator(boolean ascending) {
@@ -22,7 +22,7 @@
import org.apache.flink.api.common.typeutils.TypeComparator;
import org.apache.flink.api.common.typeutils.TypeSerializer;

public class BooleanComparatorTest extends ComparatorTestBase<Boolean> {
class BooleanComparatorTest extends ComparatorTestBase<Boolean> {

@Override
protected TypeComparator<Boolean> createComparator(boolean ascending) {
@@ -23,7 +23,7 @@
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.types.BooleanValue;

public class BooleanValueComparatorTest extends ComparatorTestBase<BooleanValue> {
class BooleanValueComparatorTest extends ComparatorTestBase<BooleanValue> {

@Override
protected TypeComparator<BooleanValue> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.util.Random;

public class ByteComparatorTest extends ComparatorTestBase<Byte> {
class ByteComparatorTest extends ComparatorTestBase<Byte> {

@Override
protected TypeComparator<Byte> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@

import java.util.Random;

public class ByteValueComparatorTest extends ComparatorTestBase<ByteValue> {
class ByteValueComparatorTest extends ComparatorTestBase<ByteValue> {

@Override
protected TypeComparator<ByteValue> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.util.Random;

public class CharComparatorTest extends ComparatorTestBase<Character> {
class CharComparatorTest extends ComparatorTestBase<Character> {

@Override
protected TypeComparator<Character> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@

import java.util.Random;

public class CharValueComparatorTest extends ComparatorTestBase<CharValue> {
class CharValueComparatorTest extends ComparatorTestBase<CharValue> {

@Override
protected TypeComparator<CharValue> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@
import java.util.Date;
import java.util.Random;

public class DateComparatorTest extends ComparatorTestBase<Date> {
class DateComparatorTest extends ComparatorTestBase<Date> {

@Override
protected TypeComparator<Date> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.util.Random;

public class DoubleComparatorTest extends ComparatorTestBase<Double> {
class DoubleComparatorTest extends ComparatorTestBase<Double> {

@Override
protected TypeComparator<Double> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@

import java.util.Random;

public class DoubleValueComparatorTest extends ComparatorTestBase<DoubleValue> {
class DoubleValueComparatorTest extends ComparatorTestBase<DoubleValue> {

@Override
protected TypeComparator<DoubleValue> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.util.Random;

public class FloatComparatorTest extends ComparatorTestBase<Float> {
class FloatComparatorTest extends ComparatorTestBase<Float> {

@Override
protected TypeComparator<Float> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@

import java.util.Random;

public class FloatValueComparatorTest extends ComparatorTestBase<FloatValue> {
class FloatValueComparatorTest extends ComparatorTestBase<FloatValue> {

@Override
protected TypeComparator<FloatValue> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@
import java.time.Instant;

/** A test for the {@link InstantComparator}. */
public class InstantComparatorTest extends ComparatorTestBase<Instant> {
class InstantComparatorTest extends ComparatorTestBase<Instant> {

@Override
protected TypeComparator<Instant> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.util.Random;

public class IntComparatorTest extends ComparatorTestBase<Integer> {
class IntComparatorTest extends ComparatorTestBase<Integer> {

@Override
protected TypeComparator<Integer> createComparator(boolean ascending) {
@@ -25,7 +25,7 @@

import java.util.Random;

public class IntValueComparatorTest extends ComparatorTestBase<IntValue> {
class IntValueComparatorTest extends ComparatorTestBase<IntValue> {

@Override
protected TypeComparator<IntValue> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.time.LocalDate;

public class LocalDateComparatorTest extends ComparatorTestBase<LocalDate> {
class LocalDateComparatorTest extends ComparatorTestBase<LocalDate> {

@Override
protected TypeComparator<LocalDate> createComparator(boolean ascending) {
@@ -24,7 +24,7 @@

import java.time.LocalDateTime;

public class LocalDateTimeComparatorTest extends ComparatorTestBase<LocalDateTime> {
class LocalDateTimeComparatorTest extends ComparatorTestBase<LocalDateTime> {

@Override
protected TypeComparator<LocalDateTime> createComparator(boolean ascending) {

0 comments on commit df30751

Please sign in to comment.