Package org.agoncal.book.javaee7.chapter06.ex34

Source Code of org.agoncal.book.javaee7.chapter06.ex34.Caching34IT

package org.agoncal.book.javaee7.chapter06.ex34;

import org.agoncal.book.javaee7.chapter06.AbstractPersistentTest;
import org.junit.Test;

import javax.persistence.Cache;

import static org.junit.Assert.*;

/**
* @author Antonio Goncalves
*         APress Book - Beginning Java EE 7 with Glassfish 4
*         http://www.apress.com/
*         http://www.antoniogoncalves.org
*         --
*/
public class Caching34IT extends AbstractPersistentTest {

  // ======================================
  // =              Unit tests            =
  // ======================================

  @Test
  public void shouldCheckThatAddressIsCacheableEqualsFalse() throws Exception {

    Address34 address = new Address34("Ritherdon Rd", "London", "8QE", "UK");

    // Persist the object
    tx.begin();
    em.persist(address);
    tx.commit();

    assertNotNull(address.getId());

    Cache cache = emf.getCache();

    // Address should not be in the cache
    assertFalse(cache.contains(Address34.class, address.getId()));
  }

  @Test
  public void shouldCheckThatCustomerIsCacheableEqualsTrue() throws Exception {

    Customer34 customer = new Customer34("Antony", "Balla", "tballa@mail.com");

    // Persist the object
    tx.begin();
    em.persist(customer);
    tx.commit();

    assertNotNull(customer.getId());

    Cache cache = emf.getCache();

    // Customer should be in the cache
    assertTrue(cache.contains(Customer34.class, customer.getId()));

    cache.evict(Customer34.class);

    // After clearing the cache Customer should not be in the cache
    assertFalse(cache.contains(Customer34.class, customer.getId()));
  }

  @Test
  public void shouldCheckThatBookIsNotCacheableByDefault() throws Exception {

    Book34 book = new Book34(getRandomId(), "The Hitchhiker's Guide to the Galaxy", 12.5F, "The Hitchhiker's Guide to the Galaxy is a science fiction comedy series created by Douglas Adams.", "1-84023-742-2", 354, false);

    // Persist the object
    tx.begin();
    em.persist(book);
    tx.commit();

    assertNotNull(book.getId());

    Cache cache = emf.getCache();

    // Customer should not be in the cache by default
    assertFalse(cache.contains(Customer34.class, book.getId()));
  }
}
TOP

Related Classes of org.agoncal.book.javaee7.chapter06.ex34.Caching34IT

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.