Java 11 migraiton: mute-idiom

This commit is contained in:
Anurag Agarwal 2020-04-12 22:58:50 +00:00
parent 9b105d770d
commit 2fa938c02d
No known key found for this signature in database
GPG Key ID: CF5E14552DA23F13
3 changed files with 20 additions and 26 deletions

View File

@ -25,7 +25,7 @@ package com.iluwatar.mute;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.util.Optional;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -52,9 +52,8 @@ public class App {
* Program entry point. * Program entry point.
* *
* @param args command line args. * @param args command line args.
* @throws Exception if any exception occurs
*/ */
public static void main(String[] args) throws Exception { public static void main(String[] args) {
useOfLoggedMute(); useOfLoggedMute();
@ -68,17 +67,17 @@ public class App {
* exception occurs. * exception occurs.
*/ */
private static void useOfMute() { private static void useOfMute() {
ByteArrayOutputStream out = new ByteArrayOutputStream(); var out = new ByteArrayOutputStream();
Mute.mute(() -> out.write("Hello".getBytes())); Mute.mute(() -> out.write("Hello".getBytes()));
} }
private static void useOfLoggedMute() throws SQLException { private static void useOfLoggedMute() {
Resource resource = null; Optional<Resource> resource = Optional.empty();
try { try {
resource = acquireResource(); resource = Optional.of(acquireResource());
utilizeResource(resource); utilizeResource(resource.get());
} finally { } finally {
closeResource(resource); resource.ifPresent(App::closeResource);
} }
} }
@ -86,14 +85,14 @@ public class App {
* All we can do while failed close of a resource is to log it. * All we can do while failed close of a resource is to log it.
*/ */
private static void closeResource(Resource resource) { private static void closeResource(Resource resource) {
Mute.loggedMute(() -> resource.close()); Mute.loggedMute(resource::close);
} }
private static void utilizeResource(Resource resource) throws SQLException { private static void utilizeResource(Resource resource) {
LOGGER.info("Utilizing acquired resource: {}", resource); LOGGER.info("Utilizing acquired resource: {}", resource);
} }
private static Resource acquireResource() throws SQLException { private static Resource acquireResource() {
return new Resource() { return new Resource() {
@Override @Override

View File

@ -27,12 +27,11 @@ import org.junit.jupiter.api.Test;
/** /**
* Tests that Mute idiom example runs without errors. * Tests that Mute idiom example runs without errors.
*
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() throws Exception { public void test() {
App.main(null); App.main(new String[]{});
} }
} }

View File

@ -23,17 +23,15 @@
package com.iluwatar.mute; package com.iluwatar.mute;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test for the mute-idiom pattern * Test for the mute-idiom pattern
*/ */
@ -50,9 +48,7 @@ public class MuteTest {
@Test @Test
public void muteShouldRethrowUnexpectedExceptionAsAssertionError() { public void muteShouldRethrowUnexpectedExceptionAsAssertionError() {
assertThrows(AssertionError.class, () -> { assertThrows(AssertionError.class, () -> Mute.mute(this::methodThrowingException));
Mute.mute(this::methodThrowingException);
});
} }
@Test @Test
@ -62,7 +58,7 @@ public class MuteTest {
@Test @Test
public void loggedMuteShouldLogExceptionTraceBeforeSwallowingIt() { public void loggedMuteShouldLogExceptionTraceBeforeSwallowingIt() {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); var stream = new ByteArrayOutputStream();
System.setErr(new PrintStream(stream)); System.setErr(new PrintStream(stream));
Mute.loggedMute(this::methodThrowingException); Mute.loggedMute(this::methodThrowingException);