Added tutorials of the hibernate-release 4.3.9.Final as integration-tests
[hibernate4-maven-plugin] / src / it / tutorials / envers / src / test / java / org / hibernate / tutorial / envers / EnversIllustrationTest.java
1 /*
2  * Hibernate, Relational Persistence for Idiomatic Java
3  *
4  * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
5  * indicated by the @author tags or express copyright attribution
6  * statements applied by the authors.  All third-party contributions are
7  * distributed under license by Red Hat Inc.
8  *
9  * This copyrighted material is made available to anyone wishing to use, modify,
10  * copy, or redistribute it subject to the terms and conditions of the GNU
11  * Lesser General Public License, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this distribution; if not, write to:
20  * Free Software Foundation, Inc.
21  * 51 Franklin Street, Fifth Floor
22  * Boston, MA  02110-1301  USA
23  */
24 package org.hibernate.tutorial.envers;
25
26 import java.util.Date;
27 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.Persistence;
31
32 import junit.framework.TestCase;
33
34 import org.hibernate.envers.AuditReader;
35 import org.hibernate.envers.AuditReaderFactory;
36
37 /**
38  * Illustrates the set up and use of Envers.
39  * <p>
40  * This example is different from the others in that we really need to save multiple revisions to the entity in
41  * order to get a good look at Envers in action.
42  *
43  * @author Steve Ebersole
44  */
45 public class EnversIllustrationTest extends TestCase {
46         private EntityManagerFactory entityManagerFactory;
47
48         @Override
49         protected void setUp() throws Exception {
50                 // like discussed with regards to SessionFactory, an EntityManagerFactory is set up once for an application
51                 //              IMPORTANT: notice how the name here matches the name we gave the persistence-unit in persistence.xml!
52                 entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.envers" );
53         }
54
55         @Override
56         protected void tearDown() throws Exception {
57                 entityManagerFactory.close();
58         }
59
60         public void testBasicUsage() {
61                 // create a couple of events
62                 EntityManager entityManager = entityManagerFactory.createEntityManager();
63                 entityManager.getTransaction().begin();
64                 entityManager.persist( new Event( "Our very first event!", new Date() ) );
65                 entityManager.persist( new Event( "A follow up event", new Date() ) );
66                 entityManager.getTransaction().commit();
67                 entityManager.close();
68
69                 // now lets pull events from the database and list them
70                 entityManager = entityManagerFactory.createEntityManager();
71                 entityManager.getTransaction().begin();
72         List<Event> result = entityManager.createQuery( "from Event", Event.class ).getResultList();
73                 for ( Event event : result ) {
74                         System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
75                 }
76         entityManager.getTransaction().commit();
77         entityManager.close();
78
79                 // so far the code is the same as we have seen in previous tutorials.  Now lets leverage Envers...
80
81                 // first lets create some revisions
82                 entityManager = entityManagerFactory.createEntityManager();
83                 entityManager.getTransaction().begin();
84                 Event myEvent = entityManager.find( Event.class, 2L ); // we are using the increment generator, so we know 2 is a valid id
85                 myEvent.setDate( new Date() );
86                 myEvent.setTitle( myEvent.getTitle() + " (rescheduled)" );
87         entityManager.getTransaction().commit();
88         entityManager.close();
89
90                 // and then use an AuditReader to look back through history
91                 entityManager = entityManagerFactory.createEntityManager();
92                 entityManager.getTransaction().begin();
93                 myEvent = entityManager.find( Event.class, 2L );
94                 assertEquals( "A follow up event (rescheduled)", myEvent.getTitle() );
95                 AuditReader reader = AuditReaderFactory.get( entityManager );
96                 Event firstRevision = reader.find( Event.class, 2L, 1 );
97                 assertFalse( firstRevision.getTitle().equals( myEvent.getTitle() ) );
98                 assertFalse( firstRevision.getDate().equals( myEvent.getDate() ) );
99                 Event secondRevision = reader.find( Event.class, 2L, 2 );
100                 assertTrue( secondRevision.getTitle().equals( myEvent.getTitle() ) );
101                 assertTrue( secondRevision.getDate().equals( myEvent.getDate() ) );
102                 entityManager.getTransaction().commit();
103         entityManager.close();
104         }
105 }