Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import org.junit.Test;
  2. import sportsandleisurevillage.data.InvoiceRepoImpl;
  3. import sportsandleisurevillage.domain.Invoice;
  4.  
  5. import java.sql.*;
  6. import java.util.HashMap;
  7.  
  8. import static org.junit.Assert.*;
  9.  
  10. public class InvoiceRepoImplTest {
  11. private Connection conn;
  12. private HashMap<Integer, Invoice> list = new HashMap<>();
  13. private static String url = "jdbc:mysql://charlesbarry.coventry.domains:3306/charlesb_260CT";
  14. private static String username = "charlesb_charlie";
  15. private static String password = "Password1234";
  16.  
  17. private void connect(){
  18. try{
  19. conn = DriverManager.getConnection(url, username, password);
  20. }catch(SQLException e){
  21. throw new IllegalStateException("Failed to Connect", e);
  22. }
  23. }
  24.  
  25. private static int customerId = 1;//Charlie, barryc2@uni.coventry.ac.uk
  26. private static int bookingId = 1;//Swimming, 2019-05-27, 30
  27.  
  28. private void addInvoice(int numofitems){
  29. PreparedStatement stmt;
  30. try {
  31. stmt = conn.prepareStatement("INSERT INTO Invoice VALUES (NULL, ?, 0, 0, '2019-03-27');");
  32. stmt.setInt(1, customerId);
  33. stmt.executeUpdate();
  34.  
  35. for(int i = 0; i < numofitems; ++i){
  36. stmt = conn.prepareStatement("INSERT INTO InvoiceBooking VALUES (NULL, ?, ?);");
  37. stmt.setInt(1, customerId);
  38. stmt.setInt(2, bookingId);
  39. stmt.executeUpdate();
  40. }
  41.  
  42. }catch(SQLException e){
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. @Test
  48. public void read() {
  49. connect(); addInvoice(1);//atleast 1 invoice must exist
  50. InvoiceRepoImpl readDb = new InvoiceRepoImpl();
  51. ResultSet result = readDb.read();//get all invoices
  52. int count = 0;
  53. try{
  54. while(result.next()) {//put values
  55. count += 1;
  56. }
  57. }catch(Exception e){
  58. e.printStackTrace();
  59. }
  60. assertTrue(count >= 1);//atleast one invoice must exist
  61. }
  62.  
  63. @Test (expected = NullPointerException.class)
  64. public void updateWithoutArgs() {
  65. InvoiceRepoImpl updateNoArgs = new InvoiceRepoImpl();//no data provided about update query
  66. updateNoArgs.update();
  67. }
  68.  
  69. @Test (expected = AssertionError.class)
  70. public void updateIncorrectArgs(){
  71. //int id, String col, Boolean bool
  72. InvoiceRepoImpl updateWrongArgs = new InvoiceRepoImpl(0, "asdfgh", false);//incorrect data given
  73. //id 0 (not possible unless specified + column doesnt exist
  74. updateWrongArgs.update();
  75. }
  76.  
  77. @Test
  78. public void update() {
  79.  
  80. }
  81.  
  82. @Test
  83. public void deleteWithoutArgs() {
  84.  
  85. }
  86.  
  87. @Test
  88. public void deleteIncorrectArgs() {
  89.  
  90. }
  91.  
  92. @Test
  93. public void delete() {
  94.  
  95. }
  96.  
  97. @Test
  98. public void closeconn() {
  99. InvoiceRepoImpl connectionclose = new InvoiceRepoImpl();
  100. connectionclose.closeconn();//will pass unless exception
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement