public void testAddIndirectElimination() { EntityManager em = createEntityManager(); try { beginTransaction(em); Assassin assassin = em.find(Assassin.class, assassinId); Bomb bomb = new Bomb(); bomb.setBombType(Bomb.BOMBTYPE.DIRTY); bomb.setDescription("Nasty blasty"); em.persist(bomb); // Must set the correct weapon before an elimination. assassin.setWeapon(bomb); IndirectElimination indirectElimination = new IndirectElimination(); indirectElimination.setId(new Long(System.currentTimeMillis()).intValue()); indirectElimination.setName("Jill Smuck"); indirectElimination.setDescription("Because she has a big mouth"); indirectElimination.setAssassin(assassin); em.persist(indirectElimination); indirectEliminationPK = indirectElimination.getPK(); assassin.getEliminations().add(indirectElimination); commitTransaction(em); } catch (Exception e) { fail("Error adding new indirect elimination: " + e.getMessage()); } finally { closeEntityManager(em); } }
public void testAddDirectElimination() { EntityManager em = createEntityManager(); try { beginTransaction(em); Assassin assassin = em.find(Assassin.class, assassinId); // Assassin already has a gun, therefore, correct weapon already set // for a direct elimination. DirectElimination directElimination = new DirectElimination(); directElimination.setId(new Long(System.currentTimeMillis()).intValue()); directElimination.setName("Joe Smuck"); directElimination.setDescription("Because he has a big mouth"); directElimination.setAssassin(assassin); em.persist(directElimination); directEliminationPK = directElimination.getPK(); assassin.getEliminations().add(directElimination); commitTransaction(em); } catch (Exception e) { fail("Error adding new direct elimination: " + e.getMessage()); } finally { closeEntityManager(em); } }
public void testCreateAssassinWithGun() { EntityManager em = createEntityManager(); try { beginTransaction(em); Assassin assassin = new Assassin(); assassin.setName("Assassin1"); Gun gun = new Gun(); gun.setCaliber(new Integer(50)); gun.setDescription("Sniper rifle"); gun.setModel("9-112"); assassin.setWeapon(gun); em.persist(assassin); assassinId = assassin.getId(); gunSerialNumber = gun.getSerialNumber(); commitTransaction(em); } catch (Exception exception) { exception.printStackTrace(); fail("Error persisting new assassin: " + exception.getMessage()); } finally { closeEntityManager(em); } }
public void testValidateAssassinWithGun() { EntityManager em = createEntityManager(); Assassin assassin = em.find(Assassin.class, assassinId); assertNotNull("The assassin could not be read back.", assassin); Weapon weapon = assassin.getWeapon(); assertNotNull("The assassin's weapon was null", weapon); assertTrue("The assassin's weapon was not a direct weapon", weapon.isDirectWeapon()); assertTrue("The assassin's weapon was not a gun", ((DirectWeapon) weapon).isGun()); closeEntityManager(em); }
public void testValidateGunToAssassin() { EntityManager em = createEntityManager(); Gun gun = em.find(Gun.class, gunSerialNumber); assertNotNull("The gun could not be read back.", gun); Assassin assassin = gun.getAssassin(); assertNotNull("The gun's assassin was null", assassin); assertTrue( "The gun is currently not assigned to the correct assassin", assassin.getId().equals(assassinId)); closeEntityManager(em); }
public void testValidateAssassinWithBombAndEliminations() { EntityManager em = createEntityManager(); Assassin assassin = em.find(Assassin.class, assassinId); assertNotNull("The assassin could not be read back.", assassin); assassin.getEliminations(); assertFalse("The assassin didn't have any eliminations", assassin.getEliminations().isEmpty()); Weapon weapon = assassin.getWeapon(); assertNotNull("The assassin's weapon was null", weapon); assertTrue("The assassin's weapon was not an indirect weapon", weapon.isIndirectWeapon()); assertTrue("The assassin's weapon was not a bomb", ((IndirectWeapon) weapon).isBomb()); closeEntityManager(em); }
// Tested for an Assassin and a SpecialAssasin protected void optimisticLockTestOnAssassin(Integer id) { // Cannot create parallel entity managers in the server. if (!isOnServer()) { EntityManager em1 = createEntityManager(); EntityManager em2 = createEntityManager(); em1.getTransaction().begin(); em2.getTransaction().begin(); RuntimeException caughtException = null; try { Assassin assassin1 = em1.find(Assassin.class, id); Assassin assassin2 = em2.find(Assassin.class, id); assassin1.setName("Geezer"); assassin2.setName("Guyzer"); em1.getTransaction().commit(); em2.getTransaction().commit(); em1.close(); em2.close(); } catch (RuntimeException e) { if (em1.getTransaction().isActive()) { em1.getTransaction().rollback(); } if (em2.getTransaction().isActive()) { em2.getTransaction().rollback(); } if (e.getCause() instanceof javax.persistence.OptimisticLockException) { caughtException = e; } else { throw e; } } finally { em1.close(); em2.close(); } if (caughtException == null) { fail("Optimistic lock exception was not thrown."); } } }
// Note: Update all for Table Per class only works with the one single // reference class. So it's not a full feature test. public void testUpdateAllQuery() { EntityManager em = createEntityManager(); beginTransaction(em); try { ExpressionBuilder eb = new ExpressionBuilder(); UpdateAllQuery updateQuery = new UpdateAllQuery(Assassin.class); updateQuery.addUpdate(eb.get("name"), "Generic Assassin Name"); ((JpaEntityManager) em.getDelegate()).getServerSession().executeQuery(updateQuery); Assassin assassin = (Assassin) em.find(ContractedPersonel.class, assassinId); em.refresh(assassin); commitTransaction(em); assertTrue("Update all did not work", assassin.getName().equals("Generic Assassin Name")); } catch (Exception e) { e.printStackTrace(); fail("Error issuing update all contracted personel query: " + e.getMessage()); } finally { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); } }
public void testAssassinOptimisticLockingUsingEntityManagerAPI() { // Cannot create parallel entity managers in the server. if (!isOnServer()) { EntityManager em = createEntityManager(); beginTransaction(em); Assassin assassin = null; try { assassin = new Assassin(); assassin.setName("OptLockAssassin"); em.persist(assassin); commitTransaction(em); } catch (RuntimeException ex) { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); throw ex; } EntityManager em2 = createEntityManager(); Exception optimisticLockException = null; beginTransaction(em); try { assassin = em.find(Assassin.class, assassin.getId()); em.lock(assassin, LockModeType.WRITE); beginTransaction(em2); try { Assassin assassin2 = em2.find(Assassin.class, assassin.getId()); assassin2.setName("OptLockAssassin2"); commitTransaction(em2); em2.close(); } catch (RuntimeException ex) { if (isTransactionActive(em2)) { rollbackTransaction(em2); } closeEntityManager(em2); throw ex; } try { em.flush(); } catch (PersistenceException exception) { if (exception instanceof OptimisticLockException) { optimisticLockException = exception; } else { throw exception; } } rollbackTransaction(em); } catch (RuntimeException ex) { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); throw ex; } try { beginTransaction(em); assassin = em.find(Assassin.class, assassin.getId()); em.remove(assassin); commitTransaction(em); } catch (RuntimeException ex) { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); throw ex; } assertFalse( "Proper exception not thrown when EntityManager.lock(object, OPTIMISTIC) is used.", optimisticLockException == null); } }