Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.SQLException;
- import core.*;
- import database.*;
- import java.sql.PreparedStatement;
- import javax.sql.DataSource;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import static org.junit.Assert.*;
- import org.apache.commons.dbcp.BasicDataSource;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Rainbow
- */
- public class CustomerManagerTest
- {
- private CustomerManagerImpl cm;
- private DataSource ds;
- public CustomerManagerTest()
- {
- }
- 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);
- cm = new CustomerManagerImpl();
- cm.setDataSource(ds);
- PreparedStatement st = ds.getConnection().prepareStatement("TRUNCATE `customer`");
- st.execute();
- }
- @After
- public void tearDown() throws Exception
- {
- }
- // TODO add test methods here.
- // The methods must be annotated with annotation @Test. For example:
- //
- // @Test
- // public void hello() {}
- 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 void compareCustomersDeeply(Customer expected, Customer actual)
- {
- assertEquals(expected.getID(), actual.getID());
- assertEquals(expected.getFirstName(), actual.getFirstName());
- assertEquals(expected.getLastName(), actual.getLastName());
- assertEquals(expected.getAdress(), actual.getAdress());
- assertEquals(expected.getBirthID(), actual.getBirthID());
- }
- @Test
- public void testCreateCustomer()
- {
- //newCustomer(long ID, String adress, String firstName, String lastName, long birthID)
- Customer domco = newCustomer(1, "Mamateyova 12", "Dominik", "Szalai", 123456);
- cm.createCustomer(domco);
- Customer tempCust = cm.getCustomer(1);
- assertEquals(domco, tempCust);
- compareCustomersDeeply(domco,tempCust);
- }
- @Test
- public void testUpdateCustomer()
- {
- Customer customer = newCustomer(1, "Mamateyova 12", "Dominik", "Szalai", 123456);
- cm.createCustomer(customer);
- customer.setAdress("sololol");
- cm.updateCustomer(customer);
- assertEquals("sololol",cm.getCustomer(1).getAdress());
- }
- @Test
- public void deleteCustomer()
- {
- Customer customer = newCustomer(1, "Mamateyova 12", "Dominik", "Szalai", 123456);
- cm.createCustomer(customer);
- cm.deleteCustomer(1);
- assertTrue(cm.getAllCustomers().isEmpty());
- }
- @Test
- public void testCreateCustomerError()
- {
- Customer wrongCustomer = newCustomer(1,"blabol",null,"szalai",123456);
- try
- {
- cm.createCustomer(wrongCustomer);
- fail();
- }
- catch(IllegalArgumentException iae)
- {
- }
- wrongCustomer.setFirstName("domco");
- wrongCustomer.setAdress(null);
- try
- {
- cm.createCustomer(wrongCustomer);
- fail();
- }
- catch(IllegalArgumentException iae)
- {
- }
- wrongCustomer.setAdress("a");
- wrongCustomer.setFirstName(null);
- try
- {
- cm.createCustomer(wrongCustomer);
- fail();
- }
- catch(IllegalArgumentException iae)
- {
- }
- }
- @Test
- public void testUpdateCustomerError() {
- Customer wrongCustomer = newCustomer(1,"Jasovska", "Andrej", "Gaspar", 28021990 );
- try{
- cm.updateCustomer(wrongCustomer);
- }
- catch(IllegalArgumentException e){
- }
- wrongCustomer.setAdress(null);
- try {
- cm.updateCustomer(wrongCustomer);
- }
- catch(IllegalArgumentException e) {
- }
- }
- @Test
- public void testDeleteCustomerError() {
- try {
- cm.deleteCustomer(-1);
- fail();
- }
- catch(IllegalArgumentException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement