Extended integration-test "hib-test" to check for package-level annotations
[hibernate4-maven-plugin] / src / it / hibernate4-maven-plugin-envers-sample / src / test / java / org / bitbucket / fbascheper / tutorial / envers / EnversIllustrationTest.java
1 /**
2  * Copyright 2013 F.B.A. Scheper.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. *
15  */
16 package org.bitbucket.fbascheper.tutorial.envers;
17
18 import org.hibernate.envers.AuditReader;
19 import org.hibernate.envers.AuditReaderFactory;
20 import org.junit.Test;
21 import org.springframework.transaction.TransactionStatus;
22 import org.springframework.transaction.support.TransactionCallback;
23 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
24
25 import java.util.Date;
26 import java.util.List;
27
28 import static org.hamcrest.CoreMatchers.is;
29 import static org.hamcrest.CoreMatchers.notNullValue;
30 import static org.hamcrest.core.IsNot.not;
31 import static org.junit.Assert.assertThat;
32
33 /**
34  * Illustrates the set up and use of Envers using JTA; based on the original hibernate tutorial by Steve Ebersole.
35  *
36  * @author Erik-Berndt Scheper
37  * @since 11-09-2013
38  */
39 public class EnversIllustrationTest extends AbstractIntegrationTest {
40
41     @Test
42     public void testOne() {
43
44         // create a couple of events
45         final Event event1 = getTransactionTemplate().execute(new TransactionCallback<Event>() {
46             @Override
47             public Event doInTransaction(TransactionStatus status) {
48                 // revision 1
49                 Event event = new Event("Our very first event!", new Date());
50                 getEntityManager().persist(event);
51                 return event;
52
53             }
54         });
55         final Event event2 = getTransactionTemplate().execute(new TransactionCallback<Event>() {
56             @Override
57             public Event doInTransaction(TransactionStatus status) {
58                 // revision 2
59                 Event event = new Event("A follow up event", new Date());
60                 getEntityManager().persist(event);
61                 return event;
62
63             }
64         });
65
66         // now lets pull events from the database and list them
67
68         List<Event> result = getTransactionTemplate().execute(new TransactionCallback<List<Event>>() {
69             @Override
70             public List<Event> doInTransaction(TransactionStatus status) {
71                 List<Event> result = getEntityManager().createQuery("select evt from Event evt", Event.class).getResultList();
72                 for (Event event : result) {
73                     System.out.println("Event (" + event.getDate() + ") : " + event.getTitle());
74                 }
75
76                 return result;
77             }
78         });
79
80         // verify that id's were generated
81         final Long event1Id = event1.getId();
82         final Long event2Id = event2.getId();
83
84         assertThat(event1Id, notNullValue());
85         assertThat(event2Id, notNullValue());
86
87         // so far the code is the same as we have seen in previous tutorials.  Now lets leverage Envers...
88         // first lets create some revisions
89         getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
90             @Override
91             protected void doInTransactionWithoutResult(TransactionStatus status) {
92                 // revision 3
93                 Event myEvent = getEntityManager().find(Event.class, event2Id);
94                 myEvent.setDate(new Date());
95                 myEvent.setTitle(myEvent.getTitle() + " (rescheduled)");
96
97             }
98         });
99
100         // and then use an AuditReader to look back through history
101         getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
102             @Override
103             protected void doInTransactionWithoutResult(TransactionStatus status) {
104
105                 Event myEvent = getEntityManager().find(Event.class, event2Id);
106                 assertThat("A follow up event (rescheduled)", is(myEvent.getTitle()));
107
108                 AuditReader reader = AuditReaderFactory.get(getEntityManager());
109
110                 List<? extends Number> event2Revisions = reader.getRevisions(Event.class, event2Id);
111                 assertThat(event2Revisions.size(), is(2));
112
113                 long event2Revision1 = event2Revisions.get(0).longValue();
114                 long event2Revision2 = event2Revisions.get(1).longValue();
115
116                 assertThat(event2Revision1, is(2L));
117                 assertThat(event2Revision2, is(3L));
118
119                 Event firstRevision = reader.find(Event.class, event2Id, event2Revision1);
120
121                 assertThat(firstRevision, notNullValue());
122                 assertThat(firstRevision.getTitle(), notNullValue());
123                 assertThat(firstRevision.getTitle(), not(is(myEvent.getTitle())));
124                 assertThat(firstRevision.getDate(), not(is(myEvent.getDate())));
125
126                 Event secondRevision = reader.find(Event.class, event2Id, event2Revision2);
127                 assertThat(secondRevision, notNullValue());
128                 assertThat(secondRevision.getTitle(), notNullValue());
129                 assertThat(secondRevision.getTitle(), is(myEvent.getTitle()));
130                 assertThat(secondRevision.getDate(), is(myEvent.getDate()));
131
132             }
133
134         });
135
136     }
137
138 }