2016-03-01 17:28:28 +05:30
|
|
|
/**
|
|
|
|
* The MIT License
|
|
|
|
* Copyright (c) 2014 Ilkka Seppälä
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
package com.iluwatar.mute;
|
|
|
|
|
2016-03-15 18:44:59 +05:30
|
|
|
import static org.mockito.Mockito.doThrow;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
2016-03-01 17:28:28 +05:30
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
public class App {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
useOfLoggedMute();
|
|
|
|
|
|
|
|
useOfMute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Typically used when the API declares some exception but cannot do so. Usually a signature mistake.
|
|
|
|
* In this example out is not supposed to throw exception as it is a ByteArrayOutputStream. So we
|
|
|
|
* utilize mute, which will throw AssertionError if unexpected exception occurs.
|
|
|
|
*/
|
|
|
|
private static void useOfMute() {
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
Mute.mute(() -> out.write("Hello".getBytes()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void useOfLoggedMute() {
|
2016-03-15 18:44:59 +05:30
|
|
|
Connection connection = null;
|
2016-03-01 17:28:28 +05:30
|
|
|
try {
|
2016-03-15 18:44:59 +05:30
|
|
|
connection = openConnection();
|
2016-03-01 17:28:28 +05:30
|
|
|
readStuff(connection);
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
closeConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All we can do while failed close of connection is to log it.
|
|
|
|
*/
|
|
|
|
private static void closeConnection(Connection connection) {
|
|
|
|
if (connection != null) {
|
|
|
|
Mute.loggedMute(() -> connection.close());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void readStuff(Connection connection) throws SQLException {
|
|
|
|
if (connection != null) {
|
|
|
|
connection.createStatement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 18:44:59 +05:30
|
|
|
private static Connection openConnection() throws SQLException {
|
|
|
|
Connection mockedConnection = mock(Connection.class);
|
|
|
|
doThrow(SQLException.class).when(mockedConnection).close();
|
|
|
|
return mockedConnection;
|
2016-03-01 17:28:28 +05:30
|
|
|
}
|
|
|
|
}
|