Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.List;
- import java.util.ArrayList;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.text.DateFormat;
- import java.sql.PreparedStatement;
- import javax.sql.DataSource;
- import java.sql.SQLException;
- import org.apache.commons.dbcp.BasicDataSource;
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.TimeZone;
- import core.*;
- import database.*;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import static org.junit.Assert.*;
- /**
- *
- * @author Rainbow
- */
- public class AccountManagerTest
- {
- private AccountManagerImpl am;
- private PaymentManagerImpl pm;
- private DataSource ds;
- public AccountManagerTest()
- {
- }
- private static DataSource prepareDataSource() throws SQLException
- {
- BasicDataSource ds = new BasicDataSource();
- ds.setUrl("jdbc:mysql://localhost:3306/Database1");
- ds.setUsername("root");
- ds.setPassword("");
- ds.setDriverClassName("com.mysql.jdbc.Driver");
- return ds;
- }
- @Before
- public void setUp() throws SQLException
- {
- ds = prepareDataSource();
- //DBUtils.createTables(ds);
- am = new AccountManagerImpl();
- pm = new PaymentManagerImpl();
- am.setDataSource(ds);
- pm.setDataSource(ds);
- PreparedStatement st = ds.getConnection().prepareStatement("TRUNCATE `payment`");
- st.execute();
- PreparedStatement st2 = ds.getConnection().prepareStatement("TRUNCATE `account`");
- st2.execute();
- }
- @After
- public void tearDown() throws Exception
- {
- }
- private Customer newCustomer(long ID, String adress, String firstName, String lastName, long birthID)
- {
- Customer cust = new Customer();
- cust.setID(ID);
- cust.setAdress(adress);
- cust.setFirstName(firstName);
- cust.setLastName(lastName);
- cust.setBirthID(birthID);
- return cust;
- }
- private Account newAccount(long id, long accountNumber, Customer customer, String type)
- {
- Account acc = new Account();
- acc.setAccountID(id);
- acc.setAccountNumber(accountNumber);
- acc.setOwner(customer);
- acc.setType(type);
- return acc;
- }
- private void compareAccountsDeeply(Account expected, Account actual)
- {
- assertEquals(expected.getAccountNumberID(),actual.getAccountNumberID());
- assertEquals(expected.getAccountNumber(),actual.getAccountNumber());
- assertEquals(expected.getOwner(),actual.getOwner());
- }
- private static Date newDate(String date) {
- DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1"));
- try {
- return simpleDateFormat.parse(date);
- } catch (ParseException ex) {
- throw new IllegalArgumentException("Wrong date format", ex);
- }
- }
- private Payment newPayment(long id,String description, Date dueDate, long senderAccount, long receiverAccount, BigDecimal value)
- {
- Payment pay = new Payment();
- pay.setPaymentID(id);
- pay.setDueDate(dueDate);
- pay.setDescription(description);
- pay.setDueDate(dueDate);
- pay.setSenderAccount(senderAccount);
- pay.setReceiverAccount(receiverAccount);
- pay.setValue(value);
- return pay;
- }
- @Test
- public void testCreateAccount()
- {
- Customer domco = newCustomer(1,"Mamateyova 12","Dominik","Szalai",123456789);
- Account account1 = newAccount(1,123456,domco,"SPORIACI");
- am.createAccount(account1);
- compareAccountsDeeply(account1,am.searchAccountByID(new Long(1)));
- assertEquals(account1,am.searchAccountByID(new Long(1)));
- }
- @Test
- public void testCreateAccountError()
- {
- Customer domco = newCustomer(1,"salatova","salat","hlavickovy",1234657);
- Account account = newAccount(1,123456,domco,"SPORIACI");
- account.setOwner(null);
- try
- {
- am.createAccount(account);
- fail();
- }
- catch(IllegalArgumentException iae)
- {
- }
- account.setOwner(domco);
- account.setAccountNumber(null);
- try
- {
- am.createAccount(account);
- fail();
- }
- catch(IllegalArgumentException iae)
- {
- }
- }
- @Test
- public void testCalculateBalance()
- {
- Customer domco = newCustomer(1,"Mamateyova 12","Dominik","Szalai",123456789);
- Account account1 = newAccount(1,123456,domco,"SPORIACI");
- //Date datum = newDate("2012-10-4");
- Date datum = newDate("2012-11-15");
- //Payment payment1 = newPayment(1,"poplatok za kablovku", datum, 654321, 123456, new BigDecimal("100.05"));
- Payment payment1 = newPayment(1,"poplatok za kablovku", datum, 654321, 123456, new BigDecimal("100.05")); //p
- Payment payment2 = newPayment(2,"poplatok za internat", datum, 654321, 123456, new BigDecimal("200.35")); //p
- Payment payment3 = newPayment(3,"poplatok za dane", datum, 654321, 123456, new BigDecimal("20.00")); // p
- Payment payment4 = newPayment(4,"poplatok za socialku", datum, 654321, 123456, new BigDecimal("75.31")); //p
- Payment payment5 = newPayment(5,"poplatok za psa", datum, 654321, 123456, new BigDecimal("16.91")); //p
- Payment payment6 = newPayment(6,"vypalne pre nuckyho", datum, 123456,654321, new BigDecimal("71.86")); //o
- //System.out.println(payment1);
- pm.createPayment(payment1);
- pm.createPayment(payment2);
- pm.createPayment(payment3);
- pm.createPayment(payment4);
- pm.createPayment(payment5);
- pm.createPayment(payment6);
- assertEquals(new BigDecimal("340.76"),am.getAccountBalance(account1));
- }
- @Test
- public void testSearchByID()
- {
- Customer domco = newCustomer(1,"Mamateyova 12","Dominik","Szalai",123456789);
- Account acc = newAccount(1,123456,domco,"SPORIACI");
- am.createAccount(acc);
- assertEquals(acc,am.searchAccountByID(new Long(1)));
- }
- @Test
- public void testSearchByIDError()
- {
- try
- {
- am.searchAccountByID(null);
- }
- catch(IllegalArgumentException iae)
- {
- }
- }
- @Test
- public void testGetAccountsError()
- {
- Customer wrongCustomer = newCustomer(1,"jasovska","andrej","gaspar",1234657);
- Account account = newAccount(1,123456,wrongCustomer,"STUDENTSKY");
- try {
- am.getAccounts(null);
- fail();
- }
- catch(IllegalArgumentException e){
- }
- try {
- am.getAccounts(wrongCustomer);
- }
- catch(IllegalArgumentException e) {
- }
- }
- @Test
- public void testGetAccounts()
- {
- Customer andrej = newCustomer(1,"Jasovska 47", "Andrej", "Gaspar", 28021990 );
- Account accountOfGods = newAccount(1, 888888, andrej, "TERMINOVANY");
- Account accountOfEvil = newAccount(1, 666666, andrej, "STUDENTSKY");
- am.createAccount(accountOfGods);
- am.createAccount(accountOfEvil);
- List<Account> list = new ArrayList<Account>();
- list = am.getAccounts(andrej);
- assertEquals(accountOfGods,list.get(0));
- assertEquals(accountOfEvil,list.get(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement