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 ; 12 import java.util.*; 13 14 public class AuditTest extends TestCase { 15 16 18 public void testAuditLog() throws Exception { 19 20 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 AuditLogInterceptor interceptor = new AuditLogInterceptor(); 29 HibernateUtil.registerInterceptor(interceptor); 30 interceptor.setSession(HibernateUtil.getSession()); 31 interceptor.setUserId(u1.getId()); 32 33 Item item = new Item("ONE", "Foo", 35 u1, 36 new MonetaryAmount(new BigDecimal ("1.99"), Currency.getInstance(Locale.US)), 37 new MonetaryAmount(new BigDecimal ("50.33"), Currency.getInstance(Locale.US)), 38 new Date(), new Date()); 39 ItemDAO itemDAO = new ItemDAO(); 40 itemDAO.makePersistent(item); 41 42 HibernateUtil.getSession().flush(); 44 45 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 HibernateUtil.registerInterceptor(null); 56 } 57 58 60 public AuditTest(String 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 [] args) throws Exception { 69 TestRunner.run( suite() ); 70 } 71 72 } 73 | Popular Tags |