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.IOException;
import java.sql.SQLException;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -52,9 +52,8 @@ public class App {
* Program entry point.
*
* @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();
@ -68,17 +67,17 @@ public class App {
* exception occurs.
*/
private static void useOfMute() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
var out = new ByteArrayOutputStream();
Mute.mute(() -> out.write("Hello".getBytes()));
}
private static void useOfLoggedMute() throws SQLException {
Resource resource = null;
private static void useOfLoggedMute() {
Optional<Resource> resource = Optional.empty();
try {
resource = acquireResource();
utilizeResource(resource);
resource = Optional.of(acquireResource());
utilizeResource(resource.get());
} 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.
*/
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);
}
private static Resource acquireResource() throws SQLException {
private static Resource acquireResource() {
return new Resource() {
@Override

View File

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

View File

@ -23,17 +23,15 @@
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.slf4j.Logger;
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
*/
@ -50,9 +48,7 @@ public class MuteTest {
@Test
public void muteShouldRethrowUnexpectedExceptionAsAssertionError() {
assertThrows(AssertionError.class, () -> {
Mute.mute(this::methodThrowingException);
});
assertThrows(AssertionError.class, () -> Mute.mute(this::methodThrowingException));
}
@Test
@ -62,7 +58,7 @@ public class MuteTest {
@Test
public void loggedMuteShouldLogExceptionTraceBeforeSwallowingIt() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
var stream = new ByteArrayOutputStream();
System.setErr(new PrintStream(stream));
Mute.loggedMute(this::methodThrowingException);