Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1.  
  2. import java.sql.SQLException;
  3. import core.*;
  4. import database.*;
  5. import java.sql.PreparedStatement;
  6. import javax.sql.DataSource;
  7.  
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import static org.junit.Assert.*;
  12. import org.apache.commons.dbcp.BasicDataSource;
  13. /*
  14. * To change this template, choose Tools | Templates
  15. * and open the template in the editor.
  16. */
  17.  
  18. /**
  19. *
  20. * @author Rainbow
  21. */
  22. public class CustomerManagerTest
  23. {
  24.  
  25. private CustomerManagerImpl cm;
  26. private DataSource ds;
  27.  
  28. public CustomerManagerTest()
  29. {
  30. }
  31.  
  32. private static DataSource prepareDataSource() throws SQLException
  33. {
  34. BasicDataSource ds = new BasicDataSource();
  35.  
  36. ds.setUrl("jdbc:mysql://localhost:3306/Database1");
  37. ds.setUsername("root");
  38. ds.setPassword("");
  39. ds.setDriverClassName("com.mysql.jdbc.Driver");
  40.  
  41. return ds;
  42. }
  43.  
  44. @Before
  45. public void setUp() throws SQLException
  46. {
  47. ds = prepareDataSource();
  48. //DBUtils.createTables(ds);
  49. cm = new CustomerManagerImpl();
  50. cm.setDataSource(ds);
  51.  
  52. PreparedStatement st = ds.getConnection().prepareStatement("TRUNCATE `customer`");
  53. st.execute();
  54.  
  55. }
  56.  
  57. @After
  58. public void tearDown() throws Exception
  59. {
  60. }
  61.  
  62. // TODO add test methods here.
  63. // The methods must be annotated with annotation @Test. For example:
  64. //
  65. // @Test
  66. // public void hello() {}
  67. private Customer newCustomer(long ID, String adress, String firstName, String lastName, long birthID)
  68. {
  69. Customer cust = new Customer();
  70. cust.setID(ID);
  71. cust.setAdress(adress);
  72. cust.setFirstName(firstName);
  73. cust.setLastName(lastName);
  74. cust.setBirthID(birthID);
  75.  
  76. return cust;
  77.  
  78. }
  79.  
  80. private void compareCustomersDeeply(Customer expected, Customer actual)
  81. {
  82. assertEquals(expected.getID(), actual.getID());
  83. assertEquals(expected.getFirstName(), actual.getFirstName());
  84. assertEquals(expected.getLastName(), actual.getLastName());
  85. assertEquals(expected.getAdress(), actual.getAdress());
  86. assertEquals(expected.getBirthID(), actual.getBirthID());
  87. }
  88.  
  89. @Test
  90. public void testCreateCustomer()
  91. {
  92. //newCustomer(long ID, String adress, String firstName, String lastName, long birthID)
  93. Customer domco = newCustomer(1, "Mamateyova 12", "Dominik", "Szalai", 123456);
  94. cm.createCustomer(domco);
  95.  
  96. Customer tempCust = cm.getCustomer(1);
  97. assertEquals(domco, tempCust);
  98.  
  99. compareCustomersDeeply(domco,tempCust);
  100.  
  101. }
  102.  
  103. @Test
  104. public void testUpdateCustomer()
  105. {
  106. Customer customer = newCustomer(1, "Mamateyova 12", "Dominik", "Szalai", 123456);
  107.  
  108. cm.createCustomer(customer);
  109.  
  110. customer.setAdress("sololol");
  111.  
  112. cm.updateCustomer(customer);
  113.  
  114. assertEquals("sololol",cm.getCustomer(1).getAdress());
  115. }
  116.  
  117. @Test
  118. public void deleteCustomer()
  119. {
  120. Customer customer = newCustomer(1, "Mamateyova 12", "Dominik", "Szalai", 123456);
  121. cm.createCustomer(customer);
  122.  
  123. cm.deleteCustomer(1);
  124.  
  125. assertTrue(cm.getAllCustomers().isEmpty());
  126.  
  127. }
  128.  
  129. @Test
  130. public void testCreateCustomerError()
  131. {
  132. Customer wrongCustomer = newCustomer(1,"blabol",null,"szalai",123456);
  133.  
  134. try
  135. {
  136. cm.createCustomer(wrongCustomer);
  137. fail();
  138. }
  139. catch(IllegalArgumentException iae)
  140. {
  141.  
  142. }
  143. wrongCustomer.setFirstName("domco");
  144. wrongCustomer.setAdress(null);
  145.  
  146. try
  147. {
  148. cm.createCustomer(wrongCustomer);
  149. fail();
  150. }
  151. catch(IllegalArgumentException iae)
  152. {
  153.  
  154. }
  155.  
  156. wrongCustomer.setAdress("a");
  157. wrongCustomer.setFirstName(null);
  158.  
  159. try
  160. {
  161. cm.createCustomer(wrongCustomer);
  162. fail();
  163. }
  164. catch(IllegalArgumentException iae)
  165. {
  166.  
  167. }
  168.  
  169. }
  170.  
  171. @Test
  172. public void testUpdateCustomerError() {
  173. Customer wrongCustomer = newCustomer(1,"Jasovska", "Andrej", "Gaspar", 28021990 );
  174.  
  175.  
  176. try{
  177. cm.updateCustomer(wrongCustomer);
  178. }
  179. catch(IllegalArgumentException e){
  180.  
  181. }
  182. wrongCustomer.setAdress(null);
  183.  
  184. try {
  185. cm.updateCustomer(wrongCustomer);
  186. }
  187. catch(IllegalArgumentException e) {
  188.  
  189. }
  190.  
  191.  
  192.  
  193. }
  194.  
  195. @Test
  196. public void testDeleteCustomerError() {
  197.  
  198.  
  199. try {
  200. cm.deleteCustomer(-1);
  201. fail();
  202.  
  203. }
  204. catch(IllegalArgumentException e) {
  205. }
  206.  
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement