Work on #385, added documentation and class diagram. Made refactoring changes to pass checkstyle and PMD checks

This commit is contained in:
Narendra Pathai 2016-03-16 12:40:46 +05:30
parent 7aff77ab27
commit c78dd2667a
8 changed files with 106 additions and 18 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="true" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.mute.App" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/App.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="519" y="122"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="false" static="true"/>
<operations public="true" package="true" protected="true" private="false" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.mute.Mute" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/Mute.java" binary="false" corner="BOTTOM_RIGHT">
<position height="115" width="203" x="291" y="267"/>
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="false" static="true"/>
</display>
</class>
<interface id="3" language="java" name="com.iluwatar.mute.CheckedRunnable" project="mute-idiom"
file="/mute-idiom/src/main/java/com/iluwatar/mute/CheckedRunnable.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="723" y="322"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<dependency id="4">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="2"/>
</dependency>
<dependency id="5">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="3"/>
</dependency>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package com.iluwatar.mute; package com.iluwatar.mute;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
@ -28,10 +29,34 @@ import static org.mockito.Mockito.mock;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
/**
* Mute pattern is utilized when we need to suppress an exception due to an API flaw or in
* situation when all we can do to handle the exception is to log it.
* This pattern should not be used everywhere. It is very important to logically handle the
* exceptions in a system, but some situations like the ones described above require this pattern,
* so that we don't need to repeat
* <pre>
* <code>
* try {
* // code that may throwing exception we need to ignore or may never be thrown
* } catch (Exception ex) {
* // ignore by logging or throw error if unexpected exception occurs
* }
* </code>
* </pre> every time we need to ignore an exception.
*
*/
public class App { public class App {
public static void main(String[] args) { /**
* Program entry point.
*
* @param args command line args.
* @throws Exception if any exception occurs
*/
public static void main(String[] args) throws Exception {
useOfLoggedMute(); useOfLoggedMute();
@ -39,22 +64,21 @@ public class App {
} }
/* /*
* Typically used when the API declares some exception but cannot do so. Usually a signature mistake. * Typically used when the API declares some exception but cannot do so. Usually a
* In this example out is not supposed to throw exception as it is a ByteArrayOutputStream. So we * signature mistake.In this example out is not supposed to throw exception as it is a
* utilize mute, which will throw AssertionError if unexpected exception occurs. * ByteArrayOutputStream. So we utilize mute, which will throw AssertionError if unexpected
* exception occurs.
*/ */
private static void useOfMute() { private static void useOfMute() {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
Mute.mute(() -> out.write("Hello".getBytes())); Mute.mute(() -> out.write("Hello".getBytes()));
} }
private static void useOfLoggedMute() { private static void useOfLoggedMute() throws SQLException {
Connection connection = null; Connection connection = null;
try { try {
connection = openConnection(); connection = openConnection();
readStuff(connection); readStuff(connection);
} catch (SQLException ex) {
ex.printStackTrace();
} finally { } finally {
closeConnection(connection); closeConnection(connection);
} }
@ -64,14 +88,12 @@ public class App {
* All we can do while failed close of connection is to log it. * All we can do while failed close of connection is to log it.
*/ */
private static void closeConnection(Connection connection) { private static void closeConnection(Connection connection) {
if (connection != null) {
Mute.loggedMute(() -> connection.close()); Mute.loggedMute(() -> connection.close());
} }
}
private static void readStuff(Connection connection) throws SQLException { private static void readStuff(Connection connection) throws SQLException {
if (connection != null) { try (Statement statement = connection.createStatement()) {
connection.createStatement(); System.out.println("Read data from statement");
} }
} }

View File

@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package com.iluwatar.mute; package com.iluwatar.mute;
/** /**
@ -32,5 +33,5 @@ public interface CheckedRunnable {
* Same as {@link Runnable#run()} with a possibility of exception in execution. * Same as {@link Runnable#run()} with a possibility of exception in execution.
* @throws Exception if any exception occurs. * @throws Exception if any exception occurs.
*/ */
public void run() throws Exception; void run() throws Exception;
} }

View File

@ -30,6 +30,9 @@ import java.io.IOException;
*/ */
public final class Mute { public final class Mute {
// The constructor is never meant to be called.
private Mute() {}
/** /**
* Executes the <code>runnable</code> and throws the exception occurred within a {@link AssertionError}. * Executes the <code>runnable</code> and throws the exception occurred within a {@link AssertionError}.
* This method should be utilized to mute the operations that are guaranteed not to throw an exception. * This method should be utilized to mute the operations that are guaranteed not to throw an exception.

View File

@ -9,7 +9,7 @@ import org.junit.Test;
public class AppTest { public class AppTest {
@Test @Test
public void test() { public void test() throws Exception {
App.main(null); App.main(null);
} }
} }

View File

@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package com.iluwatar.mute; package com.iluwatar.mute;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -38,6 +39,11 @@ public class MuteTest {
@Rule public ExpectedException exception = ExpectedException.none(); @Rule public ExpectedException exception = ExpectedException.none();
@Test
public void muteShouldRunTheCheckedRunnableAndNotThrowAnyExceptionIfCheckedRunnableDoesNotThrowAnyException() {
Mute.mute(() -> methodNotThrowingAnyException());
}
@Test @Test
public void muteShouldRethrowUnexpectedExceptionAsAssertionError() throws Exception { public void muteShouldRethrowUnexpectedExceptionAsAssertionError() throws Exception {
exception.expect(AssertionError.class); exception.expect(AssertionError.class);
@ -46,8 +52,9 @@ public class MuteTest {
Mute.mute(() -> methodThrowingException()); Mute.mute(() -> methodThrowingException());
} }
private void methodThrowingException() throws Exception { @Test
throw new Exception(MESSAGE); public void loggedMuteShouldRunTheCheckedRunnableAndNotThrowAnyExceptionIfCheckedRunnableDoesNotThrowAnyException() {
Mute.loggedMute(() -> methodNotThrowingAnyException());
} }
@Test @Test
@ -59,4 +66,13 @@ public class MuteTest {
assertTrue(new String(stream.toByteArray()).contains(MESSAGE)); assertTrue(new String(stream.toByteArray()).contains(MESSAGE));
} }
private void methodNotThrowingAnyException() {
System.out.println("Executed successfully");
}
private void methodThrowingException() throws Exception {
throw new Exception(MESSAGE);
}
} }

View File

@ -123,6 +123,7 @@
<module>feature-toggle</module> <module>feature-toggle</module>
<module>value-object</module> <module>value-object</module>
<module>monad</module> <module>monad</module>
<module>mute-idiom</module>
</modules> </modules>
<dependencyManagement> <dependencyManagement>