diff --git a/parquet-column/src/test/java/org/apache/parquet/schema/TestPrimitiveStringifier.java b/parquet-column/src/test/java/org/apache/parquet/schema/TestPrimitiveStringifier.java index b165b200d2..c76f69dcf9 100644 --- a/parquet-column/src/test/java/org/apache/parquet/schema/TestPrimitiveStringifier.java +++ b/parquet-column/src/test/java/org/apache/parquet/schema/TestPrimitiveStringifier.java @@ -40,6 +40,7 @@ import static org.apache.parquet.schema.PrimitiveStringifier.TIME_UTC_STRINGIFIER; import static org.apache.parquet.schema.PrimitiveStringifier.UNSIGNED_STRINGIFIER; import static org.apache.parquet.schema.PrimitiveStringifier.UTF8_STRINGIFIER; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -51,7 +52,6 @@ import java.util.Set; import java.util.TimeZone; import java.util.concurrent.TimeUnit; -import org.apache.parquet.TestUtils; import org.apache.parquet.io.api.Binary; import org.junit.Test; @@ -416,11 +416,9 @@ public void testUUIDStringifier() { 0x00, 0x00, 0x00))); // As there is no validation implemented, if the 16 bytes is not available, the array will be over-indexed - TestUtils.assertThrows( - "Expected exception for over-indexing", - ArrayIndexOutOfBoundsException.class, - () -> stringifier.stringify(toBinary( - 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee))); + assertThatThrownBy(() -> stringifier.stringify(toBinary( + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee))) + .isInstanceOf(ArrayIndexOutOfBoundsException.class); checkThrowingUnsupportedException(stringifier, Binary.class); } diff --git a/parquet-common/src/test/java/org/apache/parquet/SemanticVersionTest.java b/parquet-common/src/test/java/org/apache/parquet/SemanticVersionTest.java index e29352d402..37d2c1075f 100644 --- a/parquet-common/src/test/java/org/apache/parquet/SemanticVersionTest.java +++ b/parquet-common/src/test/java/org/apache/parquet/SemanticVersionTest.java @@ -18,8 +18,7 @@ */ package org.apache.parquet; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.util.List; import org.junit.Test; @@ -27,25 +26,21 @@ public class SemanticVersionTest { @Test public void testCompare() { - assertTrue(new SemanticVersion(1, 8, 1).compareTo(new SemanticVersion(1, 8, 1)) == 0); - assertTrue(new SemanticVersion(1, 8, 0).compareTo(new SemanticVersion(1, 8, 1)) < 0); - assertTrue(new SemanticVersion(1, 8, 2).compareTo(new SemanticVersion(1, 8, 1)) > 0); + assertThat(new SemanticVersion(1, 8, 1)).isEqualByComparingTo(new SemanticVersion(1, 8, 1)); + assertThat(new SemanticVersion(1, 8, 0)).isLessThan(new SemanticVersion(1, 8, 1)); + assertThat(new SemanticVersion(1, 8, 2)).isGreaterThan(new SemanticVersion(1, 8, 1)); - assertTrue(new SemanticVersion(1, 8, 1).compareTo(new SemanticVersion(1, 8, 1)) == 0); - assertTrue(new SemanticVersion(1, 8, 0).compareTo(new SemanticVersion(1, 8, 1)) < 0); - assertTrue(new SemanticVersion(1, 8, 2).compareTo(new SemanticVersion(1, 8, 1)) > 0); + assertThat(new SemanticVersion(1, 7, 0)).isLessThan(new SemanticVersion(1, 8, 0)); + assertThat(new SemanticVersion(1, 9, 0)).isGreaterThan(new SemanticVersion(1, 8, 0)); - assertTrue(new SemanticVersion(1, 7, 0).compareTo(new SemanticVersion(1, 8, 0)) < 0); - assertTrue(new SemanticVersion(1, 9, 0).compareTo(new SemanticVersion(1, 8, 0)) > 0); + assertThat(new SemanticVersion(0, 0, 0)).isLessThan(new SemanticVersion(1, 0, 0)); + assertThat(new SemanticVersion(2, 0, 0)).isGreaterThan(new SemanticVersion(1, 0, 0)); - assertTrue(new SemanticVersion(0, 0, 0).compareTo(new SemanticVersion(1, 0, 0)) < 0); - assertTrue(new SemanticVersion(2, 0, 0).compareTo(new SemanticVersion(1, 0, 0)) > 0); + assertThat(new SemanticVersion(1, 8, 100)).isLessThan(new SemanticVersion(1, 9, 0)); - assertTrue(new SemanticVersion(1, 8, 100).compareTo(new SemanticVersion(1, 9, 0)) < 0); - - assertTrue(new SemanticVersion(1, 8, 0).compareTo(new SemanticVersion(1, 8, 0, true)) > 0); - assertTrue(new SemanticVersion(1, 8, 0, true).compareTo(new SemanticVersion(1, 8, 0, true)) == 0); - assertTrue(new SemanticVersion(1, 8, 0, true).compareTo(new SemanticVersion(1, 8, 0)) < 0); + assertThat(new SemanticVersion(1, 8, 0)).isGreaterThan(new SemanticVersion(1, 8, 0, true)); + assertThat(new SemanticVersion(1, 8, 0, true)).isEqualByComparingTo(new SemanticVersion(1, 8, 0, true)); + assertThat(new SemanticVersion(1, 8, 0, true)).isLessThan(new SemanticVersion(1, 8, 0)); } @Test @@ -97,19 +92,24 @@ public void testDistributionVersions() throws Exception { @Test public void testParse() throws Exception { - assertEquals(new SemanticVersion(1, 8, 0), SemanticVersion.parse("1.8.0")); - assertEquals(new SemanticVersion(1, 8, 0, true), SemanticVersion.parse("1.8.0rc3")); - assertEquals(new SemanticVersion(1, 8, 0, "rc3", "SNAPSHOT", null), SemanticVersion.parse("1.8.0rc3-SNAPSHOT")); - assertEquals(new SemanticVersion(1, 8, 0, null, "SNAPSHOT", null), SemanticVersion.parse("1.8.0-SNAPSHOT")); - assertEquals(new SemanticVersion(1, 5, 0, null, "cdh5.5.0", null), SemanticVersion.parse("1.5.0-cdh5.5.0")); + assertThat(SemanticVersion.parse("1.8.0")).isEqualTo(new SemanticVersion(1, 8, 0)); + assertThat(SemanticVersion.parse("1.8.0rc3")).isEqualTo(new SemanticVersion(1, 8, 0, true)); + assertThat(SemanticVersion.parse("1.8.0rc3-SNAPSHOT")) + .isEqualTo(new SemanticVersion(1, 8, 0, "rc3", "SNAPSHOT", null)); + assertThat(SemanticVersion.parse("1.8.0-SNAPSHOT")) + .isEqualTo(new SemanticVersion(1, 8, 0, null, "SNAPSHOT", null)); + assertThat(SemanticVersion.parse("1.5.0-cdh5.5.0")) + .isEqualTo(new SemanticVersion(1, 5, 0, null, "cdh5.5.0", null)); } private static void assertLessThan(String a, String b) throws SemanticVersion.SemanticVersionParseException { - assertTrue(a + " should be < " + b, SemanticVersion.parse(a).compareTo(SemanticVersion.parse(b)) < 0); - assertTrue(b + " should be > " + a, SemanticVersion.parse(b).compareTo(SemanticVersion.parse(a)) > 0); + assertThat(SemanticVersion.parse(a)).as(a + " should be < " + b).isLessThan(SemanticVersion.parse(b)); + assertThat(SemanticVersion.parse(b)).as(b + " should be > " + a).isGreaterThan(SemanticVersion.parse(a)); } private static void assertEqualTo(String a, String b) throws SemanticVersion.SemanticVersionParseException { - assertTrue(a + " should equal " + b, SemanticVersion.parse(a).compareTo(SemanticVersion.parse(b)) == 0); + assertThat(SemanticVersion.parse(a)) + .as(a + " should equal " + b) + .isEqualByComparingTo(SemanticVersion.parse(b)); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/TestPreconditions.java b/parquet-common/src/test/java/org/apache/parquet/TestPreconditions.java index 514ead752e..31ea60bcb5 100644 --- a/parquet-common/src/test/java/org/apache/parquet/TestPreconditions.java +++ b/parquet-common/src/test/java/org/apache/parquet/TestPreconditions.java @@ -18,216 +18,139 @@ */ package org.apache.parquet; -import org.junit.Assert; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import org.junit.Test; public class TestPreconditions { @Test public void testCheckArgumentWithoutParams() { - try { - Preconditions.checkArgument(true, "Test message"); - } catch (IllegalArgumentException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkArgument(false, "Test message"); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("Should format message", "Test message", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkArgument(true, "Test message")).doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkArgument(false, "Test message")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Test message"); } @Test public void testCheckArgumentWithOneParam() { - try { - Preconditions.checkArgument(true, "Test message %s", 12); - } catch (IllegalArgumentException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkArgument(false, "Test message %s", 12); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("Should format message", "Test message 12", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkArgument(true, "Test message %s", 12)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkArgument(false, "Test message %s", 12)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Test message 12"); } @Test public void testCheckArgumentWithTwoParams() { - try { - Preconditions.checkArgument(true, "Test message %s %s", 12, null); - } catch (IllegalArgumentException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkArgument(false, "Test message %s %s", 12, null); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("Should format message", "Test message 12 null", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkArgument(true, "Test message %s %s", 12, null)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkArgument(false, "Test message %s %s", 12, null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Test message 12 null"); } @Test public void testCheckArgumentWithThreeParams() { - try { - Preconditions.checkArgument(true, "Test message %s %s %s", 12, null, "column"); - } catch (IllegalArgumentException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkArgument(false, "Test message %s %s %s", 12, null, "column"); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("Should format message", "Test message 12 null column", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkArgument(true, "Test message %s %s %s", 12, null, "column")) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkArgument(false, "Test message %s %s %s", 12, null, "column")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Test message 12 null column"); } @Test public void testCheckArgumentWithMoreThanThreeParams() { - try { - Preconditions.checkArgument(true, "Test message %s %s %s %s", 12, null, "column", true); - } catch (IllegalArgumentException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkArgument(false, "Test message %s %s %s %s", 12, null, "column", true); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("Should format message", "Test message 12 null column true", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkArgument(true, "Test message %s %s %s %s", 12, null, "column", true)) + .doesNotThrowAnyException(); + + assertThatThrownBy( + () -> Preconditions.checkArgument(false, "Test message %s %s %s %s", 12, null, "column", true)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Test message 12 null column true"); } @Test public void checkArgumentMessageOnlySupportsStringTypeTemplate() { - try { - Preconditions.checkArgument(true, "Test message %d", 12); - } catch (IllegalArgumentException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkArgument(false, "Test message %d", 12); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("%d is not a valid format option", "d != java.lang.String", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkArgument(true, "Test message %d", 12)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkArgument(false, "Test message %d", 12)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("d != java.lang.String"); } @Test public void testCheckState() { - try { - Preconditions.checkState(true, "Test message: %s %s", 12, null); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message: %s %s", 12, null); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalStateException e) { - Assert.assertEquals("Should format message", "Test message: 12 null", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message: %s %s", 12, null)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message: %s %s", 12, null)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Test message: 12 null"); } @Test public void testCheckStateWithoutArguments() { - try { - Preconditions.checkState(true, "Test message"); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message"); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalStateException e) { - Assert.assertEquals("Should format message", "Test message", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message")).doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Test message"); } @Test public void testCheckStateWithOneArgument() { - try { - Preconditions.checkState(true, "Test message %s", 12); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message %s", 12); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalStateException e) { - Assert.assertEquals("Should format message", "Test message 12", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message %s", 12)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message %s", 12)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Test message 12"); } @Test public void testCheckStateWithTwoArguments() { - try { - Preconditions.checkState(true, "Test message %s %s", 12, null); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message %s %s", 12, null); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalStateException e) { - Assert.assertEquals("Should format message", "Test message 12 null", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message %s %s", 12, null)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message %s %s", 12, null)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Test message 12 null"); } @Test public void testCheckStateWithThreeArguments() { - try { - Preconditions.checkState(true, "Test message %s %s %s", 12, null, "column"); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message %s %s %s", 12, null, "column"); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalStateException e) { - Assert.assertEquals("Should format message", "Test message 12 null column", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message %s %s %s", 12, null, "column")) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message %s %s %s", 12, null, "column")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Test message 12 null column"); } @Test public void testCheckStateWithMoreThanThreeParams() { - try { - Preconditions.checkState(true, "Test message %s %s %s %s", 12, null, "column", true); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message %s %s %s %s", 12, null, "column", true); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalStateException e) { - Assert.assertEquals("Should format message", "Test message 12 null column true", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message %s %s %s %s", 12, null, "column", true)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message %s %s %s %s", 12, null, "column", true)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Test message 12 null column true"); } @Test public void checkStateMessageOnlySupportsStringTypeTemplate() { - try { - Preconditions.checkState(true, "Test message %d", 12); - } catch (IllegalStateException e) { - Assert.fail("Should not throw exception when isValid is true"); - } - - try { - Preconditions.checkState(false, "Test message %d", 12); - Assert.fail("Should throw exception when isValid is false"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("%d is not a valid format option", "d != java.lang.String", e.getMessage()); - } + assertThatCode(() -> Preconditions.checkState(true, "Test message %d", 12)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> Preconditions.checkState(false, "Test message %d", 12)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("d != java.lang.String"); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/TestUtils.java b/parquet-common/src/test/java/org/apache/parquet/TestUtils.java deleted file mode 100644 index 088fedd05a..0000000000 --- a/parquet-common/src/test/java/org/apache/parquet/TestUtils.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.parquet; - -import java.util.concurrent.Callable; -import org.junit.Assert; - -public class TestUtils { - - /** - * A convenience method to avoid a large number of @Test(expected=...) tests - * - * @param message A String message to describe this assertion - * @param expected An Exception class that the Runnable should throw - * @param callable A Callable that is expected to throw the exception - */ - public static void assertThrows(String message, Class expected, Callable callable) { - try { - callable.call(); - Assert.fail("No exception was thrown (" + message + "), expected: " + expected.getName()); - } catch (Exception actual) { - try { - Assert.assertEquals(message, expected, actual.getClass()); - } catch (AssertionError e) { - e.addSuppressed(actual); - throw e; - } - } - } - - /** - * A convenience method to avoid a large number of @Test(expected=...) tests - * - * @param message A String message to describe this assertion - * @param expected An Exception class that the Runnable should throw - * @param runnable A Runnable that is expected to throw the runtime exception - */ - public static void assertThrows(String message, Class expected, Runnable runnable) { - try { - runnable.run(); - Assert.fail("No exception was thrown (" + message + "), expected: " + expected.getName()); - } catch (Exception actual) { - try { - Assert.assertEquals(message, expected, actual.getClass()); - } catch (AssertionError e) { - e.addSuppressed(actual); - throw e; - } - } - } -} diff --git a/parquet-common/src/test/java/org/apache/parquet/VersionTest.java b/parquet-common/src/test/java/org/apache/parquet/VersionTest.java index 61946e8307..4eab722e02 100644 --- a/parquet-common/src/test/java/org/apache/parquet/VersionTest.java +++ b/parquet-common/src/test/java/org/apache/parquet/VersionTest.java @@ -18,8 +18,8 @@ */ package org.apache.parquet; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.apache.parquet.VersionParser.ParsedVersion; import org.apache.parquet.VersionParser.VersionParseException; @@ -51,72 +51,63 @@ public void testFullVersion() throws Exception { ParsedVersion version = VersionParser.parse(Version.FULL_VERSION); assertVersionValid(version.version); - assertEquals(Version.VERSION_NUMBER, version.version); - assertEquals("parquet-mr", version.application); + assertThat(version.version).isEqualTo(Version.VERSION_NUMBER); + assertThat(version.application).isEqualTo("parquet-mr"); } @Test public void testVersionParser() throws Exception { - assertEquals( - new ParsedVersion("parquet-mr", "1.6.0", "abcd"), - VersionParser.parse("parquet-mr version 1.6.0 (build abcd)")); + assertThat(VersionParser.parse("parquet-mr version 1.6.0 (build abcd)")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", "abcd")); - assertEquals( - new ParsedVersion("parquet-mr", "1.6.22rc99-SNAPSHOT", "abcd"), - VersionParser.parse("parquet-mr version 1.6.22rc99-SNAPSHOT (build abcd)")); + assertThat(VersionParser.parse("parquet-mr version 1.6.22rc99-SNAPSHOT (build abcd)")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.22rc99-SNAPSHOT", "abcd")); - try { - VersionParser.parse("unparseable string"); - fail("this should throw"); - } catch (VersionParseException e) { - // - } + assertThatThrownBy(() -> VersionParser.parse("unparseable string")) + .isInstanceOf(VersionParseException.class) + .hasMessage("Could not parse created_by: unparseable string using format: " + VersionParser.FORMAT); // missing semver - assertEquals( - new ParsedVersion("parquet-mr", null, "abcd"), VersionParser.parse("parquet-mr version (build abcd)")); - assertEquals( - new ParsedVersion("parquet-mr", null, "abcd"), VersionParser.parse("parquet-mr version (build abcd)")); + assertThat(VersionParser.parse("parquet-mr version (build abcd)")) + .isEqualTo(new ParsedVersion("parquet-mr", null, "abcd")); + assertThat(VersionParser.parse("parquet-mr version (build abcd)")) + .isEqualTo(new ParsedVersion("parquet-mr", null, "abcd")); // missing build hash - assertEquals( - new ParsedVersion("parquet-mr", "1.6.0", null), - VersionParser.parse("parquet-mr version 1.6.0 (build )")); - assertEquals( - new ParsedVersion("parquet-mr", "1.6.0", null), - VersionParser.parse("parquet-mr version 1.6.0 (build)")); - assertEquals(new ParsedVersion("parquet-mr", null, null), VersionParser.parse("parquet-mr version (build)")); - assertEquals(new ParsedVersion("parquet-mr", null, null), VersionParser.parse("parquet-mr version (build )")); + assertThat(VersionParser.parse("parquet-mr version 1.6.0 (build )")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", null)); + assertThat(VersionParser.parse("parquet-mr version 1.6.0 (build)")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", null)); + assertThat(VersionParser.parse("parquet-mr version (build)")) + .isEqualTo(new ParsedVersion("parquet-mr", null, null)); + assertThat(VersionParser.parse("parquet-mr version (build )")) + .isEqualTo(new ParsedVersion("parquet-mr", null, null)); // Missing entire build section - assertEquals(new ParsedVersion("parquet-mr", "1.6.0", null), VersionParser.parse("parquet-mr version 1.6.0")); - assertEquals( - new ParsedVersion("parquet-mr", "1.8.0rc4", null), VersionParser.parse("parquet-mr version 1.8.0rc4")); - assertEquals( - new ParsedVersion("parquet-mr", "1.8.0rc4-SNAPSHOT", null), - VersionParser.parse("parquet-mr version 1.8.0rc4-SNAPSHOT")); - assertEquals(new ParsedVersion("parquet-mr", null, null), VersionParser.parse("parquet-mr version")); + assertThat(VersionParser.parse("parquet-mr version 1.6.0")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", null)); + assertThat(VersionParser.parse("parquet-mr version 1.8.0rc4")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.8.0rc4", null)); + assertThat(VersionParser.parse("parquet-mr version 1.8.0rc4-SNAPSHOT")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.8.0rc4-SNAPSHOT", null)); + assertThat(VersionParser.parse("parquet-mr version")).isEqualTo(new ParsedVersion("parquet-mr", null, null)); // Various spaces - assertEquals( - new ParsedVersion("parquet-mr", "1.6.0", null), VersionParser.parse("parquet-mr version 1.6.0")); - assertEquals( - new ParsedVersion("parquet-mr", "1.8.0rc4", null), - VersionParser.parse("parquet-mr version 1.8.0rc4")); - assertEquals( - new ParsedVersion("parquet-mr", "1.8.0rc4-SNAPSHOT", null), - VersionParser.parse("parquet-mr version 1.8.0rc4-SNAPSHOT ")); - assertEquals(new ParsedVersion("parquet-mr", null, null), VersionParser.parse("parquet-mr version")); - assertEquals( - new ParsedVersion("parquet-mr", "1.6.0", null), - VersionParser.parse("parquet-mr version 1.6.0 ( build )")); - assertEquals( - new ParsedVersion("parquet-mr", "1.6.0", null), - VersionParser.parse("parquet-mr version 1.6.0 ( build)")); - assertEquals( - new ParsedVersion("parquet-mr", null, null), VersionParser.parse("parquet-mr version ( build)")); - assertEquals( - new ParsedVersion("parquet-mr", null, null), - VersionParser.parse("parquet-mr version (build )")); + assertThat(VersionParser.parse("parquet-mr version 1.6.0")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", null)); + assertThat(VersionParser.parse("parquet-mr version 1.8.0rc4")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.8.0rc4", null)); + assertThat(VersionParser.parse("parquet-mr version 1.8.0rc4-SNAPSHOT ")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.8.0rc4-SNAPSHOT", null)); + assertThat(VersionParser.parse("parquet-mr version")) + .isEqualTo(new ParsedVersion("parquet-mr", null, null)); + assertThat(VersionParser.parse("parquet-mr version 1.6.0 ( build )")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", null)); + assertThat(VersionParser.parse("parquet-mr version 1.6.0 ( build)")) + .isEqualTo(new ParsedVersion("parquet-mr", "1.6.0", null)); + assertThat(VersionParser.parse("parquet-mr version ( build)")) + .isEqualTo(new ParsedVersion("parquet-mr", null, null)); + assertThat(VersionParser.parse("parquet-mr version (build )")) + .isEqualTo(new ParsedVersion("parquet-mr", null, null)); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestByteBufferInputStreams.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestByteBufferInputStreams.java index a5d0a50cf3..0cfca6b501 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestByteBufferInputStreams.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestByteBufferInputStreams.java @@ -19,16 +19,14 @@ package org.apache.parquet.bytes; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.EOFException; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.concurrent.Callable; -import org.junit.Assert; import org.junit.Test; public abstract class TestByteBufferInputStreams { @@ -44,12 +42,14 @@ public void testRead0() throws Exception { ByteBufferInputStream stream = newStream(); - Assert.assertEquals("Should read 0 bytes", 0, stream.read(bytes)); + assertThat(stream.read(bytes)).as("Should read 0 bytes").isEqualTo(0); int bytesRead = stream.read(new byte[100]); - Assert.assertTrue("Should read to end of stream", bytesRead < 100); + assertThat(bytesRead).as("Should read to end of stream").isLessThan(100); - Assert.assertEquals("Should read 0 bytes at end of stream", 0, stream.read(bytes)); + assertThat(stream.read(bytes)) + .as("Should read 0 bytes at end of stream") + .isEqualTo(0); } @Test @@ -59,18 +59,22 @@ public void testReadAll() throws Exception { ByteBufferInputStream stream = newStream(); int bytesRead = stream.read(bytes); - Assert.assertEquals("Should read the entire buffer", bytes.length, bytesRead); + assertThat(bytesRead).as("Should read the entire buffer").isEqualTo(bytes.length); for (int i = 0; i < bytes.length; i += 1) { - Assert.assertEquals("Byte i should be i", i, bytes[i]); - Assert.assertEquals("Should advance position", 35, stream.position()); + assertThat(bytes[i]).as("Byte i should be i").isEqualTo((byte) i); + assertThat(stream.position()).as("Should advance position").isEqualTo(35); } - Assert.assertEquals("Should have no more remaining content", 0, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(0); - Assert.assertEquals("Should return -1 at end of stream", -1, stream.read(bytes)); + assertThat(stream.read(bytes)).as("Should return -1 at end of stream").isEqualTo(-1); - Assert.assertEquals("Should have no more remaining content", 0, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(0); checkOriginalData(); } @@ -85,26 +89,33 @@ public void testSmallReads() throws Exception { int lastBytesRead = bytes.length; for (int offset = 0; offset < length; offset += bytes.length) { - Assert.assertEquals("Should read requested len", bytes.length, lastBytesRead); + assertThat(lastBytesRead).as("Should read requested len").isEqualTo(bytes.length); lastBytesRead = stream.read(bytes, 0, bytes.length); - Assert.assertEquals("Should advance position", offset + lastBytesRead, stream.position()); + assertThat(stream.position()).as("Should advance position").isEqualTo(offset + lastBytesRead); // validate the bytes that were read for (int i = 0; i < lastBytesRead; i += 1) { - Assert.assertEquals("Byte i should be i", offset + i, bytes[i]); + assertThat(bytes[i]).as("Byte i should be i").isEqualTo((byte) (offset + i)); } } - Assert.assertEquals( - "Should read fewer bytes at end of buffer", length % bytes.length, lastBytesRead % bytes.length); + assertThat(lastBytesRead % bytes.length) + .as("Should read fewer bytes at end of buffer") + .isEqualTo(length % bytes.length); - Assert.assertEquals("Should have no more remaining content", 0, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(0); - Assert.assertEquals("Should return -1 at end of stream", -1, stream.read(bytes)); + assertThat(stream.read(bytes)) + .as("Should return -1 at end of stream") + .isEqualTo(-1); - Assert.assertEquals("Should have no more remaining content", 0, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(0); } checkOriginalData(); @@ -119,31 +130,40 @@ public void testPartialBufferReads() throws Exception { int lastBytesRead = size; for (int offset = 0; offset < bytes.length; offset += size) { - Assert.assertEquals("Should read requested len", size, lastBytesRead); + assertThat(lastBytesRead).as("Should read requested len").isEqualTo(size); lastBytesRead = stream.read(bytes, offset, Math.min(size, bytes.length - offset)); - Assert.assertEquals( - "Should advance position", - lastBytesRead > 0 ? offset + lastBytesRead : offset, - stream.position()); + assertThat(stream.position()) + .as("Should advance position") + .isEqualTo(lastBytesRead > 0 ? offset + lastBytesRead : offset); } - Assert.assertEquals("Should read fewer bytes at end of buffer", bytes.length % size, lastBytesRead % size); + assertThat(lastBytesRead % size) + .as("Should read fewer bytes at end of buffer") + .isEqualTo(bytes.length % size); for (int i = 0; i < bytes.length; i += 1) { - Assert.assertEquals("Byte i should be i", i, bytes[i]); + assertThat(bytes[i]).as("Byte i should be i").isEqualTo((byte) i); } - Assert.assertEquals("Should have no more remaining content", 2, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(2); - Assert.assertEquals("Should return 2 more bytes", 2, stream.read(bytes)); + assertThat(stream.read(bytes)).as("Should return 2 more bytes").isEqualTo(2); - Assert.assertEquals("Should have no more remaining content", 0, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(0); - Assert.assertEquals("Should return -1 at end of stream", -1, stream.read(bytes)); + assertThat(stream.read(bytes)) + .as("Should return -1 at end of stream") + .isEqualTo(-1); - Assert.assertEquals("Should have no more remaining content", 0, stream.available()); + assertThat(stream.available()) + .as("Should have no more remaining content") + .isEqualTo(0); } checkOriginalData(); @@ -155,12 +175,11 @@ public void testReadByte() throws Exception { int length = stream.available(); for (int i = 0; i < length; i += 1) { - Assert.assertEquals("Position should increment", i, stream.position()); - Assert.assertEquals(i, stream.read()); + assertThat(stream.position()).as("Position should increment").isEqualTo(i); + assertThat(stream.read()).isEqualTo(i); } - assertThrows( - "Should throw EOFException at end of stream", EOFException.class, (Callable) stream::read); + assertThatThrownBy(stream::read).isInstanceOf(EOFException.class); checkOriginalData(); } @@ -171,10 +190,12 @@ public void testSlice() throws Exception { int length = stream.available(); ByteBuffer empty = stream.slice(0); - Assert.assertNotNull("slice(0) should produce a non-null buffer", empty); - Assert.assertEquals("slice(0) should produce an empty buffer", 0, empty.remaining()); + assertThat(empty).as("slice(0) should produce a non-null buffer").isNotNull(); + assertThat(empty.remaining()) + .as("slice(0) should produce an empty buffer") + .isEqualTo(0); - Assert.assertEquals("Position should be at start", 0, stream.position()); + assertThat(stream.position()).as("Position should be at start").isEqualTo(0); int i = 0; while (stream.available() > 0) { @@ -182,13 +203,13 @@ public void testSlice() throws Exception { ByteBuffer buffer = stream.slice(bytesToSlice); for (int j = 0; j < bytesToSlice; j += 1) { - Assert.assertEquals("Data should be correct", i + j, buffer.get()); + assertThat((int) buffer.get()).as("Data should be correct").isEqualTo(i + j); } i += bytesToSlice; } - Assert.assertEquals("Position should be at end", length, stream.position()); + assertThat(stream.position()).as("Position should be at end").isEqualTo(length); checkOriginalData(); } @@ -197,7 +218,7 @@ public void testSlice() throws Exception { public void testSliceBuffers0() throws Exception { ByteBufferInputStream stream = newStream(); - Assert.assertEquals("Should return an empty list", Collections.emptyList(), stream.sliceBuffers(0)); + assertThat(stream.sliceBuffers(0)).as("Should return an empty list").isEmpty(); } @Test @@ -207,14 +228,13 @@ public void testWholeSliceBuffers() throws Exception { List buffers = stream.sliceBuffers(stream.available()); - Assert.assertEquals("Should consume all buffers", length, stream.position()); + assertThat(stream.position()).as("Should consume all buffers").isEqualTo(length); - assertThrows("Should throw EOFException when empty", EOFException.class, (Callable>) - () -> stream.sliceBuffers(length)); + assertThatThrownBy(() -> stream.sliceBuffers(length)).isInstanceOf(EOFException.class); ByteBufferInputStream copy = ByteBufferInputStream.wrap(buffers); for (int i = 0; i < length; i += 1) { - Assert.assertEquals("Slice should have identical data", i, copy.read()); + assertThat(copy.read()).as("Slice should have identical data").isEqualTo(i); } checkOriginalData(); @@ -231,12 +251,12 @@ public void testSliceBuffersCoverage() throws Exception { buffers.addAll(stream.sliceBuffers(Math.min(size, stream.available()))); } - Assert.assertEquals("Should consume all content", length, stream.position()); + assertThat(stream.position()).as("Should consume all content").isEqualTo(length); ByteBufferInputStream newStream = new MultiBufferInputStream(buffers); for (int i = 0; i < length; i += 1) { - Assert.assertEquals("Data should be correct", i, newStream.read()); + assertThat(newStream.read()).as("Data should be correct").isEqualTo(i); } } @@ -250,23 +270,29 @@ public void testSliceBuffersModification() throws Exception { int sliceLength = 5; List buffers = stream.sliceBuffers(sliceLength); - Assert.assertEquals("Should advance the original stream", length - sliceLength, stream.available()); - Assert.assertEquals("Should advance the original stream position", sliceLength, stream.position()); + assertThat(stream.available()).as("Should advance the original stream").isEqualTo(length - sliceLength); + assertThat(stream.position()) + .as("Should advance the original stream position") + .isEqualTo(sliceLength); - Assert.assertEquals("Should return a slice of the first buffer", 1, buffers.size()); + assertThat(buffers).as("Should return a slice of the first buffer").hasSize(1); ByteBuffer buffer = buffers.get(0); - Assert.assertEquals("Should have requested bytes", sliceLength, buffer.remaining()); + assertThat(buffer.remaining()).as("Should have requested bytes").isEqualTo(sliceLength); // read the buffer one past the returned limit. this should not change the // next value in the original stream buffer.limit(sliceLength + 1); for (int i = 0; i < sliceLength + 1; i += 1) { - Assert.assertEquals("Should have correct data", i, buffer.get()); + assertThat((int) buffer.get()).as("Should have correct data").isEqualTo(i); } - Assert.assertEquals("Reading a slice shouldn't advance the original stream", sliceLength, stream.position()); - Assert.assertEquals("Reading a slice shouldn't change the underlying data", sliceLength, stream.read()); + assertThat(stream.position()) + .as("Reading a slice shouldn't advance the original stream") + .isEqualTo(sliceLength); + assertThat(stream.read()) + .as("Reading a slice shouldn't change the underlying data") + .isEqualTo(sliceLength); // change the underlying data buffer buffer.limit(sliceLength + 2); @@ -276,9 +302,12 @@ public void testSliceBuffersModification() throws Exception { try { buffer.put((byte) 255); - Assert.assertEquals( - "Writing to a slice shouldn't advance the original stream", sliceLength + 1, stream.position()); - Assert.assertEquals("Writing to a slice should change the underlying data", 255, stream.read()); + assertThat(stream.position()) + .as("Writing to a slice shouldn't advance the original stream") + .isEqualTo(sliceLength + 1); + assertThat(stream.read()) + .as("Writing to a slice should change the underlying data") + .isEqualTo(255); } finally { undoBuffer.put((byte) originalValue); } @@ -290,16 +319,19 @@ public void testSkip() throws Exception { while (stream.available() > 0) { int bytesToSkip = Math.min(stream.available(), 10); - Assert.assertEquals( - "Should skip all, regardless of backing buffers", bytesToSkip, stream.skip(bytesToSkip)); + assertThat(stream.skip(bytesToSkip)) + .as("Should skip all, regardless of backing buffers") + .isEqualTo(bytesToSkip); } stream = newStream(); - Assert.assertEquals(0, stream.skip(0)); + assertThat(stream.skip(0)).isEqualTo(0); int length = stream.available(); - Assert.assertEquals("Should stop at end when out of bytes", length, stream.skip(length + 10)); - Assert.assertEquals("Should return -1 when at end", -1, stream.skip(10)); + assertThat(stream.skip(length + 10)) + .as("Should stop at end when out of bytes") + .isEqualTo(length); + assertThat(stream.skip(10)).as("Should return -1 when at end").isEqualTo(-1); } @Test @@ -312,21 +344,21 @@ public void testSkipFully() throws Exception { stream.skipFully(bytesToSkip); - Assert.assertEquals( - "Should skip all, regardless of backing buffers", bytesToSkip, stream.position() - lastPosition); + assertThat(stream.position() - lastPosition) + .as("Should skip all, regardless of backing buffers") + .isEqualTo(bytesToSkip); lastPosition = stream.position(); } final ByteBufferInputStream stream2 = newStream(); stream2.skipFully(0); - Assert.assertEquals(0, stream2.position()); + assertThat(stream2.position()).isEqualTo(0); final int length = stream2.available(); - assertThrows("Should throw when out of bytes", EOFException.class, () -> { - stream2.skipFully(length + 10); - return null; - }); + assertThatThrownBy(() -> stream2.skipFully(length + 10)) + .isInstanceOf(EOFException.class) + .hasMessage("Not enough bytes to skip: 35 < 45"); } @Test @@ -345,16 +377,18 @@ public void testMark() throws Exception { stream.reset(); - Assert.assertEquals("Position should return to the mark", mark, stream.position()); + assertThat(stream.position()).as("Position should return to the mark").isEqualTo(mark); byte[] afterReset = new byte[100]; int bytesReadAfterReset = stream.read(afterReset); - Assert.assertEquals("Should read the same number of bytes", expectedBytesRead, bytesReadAfterReset); + assertThat(bytesReadAfterReset) + .as("Should read the same number of bytes") + .isEqualTo(expectedBytesRead); - Assert.assertEquals("Read should end at the same position", end, stream.position()); + assertThat(stream.position()).as("Read should end at the same position").isEqualTo(end); - Assert.assertArrayEquals("Content should be equal", expected, afterReset); + assertThat(afterReset).as("Content should be equal").isEqualTo(expected); } @Test @@ -374,16 +408,18 @@ public void testMarkTwice() throws Exception { stream.reset(); - Assert.assertEquals("Position should return to the mark", mark, stream.position()); + assertThat(stream.position()).as("Position should return to the mark").isEqualTo(mark); byte[] afterReset = new byte[100]; int bytesReadAfterReset = stream.read(afterReset); - Assert.assertEquals("Should read the same number of bytes", expectedBytesRead, bytesReadAfterReset); + assertThat(bytesReadAfterReset) + .as("Should read the same number of bytes") + .isEqualTo(expectedBytesRead); - Assert.assertEquals("Read should end at the same position", end, stream.position()); + assertThat(stream.position()).as("Read should end at the same position").isEqualTo(end); - Assert.assertArrayEquals("Content should be equal", expected, afterReset); + assertThat(afterReset).as("Content should be equal").isEqualTo(expected); } @Test @@ -395,20 +431,20 @@ public void testMarkAtStart() throws Exception { long mark = stream.position(); byte[] expected = new byte[10]; - Assert.assertEquals("Should read 10 bytes", 10, stream.read(expected)); + assertThat(stream.read(expected)).as("Should read 10 bytes").isEqualTo(10); long end = stream.position(); stream.reset(); - Assert.assertEquals("Position should return to the mark", mark, stream.position()); + assertThat(stream.position()).as("Position should return to the mark").isEqualTo(mark); byte[] afterReset = new byte[10]; - Assert.assertEquals("Should read 10 bytes", 10, stream.read(afterReset)); + assertThat(stream.read(afterReset)).as("Should read 10 bytes").isEqualTo(10); - Assert.assertEquals("Read should end at the same position", end, stream.position()); + assertThat(stream.position()).as("Read should end at the same position").isEqualTo(end); - Assert.assertArrayEquals("Content should be equal", expected, afterReset); + assertThat(afterReset).as("Content should be equal").isEqualTo(expected); } @Test @@ -416,37 +452,34 @@ public void testMarkAtEnd() throws Exception { ByteBufferInputStream stream = newStream(); int bytesRead = stream.read(new byte[100]); - Assert.assertTrue("Should read to end of stream", bytesRead < 100); + assertThat(bytesRead).as("Should read to end of stream").isLessThan(100); stream.mark(100); long mark = stream.position(); byte[] expected = new byte[10]; - Assert.assertEquals("Should read 0 bytes", -1, stream.read(expected)); + assertThat(stream.read(expected)).as("Should read 0 bytes").isEqualTo(-1); long end = stream.position(); stream.reset(); - Assert.assertEquals("Position should return to the mark", mark, stream.position()); + assertThat(stream.position()).as("Position should return to the mark").isEqualTo(mark); byte[] afterReset = new byte[10]; - Assert.assertEquals("Should read 0 bytes", -1, stream.read(afterReset)); + assertThat(stream.read(afterReset)).as("Should read 0 bytes").isEqualTo(-1); - Assert.assertEquals("Read should end at the same position", end, stream.position()); + assertThat(stream.position()).as("Read should end at the same position").isEqualTo(end); - Assert.assertArrayEquals("Content should be equal", expected, afterReset); + assertThat(afterReset).as("Content should be equal").isEqualTo(expected); } @Test public void testMarkUnset() { final ByteBufferInputStream stream = newStream(); - assertThrows("Should throw an error for reset() without mark()", IOException.class, () -> { - stream.reset(); - return null; - }); + assertThatThrownBy(stream::reset).isInstanceOf(IOException.class).hasMessageContaining("No mark defined"); } @Test @@ -455,22 +488,22 @@ public void testMarkAndResetTwiceOverSameRange() throws Exception { byte[] expected = new byte[6]; stream.mark(10); - Assert.assertEquals("Should read expected bytes", expected.length, stream.read(expected)); + assertThat(stream.read(expected)).as("Should read expected bytes").isEqualTo(expected.length); stream.reset(); stream.mark(10); byte[] firstRead = new byte[6]; - Assert.assertEquals("Should read firstRead bytes", firstRead.length, stream.read(firstRead)); + assertThat(stream.read(firstRead)).as("Should read firstRead bytes").isEqualTo(firstRead.length); stream.reset(); byte[] secondRead = new byte[6]; - Assert.assertEquals("Should read secondRead bytes", secondRead.length, stream.read(secondRead)); + assertThat(stream.read(secondRead)).as("Should read secondRead bytes").isEqualTo(secondRead.length); - Assert.assertArrayEquals("First read should be correct", expected, firstRead); + assertThat(firstRead).as("First read should be correct").isEqualTo(expected); - Assert.assertArrayEquals("Second read should be correct", expected, secondRead); + assertThat(secondRead).as("Second read should be correct").isEqualTo(expected); } @Test @@ -478,16 +511,13 @@ public void testMarkLimit() throws Exception { final ByteBufferInputStream stream = newStream(); stream.mark(5); - Assert.assertEquals("Should read 5 bytes", 5, stream.read(new byte[5])); + assertThat(stream.read(new byte[5])).as("Should read 5 bytes").isEqualTo(5); stream.reset(); - Assert.assertEquals("Should read 6 bytes", 6, stream.read(new byte[6])); + assertThat(stream.read(new byte[6])).as("Should read 6 bytes").isEqualTo(6); - assertThrows("Should throw an error for reset() after limit", IOException.class, () -> { - stream.reset(); - return null; - }); + assertThatThrownBy(stream::reset).isInstanceOf(IOException.class).hasMessageContaining("No mark defined"); } @Test @@ -495,14 +525,11 @@ public void testMarkDoubleReset() throws Exception { final ByteBufferInputStream stream = newStream(); stream.mark(5); - Assert.assertEquals("Should read 5 bytes", 5, stream.read(new byte[5])); + assertThat(stream.read(new byte[5])).as("Should read 5 bytes").isEqualTo(5); stream.reset(); - assertThrows("Should throw an error for double reset()", IOException.class, () -> { - stream.reset(); - return null; - }); + assertThatThrownBy(stream::reset).isInstanceOf(IOException.class).hasMessageContaining("No mark defined"); } @Test @@ -511,28 +538,7 @@ public void testToByteBuffer() { ByteBuffer buffer = stream.toByteBuffer(); for (int i = 0; i < DATA_LENGTH; ++i) { - assertEquals(i, buffer.get()); - } - } - - /** - * A convenience method to avoid a large number of @Test(expected=...) tests - * - * @param message A String message to describe this assertion - * @param expected An Exception class that the Runnable should throw - * @param callable A Callable that is expected to throw the exception - */ - public static void assertThrows(String message, Class expected, Callable callable) { - try { - callable.call(); - Assert.fail("No exception was thrown (" + message + "), expected: " + expected.getName()); - } catch (Exception actual) { - try { - Assert.assertEquals(message, expected, actual.getClass()); - } catch (AssertionError e) { - e.addSuppressed(actual); - throw e; - } + assertThat((int) buffer.get()).isEqualTo(i); } } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java index 38d4b79219..cbcc156540 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java @@ -18,9 +18,8 @@ */ package org.apache.parquet.bytes; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.never; @@ -346,7 +345,7 @@ private ByteBuffer toByteBuffer(byte[] data, int offset, int length) { } private void validate(byte[] data, Supplier factory) throws IOException { - assertEquals(data.length, factory.get().size()); + assertThat(factory.get().size()).isEqualTo(data.length); validateToByteBuffer(data, factory); validateCopy(data, factory); validateToInputStream(data, factory); @@ -384,14 +383,14 @@ private void validateToInputStream(byte[] data, Supplier factory) th private void assertContentEquals(byte[] expected, InputStream is) throws IOException { byte[] actual = new byte[expected.length]; is.read(actual); - assertArrayEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } private void validateWriteAllTo(byte[] data, Supplier factory) throws IOException { BytesInput bi = factory.get(); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { bi.writeAllTo(baos); - assertArrayEquals(data, baos.toByteArray()); + assertThat(baos.toByteArray()).isEqualTo(data); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesUtil.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesUtil.java index a146570d8a..585b7434af 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesUtil.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesUtil.java @@ -19,7 +19,7 @@ package org.apache.parquet.bytes; import static org.apache.parquet.bytes.BytesUtils.getWidthFromMaxInt; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; @@ -27,23 +27,23 @@ public class TestBytesUtil { @Test public void testWidth() { - assertEquals(0, getWidthFromMaxInt(0)); - assertEquals(1, getWidthFromMaxInt(1)); - assertEquals(2, getWidthFromMaxInt(2)); - assertEquals(2, getWidthFromMaxInt(3)); - assertEquals(3, getWidthFromMaxInt(4)); - assertEquals(3, getWidthFromMaxInt(5)); - assertEquals(3, getWidthFromMaxInt(6)); - assertEquals(3, getWidthFromMaxInt(7)); - assertEquals(4, getWidthFromMaxInt(8)); - assertEquals(4, getWidthFromMaxInt(15)); - assertEquals(5, getWidthFromMaxInt(16)); - assertEquals(5, getWidthFromMaxInt(31)); - assertEquals(6, getWidthFromMaxInt(32)); - assertEquals(6, getWidthFromMaxInt(63)); - assertEquals(7, getWidthFromMaxInt(64)); - assertEquals(7, getWidthFromMaxInt(127)); - assertEquals(8, getWidthFromMaxInt(128)); - assertEquals(8, getWidthFromMaxInt(255)); + assertThat(getWidthFromMaxInt(0)).isEqualTo(0); + assertThat(getWidthFromMaxInt(1)).isEqualTo(1); + assertThat(getWidthFromMaxInt(2)).isEqualTo(2); + assertThat(getWidthFromMaxInt(3)).isEqualTo(2); + assertThat(getWidthFromMaxInt(4)).isEqualTo(3); + assertThat(getWidthFromMaxInt(5)).isEqualTo(3); + assertThat(getWidthFromMaxInt(6)).isEqualTo(3); + assertThat(getWidthFromMaxInt(7)).isEqualTo(3); + assertThat(getWidthFromMaxInt(8)).isEqualTo(4); + assertThat(getWidthFromMaxInt(15)).isEqualTo(4); + assertThat(getWidthFromMaxInt(16)).isEqualTo(5); + assertThat(getWidthFromMaxInt(31)).isEqualTo(5); + assertThat(getWidthFromMaxInt(32)).isEqualTo(6); + assertThat(getWidthFromMaxInt(63)).isEqualTo(6); + assertThat(getWidthFromMaxInt(64)).isEqualTo(7); + assertThat(getWidthFromMaxInt(127)).isEqualTo(7); + assertThat(getWidthFromMaxInt(128)).isEqualTo(8); + assertThat(getWidthFromMaxInt(255)).isEqualTo(8); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStreamOverflow.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStreamOverflow.java index 771d9e17b4..cead063379 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStreamOverflow.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStreamOverflow.java @@ -18,8 +18,8 @@ */ package org.apache.parquet.bytes; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.lang.reflect.Field; import org.junit.After; @@ -67,7 +67,7 @@ public void testAddSlabCapsSlabSizeNearIntegerMaxValue() throws Exception { // Without the fix, the doubling strategy would compute nextSlabSize = bytesUsed (1024), // and bytesAllocated + 1024 would overflow. With the fix, nextSlabSize is capped to 100. cbaos.write(1); - assertEquals(slabSize + 1, cbaos.size()); + assertThat(cbaos.size()).isEqualTo(slabSize + 1); } } @@ -91,7 +91,9 @@ public void testAddSlabThrowsOOMOnTrueOverflow() throws Exception { // Writing 200 bytes requires minimumSize=200, but only 50 bytes remain. // The addExact(bytesAllocated, minimumSize) check should throw OOM. byte[] tooLarge = new byte[200]; - assertThrows(OutOfMemoryError.class, () -> cbaos.write(tooLarge, 0, tooLarge.length)); + assertThatThrownBy(() -> cbaos.write(tooLarge, 0, tooLarge.length)) + .isInstanceOf(OutOfMemoryError.class) + .hasMessageContaining("Size of data exceeded Integer.MAX_VALUE"); } } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestConcatenatingByteBufferCollector.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestConcatenatingByteBufferCollector.java index d973a7c96a..899e712c1f 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestConcatenatingByteBufferCollector.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestConcatenatingByteBufferCollector.java @@ -18,6 +18,8 @@ */ package org.apache.parquet.bytes; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -26,7 +28,6 @@ import java.nio.charset.StandardCharsets; import java.util.List; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -72,15 +73,14 @@ public void test() throws IOException { result = baos.toByteArray(); } - Assert.assertEquals( - "This is a test text to validate the class ConcatenatingByteBufferCollector", - new String(result, 0, 74)); + assertThat(new String(result, 0, 74)) + .isEqualTo("This is a test text to validate the class ConcatenatingByteBufferCollector"); InputStream in = new ByteArrayInputStream(result, 74, result.length - 74); - Assert.assertEquals(12345, BytesUtils.readIntLittleEndian(in)); - Assert.assertEquals(67891, BytesUtils.readUnsignedVarInt(in)); - Assert.assertEquals(2345678901L, BytesUtils.readUnsignedVarLong(in)); - Assert.assertEquals(-234567, BytesUtils.readZigZagVarInt(in)); - Assert.assertEquals(-890123456789L, BytesUtils.readZigZagVarLong(in)); + assertThat(BytesUtils.readIntLittleEndian(in)).isEqualTo(12345); + assertThat(BytesUtils.readUnsignedVarInt(in)).isEqualTo(67891); + assertThat(BytesUtils.readUnsignedVarLong(in)).isEqualTo(2345678901L); + assertThat(BytesUtils.readZigZagVarInt(in)).isEqualTo(-234567); + assertThat(BytesUtils.readZigZagVarLong(in)).isEqualTo(-890123456789L); } private static byte[] bytes(String str) { diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestDeprecatedBufferInputStream.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestDeprecatedBufferInputStream.java index 418b4264d6..9f843c238c 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestDeprecatedBufferInputStream.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestDeprecatedBufferInputStream.java @@ -18,12 +18,12 @@ */ package org.apache.parquet.bytes; +import static org.assertj.core.api.Assertions.assertThat; + import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -80,8 +80,8 @@ protected ByteBufferInputStream newStream() { @Override protected void checkOriginalData() { - Assert.assertEquals("Position should not change", 0, data.position()); - Assert.assertEquals("Limit should not change", data.array().length, data.limit()); + assertThat(data.position()).as("Position should not change").isEqualTo(0); + assertThat(data.limit()).as("Limit should not change").isEqualTo(data.array().length); } @Test @@ -96,57 +96,57 @@ public void testSliceData() throws Exception { buffers.add(stream.slice(bytesToSlice)); } - Assert.assertEquals("Position should be at end", length, stream.position()); - Assert.assertEquals("Should produce 5 buffers", 5, buffers.size()); + assertThat(stream.position()).as("Position should be at end").isEqualTo(length); + assertThat(buffers).as("Should produce 5 buffers").hasSize(5); int i = 0; ByteBuffer one = buffers.get(0); - Assert.assertSame("Should use the same backing array", one.array(), data.array()); - Assert.assertEquals(8, one.remaining()); - Assert.assertEquals(0, one.position()); - Assert.assertEquals(8, one.limit()); + assertThat(one.array()).as("Should use the same backing array").isSameAs(data.array()); + assertThat(one.remaining()).isEqualTo(8); + assertThat(one.position()).isEqualTo(0); + assertThat(one.limit()).isEqualTo(8); for (; i < 8; i += 1) { - Assert.assertEquals("Should produce correct values", i, one.get()); + assertThat((int) one.get()).as("Should produce correct values").isEqualTo(i); } ByteBuffer two = buffers.get(1); - Assert.assertSame("Should use the same backing array", two.array(), data.array()); - Assert.assertEquals(8, two.remaining()); - Assert.assertEquals(8, two.position()); - Assert.assertEquals(16, two.limit()); + assertThat(two.array()).as("Should use the same backing array").isSameAs(data.array()); + assertThat(two.remaining()).isEqualTo(8); + assertThat(two.position()).isEqualTo(8); + assertThat(two.limit()).isEqualTo(16); for (; i < 16; i += 1) { - Assert.assertEquals("Should produce correct values", i, two.get()); + assertThat((int) two.get()).as("Should produce correct values").isEqualTo(i); } // three is a copy of part of the 4th buffer ByteBuffer three = buffers.get(2); - Assert.assertSame("Should use the same backing array", three.array(), data.array()); - Assert.assertEquals(8, three.remaining()); - Assert.assertEquals(16, three.position()); - Assert.assertEquals(24, three.limit()); + assertThat(three.array()).as("Should use the same backing array").isSameAs(data.array()); + assertThat(three.remaining()).isEqualTo(8); + assertThat(three.position()).isEqualTo(16); + assertThat(three.limit()).isEqualTo(24); for (; i < 24; i += 1) { - Assert.assertEquals("Should produce correct values", i, three.get()); + assertThat((int) three.get()).as("Should produce correct values").isEqualTo(i); } // four should be a copy of the next 8 bytes ByteBuffer four = buffers.get(3); - Assert.assertSame("Should use the same backing array", four.array(), data.array()); - Assert.assertEquals(8, four.remaining()); - Assert.assertEquals(24, four.position()); - Assert.assertEquals(32, four.limit()); + assertThat(four.array()).as("Should use the same backing array").isSameAs(data.array()); + assertThat(four.remaining()).isEqualTo(8); + assertThat(four.position()).isEqualTo(24); + assertThat(four.limit()).isEqualTo(32); for (; i < 32; i += 1) { - Assert.assertEquals("Should produce correct values", i, four.get()); + assertThat((int) four.get()).as("Should produce correct values").isEqualTo(i); } // five should be a copy of the next 8 bytes ByteBuffer five = buffers.get(4); - Assert.assertSame("Should use the same backing array", five.array(), data.array()); - Assert.assertEquals(3, five.remaining()); - Assert.assertEquals(32, five.position()); - Assert.assertEquals(35, five.limit()); + assertThat(five.array()).as("Should use the same backing array").isSameAs(data.array()); + assertThat(five.remaining()).isEqualTo(3); + assertThat(five.position()).isEqualTo(32); + assertThat(five.limit()).isEqualTo(35); for (; i < 35; i += 1) { - Assert.assertEquals("Should produce correct values", i, five.get()); + assertThat((int) five.get()).as("Should produce correct values").isEqualTo(i); } } @@ -155,9 +155,8 @@ public void testWholeSliceBuffersData() throws Exception { ByteBufferInputStream stream = newStream(); List buffers = stream.sliceBuffers(stream.available()); - Assert.assertEquals( - "Should return duplicates of all non-empty buffers", - Collections.singletonList(TestSingleBufferInputStream.DATA), - buffers); + assertThat(buffers) + .as("Should return duplicates of all non-empty buffers") + .containsExactly(TestSingleBufferInputStream.DATA); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestMultiBufferInputStream.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestMultiBufferInputStream.java index 2c1b1ff52a..4bb6bb913b 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestMultiBufferInputStream.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestMultiBufferInputStream.java @@ -19,10 +19,11 @@ package org.apache.parquet.bytes; +import static org.assertj.core.api.Assertions.assertThat; + import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; import org.junit.Test; public class TestMultiBufferInputStream extends TestByteBufferInputStreams { @@ -43,8 +44,8 @@ protected ByteBufferInputStream newStream() { @Override protected void checkOriginalData() { for (ByteBuffer buffer : DATA) { - Assert.assertEquals("Position should not change", 0, buffer.position()); - Assert.assertEquals("Limit should not change", buffer.array().length, buffer.limit()); + assertThat(buffer.position()).as("Position should not change").isEqualTo(0); + assertThat(buffer.limit()).as("Limit should not change").isEqualTo(buffer.array().length); } } @@ -60,67 +61,65 @@ public void testSliceData() throws Exception { buffers.add(stream.slice(bytesToSlice)); } - Assert.assertEquals("Position should be at end", length, stream.position()); - Assert.assertEquals("Should produce 5 buffers", 5, buffers.size()); + assertThat(stream.position()).as("Position should be at end").isEqualTo(length); + assertThat(buffers).as("Should produce 5 buffers").hasSize(5); int i = 0; // one is a view of the first buffer because it is smaller ByteBuffer one = buffers.get(0); - Assert.assertSame( - "Should be a duplicate of the first array", - one.array(), - DATA.get(0).array()); - Assert.assertEquals(8, one.remaining()); - Assert.assertEquals(0, one.position()); - Assert.assertEquals(8, one.limit()); - Assert.assertEquals(9, one.capacity()); + assertThat(one.array()) + .as("Should be a duplicate of the first array") + .isSameAs(DATA.get(0).array()); + assertThat(one.remaining()).isEqualTo(8); + assertThat(one.position()).isEqualTo(0); + assertThat(one.limit()).isEqualTo(8); + assertThat(one.capacity()).isEqualTo(9); for (; i < 8; i += 1) { - Assert.assertEquals("Should produce correct values", i, one.get()); + assertThat((int) one.get()).as("Should produce correct values").isEqualTo(i); } // two should be a copy of the next 8 bytes ByteBuffer two = buffers.get(1); - Assert.assertEquals(8, two.remaining()); - Assert.assertEquals(0, two.position()); - Assert.assertEquals(8, two.limit()); - Assert.assertEquals(8, two.capacity()); + assertThat(two.remaining()).isEqualTo(8); + assertThat(two.position()).isEqualTo(0); + assertThat(two.limit()).isEqualTo(8); + assertThat(two.capacity()).isEqualTo(8); for (; i < 16; i += 1) { - Assert.assertEquals("Should produce correct values", i, two.get()); + assertThat((int) two.get()).as("Should produce correct values").isEqualTo(i); } // three is a copy of part of the 4th buffer ByteBuffer three = buffers.get(2); - Assert.assertSame( - "Should be a duplicate of the fourth array", - three.array(), - DATA.get(3).array()); - Assert.assertEquals(8, three.remaining()); - Assert.assertEquals(3, three.position()); - Assert.assertEquals(11, three.limit()); - Assert.assertEquals(12, three.capacity()); + assertThat(three.array()) + .as("Should be a duplicate of the fourth array") + .isSameAs(DATA.get(3).array()); + assertThat(three.remaining()).isEqualTo(8); + assertThat(three.position()).isEqualTo(3); + assertThat(three.limit()).isEqualTo(11); + assertThat(three.capacity()).isEqualTo(12); for (; i < 24; i += 1) { - Assert.assertEquals("Should produce correct values", i, three.get()); + assertThat((int) three.get()).as("Should produce correct values").isEqualTo(i); } // four should be a copy of the next 8 bytes ByteBuffer four = buffers.get(3); - Assert.assertEquals(8, four.remaining()); - Assert.assertEquals(0, four.position()); - Assert.assertEquals(8, four.limit()); - Assert.assertEquals(8, four.capacity()); + assertThat(four.remaining()).isEqualTo(8); + assertThat(four.position()).isEqualTo(0); + assertThat(four.limit()).isEqualTo(8); + assertThat(four.capacity()).isEqualTo(8); for (; i < 32; i += 1) { - Assert.assertEquals("Should produce correct values", i, four.get()); + assertThat((int) four.get()).as("Should produce correct values").isEqualTo(i); } // five should be a copy of the next 8 bytes ByteBuffer five = buffers.get(4); - Assert.assertEquals(3, five.remaining()); - Assert.assertEquals(0, five.position()); - Assert.assertEquals(3, five.limit()); - Assert.assertEquals(3, five.capacity()); + assertThat(five.remaining()).isEqualTo(3); + assertThat(five.position()).isEqualTo(0); + assertThat(five.limit()).isEqualTo(3); + assertThat(five.capacity()).isEqualTo(3); for (; i < 35; i += 1) { - Assert.assertEquals("Should produce correct values", i, five.get()); + assertThat((int) five.get()).as("Should produce correct values").isEqualTo(i); } } @@ -136,6 +135,8 @@ public void testSliceBuffersData() throws Exception { } } - Assert.assertEquals("Should return duplicates of all non-empty buffers", nonEmptyBuffers, buffers); + assertThat(buffers) + .as("Should return duplicates of all non-empty buffers") + .isEqualTo(nonEmptyBuffers); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestReusingByteBufferAllocator.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestReusingByteBufferAllocator.java index e200b58728..a1b3c8b458 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestReusingByteBufferAllocator.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestReusingByteBufferAllocator.java @@ -18,8 +18,8 @@ */ package org.apache.parquet.bytes; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.nio.ByteBuffer; import java.nio.InvalidMarkException; @@ -99,7 +99,7 @@ public void closeAllocator() { @Test public void normalUseCase() { try (ReusingByteBufferAllocator reusingAllocator = type.create(allocator)) { - assertEquals(innerAllocator.isDirect(), reusingAllocator.isDirect()); + assertThat(reusingAllocator.isDirect()).isEqualTo(innerAllocator.isDirect()); for (int i = 0; i < 10; ++i) { try (ByteBufferReleaser releaser = reusingAllocator.getReleaser()) { int size = RANDOM.nextInt(1024); @@ -121,11 +121,11 @@ public void normalUseCase() { } private void validateBuffer(ByteBuffer buf, int size) { - assertEquals(0, buf.position()); - assertEquals(size, buf.capacity()); - assertEquals(size, buf.remaining()); - assertEquals(allocator.isDirect(), buf.isDirect()); - assertThrows(InvalidMarkException.class, buf::reset); + assertThat(buf.position()).isEqualTo(0); + assertThat(buf.capacity()).isEqualTo(size); + assertThat(buf.remaining()).isEqualTo(size); + assertThat(buf.isDirect()).isEqualTo(allocator.isDirect()); + assertThatThrownBy(buf::reset).isInstanceOf(InvalidMarkException.class); } @Test @@ -135,14 +135,20 @@ public void validateExceptions() { ByteBuffer fromOther = allocator.allocate(10); releaser.releaseLater(fromOther); - assertThrows(IllegalStateException.class, () -> reusingAllocator.release(fromOther)); + assertThatThrownBy(() -> reusingAllocator.release(fromOther)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("The single buffer has already been released or never allocated"); ByteBuffer fromReusing = reusingAllocator.allocate(10); - assertThrows(IllegalArgumentException.class, () -> reusingAllocator.release(fromOther)); + assertThatThrownBy(() -> reusingAllocator.release(fromOther)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The buffer to be released is not the one allocated by this allocator"); switch (type) { case STRICT: - assertThrows(IllegalStateException.class, () -> reusingAllocator.allocate(5)); + assertThatThrownBy(() -> reusingAllocator.allocate(5)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("The single buffer is not yet released"); break; case UNSAFE: fromReusing = reusingAllocator.allocate(5); @@ -152,8 +158,12 @@ public void validateExceptions() { reusingAllocator.release(fromReusing); ByteBuffer fromReusingFinal = fromReusing; - assertThrows(IllegalStateException.class, () -> reusingAllocator.release(fromOther)); - assertThrows(IllegalStateException.class, () -> reusingAllocator.release(fromReusingFinal)); + assertThatThrownBy(() -> reusingAllocator.release(fromOther)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("The single buffer has already been released or never allocated"); + assertThatThrownBy(() -> reusingAllocator.release(fromReusingFinal)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("The single buffer has already been released or never allocated"); } } } diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestSingleBufferInputStream.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestSingleBufferInputStream.java index 174a08c854..74c53f0ad1 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestSingleBufferInputStream.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestSingleBufferInputStream.java @@ -19,11 +19,11 @@ package org.apache.parquet.bytes; +import static org.assertj.core.api.Assertions.assertThat; + import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import org.junit.Assert; import org.junit.Test; public class TestSingleBufferInputStream extends TestByteBufferInputStreams { @@ -39,8 +39,8 @@ protected ByteBufferInputStream newStream() { @Override protected void checkOriginalData() { - Assert.assertEquals("Position should not change", 0, DATA.position()); - Assert.assertEquals("Limit should not change", DATA.array().length, DATA.limit()); + assertThat(DATA.position()).as("Position should not change").isEqualTo(0); + assertThat(DATA.limit()).as("Limit should not change").isEqualTo(DATA.array().length); } @Test @@ -55,62 +55,62 @@ public void testSliceData() throws Exception { buffers.add(stream.slice(bytesToSlice)); } - Assert.assertEquals("Position should be at end", length, stream.position()); - Assert.assertEquals("Should produce 5 buffers", 5, buffers.size()); + assertThat(stream.position()).as("Position should be at end").isEqualTo(length); + assertThat(buffers).as("Should produce 5 buffers").hasSize(5); int i = 0; ByteBuffer one = buffers.get(0); - Assert.assertSame("Should use the same backing array", one.array(), DATA.array()); - Assert.assertEquals(8, one.remaining()); - Assert.assertEquals(0, one.position()); - Assert.assertEquals(8, one.limit()); - Assert.assertEquals(35, one.capacity()); + assertThat(one.array()).as("Should use the same backing array").isSameAs(DATA.array()); + assertThat(one.remaining()).isEqualTo(8); + assertThat(one.position()).isEqualTo(0); + assertThat(one.limit()).isEqualTo(8); + assertThat(one.capacity()).isEqualTo(35); for (; i < 8; i += 1) { - Assert.assertEquals("Should produce correct values", i, one.get()); + assertThat((int) one.get()).as("Should produce correct values").isEqualTo(i); } ByteBuffer two = buffers.get(1); - Assert.assertSame("Should use the same backing array", two.array(), DATA.array()); - Assert.assertEquals(8, two.remaining()); - Assert.assertEquals(8, two.position()); - Assert.assertEquals(16, two.limit()); - Assert.assertEquals(35, two.capacity()); + assertThat(two.array()).as("Should use the same backing array").isSameAs(DATA.array()); + assertThat(two.remaining()).isEqualTo(8); + assertThat(two.position()).isEqualTo(8); + assertThat(two.limit()).isEqualTo(16); + assertThat(two.capacity()).isEqualTo(35); for (; i < 16; i += 1) { - Assert.assertEquals("Should produce correct values", i, two.get()); + assertThat((int) two.get()).as("Should produce correct values").isEqualTo(i); } // three is a copy of part of the 4th buffer ByteBuffer three = buffers.get(2); - Assert.assertSame("Should use the same backing array", three.array(), DATA.array()); - Assert.assertEquals(8, three.remaining()); - Assert.assertEquals(16, three.position()); - Assert.assertEquals(24, three.limit()); - Assert.assertEquals(35, three.capacity()); + assertThat(three.array()).as("Should use the same backing array").isSameAs(DATA.array()); + assertThat(three.remaining()).isEqualTo(8); + assertThat(three.position()).isEqualTo(16); + assertThat(three.limit()).isEqualTo(24); + assertThat(three.capacity()).isEqualTo(35); for (; i < 24; i += 1) { - Assert.assertEquals("Should produce correct values", i, three.get()); + assertThat((int) three.get()).as("Should produce correct values").isEqualTo(i); } // four should be a copy of the next 8 bytes ByteBuffer four = buffers.get(3); - Assert.assertSame("Should use the same backing array", four.array(), DATA.array()); - Assert.assertEquals(8, four.remaining()); - Assert.assertEquals(24, four.position()); - Assert.assertEquals(32, four.limit()); - Assert.assertEquals(35, four.capacity()); + assertThat(four.array()).as("Should use the same backing array").isSameAs(DATA.array()); + assertThat(four.remaining()).isEqualTo(8); + assertThat(four.position()).isEqualTo(24); + assertThat(four.limit()).isEqualTo(32); + assertThat(four.capacity()).isEqualTo(35); for (; i < 32; i += 1) { - Assert.assertEquals("Should produce correct values", i, four.get()); + assertThat((int) four.get()).as("Should produce correct values").isEqualTo(i); } // five should be a copy of the next 8 bytes ByteBuffer five = buffers.get(4); - Assert.assertSame("Should use the same backing array", five.array(), DATA.array()); - Assert.assertEquals(3, five.remaining()); - Assert.assertEquals(32, five.position()); - Assert.assertEquals(35, five.limit()); - Assert.assertEquals(35, five.capacity()); + assertThat(five.array()).as("Should use the same backing array").isSameAs(DATA.array()); + assertThat(five.remaining()).isEqualTo(3); + assertThat(five.position()).isEqualTo(32); + assertThat(five.limit()).isEqualTo(35); + assertThat(five.capacity()).isEqualTo(35); for (; i < 35; i += 1) { - Assert.assertEquals("Should produce correct values", i, five.get()); + assertThat((int) five.get()).as("Should produce correct values").isEqualTo(i); } } @@ -119,7 +119,8 @@ public void testWholeSliceBuffersData() throws Exception { ByteBufferInputStream stream = newStream(); List buffers = stream.sliceBuffers(stream.available()); - Assert.assertEquals( - "Should return duplicates of all non-empty buffers", Collections.singletonList(DATA), buffers); + assertThat(buffers) + .as("Should return duplicates of all non-empty buffers") + .containsExactly(DATA); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/glob/TestGlob.java b/parquet-common/src/test/java/org/apache/parquet/glob/TestGlob.java index 433cc360f6..2784465ccf 100644 --- a/parquet-common/src/test/java/org/apache/parquet/glob/TestGlob.java +++ b/parquet-common/src/test/java/org/apache/parquet/glob/TestGlob.java @@ -18,11 +18,9 @@ */ package org.apache.parquet.glob; -import static junit.framework.Assert.fail; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; -import java.util.List; -import junit.framework.Assert; import org.apache.parquet.Strings; import org.apache.parquet.glob.GlobParser.GlobParseException; import org.junit.Test; @@ -31,28 +29,28 @@ public class TestGlob { @Test public void testNoGlobs() { - assertEquals(List.of("foo"), Strings.expandGlob("foo")); + assertThat(Strings.expandGlob("foo")).containsExactly("foo"); } @Test public void testEmptyGroup() { - assertEquals(List.of(""), Strings.expandGlob("")); - assertEquals(List.of(""), Strings.expandGlob("{}")); - assertEquals(List.of("a"), Strings.expandGlob("a{}")); - assertEquals(List.of("ab"), Strings.expandGlob("a{}b")); - assertEquals(List.of("a"), Strings.expandGlob("{}a")); - assertEquals(List.of("a"), Strings.expandGlob("a{}")); - assertEquals(List.of("", ""), Strings.expandGlob("{,}")); - assertEquals(List.of("ab", "a", "ac"), Strings.expandGlob("a{b,{},c}")); + assertThat(Strings.expandGlob("")).containsExactly(""); + assertThat(Strings.expandGlob("{}")).containsExactly(""); + assertThat(Strings.expandGlob("a{}")).containsExactly("a"); + assertThat(Strings.expandGlob("a{}b")).containsExactly("ab"); + assertThat(Strings.expandGlob("{}a")).containsExactly("a"); + assertThat(Strings.expandGlob("a{}")).containsExactly("a"); + assertThat(Strings.expandGlob("{,}")).containsExactly("", ""); + assertThat(Strings.expandGlob("a{b,{},c}")).containsExactly("ab", "a", "ac"); } @Test public void testSingleLevel() { - assertEquals(List.of("foobar", "foobaz"), Strings.expandGlob("foo{bar,baz}")); - assertEquals(List.of("startfooend", "startbarend"), Strings.expandGlob("start{foo,bar}end")); - assertEquals(List.of("fooend", "barend"), Strings.expandGlob("{foo,bar}end")); - assertEquals( - List.of( + assertThat(Strings.expandGlob("foo{bar,baz}")).containsExactly("foobar", "foobaz"); + assertThat(Strings.expandGlob("start{foo,bar}end")).containsExactly("startfooend", "startbarend"); + assertThat(Strings.expandGlob("{foo,bar}end")).containsExactly("fooend", "barend"); + assertThat(Strings.expandGlob("start{foo,bar}end{a,b,c,d}")) + .containsExactly( "startfooenda", "startfooendb", "startfooendc", @@ -60,16 +58,15 @@ public void testSingleLevel() { "startbarenda", "startbarendb", "startbarendc", - "startbarendd"), - Strings.expandGlob("start{foo,bar}end{a,b,c,d}")); - assertEquals(List.of("xa", "xb", "xc", "ya", "yb", "yc"), Strings.expandGlob("{x,y}{a,b,c}")); - assertEquals(List.of("x", "y", "z"), Strings.expandGlob("{x,y,z}")); + "startbarendd"); + assertThat(Strings.expandGlob("{x,y}{a,b,c}")).containsExactly("xa", "xb", "xc", "ya", "yb", "yc"); + assertThat(Strings.expandGlob("{x,y,z}")).containsExactly("x", "y", "z"); } @Test public void testNested() { - assertEquals( - List.of( + assertThat(Strings.expandGlob("{start{one,pre{two,three}post,{four,five}}end,a,b,foo{x,y}}")) + .containsExactly( "startoneend", "startpretwopostend", "startprethreepostend", @@ -78,61 +75,49 @@ public void testNested() { "a", "b", "foox", - "fooy"), - Strings.expandGlob("{start{one,pre{two,three}post,{four,five}}end,a,b,foo{x,y}}")); + "fooy"); } @Test public void testExtraBraces() { - assertEquals(List.of("x", "y", "z"), Strings.expandGlob("{{x,y,z}}")); - assertEquals(List.of("x", "y", "z"), Strings.expandGlob("{{{x,y,z}}}")); - assertEquals(List.of("startx", "starta", "startb", "starty"), Strings.expandGlob("start{x,{a,b},y}")); + assertThat(Strings.expandGlob("{{x,y,z}}")).containsExactly("x", "y", "z"); + assertThat(Strings.expandGlob("{{{x,y,z}}}")).containsExactly("x", "y", "z"); + assertThat(Strings.expandGlob("start{x,{a,b},y}")).containsExactly("startx", "starta", "startb", "starty"); } @Test public void testCommaInTopLevel() { - try { - Strings.expandGlob("foo,bar"); - fail("This should throw"); - } catch (GlobParseException e) { - Assert.assertEquals("Unexpected comma outside of a {} group:\n" + "foo,bar\n" + "---^", e.getMessage()); - } + assertThatThrownBy(() -> Strings.expandGlob("foo,bar")) + .isInstanceOf(GlobParseException.class) + .hasMessage("Unexpected comma outside of a {} group:\n" + "foo,bar\n" + "---^"); } @Test public void testCommaCornerCases() { // single empty string in each location - assertEquals(List.of("foobar", "foo", "foobaz"), Strings.expandGlob("foo{bar,,baz}")); - assertEquals(List.of("foo", "foobar", "foobaz"), Strings.expandGlob("foo{,bar,baz}")); - assertEquals(List.of("foobar", "foobaz", "foo"), Strings.expandGlob("foo{bar,baz,}")); + assertThat(Strings.expandGlob("foo{bar,,baz}")).containsExactly("foobar", "foo", "foobaz"); + assertThat(Strings.expandGlob("foo{,bar,baz}")).containsExactly("foo", "foobar", "foobaz"); + assertThat(Strings.expandGlob("foo{bar,baz,}")).containsExactly("foobar", "foobaz", "foo"); // multiple empty strings - assertEquals(List.of("foobar", "foo", "foo", "foobaz"), Strings.expandGlob("foo{bar,,,baz}")); - assertEquals(List.of("foo", "foo", "foobar", "foobaz"), Strings.expandGlob("foo{,,bar,baz}")); - assertEquals(List.of("foobar", "foobaz", "foo", "foo"), Strings.expandGlob("foo{bar,baz,,}")); + assertThat(Strings.expandGlob("foo{bar,,,baz}")).containsExactly("foobar", "foo", "foo", "foobaz"); + assertThat(Strings.expandGlob("foo{,,bar,baz}")).containsExactly("foo", "foo", "foobar", "foobaz"); + assertThat(Strings.expandGlob("foo{bar,baz,,}")).containsExactly("foobar", "foobaz", "foo", "foo"); // between groups - assertEquals(List.of("x", "y", "", "a", "b"), Strings.expandGlob("{{x,y},,{a,b}}")); + assertThat(Strings.expandGlob("{{x,y},,{a,b}}")).containsExactly("x", "y", "", "a", "b"); } private void assertNotEnoughCloseBraces(String s) { - String expected = "Not enough close braces in: "; - try { - Strings.expandGlob(s); - fail("this should throw"); - } catch (GlobParseException e) { - Assert.assertEquals(expected, e.getMessage().substring(0, expected.length())); - } + assertThatThrownBy(() -> Strings.expandGlob(s)) + .isInstanceOf(GlobParseException.class) + .hasMessageStartingWith("Not enough close braces in: "); } private void assertTooManyCloseBraces(String s) { - String expected = "Unexpected closing }:"; - try { - Strings.expandGlob(s); - fail("this should throw"); - } catch (GlobParseException e) { - Assert.assertEquals(expected, e.getMessage().substring(0, expected.length())); - } + assertThatThrownBy(() -> Strings.expandGlob(s)) + .isInstanceOf(GlobParseException.class) + .hasMessageStartingWith("Unexpected closing }:"); } @Test diff --git a/parquet-common/src/test/java/org/apache/parquet/glob/TestWildcardPath.java b/parquet-common/src/test/java/org/apache/parquet/glob/TestWildcardPath.java index ccfb9fa85a..57e1fcd56c 100644 --- a/parquet-common/src/test/java/org/apache/parquet/glob/TestWildcardPath.java +++ b/parquet-common/src/test/java/org/apache/parquet/glob/TestWildcardPath.java @@ -18,121 +18,172 @@ */ package org.apache.parquet.glob; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class TestWildcardPath { - private static void assertMatches(WildcardPath wp, String... strings) { - for (String s : strings) { - if (!wp.matches(s)) { - fail(String.format("String '%s' was expected to match '%s'", s, wp)); - } - } - } - - private static void assertDoesNotMatch(WildcardPath wp, String... strings) { - for (String s : strings) { - if (wp.matches(s)) { - fail(String.format("String '%s' was not expected to match '%s'", s, wp)); - } - } - } - @Test public void testNoWildcards() { WildcardPath wp = new WildcardPath("", "foo", '.'); - assertMatches(wp, "foo", "foo.x", "foo.x.y"); - assertDoesNotMatch(wp, "xfoo", "xfoox", "fooa.x.y"); + + assertThat(wp.matches("foo")).isTrue(); + assertThat(wp.matches("foo.x")).isTrue(); + assertThat(wp.matches("foo.x.y")).isTrue(); + + assertThat(wp.matches("xfoo")).isFalse(); + assertThat(wp.matches("xfoox")).isFalse(); + assertThat(wp.matches("fooa.x.y")).isFalse(); } @Test public void testStarMatchesEverything() { WildcardPath wp = new WildcardPath("", "*", '.'); - assertMatches(wp, "", ".", "hi", "foo.bar", "*", "foo."); + + assertThat(wp.matches("")).isTrue(); + assertThat(wp.matches(".")).isTrue(); + assertThat(wp.matches("hi")).isTrue(); + assertThat(wp.matches("foo.bar")).isTrue(); + assertThat(wp.matches("*")).isTrue(); + assertThat(wp.matches("foo.")).isTrue(); } @Test public void testChildrenPathsMatch() { WildcardPath wp = new WildcardPath("", "x.y.z", '.'); - assertMatches(wp, "x.y.z", "x.y.z.bar", "x.y.z.bar.baz.bop"); - assertDoesNotMatch(wp, "x.y.zzzz", "x.y.b", "x.y.a.z", "x.y.zhi.z"); + + assertThat(wp.matches("x.y.z")).isTrue(); + assertThat(wp.matches("x.y.z.bar")).isTrue(); + assertThat(wp.matches("x.y.z.bar.baz.bop")).isTrue(); + + assertThat(wp.matches("x.y.zzzz")).isFalse(); + assertThat(wp.matches("x.y.b")).isFalse(); + assertThat(wp.matches("x.y.a.z")).isFalse(); + assertThat(wp.matches("x.y.zhi.z")).isFalse(); } @Test public void testEmptyString() { WildcardPath wp = new WildcardPath("", "", '.'); - assertMatches(wp, ""); - assertDoesNotMatch(wp, "x"); + + assertThat(wp.matches("")).isTrue(); + assertThat(wp.matches("x")).isFalse(); } @Test public void testDoubleStarsIgnored() { WildcardPath wp = new WildcardPath("", "foo**bar", '.'); - assertMatches(wp, "foobar", "fooxyzbar", "foo.x.y.z.bar"); - assertDoesNotMatch(wp, "fobar", "hi", "foobazr"); + + assertThat(wp.matches("foobar")).isTrue(); + assertThat(wp.matches("fooxyzbar")).isTrue(); + assertThat(wp.matches("foo.x.y.z.bar")).isTrue(); + + assertThat(wp.matches("fobar")).isFalse(); + assertThat(wp.matches("hi")).isFalse(); + assertThat(wp.matches("foobazr")).isFalse(); wp = new WildcardPath("", "foo********bar", '.'); - assertMatches(wp, "foobar", "fooxyzbar", "foo.x.y.z.bar"); - assertDoesNotMatch(wp, "fobar", "hi", "foobazr"); + + assertThat(wp.matches("foobar")).isTrue(); + assertThat(wp.matches("fooxyzbar")).isTrue(); + assertThat(wp.matches("foo.x.y.z.bar")).isTrue(); + + assertThat(wp.matches("fobar")).isFalse(); + assertThat(wp.matches("hi")).isFalse(); + assertThat(wp.matches("foobazr")).isFalse(); } @Test public void testStarsAtBeginAndEnd() { WildcardPath wp = new WildcardPath("", "*x.y.z", '.'); - assertMatches(wp, "a.b.c.x.y.z", "x.y.z", "zoopx.y.z", "zoopx.y.z.child"); - assertDoesNotMatch(wp, "a.b.c.x.y", "xy.z", "hi"); + + assertThat(wp.matches("a.b.c.x.y.z")).isTrue(); + assertThat(wp.matches("x.y.z")).isTrue(); + assertThat(wp.matches("zoopx.y.z")).isTrue(); + assertThat(wp.matches("zoopx.y.z.child")).isTrue(); + + assertThat(wp.matches("a.b.c.x.y")).isFalse(); + assertThat(wp.matches("xy.z")).isFalse(); + assertThat(wp.matches("hi")).isFalse(); wp = new WildcardPath("", "*.x.y.z", '.'); - assertMatches(wp, "a.b.c.x.y.z", "foo.x.y.z", "foo.x.y.z.child"); - assertDoesNotMatch(wp, "x.y.z", "a.b.c.x.y", "xy.z", "hi", "zoopx.y.z", "zoopx.y.z.child"); + + assertThat(wp.matches("a.b.c.x.y.z")).isTrue(); + assertThat(wp.matches("foo.x.y.z")).isTrue(); + assertThat(wp.matches("foo.x.y.z.child")).isTrue(); + + assertThat(wp.matches("x.y.z")).isFalse(); + assertThat(wp.matches("a.b.c.x.y")).isFalse(); + assertThat(wp.matches("xy.z")).isFalse(); + assertThat(wp.matches("hi")).isFalse(); + assertThat(wp.matches("zoopx.y.z")).isFalse(); + assertThat(wp.matches("zoopx.y.z.child")).isFalse(); wp = new WildcardPath("", "x.y.z*", '.'); - assertMatches(wp, "x.y.z", "x.y.z.foo", "x.y.zoo", "x.y.zoo.bar"); - assertDoesNotMatch(wp, "a.b.c.x.y.z", "foo.x.y.z", "hi"); + + assertThat(wp.matches("x.y.z")).isTrue(); + assertThat(wp.matches("x.y.z.foo")).isTrue(); + assertThat(wp.matches("x.y.zoo")).isTrue(); + assertThat(wp.matches("x.y.zoo.bar")).isTrue(); + + assertThat(wp.matches("a.b.c.x.y.z")).isFalse(); + assertThat(wp.matches("foo.x.y.z")).isFalse(); + assertThat(wp.matches("hi")).isFalse(); wp = new WildcardPath("", "x.y.z.*", '.'); - assertMatches(wp, "x.y.z.foo", "x.y.z.bar.baz"); - assertDoesNotMatch(wp, "x.y.z", "a.b.c.x.y.z", "x.y.zoo", "foo.x.y.z", "hi", "x.y.zoo.bar"); + + assertThat(wp.matches("x.y.z.foo")).isTrue(); + assertThat(wp.matches("x.y.z.bar.baz")).isTrue(); + + assertThat(wp.matches("x.y.z")).isFalse(); + assertThat(wp.matches("a.b.c.x.y.z")).isFalse(); + assertThat(wp.matches("x.y.zoo")).isFalse(); + assertThat(wp.matches("foo.x.y.z")).isFalse(); + assertThat(wp.matches("hi")).isFalse(); + assertThat(wp.matches("x.y.zoo.bar")).isFalse(); } @Test public void testComplex() { WildcardPath wp = new WildcardPath("", "*.street", '.'); - assertMatches( - wp, - "home.address.street", - "home.address.street.number", - "work.address.street", - "work.address.street.foo", - "street.street", - "street.street.street.street", - "thing.street.thing"); - - assertDoesNotMatch( - wp, - "home.address.street_2", - "home.address.street_2.number", - "work.addressstreet", - "work.addressstreet.foo", - "", - "x.y.z.street2", - "x.y.z.street2.z"); + + assertThat(wp.matches("home.address.street")).isTrue(); + assertThat(wp.matches("home.address.street.number")).isTrue(); + assertThat(wp.matches("work.address.street")).isTrue(); + assertThat(wp.matches("work.address.street.foo")).isTrue(); + assertThat(wp.matches("street.street")).isTrue(); + assertThat(wp.matches("street.street.street.street")).isTrue(); + assertThat(wp.matches("thing.street.thing")).isTrue(); + + assertThat(wp.matches("home.address.street_2")).isFalse(); + assertThat(wp.matches("home.address.street_2.number")).isFalse(); + assertThat(wp.matches("work.addressstreet")).isFalse(); + assertThat(wp.matches("work.addressstreet.foo")).isFalse(); + assertThat(wp.matches("")).isFalse(); + assertThat(wp.matches("x.y.z.street2")).isFalse(); + assertThat(wp.matches("x.y.z.street2.z")).isFalse(); wp = new WildcardPath("", "x.y.*_stat.average", '.'); - assertMatches( - wp, - "x.y.z_stat.average", - "x.y.foo_stat.average", - "x.y.z.a.b_stat.average", - "x.y.z.a.b_stat.average.child", - "x.y.z._stat.average"); - assertDoesNotMatch( - wp, "x.y.z_stats.average", "x.y.z_stat.averages", "x.y_stat.average", "x.yyy.foo_stat.average"); + + assertThat(wp.matches("x.y.z_stat.average")).isTrue(); + assertThat(wp.matches("x.y.foo_stat.average")).isTrue(); + assertThat(wp.matches("x.y.z.a.b_stat.average")).isTrue(); + assertThat(wp.matches("x.y.z.a.b_stat.average.child")).isTrue(); + assertThat(wp.matches("x.y.z._stat.average")).isTrue(); + + assertThat(wp.matches("x.y.z_stats.average")).isFalse(); + assertThat(wp.matches("x.y.z_stat.averages")).isFalse(); + assertThat(wp.matches("x.y_stat.average")).isFalse(); + assertThat(wp.matches("x.yyy.foo_stat.average")).isFalse(); wp = new WildcardPath("", "x.y.pre*.bar", '.'); - assertMatches(wp, "x.y.pre.bar", "x.y.preabc.bar", "x.y.prebar.bar"); - assertDoesNotMatch(wp, "x.y.pre.baraaaa", "x.y.preabc.baraaaa"); + + assertThat(wp.matches("x.y.pre.bar")).isTrue(); + assertThat(wp.matches("x.y.preabc.bar")).isTrue(); + assertThat(wp.matches("x.y.prebar.bar")).isTrue(); + + assertThat(wp.matches("x.y.pre.baraaaa")).isFalse(); + assertThat(wp.matches("x.y.preabc.baraaaa")).isFalse(); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/io/TestDelegatingSeekableInputStream.java b/parquet-common/src/test/java/org/apache/parquet/io/TestDelegatingSeekableInputStream.java index f69e6ef26f..9b5c140e52 100644 --- a/parquet-common/src/test/java/org/apache/parquet/io/TestDelegatingSeekableInputStream.java +++ b/parquet-common/src/test/java/org/apache/parquet/io/TestDelegatingSeekableInputStream.java @@ -20,14 +20,13 @@ package org.apache.parquet.io; import static org.apache.parquet.io.MockInputStream.TEST_ARRAY; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.EOFException; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; -import java.util.concurrent.Callable; -import org.apache.parquet.TestUtils; -import org.junit.Assert; import org.junit.Test; public class TestDelegatingSeekableInputStream { @@ -39,8 +38,10 @@ public void testReadFully() throws Exception { MockInputStream stream = new MockInputStream(); DelegatingSeekableInputStream.readFully(stream, buffer, 0, buffer.length); - Assert.assertArrayEquals("Byte array contents should match", Arrays.copyOfRange(TEST_ARRAY, 0, 5), buffer); - Assert.assertEquals("Stream position should reflect bytes read", 5, stream.getPos()); + assertThat(buffer).as("Byte array contents should match").isEqualTo(Arrays.copyOfRange(TEST_ARRAY, 0, 5)); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(5); } @Test @@ -50,8 +51,10 @@ public void testReadFullySmallReads() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFully(stream, buffer, 0, buffer.length); - Assert.assertArrayEquals("Byte array contents should match", Arrays.copyOfRange(TEST_ARRAY, 0, 5), buffer); - Assert.assertEquals("Stream position should reflect bytes read", 5, stream.getPos()); + assertThat(buffer).as("Byte array contents should match").isEqualTo(Arrays.copyOfRange(TEST_ARRAY, 0, 5)); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(5); } @Test @@ -61,14 +64,14 @@ public void testReadFullyJustRight() throws Exception { final MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFully(stream, buffer, 0, buffer.length); - Assert.assertArrayEquals("Byte array contents should match", TEST_ARRAY, buffer); - Assert.assertEquals("Stream position should reflect bytes read", 10, stream.getPos()); + assertThat(buffer).as("Byte array contents should match").isEqualTo(TEST_ARRAY); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(10); - TestUtils.assertThrows( - "Should throw EOFException if no more bytes left", EOFException.class, (Callable) () -> { - DelegatingSeekableInputStream.readFully(stream, buffer, 0, 1); - return null; - }); + assertThatThrownBy(() -> DelegatingSeekableInputStream.readFully(stream, buffer, 0, 1)) + .isInstanceOf(EOFException.class) + .hasMessage("Reached the end of stream with 1 bytes left to read"); } @Test @@ -77,14 +80,16 @@ public void testReadFullyUnderflow() throws Exception { final MockInputStream stream = new MockInputStream(2, 3, 3); - TestUtils.assertThrows( - "Should throw EOFException if no more bytes left", EOFException.class, (Callable) () -> { - DelegatingSeekableInputStream.readFully(stream, buffer, 0, buffer.length); - return null; - }); + assertThatThrownBy(() -> DelegatingSeekableInputStream.readFully(stream, buffer, 0, buffer.length)) + .isInstanceOf(EOFException.class) + .hasMessage("Reached the end of stream with 1 bytes left to read"); - Assert.assertArrayEquals("Should have consumed bytes", TEST_ARRAY, Arrays.copyOfRange(buffer, 0, 10)); - Assert.assertEquals("Stream position should reflect bytes read", 10, stream.getPos()); + assertThat(Arrays.copyOfRange(buffer, 0, 10)) + .as("Should have consumed bytes") + .isEqualTo(TEST_ARRAY); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(10); } @Test @@ -94,11 +99,12 @@ public void testReadFullyStartAndLength() throws IOException { MockInputStream stream = new MockInputStream(); DelegatingSeekableInputStream.readFully(stream, buffer, 2, 5); - Assert.assertArrayEquals( - "Byte array contents should match", - Arrays.copyOfRange(TEST_ARRAY, 0, 5), - Arrays.copyOfRange(buffer, 2, 7)); - Assert.assertEquals("Stream position should reflect bytes read", 5, stream.getPos()); + assertThat(Arrays.copyOfRange(buffer, 2, 7)) + .as("Byte array contents should match") + .isEqualTo(Arrays.copyOfRange(TEST_ARRAY, 0, 5)); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(5); } @Test @@ -108,7 +114,9 @@ public void testReadFullyZeroByteRead() throws IOException { MockInputStream stream = new MockInputStream(); DelegatingSeekableInputStream.readFully(stream, buffer, 0, buffer.length); - Assert.assertEquals("Stream position should reflect bytes read", 0, stream.getPos()); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(0); } @Test @@ -118,11 +126,12 @@ public void testReadFullySmallReadsWithStartAndLength() throws IOException { MockInputStream stream = new MockInputStream(2, 2, 3); DelegatingSeekableInputStream.readFully(stream, buffer, 2, 5); - Assert.assertArrayEquals( - "Byte array contents should match", - Arrays.copyOfRange(TEST_ARRAY, 0, 5), - Arrays.copyOfRange(buffer, 2, 7)); - Assert.assertEquals("Stream position should reflect bytes read", 5, stream.getPos()); + assertThat(Arrays.copyOfRange(buffer, 2, 7)) + .as("Byte array contents should match") + .isEqualTo(Arrays.copyOfRange(TEST_ARRAY, 0, 5)); + assertThat(stream.getPos()) + .as("Stream position should reflect bytes read") + .isEqualTo(5); } private static final ThreadLocal TEMP = ThreadLocal.withInitial(() -> new byte[8192]); @@ -134,15 +143,15 @@ public void testHeapRead() throws Exception { MockInputStream stream = new MockInputStream(); int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, len); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(len).isEqualTo(10); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(20); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(-1, len); + assertThat(len).isEqualTo(-1); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -152,15 +161,15 @@ public void testHeapSmallBuffer() throws Exception { MockInputStream stream = new MockInputStream(); int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(5, len); - Assert.assertEquals(5, readBuffer.position()); - Assert.assertEquals(5, readBuffer.limit()); + assertThat(len).isEqualTo(5); + assertThat(readBuffer.position()).isEqualTo(5); + assertThat(readBuffer.limit()).isEqualTo(5); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 5), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 5)); } @Test @@ -170,27 +179,27 @@ public void testHeapSmallReads() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(2, len); - Assert.assertEquals(2, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(2); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(3, len); - Assert.assertEquals(5, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(3); + assertThat(readBuffer.position()).isEqualTo(5); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(3, len); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(3); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(2, len); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -202,20 +211,20 @@ public void testHeapPosition() throws Exception { MockInputStream stream = new MockInputStream(8); int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(8, len); - Assert.assertEquals(18, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(len).isEqualTo(8); + assertThat(readBuffer.position()).isEqualTo(18); + assertThat(readBuffer.limit()).isEqualTo(20); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(2, len); - Assert.assertEquals(20, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(20); + assertThat(readBuffer.limit()).isEqualTo(20); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(-1, len); + assertThat(len).isEqualTo(-1); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -226,20 +235,20 @@ public void testHeapLimit() throws Exception { MockInputStream stream = new MockInputStream(7); int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(7, len); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(len).isEqualTo(7); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(8); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(1, len); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(len).isEqualTo(1); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(8); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -252,20 +261,20 @@ public void testHeapPositionAndLimit() throws Exception { MockInputStream stream = new MockInputStream(7); int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(7, len); - Assert.assertEquals(12, readBuffer.position()); - Assert.assertEquals(13, readBuffer.limit()); + assertThat(len).isEqualTo(7); + assertThat(readBuffer.position()).isEqualTo(12); + assertThat(readBuffer.limit()).isEqualTo(13); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(1, len); - Assert.assertEquals(13, readBuffer.position()); - Assert.assertEquals(13, readBuffer.limit()); + assertThat(len).isEqualTo(1); + assertThat(readBuffer.position()).isEqualTo(13); + assertThat(readBuffer.limit()).isEqualTo(13); len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -275,15 +284,15 @@ public void testDirectRead() throws Exception { MockInputStream stream = new MockInputStream(); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, len); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(len).isEqualTo(10); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(20); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(-1, len); + assertThat(len).isEqualTo(-1); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -293,15 +302,15 @@ public void testDirectSmallBuffer() throws Exception { MockInputStream stream = new MockInputStream(); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(5, len); - Assert.assertEquals(5, readBuffer.position()); - Assert.assertEquals(5, readBuffer.limit()); + assertThat(len).isEqualTo(5); + assertThat(readBuffer.position()).isEqualTo(5); + assertThat(readBuffer.limit()).isEqualTo(5); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 5), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 5)); } @Test @@ -311,27 +320,27 @@ public void testDirectSmallReads() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(2, len); - Assert.assertEquals(2, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(2); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(3, len); - Assert.assertEquals(5, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(3); + assertThat(readBuffer.position()).isEqualTo(5); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(3, len); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(3); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(2, len); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -343,20 +352,20 @@ public void testDirectPosition() throws Exception { MockInputStream stream = new MockInputStream(8); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(8, len); - Assert.assertEquals(18, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(len).isEqualTo(8); + assertThat(readBuffer.position()).isEqualTo(18); + assertThat(readBuffer.limit()).isEqualTo(20); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(2, len); - Assert.assertEquals(20, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(20); + assertThat(readBuffer.limit()).isEqualTo(20); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(-1, len); + assertThat(len).isEqualTo(-1); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -367,20 +376,20 @@ public void testDirectLimit() throws Exception { MockInputStream stream = new MockInputStream(7); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(7, len); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(len).isEqualTo(7); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(8); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(1, len); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(len).isEqualTo(1); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(8); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -393,20 +402,20 @@ public void testDirectPositionAndLimit() throws Exception { MockInputStream stream = new MockInputStream(7); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(7, len); - Assert.assertEquals(12, readBuffer.position()); - Assert.assertEquals(13, readBuffer.limit()); + assertThat(len).isEqualTo(7); + assertThat(readBuffer.position()).isEqualTo(12); + assertThat(readBuffer.limit()).isEqualTo(13); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(1, len); - Assert.assertEquals(13, readBuffer.position()); - Assert.assertEquals(13, readBuffer.limit()); + assertThat(len).isEqualTo(1); + assertThat(readBuffer.position()).isEqualTo(13); + assertThat(readBuffer.limit()).isEqualTo(13); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -418,30 +427,30 @@ public void testDirectSmallTempBufferSmallReads() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(2, len); - Assert.assertEquals(2, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(2); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(3, len); - Assert.assertEquals(5, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(3); + assertThat(readBuffer.position()).isEqualTo(5); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(3, len); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(3); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(2, len); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(len).isEqualTo(2); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(-1, len); + assertThat(len).isEqualTo(-1); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -456,20 +465,20 @@ public void testDirectSmallTempBufferWithPositionAndLimit() throws Exception { MockInputStream stream = new MockInputStream(7); int len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(7, len); - Assert.assertEquals(12, readBuffer.position()); - Assert.assertEquals(13, readBuffer.limit()); + assertThat(len).isEqualTo(7); + assertThat(readBuffer.position()).isEqualTo(12); + assertThat(readBuffer.limit()).isEqualTo(13); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(1, len); - Assert.assertEquals(13, readBuffer.position()); - Assert.assertEquals(13, readBuffer.limit()); + assertThat(len).isEqualTo(1); + assertThat(readBuffer.position()).isEqualTo(13); + assertThat(readBuffer.limit()).isEqualTo(13); len = DelegatingSeekableInputStream.readDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(0, len); + assertThat(len).isEqualTo(0); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -479,15 +488,15 @@ public void testHeapReadFullySmallBuffer() throws Exception { MockInputStream stream = new MockInputStream(); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(8); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(8); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -496,13 +505,12 @@ public void testHeapReadFullyLargeBuffer() throws Exception { final MockInputStream stream = new MockInputStream(); - TestUtils.assertThrows("Should throw EOFException", EOFException.class, () -> { - DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - return null; - }); + assertThatThrownBy(() -> DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer)) + .isInstanceOf(EOFException.class) + .hasMessage("Reached the end of stream with 10 bytes left to read"); - Assert.assertEquals(0, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(0); + assertThat(readBuffer.limit()).isEqualTo(20); } @Test @@ -513,16 +521,16 @@ public void testHeapReadFullyJustRight() throws Exception { // reads all of the bytes available without EOFException DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); // trying to read 0 more bytes doesn't result in EOFException DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -532,15 +540,15 @@ public void testHeapReadFullySmallReads() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -552,15 +560,15 @@ public void testHeapReadFullyPosition() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); } @Test @@ -571,24 +579,24 @@ public void testHeapReadFullyLimit() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); readBuffer.position(7); readBuffer.limit(10); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -601,24 +609,24 @@ public void testHeapReadFullyPositionAndLimit() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 4), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 4)); readBuffer.position(7); readBuffer.limit(10); DelegatingSeekableInputStream.readFullyHeapBuffer(stream, readBuffer); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); } @Test @@ -628,15 +636,15 @@ public void testDirectReadFullySmallBuffer() throws Exception { MockInputStream stream = new MockInputStream(); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(8); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(8, readBuffer.position()); - Assert.assertEquals(8, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(8); + assertThat(readBuffer.limit()).isEqualTo(8); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 8)); } @Test @@ -645,10 +653,9 @@ public void testDirectReadFullyLargeBuffer() throws Exception { final MockInputStream stream = new MockInputStream(); - TestUtils.assertThrows("Should throw EOFException", EOFException.class, () -> { - DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - return null; - }); + assertThatThrownBy(() -> DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get())) + .isInstanceOf(EOFException.class) + .hasMessage("Reached the end of stream with 10 bytes left to read"); // NOTE: This behavior differs from readFullyHeapBuffer because direct uses // several read operations that will read up to the end of the input. This @@ -656,8 +663,8 @@ public void testDirectReadFullyLargeBuffer() throws Exception { // behavior can't be implemented for the heap buffer without using the read // method instead of the readFully method on the underlying // FSDataInputStream. - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(20, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(20); } @Test @@ -668,16 +675,16 @@ public void testDirectReadFullyJustRight() throws Exception { // reads all of the bytes available without EOFException DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); // trying to read 0 more bytes doesn't result in EOFException DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -687,15 +694,15 @@ public void testDirectReadFullySmallReads() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -707,15 +714,15 @@ public void testDirectReadFullyPosition() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); } @Test @@ -726,24 +733,24 @@ public void testDirectReadFullyLimit() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); readBuffer.position(7); readBuffer.limit(10); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.flip(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY)); } @Test @@ -756,24 +763,24 @@ public void testDirectReadFullyPositionAndLimit() throws Exception { MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 4), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 4)); readBuffer.position(7); readBuffer.limit(10); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, TEMP.get()); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); } @Test @@ -788,23 +795,23 @@ public void testDirectReadFullySmallTempBufferWithPositionAndLimit() throws Exce MockInputStream stream = new MockInputStream(2, 3, 3); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(7, readBuffer.position()); - Assert.assertEquals(7, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(7); + assertThat(readBuffer.limit()).isEqualTo(7); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 4), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 4)); readBuffer.position(7); readBuffer.limit(10); DelegatingSeekableInputStream.readFullyDirectBuffer(stream, readBuffer, temp); - Assert.assertEquals(10, readBuffer.position()); - Assert.assertEquals(10, readBuffer.limit()); + assertThat(readBuffer.position()).isEqualTo(10); + assertThat(readBuffer.limit()).isEqualTo(10); readBuffer.reset(); - Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 7), readBuffer); + assertThat(readBuffer).as("Buffer contents should match").isEqualTo(ByteBuffer.wrap(TEST_ARRAY, 0, 7)); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/io/TestLocalInputOutput.java b/parquet-common/src/test/java/org/apache/parquet/io/TestLocalInputOutput.java index f7401a1fcc..b367dfbece 100644 --- a/parquet-common/src/test/java/org/apache/parquet/io/TestLocalInputOutput.java +++ b/parquet-common/src/test/java/org/apache/parquet/io/TestLocalInputOutput.java @@ -18,9 +18,8 @@ */ package org.apache.parquet.io; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.EOFException; import java.io.File; @@ -46,8 +45,8 @@ public void outputFileOverwritesFile() throws IOException { } InputFile read = new LocalInputFile(path); try (SeekableInputStream stream = read.newStream()) { - assertEquals(stream.read(), 124); - assertEquals(stream.read(), -1); + assertThat(stream.read()).isEqualTo(124); + assertThat(stream.read()).isEqualTo(-1); } } @@ -56,7 +55,9 @@ public void outputFileCreateFailsAsFileAlreadyExists() throws IOException { Path path = Paths.get(createTempFile().getPath()); OutputFile write = new LocalOutputFile(path); write.create(512).close(); - assertThrows(FileAlreadyExistsException.class, () -> write.create(512).close()); + assertThatThrownBy(() -> write.create(512).close()) + .isInstanceOf(FileAlreadyExistsException.class) + .hasMessage(path.toString()); } @Test @@ -68,8 +69,8 @@ public void outputFileCreatesFileWithOverwrite() throws IOException { } InputFile read = new LocalInputFile(path); try (SeekableInputStream stream = read.newStream()) { - assertEquals(stream.read(), 255); - assertEquals(stream.read(), -1); + assertThat(stream.read()).isEqualTo(255); + assertThat(stream.read()).isEqualTo(-1); } } @@ -82,8 +83,8 @@ public void outputFileCreatesFile() throws IOException { } InputFile read = new LocalInputFile(path); try (SeekableInputStream stream = read.newStream()) { - assertEquals(stream.read(), 2); - assertEquals(stream.read(), -1); + assertThat(stream.read()).isEqualTo(2); + assertThat(stream.read()).isEqualTo(-1); } } @@ -100,11 +101,11 @@ public void readFullyIntoHeapByteBuffer() throws IOException { try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer buf = ByteBuffer.allocate(5); stream.readFully(buf); - assertEquals(5, buf.position()); + assertThat(buf.position()).isEqualTo(5); buf.flip(); byte[] out = new byte[5]; buf.get(out); - assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, out); + assertThat(out).isEqualTo(new byte[] {1, 2, 3, 4, 5}); } } @@ -115,11 +116,11 @@ public void readFullyIntoHeapByteBufferWithNonZeroPosition() throws IOException ByteBuffer buf = ByteBuffer.allocate(6); buf.put(new byte[] {99, 99}); // advance position to 2 stream.readFully(buf); - assertEquals(6, buf.position()); + assertThat(buf.position()).isEqualTo(6); buf.flip(); byte[] out = new byte[6]; buf.get(out); - assertArrayEquals(new byte[] {99, 99, 10, 20, 30, 40}, out); + assertThat(out).isEqualTo(new byte[] {99, 99, 10, 20, 30, 40}); } } @@ -129,11 +130,11 @@ public void readFullyIntoDirectByteBuffer() throws IOException { try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer buf = ByteBuffer.allocateDirect(3); stream.readFully(buf); - assertEquals(3, buf.position()); + assertThat(buf.position()).isEqualTo(3); buf.flip(); byte[] out = new byte[3]; buf.get(out); - assertArrayEquals(new byte[] {7, 8, 9}, out); + assertThat(out).isEqualTo(new byte[] {7, 8, 9}); } } @@ -143,7 +144,7 @@ public void readFullyIntoReadOnlyByteBuffer() throws IOException { try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer backing = ByteBuffer.allocate(3); ByteBuffer buf = backing.asReadOnlyBuffer(); - assertThrows(ReadOnlyBufferException.class, () -> stream.readFully(buf)); + assertThatThrownBy(() -> stream.readFully(buf)).isInstanceOf(ReadOnlyBufferException.class); } } @@ -153,12 +154,12 @@ public void readIntoHeapByteBuffer() throws IOException { try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer buf = ByteBuffer.allocate(4); int read = stream.read(buf); - assertEquals(4, read); - assertEquals(4, buf.position()); + assertThat(read).isEqualTo(4); + assertThat(buf.position()).isEqualTo(4); buf.flip(); byte[] out = new byte[4]; buf.get(out); - assertArrayEquals(new byte[] {1, 2, 3, 4}, out); + assertThat(out).isEqualTo(new byte[] {1, 2, 3, 4}); } } @@ -168,8 +169,8 @@ public void readIntoByteBufferAdvancesPositionByBytesRead() throws IOException { try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer buf = ByteBuffer.allocate(10); int read = stream.read(buf); - assertEquals(3, read); - assertEquals(3, buf.position()); + assertThat(read).isEqualTo(3); + assertThat(buf.position()).isEqualTo(3); } } @@ -177,11 +178,11 @@ public void readIntoByteBufferAdvancesPositionByBytesRead() throws IOException { public void readIntoByteBufferReturnsMinusOneAtEof() throws IOException { Path path = writeBytes(new byte[] {1}); try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { - assertEquals(1, stream.read()); + assertThat(stream.read()).isEqualTo(1); ByteBuffer buf = ByteBuffer.allocate(4); int read = stream.read(buf); - assertEquals(-1, read); - assertEquals(0, buf.position()); + assertThat(read).isEqualTo(-1); + assertThat(buf.position()).isEqualTo(0); } } @@ -191,12 +192,12 @@ public void readIntoDirectByteBuffer() throws IOException { try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer buf = ByteBuffer.allocateDirect(3); int read = stream.read(buf); - assertEquals(3, read); - assertEquals(3, buf.position()); + assertThat(read).isEqualTo(3); + assertThat(buf.position()).isEqualTo(3); buf.flip(); byte[] out = new byte[3]; buf.get(out); - assertArrayEquals(new byte[] {7, 8, 9}, out); + assertThat(out).isEqualTo(new byte[] {7, 8, 9}); } } @@ -207,12 +208,12 @@ public void readIntoByteBufferWithNonZeroPosition() throws IOException { ByteBuffer buf = ByteBuffer.allocate(5); buf.put(new byte[] {99, 99}); // advance position to 2 int read = stream.read(buf); - assertEquals(3, read); - assertEquals(5, buf.position()); + assertThat(read).isEqualTo(3); + assertThat(buf.position()).isEqualTo(5); buf.flip(); byte[] out = new byte[5]; buf.get(out); - assertArrayEquals(new byte[] {99, 99, 10, 20, 30}, out); + assertThat(out).isEqualTo(new byte[] {99, 99, 10, 20, 30}); } } @@ -221,7 +222,7 @@ public void readFullyThrowsEofWhenStreamTooShort() throws IOException { Path path = writeBytes(new byte[] {1, 2}); try (SeekableInputStream stream = new LocalInputFile(path).newStream()) { ByteBuffer buf = ByteBuffer.allocate(10); - assertThrows(EOFException.class, () -> stream.readFully(buf)); + assertThatThrownBy(() -> stream.readFully(buf)).isInstanceOf(EOFException.class); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/util/TestDynConstructors.java b/parquet-common/src/test/java/org/apache/parquet/util/TestDynConstructors.java index 26124b9c81..201d9845fb 100644 --- a/parquet-common/src/test/java/org/apache/parquet/util/TestDynConstructors.java +++ b/parquet-common/src/test/java/org/apache/parquet/util/TestDynConstructors.java @@ -19,10 +19,10 @@ package org.apache.parquet.util; -import java.util.concurrent.Callable; -import org.apache.parquet.TestUtils; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import org.apache.parquet.util.Concatenator.SomeCheckedException; -import org.junit.Assert; import org.junit.Test; public class TestDynConstructors { @@ -30,24 +30,26 @@ public class TestDynConstructors { public void testNoImplCall() { final DynConstructors.Builder builder = new DynConstructors.Builder(); - TestUtils.assertThrows( - "Checked build should throw NoSuchMethodException", NoSuchMethodException.class, (Callable) - builder::buildChecked); + assertThatThrownBy(builder::buildChecked) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining("Cannot find constructor for null"); - TestUtils.assertThrows( - "Normal build should throw RuntimeException", RuntimeException.class, (Runnable) builder::build); + assertThatThrownBy(builder::build) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining("Cannot find constructor for null"); } @Test public void testMissingClass() { final DynConstructors.Builder builder = new DynConstructors.Builder().impl("not.a.RealClass"); - TestUtils.assertThrows( - "Checked build should throw NoSuchMethodException", NoSuchMethodException.class, (Callable) - builder::buildChecked); + assertThatThrownBy(builder::buildChecked) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining("Cannot find constructor for null"); - TestUtils.assertThrows( - "Normal build should throw RuntimeException", RuntimeException.class, (Callable) builder::build); + assertThatThrownBy(builder::build) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining("Cannot find constructor for null"); } @Test @@ -55,12 +57,15 @@ public void testMissingConstructor() { final DynConstructors.Builder builder = new DynConstructors.Builder().impl(Concatenator.class, String.class, String.class); - TestUtils.assertThrows( - "Checked build should throw NoSuchMethodException", NoSuchMethodException.class, (Callable) - builder::buildChecked); + assertThatThrownBy(builder::buildChecked) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining( + "Missing org.apache.parquet.util.Concatenator(java.lang.String,java.lang.String)"); - TestUtils.assertThrows( - "Normal build should throw RuntimeException", RuntimeException.class, (Callable) builder::build); + assertThatThrownBy(builder::build) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining( + "Missing org.apache.parquet.util.Concatenator(java.lang.String,java.lang.String)"); } @Test @@ -72,17 +77,17 @@ public void testFirstImplReturned() throws Exception { .buildChecked(); Concatenator dashCat = sepCtor.newInstanceChecked("-"); - Assert.assertEquals("Should construct with the 1-arg version", "a-b", dashCat.concat("a", "b")); + assertThat(dashCat.concat("a", "b")) + .as("Should construct with the 1-arg version") + .isEqualTo("a-b"); - TestUtils.assertThrows( - "Should complain about extra arguments", - IllegalArgumentException.class, - () -> sepCtor.newInstanceChecked("/", "-")); + assertThatThrownBy(() -> sepCtor.newInstanceChecked("/", "-")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("wrong number of arguments"); - TestUtils.assertThrows( - "Should complain about extra arguments", - IllegalArgumentException.class, - () -> sepCtor.newInstance("/", "-")); + assertThatThrownBy(() -> sepCtor.newInstance("/", "-")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("wrong number of arguments"); DynConstructors.Ctor defaultCtor = new DynConstructors.Builder() .impl("not.a.RealClass", String.class) @@ -91,7 +96,9 @@ public void testFirstImplReturned() throws Exception { .buildChecked(); Concatenator cat = defaultCtor.newInstanceChecked(); - Assert.assertEquals("Should construct with the no-arg version", "ab", cat.concat("a", "b")); + assertThat(cat.concat("a", "b")) + .as("Should construct with the no-arg version") + .isEqualTo("ab"); } @Test @@ -102,13 +109,11 @@ public void testExceptionThrown() throws Exception { .impl(Concatenator.class, Exception.class) .buildChecked(); - TestUtils.assertThrows( - "Should re-throw the exception", SomeCheckedException.class, () -> sepCtor.newInstanceChecked(exc)); + assertThatThrownBy(() -> sepCtor.newInstanceChecked(exc)).isInstanceOf(SomeCheckedException.class); - TestUtils.assertThrows( - "Should wrap the exception in RuntimeException", - RuntimeException.class, - () -> sepCtor.newInstance(exc)); + assertThatThrownBy(() -> sepCtor.newInstance(exc)) + .isInstanceOf(RuntimeException.class) + .hasMessage("org.apache.parquet.util.Concatenator$SomeCheckedException"); } @Test @@ -117,26 +122,26 @@ public void testStringClassname() throws Exception { .impl(Concatenator.class.getName(), String.class) .buildChecked(); - Assert.assertNotNull("Should find 1-arg constructor", sepCtor.newInstance("-")); + assertThat(sepCtor.newInstance("-")).as("Should find 1-arg constructor").isNotNull(); } @Test public void testHiddenMethod() throws Exception { - TestUtils.assertThrows( - "Should fail to find hidden method", NoSuchMethodException.class, () -> new DynMethods.Builder( - "setSeparator") + assertThatThrownBy(() -> new DynMethods.Builder("setSeparator") .impl(Concatenator.class, char.class) - .buildChecked()); + .buildChecked()) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining("Cannot find method: setSeparator"); final DynConstructors.Ctor sepCtor = new DynConstructors.Builder() .hiddenImpl(Concatenator.class.getName(), char.class) .buildChecked(); - Assert.assertNotNull("Should find hidden ctor with hiddenImpl", sepCtor); + assertThat(sepCtor).as("Should find hidden ctor with hiddenImpl").isNotNull(); Concatenator slashCat = sepCtor.newInstanceChecked('/'); - Assert.assertEquals("Should use separator /", "a/b", slashCat.concat("a", "b")); + assertThat(slashCat.concat("a", "b")).as("Should use separator /").isEqualTo("a/b"); } @Test @@ -144,10 +149,11 @@ public void testBind() throws Exception { final DynConstructors.Ctor ctor = new DynConstructors.Builder().impl(Concatenator.class.getName()).buildChecked(); - Assert.assertTrue("Should always be static", ctor.isStatic()); + assertThat(ctor.isStatic()).as("Should always be static").isTrue(); - TestUtils.assertThrows( - "Should complain that method is static", IllegalStateException.class, () -> ctor.bind(null)); + assertThatThrownBy(() -> ctor.bind(null)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Cannot bind constructors"); } @Test @@ -155,15 +161,19 @@ public void testInvoke() throws Exception { final DynMethods.UnboundMethod ctor = new DynConstructors.Builder().impl(Concatenator.class.getName()).buildChecked(); - TestUtils.assertThrows( - "Should complain that target must be null", - IllegalArgumentException.class, - () -> ctor.invokeChecked("a")); + assertThatThrownBy(() -> ctor.invokeChecked("a")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid call to constructor: target must be null"); - TestUtils.assertThrows( - "Should complain that target must be null", IllegalArgumentException.class, () -> ctor.invoke("a")); + assertThatThrownBy(() -> ctor.invoke("a")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid call to constructor: target must be null"); - Assert.assertNotNull("Should allow invokeChecked(null, ...)", ctor.invokeChecked(null)); - Assert.assertNotNull("Should allow invoke(null, ...)", ctor.invoke(null)); + assertThat((Object) ctor.invokeChecked(null)) + .as("Should allow invokeChecked(null, ...)") + .isNotNull(); + assertThat((Object) ctor.invoke(null)) + .as("Should allow invoke(null, ...)") + .isNotNull(); } } diff --git a/parquet-common/src/test/java/org/apache/parquet/util/TestDynMethods.java b/parquet-common/src/test/java/org/apache/parquet/util/TestDynMethods.java index 94e9ed93a9..f49a73c0cb 100644 --- a/parquet-common/src/test/java/org/apache/parquet/util/TestDynMethods.java +++ b/parquet-common/src/test/java/org/apache/parquet/util/TestDynMethods.java @@ -19,10 +19,10 @@ package org.apache.parquet.util; -import java.util.concurrent.Callable; -import org.apache.parquet.TestUtils; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import org.apache.parquet.util.Concatenator.SomeCheckedException; -import org.junit.Assert; import org.junit.Test; public class TestDynMethods { @@ -30,12 +30,13 @@ public class TestDynMethods { public void testNoImplCall() { final DynMethods.Builder builder = new DynMethods.Builder("concat"); - TestUtils.assertThrows( - "Checked build should throw NoSuchMethodException", NoSuchMethodException.class, (Callable) - builder::buildChecked); + assertThatThrownBy(builder::buildChecked) + .isInstanceOf(NoSuchMethodException.class) + .hasMessage("Cannot find method: concat"); - TestUtils.assertThrows( - "Normal build should throw RuntimeException", RuntimeException.class, (Callable) builder::build); + assertThatThrownBy(builder::build) + .isInstanceOf(RuntimeException.class) + .hasMessage("Cannot find method: concat"); } @Test @@ -43,12 +44,13 @@ public void testMissingClass() { final DynMethods.Builder builder = new DynMethods.Builder("concat").impl("not.a.RealClass", String.class, String.class); - TestUtils.assertThrows( - "Checked build should throw NoSuchMethodException", NoSuchMethodException.class, (Callable) - builder::buildChecked); + assertThatThrownBy(builder::buildChecked) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining("Cannot find method: concat"); - TestUtils.assertThrows( - "Normal build should throw RuntimeException", RuntimeException.class, (Runnable) builder::build); + assertThatThrownBy(builder::build) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining("Cannot find method: concat"); } @Test @@ -56,12 +58,13 @@ public void testMissingMethod() { final DynMethods.Builder builder = new DynMethods.Builder("concat").impl(Concatenator.class, "cat2strings", String.class, String.class); - TestUtils.assertThrows( - "Checked build should throw NoSuchMethodException", NoSuchMethodException.class, (Callable) - builder::buildChecked); + assertThatThrownBy(builder::buildChecked) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining("Cannot find method: concat"); - TestUtils.assertThrows( - "Normal build should throw RuntimeException", RuntimeException.class, (Runnable) builder::build); + assertThatThrownBy(builder::build) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining("Cannot find method: concat"); } @Test @@ -73,9 +76,13 @@ public void testFirstImplReturned() throws Exception { .impl(Concatenator.class, String.class, String.class, String.class) .buildChecked(); - Assert.assertEquals("Should call the 2-arg version successfully", "a-b", cat2.invoke(obj, "a", "b")); + assertThat((String) cat2.invoke(obj, "a", "b")) + .as("Should call the 2-arg version successfully") + .isEqualTo("a-b"); - Assert.assertEquals("Should ignore extra arguments", "a-b", cat2.invoke(obj, "a", "b", "c")); + assertThat((String) cat2.invoke(obj, "a", "b", "c")) + .as("Should ignore extra arguments") + .isEqualTo("a-b"); DynMethods.UnboundMethod cat3 = new DynMethods.Builder("concat") .impl("not.a.RealClass", String.class, String.class) @@ -83,9 +90,13 @@ public void testFirstImplReturned() throws Exception { .impl(Concatenator.class, String.class, String.class) .build(); - Assert.assertEquals("Should call the 3-arg version successfully", "a-b-c", cat3.invoke(obj, "a", "b", "c")); + assertThat((String) cat3.invoke(obj, "a", "b", "c")) + .as("Should call the 3-arg version successfully") + .isEqualTo("a-b-c"); - Assert.assertEquals("Should call the 3-arg version null padding", "a-b-null", cat3.invoke(obj, "a", "b")); + assertThat((String) cat3.invoke(obj, "a", "b")) + .as("Should call the 3-arg version null padding") + .isEqualTo("a-b-null"); } @Test @@ -94,13 +105,13 @@ public void testVarArgs() throws Exception { .impl(Concatenator.class, String[].class) .buildChecked(); - Assert.assertEquals("Should use the varargs version", "abcde", cat.invokeChecked(new Concatenator(), (Object) - new String[] {"a", "b", "c", "d", "e"})); + assertThat((String) cat.invokeChecked(new Concatenator(), (Object) new String[] {"a", "b", "c", "d", "e"})) + .as("Should use the varargs version") + .isEqualTo("abcde"); - Assert.assertEquals( - "Should use the varargs version", - "abcde", - cat.bind(new Concatenator()).invokeChecked((Object) new String[] {"a", "b", "c", "d", "e"})); + assertThat((String) cat.bind(new Concatenator()).invokeChecked((Object) new String[] {"a", "b", "c", "d", "e"})) + .as("Should use the varargs version") + .isEqualTo("abcde"); } @Test @@ -111,15 +122,13 @@ public void testIncorrectArguments() throws Exception { .impl(Concatenator.class, String.class, String.class) .buildChecked(); - TestUtils.assertThrows( - "Should fail if non-string arguments are passed", - IllegalArgumentException.class, - () -> cat.invoke(obj, 3, 4)); + assertThatThrownBy(() -> cat.invoke(obj, 3, 4)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("argument type mismatch"); - TestUtils.assertThrows( - "Should fail if non-string arguments are passed", - IllegalArgumentException.class, - () -> cat.invokeChecked(obj, 3, 4)); + assertThatThrownBy(() -> cat.invokeChecked(obj, 3, 4)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("argument type mismatch"); } @Test @@ -131,11 +140,11 @@ public void testExceptionThrown() throws Exception { .impl(Concatenator.class, Exception.class) .buildChecked(); - TestUtils.assertThrows( - "Should re-throw the exception", SomeCheckedException.class, () -> cat.invokeChecked(obj, exc)); + assertThatThrownBy(() -> cat.invokeChecked(obj, exc)).isInstanceOf(SomeCheckedException.class); - TestUtils.assertThrows( - "Should wrap the exception in RuntimeException", RuntimeException.class, () -> cat.invoke(obj, exc)); + assertThatThrownBy(() -> cat.invoke(obj, exc)) + .isInstanceOf(RuntimeException.class) + .hasMessage("org.apache.parquet.util.Concatenator$SomeCheckedException"); } @Test @@ -145,7 +154,9 @@ public void testNameChange() throws Exception { .impl(Concatenator.class, "concat", String.class, String.class) .buildChecked(); - Assert.assertEquals("Should find 2-arg concat method", "a-b", cat.invoke(obj, "a", "b")); + assertThat((String) cat.invoke(obj, "a", "b")) + .as("Should find 2-arg concat method") + .isEqualTo("a-b"); } @Test @@ -155,28 +166,32 @@ public void testStringClassname() throws Exception { .impl(Concatenator.class.getName(), String.class, String.class) .buildChecked(); - Assert.assertEquals("Should find 2-arg concat method", "a-b", cat.invoke(obj, "a", "b")); + assertThat((String) cat.invoke(obj, "a", "b")) + .as("Should find 2-arg concat method") + .isEqualTo("a-b"); } @Test public void testHiddenMethod() throws Exception { Concatenator obj = new Concatenator("-"); - TestUtils.assertThrows( - "Should fail to find hidden method", NoSuchMethodException.class, () -> new DynMethods.Builder( - "setSeparator") + assertThatThrownBy(() -> new DynMethods.Builder("setSeparator") .impl(Concatenator.class, String.class) - .buildChecked()); + .buildChecked()) + .isInstanceOf(NoSuchMethodException.class) + .hasMessageContaining("Cannot find method: setSeparator"); DynMethods.UnboundMethod changeSep = new DynMethods.Builder("setSeparator") .hiddenImpl(Concatenator.class, String.class) .buildChecked(); - Assert.assertNotNull("Should find hidden method with hiddenImpl", changeSep); + assertThat(changeSep).as("Should find hidden method with hiddenImpl").isNotNull(); changeSep.invokeChecked(obj, "/"); - Assert.assertEquals("Should use separator / instead of -", "a/b", obj.concat("a", "b")); + assertThat(obj.concat("a", "b")) + .as("Should use separator / instead of -") + .isEqualTo("a/b"); } @Test @@ -189,37 +204,40 @@ public void testBoundMethod() throws Exception { DynMethods.BoundMethod dashCat = cat.bind(new Concatenator("-")); DynMethods.BoundMethod underCat = cat.bind(new Concatenator("_")); - Assert.assertEquals("Should use '-' object without passing", "a-b", dashCat.invoke("a", "b")); - Assert.assertEquals("Should use '_' object without passing", "a_b", underCat.invoke("a", "b")); + assertThat((String) dashCat.invoke("a", "b")) + .as("Should use '-' object without passing") + .isEqualTo("a-b"); + assertThat((String) underCat.invoke("a", "b")) + .as("Should use '_' object without passing") + .isEqualTo("a_b"); DynMethods.BoundMethod slashCat = new DynMethods.Builder("concat") .impl(Concatenator.class, String.class, String.class) .buildChecked(new Concatenator("/")); - Assert.assertEquals("Should use bound object from builder without passing", "a/b", slashCat.invoke("a", "b")); + assertThat((String) slashCat.invoke("a", "b")) + .as("Should use bound object from builder without passing") + .isEqualTo("a/b"); } @Test public void testBindStaticMethod() throws Exception { final DynMethods.Builder builder = new DynMethods.Builder("cat").impl(Concatenator.class, String[].class); - TestUtils.assertThrows( - "Should complain that method is static", - IllegalStateException.class, - () -> builder.buildChecked(new Concatenator())); + assertThatThrownBy(() -> builder.buildChecked(new Concatenator())) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Cannot bind static method"); - TestUtils.assertThrows( - "Should complain that method is static", - IllegalStateException.class, - () -> builder.build(new Concatenator())); + assertThatThrownBy(() -> builder.build(new Concatenator())) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Cannot bind static method"); final DynMethods.UnboundMethod staticCat = builder.buildChecked(); - Assert.assertTrue("Should be static", staticCat.isStatic()); + assertThat(staticCat.isStatic()).as("Should be static").isTrue(); - TestUtils.assertThrows( - "Should complain that method is static", - IllegalStateException.class, - () -> staticCat.bind(new Concatenator())); + assertThatThrownBy(() -> staticCat.bind(new Concatenator())) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Cannot bind static method"); } @Test @@ -228,9 +246,9 @@ public void testStaticMethod() throws Exception { .impl(Concatenator.class, String[].class) .buildStaticChecked(); - Assert.assertEquals( - "Should call varargs static method cat(String...)", "abcde", staticCat.invokeChecked((Object) - new String[] {"a", "b", "c", "d", "e"})); + assertThat((String) staticCat.invokeChecked((Object) new String[] {"a", "b", "c", "d", "e"})) + .as("Should call varargs static method cat(String...)") + .isEqualTo("abcde"); } @Test @@ -238,17 +256,22 @@ public void testNonStaticMethod() throws Exception { final DynMethods.Builder builder = new DynMethods.Builder("concat").impl(Concatenator.class, String.class, String.class); - TestUtils.assertThrows( - "Should complain that method is not static", IllegalStateException.class, builder::buildStatic); + assertThatThrownBy(builder::buildStatic) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Method is not static"); - TestUtils.assertThrows( - "Should complain that method is not static", IllegalStateException.class, builder::buildStaticChecked); + assertThatThrownBy(builder::buildStaticChecked) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Method is not static"); final DynMethods.UnboundMethod cat2 = builder.buildChecked(); - Assert.assertFalse("concat(String,String) should not be static", cat2.isStatic()); + assertThat(cat2.isStatic()) + .as("concat(String,String) should not be static") + .isFalse(); - TestUtils.assertThrows( - "Should complain that method is not static", IllegalStateException.class, cat2::asStatic); + assertThatThrownBy(cat2::asStatic) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Method is not static"); } @Test @@ -258,25 +281,33 @@ public void testConstructorImpl() throws Exception { .impl(Concatenator.class, String.class); DynMethods.UnboundMethod newConcatenator = builder.buildChecked(); - Assert.assertTrue("Should find constructor implementation", newConcatenator instanceof DynConstructors.Ctor); - Assert.assertTrue("Constructor should be a static method", newConcatenator.isStatic()); - Assert.assertFalse("Constructor should not be NOOP", newConcatenator.isNoop()); + assertThat(newConcatenator) + .as("Should find constructor implementation") + .isInstanceOf(DynConstructors.Ctor.class); + assertThat(newConcatenator.isStatic()) + .as("Constructor should be a static method") + .isTrue(); + assertThat(newConcatenator.isNoop()) + .as("Constructor should not be NOOP") + .isFalse(); // constructors cannot be bound - TestUtils.assertThrows( - "Should complain that ctor method is static", - IllegalStateException.class, - () -> builder.buildChecked(new Concatenator())); - TestUtils.assertThrows( - "Should complain that ctor method is static", - IllegalStateException.class, - () -> builder.build(new Concatenator())); + assertThatThrownBy(() -> builder.buildChecked(new Concatenator())) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Cannot bind constructors"); + assertThatThrownBy(() -> builder.build(new Concatenator())) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Cannot bind constructors"); Concatenator concatenator = newConcatenator.asStatic().invoke("*"); - Assert.assertEquals("Should function as a concatenator", "a*b", concatenator.concat("a", "b")); + assertThat(concatenator.concat("a", "b")) + .as("Should function as a concatenator") + .isEqualTo("a*b"); concatenator = newConcatenator.asStatic().invokeChecked("@"); - Assert.assertEquals("Should function as a concatenator", "a@b", concatenator.concat("a", "b")); + assertThat(concatenator.concat("a", "b")) + .as("Should function as a concatenator") + .isEqualTo("a@b"); } @Test @@ -286,9 +317,9 @@ public void testConstructorImplAfterFactoryMethod() throws Exception { .ctorImpl(Concatenator.class, String.class) .buildChecked(); - Assert.assertFalse( - "Should find factory method before constructor method", - newConcatenator instanceof DynConstructors.Ctor); + assertThat(newConcatenator) + .as("Should find factory method before constructor method") + .isNotInstanceOf(DynConstructors.Ctor.class); } @Test @@ -299,11 +330,23 @@ public void testNoop() throws Exception { .orNoop() .buildChecked(); - Assert.assertTrue("No implementation found, should return NOOP", noop.isNoop()); - Assert.assertNull("NOOP should always return null", noop.invoke(new Concatenator(), "a")); - Assert.assertNull("NOOP can be called with null", noop.invoke(null, "a")); - Assert.assertNull("NOOP can be bound", noop.bind(new Concatenator()).invoke("a")); - Assert.assertNull("NOOP can be bound to null", noop.bind(null).invoke("a")); - Assert.assertNull("NOOP can be static", noop.asStatic().invoke("a")); + assertThat(noop.isNoop()) + .as("No implementation found, should return NOOP") + .isTrue(); + assertThat((Object) noop.invoke(new Concatenator(), "a")) + .as("NOOP should always return null") + .isNull(); + assertThat((Object) noop.invoke(null, "a")) + .as("NOOP can be called with null") + .isNull(); + assertThat((Object) noop.bind(new Concatenator()).invoke("a")) + .as("NOOP can be bound") + .isNull(); + assertThat((Object) noop.bind(null).invoke("a")) + .as("NOOP can be bound to null") + .isNull(); + assertThat((Object) noop.asStatic().invoke("a")) + .as("NOOP can be static") + .isNull(); } }