コード例 #1
0
  public void testPostPersistSportsCarInheritAndExcludeDefault() {
    int fueledVehiclePostPersistCountBefore = FueledVehicleListener.POST_PERSIST_COUNT;
    int defaultListenerPostPersistCountBefore = DefaultListener.POST_PERSIST_COUNT;

    EntityManager em = createEntityManager();
    beginTransaction(em);

    try {
      SportsCar sportsCar = new SportsCar();
      sportsCar.setPassengerCapacity(new Integer(4));
      sportsCar.setFuelCapacity(new Integer(55));
      sportsCar.setDescription("Porshe");
      sportsCar.setFuelType("Gas");
      em.persist(sportsCar);
      commitTransaction(em);
    } catch (RuntimeException ex) {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }

      closeEntityManager(em);
      throw ex;
    }

    int fueledVehiclePostPersistCountAfter = FueledVehicleListener.POST_PERSIST_COUNT;
    int defaultListenerPostPersistCountAfter = DefaultListener.POST_PERSIST_COUNT;

    assertFalse(
        "The PostPersist callback method on FueledVehicleListener was not called.",
        fueledVehiclePostPersistCountBefore == fueledVehiclePostPersistCountAfter);
    assertTrue(
        "The PostPersist callback method on DefaultListener was called.",
        defaultListenerPostPersistCountBefore == defaultListenerPostPersistCountAfter);
  }
コード例 #2
0
  public void testPrePersistSportsCarOverride() {
    int carPrePersistCountBefore = Car.PRE_PERSIST_COUNT;
    int sportsCarPrePersistCountBefore = SportsCar.PRE_PERSIST_COUNT;

    EntityManager em = createEntityManager();
    beginTransaction(em);

    try {
      SportsCar sportsCar = new SportsCar();
      sportsCar.setPassengerCapacity(new Integer(2));
      sportsCar.setFuelCapacity(new Integer(90));
      sportsCar.setDescription("Viper");
      sportsCar.setFuelType("Gas");
      em.persist(sportsCar);
      commitTransaction(em);
    } catch (RuntimeException ex) {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }

      closeEntityManager(em);
      throw ex;
    }

    closeEntityManager(em);

    int carPrePersistCountAfter = Car.PRE_PERSIST_COUNT;
    int sportsCarPrePersistCountAfter = SportsCar.PRE_PERSIST_COUNT;

    assertTrue(
        "The PrePersist callback method on Car was called.",
        carPrePersistCountBefore == carPrePersistCountAfter);
    assertFalse(
        "The PrePersist callback method on Sports car was not called.",
        sportsCarPrePersistCountBefore == sportsCarPrePersistCountAfter);
  }
コード例 #3
0
  public void testPrePersistSportsCarInheritAndExcludeDefault() {
    int listenerSuperclassPrePersistCountBefore = ListenerSuperclass.COMMON_PRE_PERSIST_COUNT;
    int defaultListenerPrePersistCountBefore = DefaultListener.PRE_PERSIST_COUNT;

    EntityManager em = createEntityManager();
    beginTransaction(em);

    try {
      SportsCar sportsCar = new SportsCar();
      sportsCar.setPassengerCapacity(new Integer(2));
      sportsCar.setFuelCapacity(new Integer(60));
      sportsCar.setDescription("Corvette");
      sportsCar.setFuelType("Gas");
      em.persist(sportsCar);
      commitTransaction(em);
    } catch (RuntimeException ex) {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }

      closeEntityManager(em);
      throw ex;
    }

    closeEntityManager(em);

    int listenerSuperclassPrePersistCountAfter = ListenerSuperclass.COMMON_PRE_PERSIST_COUNT;
    int defaultListenerPrePersistCountAfter = DefaultListener.PRE_PERSIST_COUNT;

    assertFalse(
        "The PrePersist callback method on ListenerSuperclass was not called.",
        listenerSuperclassPrePersistCountBefore == listenerSuperclassPrePersistCountAfter);
    assertTrue(
        "The PrePersist callback method on DefaultListener was called.",
        defaultListenerPrePersistCountBefore == defaultListenerPrePersistCountAfter);
  }