📄 persistentstatetransitions.java
字号:
@Test(groups = "integration-hibernate") public void queryCategorizedItems() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs CategorizedItemDAO catItemDAO = daoFactory.getCategorizedItemDAO(); CategorizedItem.Id id = new CategorizedItem.Id(1l, 1l); CategorizedItem catItem = catItemDAO.findById(id, false); // Also initializes proxy assert "johndoe".equals(catItem.getUsername()); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); } @Test(groups = "integration-hibernate", expectedExceptions = ObjectNotFoundException.class) public void orphanDeletionOfCategorizedItems() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); // Delete all links for item4 by clearing collection Item i = itemDAO.findById(1l, false); assert i.getCategorizedItems().size() == 2; i.getCategorizedItems().clear(); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs itemDAO = daoFactory.getItemDAO(); CategorizedItemDAO catItemDAO = daoFactory.getCategorizedItemDAO(); // Check deletion i = itemDAO.findById(1l, false); assert i.getCategorizedItems().size() == 0; CategorizedItem.Id id = new CategorizedItem.Id(1l, 1l); CategorizedItem catItem = catItemDAO.findById(id, false); catItem.getUsername(); // Force proxy initialization, throws exception } @Test(groups = "integration-hibernate", expectedExceptions = BusinessException.class) public void auctionNotActive() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); UserDAO userDAO = daoFactory.getUserDAO(); Bid currentMaxBid = itemDAO.getMaxBid(2l); Bid currentMinBid = itemDAO.getMinBid(2l); Item auction = itemDAO.findById(2l, true); // Fail, auction is not active yet BigDecimal bidAmount = new BigDecimal("333"); MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD")); auction.placeBid(userDAO.findById(1l, false), newAmount, currentMaxBid, currentMinBid); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); } @Test(groups = "integration-hibernate", expectedExceptions = PermissionException.class) public void userNotAllowedToApprove() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); UserDAO userDAO = daoFactory.getUserDAO(); // Fail, user isn't an admin itemDAO.findById(2l, true).approve(userDAO.findById(2l, false)); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); } @Test(groups = "integration-hibernate") public void userAllowedToApprove() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); UserDAO userDAO = daoFactory.getUserDAO(); // Don't fail, user is an admin Item auction = itemDAO.findById(2l, true); auction.setPendingForApproval(); auction.approve(userDAO.findById(1l, false)); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); } @Test(groups = "integration-hibernate") public void placeBid() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); UserDAO userDAO = daoFactory.getUserDAO(); Item auction = itemDAO.findById(2l, true); Bid currentMaxBid = itemDAO.getMaxBid(auction.getId()); Bid currentMinBid = itemDAO.getMinBid(auction.getId()); // Activate auction.setPendingForApproval(); auction.approve(userDAO.findById(1l, false)); // Place a bid BigDecimal bidAmount = new BigDecimal("333.00"); MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD")); auction.placeBid(userDAO.findById(2l, false), newAmount, currentMaxBid, currentMinBid); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); } @Test(groups = "integration-hibernate", expectedExceptions = BusinessException.class) public void placeBidNotActive() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); UserDAO userDAO = daoFactory.getUserDAO(); Item auction = itemDAO.findById(2l, true); Bid currentMaxBid = itemDAO.getMaxBid(auction.getId()); Bid currentMinBid = itemDAO.getMinBid(auction.getId()); // Place a bid BigDecimal bidAmount = new BigDecimal("333.00"); MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD")); auction.placeBid(userDAO.findById(2l, false), newAmount, currentMaxBid, currentMinBid); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); } @Test(groups = "integration-hibernate", expectedExceptions = BusinessException.class) public void placeBidTooLow() { // Start a unit of work HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); // Prepare the DAOs ItemDAO itemDAO = daoFactory.getItemDAO(); UserDAO userDAO = daoFactory.getUserDAO(); Item auction = itemDAO.findById(2l, true); Bid currentMaxBid = itemDAO.getMaxBid(auction.getId()); Bid currentMinBid = itemDAO.getMinBid(auction.getId()); // Activate auction.setPendingForApproval(); auction.approve(userDAO.findById(1l, false)); // Place a bid BigDecimal bidAmount = new BigDecimal("100.00"); MonetaryAmount newAmount = new MonetaryAmount(bidAmount, Currency.getInstance("USD")); auction.placeBid(userDAO.findById(2l, false), newAmount, currentMaxBid, currentMinBid); // End the unit of work HibernateUtil.getSessionFactory().getCurrentSession() .getTransaction().commit(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -