Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jpa.tests;
- import static org.junit.Assert.*;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import jpa.controllers.CustomerJpaController;
- import jpa.controllers.exceptions.IllegalOrphanException;
- import jpa.controllers.exceptions.NonexistentEntityException;
- import jpa.controllers.exceptions.PreexistingEntityException;
- import jpa.controllers.exceptions.RollbackFailureException;
- import jpa.entities.Customer;
- import jpa.entities.DiscountCode;
- import jpa.entities.PurchaseOrder;
- import org.dbunit.Assertion;
- import org.dbunit.DBTestCase;
- import org.dbunit.DatabaseUnitException;
- import org.dbunit.PropertiesBasedJdbcDatabaseTester;
- import org.dbunit.database.DatabaseConfig;
- import org.dbunit.dataset.IDataSet;
- import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
- import org.dbunit.ext.mysql.MySqlDataTypeFactory;
- import org.dbunit.operation.DatabaseOperation;
- import org.hibernate.LazyInitializationException;
- import org.junit.Test;
- public class CustomerJpaControllerTest extends DBTestCase {
- public CustomerJpaControllerTest(String name) {
- super(name);
- System.setProperty(
- PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
- "com.mysql.jdbc.Driver");
- System.setProperty(
- PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
- "jdbc:mysql://localhost:3306/sqs");
- System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
- "root");
- System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
- "");
- }
- @Override
- protected DatabaseOperation getSetUpOperation() throws Exception {
- return DatabaseOperation.CLEAN_INSERT;
- }
- @Override
- protected DatabaseOperation getTearDownOperation() throws Exception {
- return DatabaseOperation.NONE;
- }
- @Test
- public void testDataLoadedProperly() {
- IDataSet databaseDataSet = null;
- try {
- databaseDataSet = getConnection().createDataSet();
- } catch (SQLException e) {
- fail("SQLException caught :" + e.getMessage());
- } catch (Exception e) {
- fail("Exception caught :" + e.getMessage());
- }
- IDataSet expectedDataSet = null;
- try {
- expectedDataSet = getDataSet();
- } catch (Exception e) {
- fail("Exception caught :" + e.getMessage());
- }
- try {
- Assertion.assertEquals(databaseDataSet, expectedDataSet);
- } catch (DatabaseUnitException e) {
- fail("DatabaseUnitException :" + e.getMessage());
- }
- }
- @Test
- public void testGetEntityManager() {
- CustomerJpaController cjpc = new CustomerJpaController();
- assertNotNull(cjpc.getEntityManager().find(Customer.class,
- new Integer(1)));
- assertNull(cjpc.getEntityManager().find(Customer.class, new Integer(6)));
- }
- @Test
- public void testCreate() {
- CustomerJpaController cjpc = new CustomerJpaController();
- Customer customer = new Customer();
- DiscountCode discountCode = new DiscountCode();
- discountCode.setDiscountCode('a');
- customer.setAddressline1("customerTESTadd1");
- customer.setAddressline2("customerTESTadd2");
- customer.setCity("customerTESTcity");
- customer.setCreditLimit(new Integer(1000));
- customer.setEmail("customerTESTemail");
- customer.setFax("customerTESTfax");
- customer.setName("customerTEST");
- customer.setPhone("customerTESTphone");
- customer.setState("customerTESTstate");
- customer.setZip("customerTESTzip");
- customer.setCustomerId(new Integer(6));
- customer.setDiscountCode(discountCode);
- try {
- cjpc.create(customer);
- } catch (PreexistingEntityException e) {
- fail("PreexistingEntityException caught should not be here :"
- + e.getMessage());
- } catch (RollbackFailureException e) {
- fail("RollbackFailureException caught should not be here :"
- + e.getMessage());
- } catch (Exception e) {
- fail("Exception caught should not be here :" + e.getMessage());
- }
- customer.setCustomerId(new Integer(1));
- try {
- cjpc.create(customer);
- fail("Should not be here PreexistingEntityException should have been thrown and caught :");
- } catch (PreexistingEntityException e) {
- // expected exception
- } catch (RollbackFailureException e) {
- fail("PreexistingEntityException expected but RollbackFailureException caught should not be here :"
- + e.getMessage());
- } catch (Exception e) {
- fail("PreexistingEntityException expected but Exception caught should not be here :"
- + e.getMessage());
- }
- }
- @Test
- public void testEdit() {
- CustomerJpaController cjpc = new CustomerJpaController();
- Customer customer;
- customer = cjpc.findCustomer(new Integer(1));
- assertNotNull(
- "FindCustomer returned null should return customer id =1",
- customer);
- customer.setAddressline1("editedAddressLine");
- try {
- cjpc.edit(customer);
- } catch (IllegalOrphanException e) {
- fail("IllegalOrphanException caught :" + e.getMessage());
- } catch (NonexistentEntityException e) {
- fail("NonexistentException caught :" + e.getMessage());
- } catch (RollbackFailureException e) {
- fail("RollbackFailureException caught :" + e.getMessage());
- } catch (Exception e) {
- fail("Exception caught :" + e.getMessage());
- }
- assertEquals("Returned customer not equal to edited customer", cjpc
- .findCustomer(customer.getCustomerId()), customer);
- customer.setPurchaseOrderCollection(new ArrayList<PurchaseOrder>());
- try {
- cjpc.edit(customer);
- fail("IllegalOrphanException expected but not thrown :");
- } catch (LazyInitializationException e) {
- fail("Laizyinit fail :" + e.getMessage());
- } catch (IllegalOrphanException e) {
- // expected
- } catch (NonexistentEntityException e) {
- fail("IllegalOrphanException expected but NonexistentException caught :"
- + e.getMessage());
- } catch (RollbackFailureException e) {
- fail("IllegalOrphanException expected but RollbackFailureException caught :"
- + e.getMessage());
- } catch (Exception e) {
- fail("IllegalOrphanException expected but Exception caught :"
- + e.getMessage());
- }
- }
- @Test
- public void testDestroy() {
- CustomerJpaController cjpc = new CustomerJpaController();
- try {
- cjpc.destroy(new Integer(3));
- } catch (IllegalOrphanException e) {
- fail("IllegalOrphanException caught :" + e.getMessage());
- } catch (NonexistentEntityException e) {
- fail("NonexistentException caught :" + e.getMessage());
- } catch (RollbackFailureException e) {
- fail("RollbackFailureException caught :" + e.getMessage());
- } catch (Exception e) {
- fail("Exception caught :" + e.getMessage());
- }
- assertNull(cjpc.findCustomer(new Integer(3)));
- }
- @Test
- public void testFindCustomerEntities() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public void testFindCustomerEntitiesIntInt() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public void testFindCustomer() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public void testGetCustomerCount() {
- fail("Not yet implemented"); // TODO
- }
- @Override
- protected void setUpDatabaseConfig(DatabaseConfig config) {
- config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
- new MySqlDataTypeFactory());
- }
- @Override
- protected IDataSet getDataSet() throws Exception {
- return new FlatXmlDataSetBuilder().build(this.getClass()
- .getClassLoader().getResourceAsStream("META-INF/dataset.xml"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement