KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > test > AuditTest


1 package org.hibernate.ce.auction.test;
2
3 import junit.framework.*;
4 import junit.textui.TestRunner;
5 import org.hibernate.*;
6 import org.hibernate.ce.auction.dao.*;
7 import org.hibernate.ce.auction.model.*;
8 import org.hibernate.ce.auction.persistence.HibernateUtil;
9 import org.hibernate.ce.auction.persistence.audit.*;
10
11 import java.math.BigDecimal JavaDoc;
12 import java.util.*;
13
14 public class AuditTest extends TestCase {
15
16     // ********************************************************** //
17

18     public void testAuditLog() throws Exception JavaDoc {
19
20         // Save a user without audit logging
21
UserDAO userDAO = new UserDAO();
22         User u1 = new User("Christian", "Bauer", "turin", "abc123", "christian@hibernate.org");
23         userDAO.makePersistent(u1);
24         HibernateUtil.commitTransaction();
25         HibernateUtil.closeSession();
26
27         // Enable interceptor
28
AuditLogInterceptor interceptor = new AuditLogInterceptor();
29         HibernateUtil.registerInterceptor(interceptor);
30         interceptor.setSession(HibernateUtil.getSession());
31         interceptor.setUserId(u1.getId());
32
33         // Save an item with audit logging enabled
34
Item item = new Item("ONE", "Foo",
35                 u1,
36                 new MonetaryAmount(new BigDecimal JavaDoc("1.99"), Currency.getInstance(Locale.US)),
37                 new MonetaryAmount(new BigDecimal JavaDoc("50.33"), Currency.getInstance(Locale.US)),
38                 new Date(), new Date());
39         ItemDAO itemDAO = new ItemDAO();
40         itemDAO.makePersistent(item);
41
42         // Synchronize state to trigger interceptor
43
HibernateUtil.getSession().flush();
44
45         // Check audit log
46
Query queryAuditOne = HibernateUtil.getSession().createQuery("from AuditLogRecord lr where lr.entityId = :id");
47         queryAuditOne.setParameter("id", item.getId());
48         AuditLogRecord logRecordOne = (AuditLogRecord)queryAuditOne.uniqueResult();
49         assertEquals(logRecordOne.userId, u1.getId());
50
51         HibernateUtil.commitTransaction();
52         HibernateUtil.closeSession();
53
54         // Deregister interceptor
55
HibernateUtil.registerInterceptor(null);
56     }
57
58     // ********************************************************** //
59

60     public AuditTest(String JavaDoc x) {
61         super(x);
62     }
63
64     public static Test suite() {
65         return new TestSuite(AuditTest.class);
66     }
67
68     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
69         TestRunner.run( suite() );
70     }
71
72 }
73
Popular Tags