1313import static org .junit .Assert .*;
1414
1515/**
16+ * In this sample we're going to query a simple +JPA Entity+, using the +JPA Criteria API+ and perform a select,
17+ * update and delete operations.
18+ *
19+ * The following +JPA Entity+, represents a Movie which has a name and a comma separated list of actors:
20+ *
21+ * include::Movie[]
22+ *
23+ * The select, update and delete operations are exposed using a simple stateless ejb.
24+ *
25+ * Select every movie:
26+ * include::MovieBean#listMovies[]
27+ *
28+ * Update all the name of the movies to "INCEPTION" where the name of the movie is "Inception":
29+ * include::MovieBean#updateMovie[]
30+ *
31+ * Delete all movies where the name of the movie is "Matrix":
32+ * include::MovieBean#deleteMovie[]
33+ *
1634 * @author Roberto Cortez
1735 */
1836@ RunWith (Arquillian .class )
1937public class JpaCriteriaTest {
2038 @ Inject
2139 private MovieBean movieBean ;
2240
41+ /**
42+ * We're just going to deploy the application as a +web archive+. Note the inclusion of the following files:
43+ *
44+ * [source,file]
45+ * ----
46+ * /META-INF/persistence.xml
47+ * /META-INF/create.sql
48+ * /META-INF/drop.sql
49+ * /META-INF/load.sql
50+ * ----
51+ *
52+ * The +persistence.xml+ file is needed of course for the persistence unit definition. A datasource is not
53+ * needed, since we can now use the new default datasource available in +JEE7+. We're also using the new
54+ * +javax.persistence.schema-generation.*+ propertires to create, populate and drop the database.
55+ */
2356 @ Deployment
2457 public static WebArchive createDeployment () {
2558 WebArchive war = ShrinkWrap .create (WebArchive .class )
@@ -32,24 +65,28 @@ public static WebArchive createDeployment() {
3265 return war ;
3366 }
3467
68+ /**
69+ * In the test, we're just going to invoke the different operations in sequence keeping in mind that each
70+ * invocation might be dependent of the previous invoked operation.
71+ */
3572 @ Test
3673 public void testCriteria () {
37- List <Movie > movies = movieBean .listMovies ();
38- assertEquals (4 , movies .size ());
74+ List <Movie > movies = movieBean .listMovies (); // <1> Get a list of all the movies in the database.
75+ assertEquals (4 , movies .size ()); // <2> 4 movies loaded on the db, so the size shoud be 4.
3976 assertTrue (movies .contains (new Movie (1 )));
4077 assertTrue (movies .contains (new Movie (2 )));
4178 assertTrue (movies .contains (new Movie (3 )));
4279 assertTrue (movies .contains (new Movie (4 )));
4380
44- movieBean .updateMovie ();
81+ movieBean .updateMovie (); // <3> Update name to "INCEPTION" where name is "Inception"
4582 movies = movieBean .listMovies ();
46- assertEquals (4 , movies .size ());
47- assertEquals ("INCEPTION" , movies .get (2 ).getName ());
83+ assertEquals (4 , movies .size ()); // <4> Size of movies should still be 4.
84+ assertEquals ("INCEPTION" , movies .get (2 ).getName ()); // <5> Verify the movie name change.
4885
49- movieBean .deleteMovie ();
86+ movieBean .deleteMovie (); // <6> Now delete the movie "Matrix"
5087 movies = movieBean .listMovies ();
5188 assertFalse (movies .isEmpty ());
52- assertEquals (3 , movies .size ());
53- assertFalse (movies .contains (new Movie (1 )));
89+ assertEquals (3 , movies .size ()); // <7> Size of movies should be 3 now.
90+ assertFalse (movies .contains (new Movie (1 ))); // <8> Check if the movie "Matrix" is not on the list.
5491 }
5592}
0 commit comments