]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/examples/ExampleTestWithDependencies.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / examples / ExampleTestWithDependencies.java
1 package dst.ass1.jpa.examples;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.hamcrest.CoreMatchers.notNullValue;
5 import static org.junit.Assert.assertThat;
6
7 import java.util.List;
8
9 import javax.persistence.EntityManager;
10
11 import org.junit.ClassRule;
12 import org.junit.FixMethodOrder;
13 import org.junit.Test;
14 import org.junit.runners.MethodSorters;
15
16 import dst.ass1.jpa.ORMService;
17 import dst.ass1.jpa.dao.ICoursePlatformDAO;
18 import dst.ass1.jpa.dao.IDAOFactory;
19 import dst.ass1.jpa.model.ICoursePlatform;
20 import dst.ass1.jpa.model.IModelFactory;
21
22 /**
23  * This test has no test fixture and dependencies between tests. This is possible by declaring {@link ORMService} as a
24  * static class-level rule via {@link ClassRule}.
25  */
26 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
27 public class ExampleTestWithDependencies {
28
29     @ClassRule
30     public static ORMService orm = new ORMService();
31
32     @Test
33     public void t01_insert() throws Exception {
34         EntityManager em = orm.getEntityManager();
35         IModelFactory modelFactory = orm.getModelFactory();
36
37         ICoursePlatform cp1 = modelFactory.createCoursePlatform();
38         cp1.setName("cp1");
39         cp1.setUrl("http://cp1.example");
40
41         ICoursePlatform cp2 = modelFactory.createCoursePlatform();
42         cp2.setName("cp2");
43         cp2.setUrl("http://cp1.example");
44
45         em.persist(cp1);
46         em.persist(cp2);
47
48         em.getTransaction().begin();
49         em.flush();
50         em.getTransaction().commit();
51
52         assertThat(cp1.getId(), notNullValue());
53         assertThat(cp2.getId(), notNullValue());
54     }
55
56     @Test
57     public void t02_query() throws Exception {
58         IDAOFactory daoFactory = orm.getDaoFactory();
59         ICoursePlatformDAO coursePlatformDAO = daoFactory.createCoursePlatformDAO();
60
61         List<ICoursePlatform> all = coursePlatformDAO.findAll();
62
63         assertThat(all.isEmpty(), is(false));
64         assertThat(all.size(), is(2));
65
66         System.out.println(all);
67     }
68 }