Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Slf4j
- public class PreparedStatementBuilder implements AutoCloseable {
- private final static ResourceBundle resourceBundle = ResourceBundle.getBundle("database/mysql");
- private final static String HOST = resourceBundle.getString("HOST");
- private final static String USER = resourceBundle.getString("USER");
- private final static String PASS = resourceBundle.getString("PASS");
- private PreparedStatement preparedStatement;
- private Connection connection;
- PreparedStatementBuilder(String sql) {
- PreparedStatement preparedStatement = null;
- LOGGER.debug("init PrepareStatementBuilder");
- try {
- preparedStatement = createConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
- } catch (SQLException e) {
- e.printStackTrace();
- LOGGER.error(e.getMessage(), e);
- }
- this.preparedStatement = preparedStatement;
- }
- Connection createConnection() throws SQLException {
- MysqlDataSource dataSource = new MysqlDataSource();
- dataSource.setUser(USER);
- dataSource.setPassword(PASS);
- dataSource.setURL(HOST);
- LOGGER.debug("create connection");
- connection = dataSource.getConnection();
- return connection;
- }
- public PreparedStatement getPreparedStatement() {
- return preparedStatement;
- }
- @Override
- public void close() {
- if (connection != null) {
- try {
- connection.close();
- LOGGER.debug("close connection");
- } catch (SQLException e) {
- e.printStackTrace();
- LOGGER.error(e.getMessage(), e);
- }
- }
- if (preparedStatement != null) {
- try {
- preparedStatement.close();
- LOGGER.debug("close preparedStatement");
- } catch (SQLException e) {
- e.printStackTrace();
- LOGGER.error(e.getMessage(), e);
- }
- }
- }
- }
- public class PreparedStatementBuilderTest {
- private final static ResourceBundle resourceBundle = ResourceBundle.getBundle("database/mysql");
- private final static String HOST = resourceBundle.getString("HOST");
- private final static String USER = resourceBundle.getString("USER");
- private final static String PASS = resourceBundle.getString("PASS");
- @Mock
- MysqlDataSource dataSource;
- @Mock
- Connection connection;
- @Spy
- PreparedStatement preparedStatement;
- @Mock
- PreparedStatementBuilder preparedStatementBuilder;
- @Mock
- private Statement mockStatement;
- @Before
- public void init(){
- MockitoAnnotations.initMocks(this);
- }
- @Test
- public void createdPreparedStatementTest() throws SQLException {
- preparedStatementBuilder = new PreparedStatementBuilder(anyString());
- dataSource.setUser(USER);
- dataSource.setPassword(PASS);
- dataSource.setURL(HOST);
- when(dataSource.getConnection()).thenReturn(connection);
- when(connection.prepareStatement(anyString(), anyInt())).thenReturn(preparedStatement);
- verify(connection, times(1)).prepareStatement(anyString(), anyInt());
- verify(connection, times(1)).close();
- verify(preparedStatement, times(1)).close();
- assertTrue(connection.isClosed());
- assertTrue(preparedStatement.isClosed());
- }
- }
Add Comment
Please, Sign In to add comment