Skip to content

Commit 2fa938c

Browse files
Java 11 migraiton: mute-idiom
1 parent 9b105d7 commit 2fa938c

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

mute-idiom/src/main/java/com/iluwatar/mute/App.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import java.io.ByteArrayOutputStream;
2727
import java.io.IOException;
28-
import java.sql.SQLException;
28+
import java.util.Optional;
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
3131

@@ -52,9 +52,8 @@ public class App {
5252
* Program entry point.
5353
*
5454
* @param args command line args.
55-
* @throws Exception if any exception occurs
5655
*/
57-
public static void main(String[] args) throws Exception {
56+
public static void main(String[] args) {
5857

5958
useOfLoggedMute();
6059

@@ -68,32 +67,32 @@ public static void main(String[] args) throws Exception {
6867
* exception occurs.
6968
*/
7069
private static void useOfMute() {
71-
ByteArrayOutputStream out = new ByteArrayOutputStream();
70+
var out = new ByteArrayOutputStream();
7271
Mute.mute(() -> out.write("Hello".getBytes()));
7372
}
7473

75-
private static void useOfLoggedMute() throws SQLException {
76-
Resource resource = null;
74+
private static void useOfLoggedMute() {
75+
Optional<Resource> resource = Optional.empty();
7776
try {
78-
resource = acquireResource();
79-
utilizeResource(resource);
77+
resource = Optional.of(acquireResource());
78+
utilizeResource(resource.get());
8079
} finally {
81-
closeResource(resource);
80+
resource.ifPresent(App::closeResource);
8281
}
8382
}
8483

8584
/*
8685
* All we can do while failed close of a resource is to log it.
8786
*/
8887
private static void closeResource(Resource resource) {
89-
Mute.loggedMute(() -> resource.close());
88+
Mute.loggedMute(resource::close);
9089
}
9190

92-
private static void utilizeResource(Resource resource) throws SQLException {
91+
private static void utilizeResource(Resource resource) {
9392
LOGGER.info("Utilizing acquired resource: {}", resource);
9493
}
9594

96-
private static Resource acquireResource() throws SQLException {
95+
private static Resource acquireResource() {
9796
return new Resource() {
9897

9998
@Override

mute-idiom/src/test/java/com/iluwatar/mute/AppTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727

2828
/**
2929
* Tests that Mute idiom example runs without errors.
30-
*
3130
*/
3231
public class AppTest {
3332

3433
@Test
35-
public void test() throws Exception {
36-
App.main(null);
34+
public void test() {
35+
App.main(new String[]{});
3736
}
3837
}

mute-idiom/src/test/java/com/iluwatar/mute/MuteTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@
2323

2424
package com.iluwatar.mute;
2525

26-
import org.junit.jupiter.api.Test;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
2928

3029
import java.io.ByteArrayOutputStream;
31-
import java.io.IOException;
3230
import java.io.PrintStream;
33-
34-
import static org.junit.jupiter.api.Assertions.assertThrows;
35-
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
import org.junit.jupiter.api.Test;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3634

3735
/**
3836
* Test for the mute-idiom pattern
@@ -50,9 +48,7 @@ public void muteShouldRunTheCheckedRunnableAndNotThrowAnyExceptionIfCheckedRunna
5048

5149
@Test
5250
public void muteShouldRethrowUnexpectedExceptionAsAssertionError() {
53-
assertThrows(AssertionError.class, () -> {
54-
Mute.mute(this::methodThrowingException);
55-
});
51+
assertThrows(AssertionError.class, () -> Mute.mute(this::methodThrowingException));
5652
}
5753

5854
@Test
@@ -62,7 +58,7 @@ public void loggedMuteShouldRunTheCheckedRunnableAndNotThrowAnyExceptionIfChecke
6258

6359
@Test
6460
public void loggedMuteShouldLogExceptionTraceBeforeSwallowingIt() {
65-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
61+
var stream = new ByteArrayOutputStream();
6662
System.setErr(new PrintStream(stream));
6763

6864
Mute.loggedMute(this::methodThrowingException);

0 commit comments

Comments
 (0)