2 * Copyright 2013 F.B.A. Scheper.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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. *
16 package org.bitbucket.fbascheper.tutorial.envers;
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;
25 import java.util.Date;
26 import java.util.List;
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;
34 * Illustrates the set up and use of Envers using JTA; based on the original hibernate tutorial by Steve Ebersole.
36 * @author Erik-Berndt Scheper
39 public class EnversIllustrationTest extends AbstractIntegrationTest {
42 public void testOne() {
44 // create a couple of events
45 final Event event1 = getTransactionTemplate().execute(new TransactionCallback<Event>() {
47 public Event doInTransaction(TransactionStatus status) {
49 Event event = new Event("Our very first event!", new Date());
50 getEntityManager().persist(event);
55 final Event event2 = getTransactionTemplate().execute(new TransactionCallback<Event>() {
57 public Event doInTransaction(TransactionStatus status) {
59 Event event = new Event("A follow up event", new Date());
60 getEntityManager().persist(event);
66 // now lets pull events from the database and list them
68 List<Event> result = getTransactionTemplate().execute(new TransactionCallback<List<Event>>() {
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());
80 // verify that id's were generated
81 final Long event1Id = event1.getId();
82 final Long event2Id = event2.getId();
84 assertThat(event1Id, notNullValue());
85 assertThat(event2Id, notNullValue());
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() {
91 protected void doInTransactionWithoutResult(TransactionStatus status) {
93 Event myEvent = getEntityManager().find(Event.class, event2Id);
94 myEvent.setDate(new Date());
95 myEvent.setTitle(myEvent.getTitle() + " (rescheduled)");
100 // and then use an AuditReader to look back through history
101 getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
103 protected void doInTransactionWithoutResult(TransactionStatus status) {
105 Event myEvent = getEntityManager().find(Event.class, event2Id);
106 assertThat("A follow up event (rescheduled)", is(myEvent.getTitle()));
108 AuditReader reader = AuditReaderFactory.get(getEntityManager());
110 List<? extends Number> event2Revisions = reader.getRevisions(Event.class, event2Id);
111 assertThat(event2Revisions.size(), is(2));
113 long event2Revision1 = event2Revisions.get(0).longValue();
114 long event2Revision2 = event2Revisions.get(1).longValue();
116 assertThat(event2Revision1, is(2L));
117 assertThat(event2Revision2, is(3L));
119 Event firstRevision = reader.find(Event.class, event2Id, event2Revision1);
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())));
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()));