Guest User

Untitled

a guest
Jan 5th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. @Slf4j
  2. public class PreparedStatementBuilder implements AutoCloseable {
  3.  
  4. private final static ResourceBundle resourceBundle = ResourceBundle.getBundle("database/mysql");
  5. private final static String HOST = resourceBundle.getString("HOST");
  6. private final static String USER = resourceBundle.getString("USER");
  7. private final static String PASS = resourceBundle.getString("PASS");
  8. private PreparedStatement preparedStatement;
  9. private Connection connection;
  10.  
  11. PreparedStatementBuilder(String sql) {
  12. PreparedStatement preparedStatement = null;
  13. LOGGER.debug("init PrepareStatementBuilder");
  14. try {
  15. preparedStatement = createConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
  16. } catch (SQLException e) {
  17. e.printStackTrace();
  18. LOGGER.error(e.getMessage(), e);
  19. }
  20. this.preparedStatement = preparedStatement;
  21. }
  22.  
  23. Connection createConnection() throws SQLException {
  24. MysqlDataSource dataSource = new MysqlDataSource();
  25. dataSource.setUser(USER);
  26. dataSource.setPassword(PASS);
  27. dataSource.setURL(HOST);
  28. LOGGER.debug("create connection");
  29. connection = dataSource.getConnection();
  30. return connection;
  31. }
  32.  
  33. public PreparedStatement getPreparedStatement() {
  34. return preparedStatement;
  35. }
  36.  
  37. @Override
  38. public void close() {
  39. if (connection != null) {
  40. try {
  41. connection.close();
  42. LOGGER.debug("close connection");
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. LOGGER.error(e.getMessage(), e);
  46. }
  47. }
  48.  
  49. if (preparedStatement != null) {
  50. try {
  51. preparedStatement.close();
  52. LOGGER.debug("close preparedStatement");
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. LOGGER.error(e.getMessage(), e);
  56. }
  57. }
  58. }
  59. }
  60.  
  61. public class PreparedStatementBuilderTest {
  62.  
  63. private final static ResourceBundle resourceBundle = ResourceBundle.getBundle("database/mysql");
  64. private final static String HOST = resourceBundle.getString("HOST");
  65. private final static String USER = resourceBundle.getString("USER");
  66. private final static String PASS = resourceBundle.getString("PASS");
  67.  
  68. @Mock
  69. MysqlDataSource dataSource;
  70. @Mock
  71. Connection connection;
  72. @Spy
  73. PreparedStatement preparedStatement;
  74. @Mock
  75. PreparedStatementBuilder preparedStatementBuilder;
  76. @Mock
  77. private Statement mockStatement;
  78.  
  79. @Before
  80. public void init(){
  81. MockitoAnnotations.initMocks(this);
  82. }
  83.  
  84. @Test
  85. public void createdPreparedStatementTest() throws SQLException {
  86.  
  87. preparedStatementBuilder = new PreparedStatementBuilder(anyString());
  88.  
  89. dataSource.setUser(USER);
  90. dataSource.setPassword(PASS);
  91. dataSource.setURL(HOST);
  92.  
  93. when(dataSource.getConnection()).thenReturn(connection);
  94. when(connection.prepareStatement(anyString(), anyInt())).thenReturn(preparedStatement);
  95.  
  96. verify(connection, times(1)).prepareStatement(anyString(), anyInt());
  97. verify(connection, times(1)).close();
  98. verify(preparedStatement, times(1)).close();
  99. assertTrue(connection.isClosed());
  100. assertTrue(preparedStatement.isClosed());
  101.  
  102. }
  103. }
Add Comment
Please, Sign In to add comment