Skip to content

Instantly share code, notes, and snippets.

@thomas-mc-work
Created November 19, 2015 10:19
Show Gist options
  • Save thomas-mc-work/a296ac40caa402b5120a to your computer and use it in GitHub Desktop.
Save thomas-mc-work/a296ac40caa402b5120a to your computer and use it in GitHub Desktop.
package org.arquillian.example;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GamePersistenceNGTest extends Arquillian {
@Deployment
public static Archive<?> createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Game.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource("jbossas-ds.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return war;
}
private static final String[] GAME_TITLES = {
"Super Mario Brothers",
"Mario Kart",
"F-Zero"
};
@PersistenceContext
EntityManager em;
@Inject
UserTransaction utx;
@Override
public void arquillianBeforeTest(Method testMethod) throws Exception {
super.arquillianBeforeTest(testMethod);
clearData();
insertData();
startTransaction();
}
private void clearData() throws Exception {
utx.begin();
em.joinTransaction();
System.out.println("Dumping old records...");
em.createQuery("delete from Game").executeUpdate();
utx.commit();
}
private void insertData() throws Exception {
utx.begin();
em.joinTransaction();
System.out.println("Inserting records...");
for (String title : GAME_TITLES) {
Game game = new Game(title);
em.persist(game);
}
utx.commit();
// reset the persistence context (cache)
em.clear();
}
private void startTransaction() throws Exception {
utx.begin();
em.joinTransaction();
}
@Override
public void arquillianAfterTest(Method testMethod) throws Exception {
super.arquillianAfterTest(testMethod);
utx.commit();
}
@Test()
public void shouldFindAllGamesUsingJpqlQuery() throws Exception {
// given
String fetchingAllGamesInJpql = "select g from Game g order by g.id";
// when
System.out.println("Selecting (using JPQL)...");
List<Game> games = em.createQuery(fetchingAllGamesInJpql, Game.class).getResultList();
// then
System.out.println("Found " + games.size() + " games (using JPQL):");
assertContainsAllGames(games);
}
@Test
public void shouldFindAllGamesUsingCriteriaApi() throws Exception {
// given
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Game> criteria = builder.createQuery(Game.class);
Root<Game> game = criteria.from(Game.class);
criteria.select(game);
// TIP: If you don't want to use the JPA 2 Metamodel,
// you can change the get() method call to get("id")
criteria.orderBy(builder.asc(game.get(Game_.id)));
// No WHERE clause, which implies select all
// when
System.out.println("Selecting (using Criteria)...");
List<Game> games = em.createQuery(criteria).getResultList();
// then
System.out.println("Found " + games.size() + " games (using Criteria):");
assertContainsAllGames(games);
}
private static void assertContainsAllGames(Collection<Game> retrievedGames) {
Assert.assertEquals(GAME_TITLES.length, retrievedGames.size());
final Set<String> retrievedGameTitles = new HashSet<String>();
for (Game game : retrievedGames) {
System.out.println("* " + game);
retrievedGameTitles.add(game.getTitle());
}
Assert.assertTrue(retrievedGameTitles.containsAll(Arrays.asList(GAME_TITLES)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment