Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. package jpa.tests;
  2.  
  3. import static org.junit.Assert.*;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.List;
  8.  
  9. import jpa.controllers.CustomerJpaController;
  10. import jpa.controllers.exceptions.IllegalOrphanException;
  11. import jpa.controllers.exceptions.NonexistentEntityException;
  12. import jpa.controllers.exceptions.PreexistingEntityException;
  13. import jpa.controllers.exceptions.RollbackFailureException;
  14. import jpa.entities.Customer;
  15. import jpa.entities.DiscountCode;
  16. import jpa.entities.PurchaseOrder;
  17.  
  18. import org.dbunit.Assertion;
  19. import org.dbunit.DBTestCase;
  20. import org.dbunit.DatabaseUnitException;
  21. import org.dbunit.PropertiesBasedJdbcDatabaseTester;
  22. import org.dbunit.database.DatabaseConfig;
  23. import org.dbunit.dataset.IDataSet;
  24. import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
  25. import org.dbunit.ext.mysql.MySqlDataTypeFactory;
  26. import org.dbunit.operation.DatabaseOperation;
  27. import org.hibernate.LazyInitializationException;
  28. import org.junit.Test;
  29.  
  30. public class CustomerJpaControllerTest extends DBTestCase {
  31.  
  32.     public CustomerJpaControllerTest(String name) {
  33.         super(name);
  34.         System.setProperty(
  35.                 PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
  36.                 "com.mysql.jdbc.Driver");
  37.         System.setProperty(
  38.                 PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
  39.                 "jdbc:mysql://localhost:3306/sqs");
  40.         System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
  41.                 "root");
  42.         System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
  43.                 "");
  44.  
  45.     }
  46.  
  47.     @Override
  48.     protected DatabaseOperation getSetUpOperation() throws Exception {
  49.         return DatabaseOperation.CLEAN_INSERT;
  50.     }
  51.  
  52.     @Override
  53.     protected DatabaseOperation getTearDownOperation() throws Exception {
  54.         return DatabaseOperation.NONE;
  55.     }
  56.  
  57.     @Test
  58.     public void testDataLoadedProperly() {
  59.         IDataSet databaseDataSet = null;
  60.         try {
  61.             databaseDataSet = getConnection().createDataSet();
  62.         } catch (SQLException e) {
  63.             fail("SQLException caught :" + e.getMessage());
  64.         } catch (Exception e) {
  65.             fail("Exception caught :" + e.getMessage());
  66.         }
  67.         IDataSet expectedDataSet = null;
  68.         try {
  69.             expectedDataSet = getDataSet();
  70.         } catch (Exception e) {
  71.             fail("Exception caught :" + e.getMessage());
  72.         }
  73.  
  74.         try {
  75.             Assertion.assertEquals(databaseDataSet, expectedDataSet);
  76.         } catch (DatabaseUnitException e) {
  77.             fail("DatabaseUnitException :" + e.getMessage());
  78.         }
  79.  
  80.     }
  81.  
  82.     @Test
  83.     public void testGetEntityManager() {
  84.         CustomerJpaController cjpc = new CustomerJpaController();
  85.         assertNotNull(cjpc.getEntityManager().find(Customer.class,
  86.                 new Integer(1)));
  87.         assertNull(cjpc.getEntityManager().find(Customer.class, new Integer(6)));
  88.     }
  89.  
  90.     @Test
  91.     public void testCreate() {
  92.         CustomerJpaController cjpc = new CustomerJpaController();
  93.         Customer customer = new Customer();
  94.         DiscountCode discountCode = new DiscountCode();
  95.         discountCode.setDiscountCode('a');
  96.         customer.setAddressline1("customerTESTadd1");
  97.         customer.setAddressline2("customerTESTadd2");
  98.         customer.setCity("customerTESTcity");
  99.         customer.setCreditLimit(new Integer(1000));
  100.         customer.setEmail("customerTESTemail");
  101.         customer.setFax("customerTESTfax");
  102.         customer.setName("customerTEST");
  103.         customer.setPhone("customerTESTphone");
  104.         customer.setState("customerTESTstate");
  105.         customer.setZip("customerTESTzip");
  106.         customer.setCustomerId(new Integer(6));
  107.         customer.setDiscountCode(discountCode);
  108.         try {
  109.             cjpc.create(customer);
  110.         } catch (PreexistingEntityException e) {
  111.             fail("PreexistingEntityException caught should not be here :"
  112.                     + e.getMessage());
  113.         } catch (RollbackFailureException e) {
  114.             fail("RollbackFailureException caught should not be here :"
  115.                     + e.getMessage());
  116.         } catch (Exception e) {
  117.             fail("Exception caught should not be here :" + e.getMessage());
  118.         }
  119.         customer.setCustomerId(new Integer(1));
  120.         try {
  121.             cjpc.create(customer);
  122.             fail("Should not be here PreexistingEntityException should have been thrown and caught :");
  123.         } catch (PreexistingEntityException e) {
  124.             // expected exception
  125.         } catch (RollbackFailureException e) {
  126.             fail("PreexistingEntityException expected but RollbackFailureException caught should not be here :"
  127.                     + e.getMessage());
  128.  
  129.         } catch (Exception e) {
  130.             fail("PreexistingEntityException expected but Exception caught should not be here :"
  131.                     + e.getMessage());
  132.         }
  133.  
  134.     }
  135.  
  136.     @Test
  137.     public void testEdit() {
  138.         CustomerJpaController cjpc = new CustomerJpaController();
  139.         Customer customer;
  140.         customer = cjpc.findCustomer(new Integer(1));
  141.         assertNotNull(
  142.                 "FindCustomer returned null should return customer id =1",
  143.                 customer);
  144.         customer.setAddressline1("editedAddressLine");
  145.         try {
  146.             cjpc.edit(customer);
  147.         } catch (IllegalOrphanException e) {
  148.             fail("IllegalOrphanException caught :" + e.getMessage());
  149.         } catch (NonexistentEntityException e) {
  150.             fail("NonexistentException caught :" + e.getMessage());
  151.         } catch (RollbackFailureException e) {
  152.             fail("RollbackFailureException caught :" + e.getMessage());
  153.         } catch (Exception e) {
  154.             fail("Exception caught :" + e.getMessage());
  155.         }
  156.         assertEquals("Returned customer not equal to edited customer", cjpc
  157.                 .findCustomer(customer.getCustomerId()), customer);
  158.         customer.setPurchaseOrderCollection(new ArrayList<PurchaseOrder>());
  159.         try {
  160.             cjpc.edit(customer);
  161.             fail("IllegalOrphanException expected but not thrown :");
  162.         } catch (LazyInitializationException e) {
  163.             fail("Laizyinit fail :" + e.getMessage());
  164.         } catch (IllegalOrphanException e) {
  165.             // expected
  166.         } catch (NonexistentEntityException e) {
  167.             fail("IllegalOrphanException expected but NonexistentException caught :"
  168.                     + e.getMessage());
  169.         } catch (RollbackFailureException e) {
  170.             fail("IllegalOrphanException expected but RollbackFailureException caught :"
  171.                     + e.getMessage());
  172.         } catch (Exception e) {
  173.             fail("IllegalOrphanException expected but Exception caught :"
  174.                     + e.getMessage());
  175.         }
  176.     }
  177.  
  178.     @Test
  179.     public void testDestroy() {
  180.         CustomerJpaController cjpc = new CustomerJpaController();
  181.  
  182.         try {
  183.             cjpc.destroy(new Integer(3));
  184.         } catch (IllegalOrphanException e) {
  185.             fail("IllegalOrphanException caught :" + e.getMessage());
  186.         } catch (NonexistentEntityException e) {
  187.             fail("NonexistentException caught :" + e.getMessage());
  188.         } catch (RollbackFailureException e) {
  189.             fail("RollbackFailureException caught :" + e.getMessage());
  190.         } catch (Exception e) {
  191.             fail("Exception caught :" + e.getMessage());
  192.         }
  193.         assertNull(cjpc.findCustomer(new Integer(3)));
  194.  
  195.     }
  196.  
  197.     @Test
  198.     public void testFindCustomerEntities() {
  199.         fail("Not yet implemented"); // TODO
  200.     }
  201.  
  202.     @Test
  203.     public void testFindCustomerEntitiesIntInt() {
  204.         fail("Not yet implemented"); // TODO
  205.     }
  206.  
  207.     @Test
  208.     public void testFindCustomer() {
  209.         fail("Not yet implemented"); // TODO
  210.     }
  211.  
  212.     @Test
  213.     public void testGetCustomerCount() {
  214.         fail("Not yet implemented"); // TODO
  215.     }
  216.  
  217.     @Override
  218.     protected void setUpDatabaseConfig(DatabaseConfig config) {
  219.         config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
  220.                 new MySqlDataTypeFactory());
  221.     }
  222.  
  223.     @Override
  224.     protected IDataSet getDataSet() throws Exception {
  225.         return new FlatXmlDataSetBuilder().build(this.getClass()
  226.                 .getClassLoader().getResourceAsStream("META-INF/dataset.xml"));
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement