Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. import java.util.List;
  6. import java.util.ArrayList;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.text.DateFormat;
  10. import java.sql.PreparedStatement;
  11. import javax.sql.DataSource;
  12. import java.sql.SQLException;
  13. import org.apache.commons.dbcp.BasicDataSource;
  14. import java.math.BigDecimal;
  15. import java.util.Date;
  16. import java.util.TimeZone;
  17. import core.*;
  18. import database.*;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import static org.junit.Assert.*;
  23. /**
  24. *
  25. * @author Rainbow
  26. */
  27. public class AccountManagerTest
  28. {
  29. private AccountManagerImpl am;
  30. private PaymentManagerImpl pm;
  31. private DataSource ds;
  32.  
  33.  
  34. public AccountManagerTest()
  35. {
  36. }
  37.  
  38. private static DataSource prepareDataSource() throws SQLException
  39. {
  40. BasicDataSource ds = new BasicDataSource();
  41.  
  42. ds.setUrl("jdbc:mysql://localhost:3306/Database1");
  43. ds.setUsername("root");
  44. ds.setPassword("");
  45. ds.setDriverClassName("com.mysql.jdbc.Driver");
  46.  
  47. return ds;
  48. }
  49.  
  50. @Before
  51. public void setUp() throws SQLException
  52. {
  53. ds = prepareDataSource();
  54. //DBUtils.createTables(ds);
  55. am = new AccountManagerImpl();
  56. pm = new PaymentManagerImpl();
  57. am.setDataSource(ds);
  58. pm.setDataSource(ds);
  59.  
  60. PreparedStatement st = ds.getConnection().prepareStatement("TRUNCATE `payment`");
  61. st.execute();
  62. PreparedStatement st2 = ds.getConnection().prepareStatement("TRUNCATE `account`");
  63. st2.execute();
  64.  
  65.  
  66. }
  67.  
  68. @After
  69. public void tearDown() throws Exception
  70. {
  71. }
  72.  
  73.  
  74. private Customer newCustomer(long ID, String adress, String firstName, String lastName, long birthID)
  75. {
  76. Customer cust = new Customer();
  77. cust.setID(ID);
  78. cust.setAdress(adress);
  79. cust.setFirstName(firstName);
  80. cust.setLastName(lastName);
  81. cust.setBirthID(birthID);
  82.  
  83. return cust;
  84.  
  85. }
  86.  
  87. private Account newAccount(long id, long accountNumber, Customer customer, String type)
  88. {
  89. Account acc = new Account();
  90.  
  91. acc.setAccountID(id);
  92. acc.setAccountNumber(accountNumber);
  93. acc.setOwner(customer);
  94. acc.setType(type);
  95.  
  96. return acc;
  97.  
  98. }
  99.  
  100. private void compareAccountsDeeply(Account expected, Account actual)
  101. {
  102.  
  103. assertEquals(expected.getAccountNumberID(),actual.getAccountNumberID());
  104. assertEquals(expected.getAccountNumber(),actual.getAccountNumber());
  105. assertEquals(expected.getOwner(),actual.getOwner());
  106. }
  107.  
  108. private static Date newDate(String date) {
  109. DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  110. simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1"));
  111. try {
  112. return simpleDateFormat.parse(date);
  113. } catch (ParseException ex) {
  114. throw new IllegalArgumentException("Wrong date format", ex);
  115. }
  116. }
  117.  
  118. private Payment newPayment(long id,String description, Date dueDate, long senderAccount, long receiverAccount, BigDecimal value)
  119. {
  120. Payment pay = new Payment();
  121.  
  122. pay.setPaymentID(id);
  123. pay.setDueDate(dueDate);
  124. pay.setDescription(description);
  125. pay.setDueDate(dueDate);
  126. pay.setSenderAccount(senderAccount);
  127. pay.setReceiverAccount(receiverAccount);
  128. pay.setValue(value);
  129.  
  130. return pay;
  131. }
  132.  
  133. @Test
  134. public void testCreateAccount()
  135. {
  136. Customer domco = newCustomer(1,"Mamateyova 12","Dominik","Szalai",123456789);
  137.  
  138. Account account1 = newAccount(1,123456,domco,"SPORIACI");
  139.  
  140. am.createAccount(account1);
  141.  
  142. compareAccountsDeeply(account1,am.searchAccountByID(new Long(1)));
  143. assertEquals(account1,am.searchAccountByID(new Long(1)));
  144. }
  145.  
  146. @Test
  147. public void testCreateAccountError()
  148. {
  149.  
  150. Customer domco = newCustomer(1,"salatova","salat","hlavickovy",1234657);
  151. Account account = newAccount(1,123456,domco,"SPORIACI");
  152. account.setOwner(null);
  153.  
  154. try
  155. {
  156. am.createAccount(account);
  157. fail();
  158. }
  159. catch(IllegalArgumentException iae)
  160. {
  161.  
  162. }
  163. account.setOwner(domco);
  164. account.setAccountNumber(null);
  165. try
  166. {
  167. am.createAccount(account);
  168. fail();
  169. }
  170. catch(IllegalArgumentException iae)
  171. {
  172.  
  173. }
  174.  
  175. }
  176.  
  177. @Test
  178. public void testCalculateBalance()
  179. {
  180.  
  181. Customer domco = newCustomer(1,"Mamateyova 12","Dominik","Szalai",123456789);
  182. Account account1 = newAccount(1,123456,domco,"SPORIACI");
  183. //Date datum = newDate("2012-10-4");
  184.  
  185.  
  186. Date datum = newDate("2012-11-15");
  187. //Payment payment1 = newPayment(1,"poplatok za kablovku", datum, 654321, 123456, new BigDecimal("100.05"));
  188.  
  189. Payment payment1 = newPayment(1,"poplatok za kablovku", datum, 654321, 123456, new BigDecimal("100.05")); //p
  190. Payment payment2 = newPayment(2,"poplatok za internat", datum, 654321, 123456, new BigDecimal("200.35")); //p
  191. Payment payment3 = newPayment(3,"poplatok za dane", datum, 654321, 123456, new BigDecimal("20.00")); // p
  192. Payment payment4 = newPayment(4,"poplatok za socialku", datum, 654321, 123456, new BigDecimal("75.31")); //p
  193. Payment payment5 = newPayment(5,"poplatok za psa", datum, 654321, 123456, new BigDecimal("16.91")); //p
  194. Payment payment6 = newPayment(6,"vypalne pre nuckyho", datum, 123456,654321, new BigDecimal("71.86")); //o
  195. //System.out.println(payment1);
  196.  
  197. pm.createPayment(payment1);
  198. pm.createPayment(payment2);
  199. pm.createPayment(payment3);
  200. pm.createPayment(payment4);
  201. pm.createPayment(payment5);
  202. pm.createPayment(payment6);
  203.  
  204. assertEquals(new BigDecimal("340.76"),am.getAccountBalance(account1));
  205. }
  206.  
  207. @Test
  208. public void testSearchByID()
  209. {
  210. Customer domco = newCustomer(1,"Mamateyova 12","Dominik","Szalai",123456789);
  211. Account acc = newAccount(1,123456,domco,"SPORIACI");
  212. am.createAccount(acc);
  213.  
  214. assertEquals(acc,am.searchAccountByID(new Long(1)));
  215. }
  216.  
  217. @Test
  218. public void testSearchByIDError()
  219. {
  220. try
  221. {
  222. am.searchAccountByID(null);
  223. }
  224. catch(IllegalArgumentException iae)
  225. {
  226.  
  227. }
  228. }
  229.  
  230. @Test
  231. public void testGetAccountsError()
  232. {
  233.  
  234. Customer wrongCustomer = newCustomer(1,"jasovska","andrej","gaspar",1234657);
  235. Account account = newAccount(1,123456,wrongCustomer,"STUDENTSKY");
  236.  
  237.  
  238.  
  239.  
  240.  
  241. try {
  242. am.getAccounts(null);
  243. fail();
  244. }
  245. catch(IllegalArgumentException e){
  246. }
  247.  
  248. try {
  249. am.getAccounts(wrongCustomer);
  250. }
  251. catch(IllegalArgumentException e) {
  252.  
  253. }
  254.  
  255. }
  256.  
  257. @Test
  258. public void testGetAccounts()
  259. {
  260.  
  261.  
  262.  
  263. Customer andrej = newCustomer(1,"Jasovska 47", "Andrej", "Gaspar", 28021990 );
  264.  
  265. Account accountOfGods = newAccount(1, 888888, andrej, "TERMINOVANY");
  266. Account accountOfEvil = newAccount(1, 666666, andrej, "STUDENTSKY");
  267.  
  268.  
  269.  
  270. am.createAccount(accountOfGods);
  271. am.createAccount(accountOfEvil);
  272.  
  273. List<Account> list = new ArrayList<Account>();
  274. list = am.getAccounts(andrej);
  275.  
  276. assertEquals(accountOfGods,list.get(0));
  277. assertEquals(accountOfEvil,list.get(1));
  278.  
  279.  
  280.  
  281. }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement