FunctionsTest method references

Use method references instead of lambda expression to clean up the
code and comply to the project's Checkstyle rules.
This commit is contained in:
Allon Mureinik 2019-02-08 19:36:29 +02:00
parent 76a243c587
commit 5e315d1ac9
1 changed files with 25 additions and 25 deletions

View File

@ -118,7 +118,7 @@ class FunctionsTest {
void testRunnable() { void testRunnable() {
FailureOnOddInvocations.invocation = 0; FailureOnOddInvocations.invocation = 0;
try { try {
Functions.run(() -> new FailureOnOddInvocations()); Functions.run(FailureOnOddInvocations::new);
fail("Expected Exception"); fail("Expected Exception");
} catch (UndeclaredThrowableException e) { } catch (UndeclaredThrowableException e) {
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();
@ -126,14 +126,14 @@ class FunctionsTest {
assertTrue(cause instanceof SomeException); assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage()); assertEquals("Odd Invocation: 1", cause.getMessage());
} }
Functions.run(() -> new FailureOnOddInvocations()); Functions.run(FailureOnOddInvocations::new);
} }
@Test @Test
void testCallable() { void testCallable() {
FailureOnOddInvocations.invocation = 0; FailureOnOddInvocations.invocation = 0;
try { try {
Functions.call(() -> new FailureOnOddInvocations()); Functions.call(FailureOnOddInvocations::new);
fail("Expected Exception"); fail("Expected Exception");
} catch (UndeclaredThrowableException e) { } catch (UndeclaredThrowableException e) {
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();
@ -141,7 +141,7 @@ class FunctionsTest {
assertTrue(cause instanceof SomeException); assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage()); assertEquals("Odd Invocation: 1", cause.getMessage());
} }
final FailureOnOddInvocations instance = Functions.call(() -> new FailureOnOddInvocations()); final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
assertNotNull(instance); assertNotNull(instance);
} }
@ -150,7 +150,7 @@ class FunctionsTest {
final IllegalStateException ise = new IllegalStateException(); final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise); final Testable testable = new Testable(ise);
try { try {
Functions.accept((t) -> t.test(), testable); Functions.accept(Testable::test, testable);
fail("Expected Exception"); fail("Expected Exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertSame(ise, e); assertSame(ise, e);
@ -158,14 +158,14 @@ class FunctionsTest {
final Error error = new OutOfMemoryError(); final Error error = new OutOfMemoryError();
testable.setThrowable(error); testable.setThrowable(error);
try { try {
Functions.accept((t) -> t.test(), testable); Functions.accept(Testable::test, testable);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
assertSame(error, e); assertSame(error, e);
} }
final IOException ioe = new IOException("Unknown I/O error"); final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe); testable.setThrowable(ioe);
try { try {
Functions.accept((t) -> t.test(), testable); Functions.accept(Testable::test, testable);
fail("Expected Exception"); fail("Expected Exception");
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
final Throwable t = e.getCause(); final Throwable t = e.getCause();
@ -174,7 +174,7 @@ class FunctionsTest {
assertSame(ioe, t); assertSame(ioe, t);
} }
testable.setThrowable(null); testable.setThrowable(null);
Functions.accept((t) -> t.test(), testable); Functions.accept(Testable::test, testable);
} }
@Test @Test
@ -182,21 +182,21 @@ class FunctionsTest {
final IllegalStateException ise = new IllegalStateException(); final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null); final Testable testable = new Testable(null);
try { try {
Functions.accept((t1,t2) -> t1.test(t2), testable, ise); Functions.accept(Testable::test, testable, ise);
fail("Expected Exception"); fail("Expected Exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertSame(ise, e); assertSame(ise, e);
} }
final Error error = new OutOfMemoryError(); final Error error = new OutOfMemoryError();
try { try {
Functions.accept((t1,t2) -> t1.test(t2), testable, error); Functions.accept(Testable::test, testable, error);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
assertSame(error, e); assertSame(error, e);
} }
final IOException ioe = new IOException("Unknown I/O error"); final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe); testable.setThrowable(ioe);
try { try {
Functions.accept((t1,t2) -> t1.test(t2), testable, ioe); Functions.accept(Testable::test, testable, ioe);
fail("Expected Exception"); fail("Expected Exception");
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
final Throwable t = e.getCause(); final Throwable t = e.getCause();
@ -205,7 +205,7 @@ class FunctionsTest {
assertSame(ioe, t); assertSame(ioe, t);
} }
testable.setThrowable(null); testable.setThrowable(null);
Functions.accept((t1,t2) -> t1.test(t2), testable, (Throwable) null); Functions.accept(Testable::test, testable, (Throwable) null);
} }
@Test @Test
@ -213,7 +213,7 @@ class FunctionsTest {
final IllegalStateException ise = new IllegalStateException(); final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise); final Testable testable = new Testable(ise);
try { try {
Functions.apply((t) -> t.testInt(), testable); Functions.apply(Testable::testInt, testable);
fail("Expected Exception"); fail("Expected Exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertSame(ise, e); assertSame(ise, e);
@ -221,14 +221,14 @@ class FunctionsTest {
final Error error = new OutOfMemoryError(); final Error error = new OutOfMemoryError();
testable.setThrowable(error); testable.setThrowable(error);
try { try {
Functions.apply((t) -> t.testInt(), testable); Functions.apply(Testable::testInt, testable);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
assertSame(error, e); assertSame(error, e);
} }
final IOException ioe = new IOException("Unknown I/O error"); final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe); testable.setThrowable(ioe);
try { try {
Functions.apply((t) -> t.testInt(), testable); Functions.apply(Testable::testInt, testable);
fail("Expected Exception"); fail("Expected Exception");
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
final Throwable t = e.getCause(); final Throwable t = e.getCause();
@ -237,7 +237,7 @@ class FunctionsTest {
assertSame(ioe, t); assertSame(ioe, t);
} }
testable.setThrowable(null); testable.setThrowable(null);
final Integer i = Functions.apply((t) -> t.testInt(), testable); final Integer i = Functions.apply(Testable::testInt, testable);
assertNotNull(i); assertNotNull(i);
assertEquals(0, i.intValue()); assertEquals(0, i.intValue());
} }
@ -247,20 +247,20 @@ class FunctionsTest {
final IllegalStateException ise = new IllegalStateException(); final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null); final Testable testable = new Testable(null);
try { try {
Functions.apply((t1,t2) -> t1.testInt(t2), testable, ise); Functions.apply(Testable::testInt, testable, ise);
fail("Expected Exception"); fail("Expected Exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertSame(ise, e); assertSame(ise, e);
} }
final Error error = new OutOfMemoryError(); final Error error = new OutOfMemoryError();
try { try {
Functions.apply((t1,t2) -> t1.testInt(t2), testable, error); Functions.apply(Testable::testInt, testable, error);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
assertSame(error, e); assertSame(error, e);
} }
final IOException ioe = new IOException("Unknown I/O error"); final IOException ioe = new IOException("Unknown I/O error");
try { try {
Functions.apply((t1,t2) -> t1.testInt(t2), testable, ioe); Functions.apply(Testable::testInt, testable, ioe);
fail("Expected Exception"); fail("Expected Exception");
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
final Throwable t = e.getCause(); final Throwable t = e.getCause();
@ -268,7 +268,7 @@ class FunctionsTest {
assertTrue(t instanceof IOException); assertTrue(t instanceof IOException);
assertSame(ioe, t); assertSame(ioe, t);
} }
final Integer i = Functions.apply((t1,t2) -> t1.testInt(t2), testable, (Throwable) null); final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
assertNotNull(i); assertNotNull(i);
assertEquals(0, i.intValue()); assertEquals(0, i.intValue());
} }
@ -276,10 +276,10 @@ class FunctionsTest {
@Test @Test
public void testTryWithResources() { public void testTryWithResources() {
final CloseableObject co = new CloseableObject(); final CloseableObject co = new CloseableObject();
final FailableConsumer<Throwable,? extends Throwable> consumer = (th) -> co.run(th); final FailableConsumer<Throwable,? extends Throwable> consumer = co::run;
final IllegalStateException ise = new IllegalStateException(); final IllegalStateException ise = new IllegalStateException();
try { try {
Functions.tryWithResources(() -> consumer.accept(ise), () -> co.close()); Functions.tryWithResources(() -> consumer.accept(ise), co::close);
fail("Expected Exception"); fail("Expected Exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertSame(ise, e); assertSame(ise, e);
@ -288,7 +288,7 @@ class FunctionsTest {
co.reset(); co.reset();
final Error error = new OutOfMemoryError(); final Error error = new OutOfMemoryError();
try { try {
Functions.tryWithResources(() -> consumer.accept(error), () -> co.close()); Functions.tryWithResources(() -> consumer.accept(error), co::close);
fail("Expected Exception"); fail("Expected Exception");
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
assertSame(error, e); assertSame(error, e);
@ -297,7 +297,7 @@ class FunctionsTest {
co.reset(); co.reset();
final IOException ioe = new IOException("Unknown I/O error"); final IOException ioe = new IOException("Unknown I/O error");
try { try {
Functions.tryWithResources(() -> consumer.accept(ioe), () -> co.close()); Functions.tryWithResources(() -> consumer.accept(ioe), co::close);
fail("Expected Exception"); fail("Expected Exception");
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
final IOException cause = e.getCause(); final IOException cause = e.getCause();
@ -305,7 +305,7 @@ class FunctionsTest {
} }
assertTrue(co.isClosed()); assertTrue(co.isClosed());
co.reset(); co.reset();
Functions.tryWithResources(() -> consumer.accept(null), () -> co.close()); Functions.tryWithResources(() -> consumer.accept(null), co::close);
assertTrue(co.isClosed()); assertTrue(co.isClosed());
} }
} }