diff --git a/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiIntegrationTest.java b/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiIntegrationTest.java index c3bfe4ff0a..a077815aeb 100644 --- a/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiIntegrationTest.java +++ b/hibernate-osgi/src/test/java/org/hibernate/osgi/test/OsgiIntegrationTest.java @@ -22,7 +22,10 @@ import org.hibernate.SessionFactory; import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.envers.AuditReader; +import org.hibernate.envers.AuditReaderFactory; import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.osgi.test.client.AuditedDataPoint; import org.hibernate.osgi.test.client.DataPoint; import org.hibernate.osgi.test.client.SomeService; import org.hibernate.osgi.test.client.TestIntegrator; @@ -290,6 +293,71 @@ public class OsgiIntegrationTest { s.close(); } + @Test + public void testNativeEnvers() throws Exception { + final ServiceReference sr = bundleContext.getServiceReference( SessionFactory.class.getName() ); + final SessionFactory sf = ( SessionFactory )bundleContext.getService( sr ); + + final Integer adpId; + + Session s = sf.openSession(); + s.getTransaction().begin(); + AuditedDataPoint adp = new AuditedDataPoint( "Chris" ); + s.persist( adp ); + s.getTransaction().commit(); + adpId = adp.getId(); + s.close(); + + s = sf.openSession(); + s.getTransaction().begin(); + adp = s.get( AuditedDataPoint.class, adpId ); + adp.setName( "Chris2" ); + s.getTransaction().commit(); + s.close(); + + s = sf.openSession(); + AuditReader ar = AuditReaderFactory.get( s ); + assertEquals( 2, ar.getRevisions( AuditedDataPoint.class, adpId ).size() ); + AuditedDataPoint rev1 = ar.find( AuditedDataPoint.class, adpId, 1 ); + AuditedDataPoint rev2 = ar.find( AuditedDataPoint.class, adpId, 2 ); + assertEquals( new AuditedDataPoint( adpId, "Chris" ), rev1 ); + assertEquals( new AuditedDataPoint( adpId, "Chris2" ), rev2 ); + s.close(); + } + + @Test + public void testJpaEnvers() throws Exception { + final ServiceReference serviceReference = bundleContext.getServiceReference( PersistenceProvider.class.getName() ); + final PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext.getService( serviceReference ); + final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null ); + + final Integer adpId; + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + AuditedDataPoint adp = new AuditedDataPoint( "Chris" ); + em.persist( adp ); + em.getTransaction().commit(); + adpId = adp.getId(); + em.close(); + + em = emf.createEntityManager(); + em.getTransaction().begin(); + adp = em.find( AuditedDataPoint.class, adpId ); + adp.setName( "Chris2" ); + em.getTransaction().commit(); + em.close(); + + em = emf.createEntityManager(); + AuditReader ar = AuditReaderFactory.get( em ); + assertEquals( 2, ar.getRevisions( AuditedDataPoint.class, adpId ).size() ); + AuditedDataPoint rev1 = ar.find( AuditedDataPoint.class, adpId, 1 ); + AuditedDataPoint rev2 = ar.find( AuditedDataPoint.class, adpId, 2 ); + assertEquals( new AuditedDataPoint( adpId, "Chris" ), rev1 ); + assertEquals( new AuditedDataPoint( adpId, "Chris2" ), rev2 ); + em.close(); + } + @Test public void testExtensionPoints() throws Exception { final ServiceReference sr = bundleContext.getServiceReference( SessionFactory.class.getName() ); diff --git a/hibernate-osgi/src/test/java/org/hibernate/osgi/test/client/AuditedDataPoint.java b/hibernate-osgi/src/test/java/org/hibernate/osgi/test/client/AuditedDataPoint.java new file mode 100644 index 0000000000..ce0f1d9261 --- /dev/null +++ b/hibernate-osgi/src/test/java/org/hibernate/osgi/test/client/AuditedDataPoint.java @@ -0,0 +1,80 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.osgi.test.client; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.envers.Audited; + +/** + * @author Chris Cranford + */ +@Entity +@Audited +public class AuditedDataPoint { + @Id + @GeneratedValue + private Integer id; + private String name; + + AuditedDataPoint() { + + } + + public AuditedDataPoint(String name) { + this( null, name ); + } + + public AuditedDataPoint(Integer id, String name) { + this.id = id; + this.name = name; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + int result; + result = ( id != null ? id.hashCode() : 0 ); + result = 31 * result + ( name != null ? name.hashCode() : 0 ); + return result; + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) { + return true; + } + if ( !( obj instanceof AuditedDataPoint ) ) { + return false; + } + AuditedDataPoint that = (AuditedDataPoint) obj; + if ( id != null ? !id.equals( that.id ) : that.id != null ) { + return false; + } + if ( name != null ? !name.equals( that.name ) : that.name != null ) { + return false; + } + return true; + } +} diff --git a/hibernate-osgi/src/test/resources/META-INF/persistence.xml b/hibernate-osgi/src/test/resources/META-INF/persistence.xml index 01c956dda8..5f34c3fffa 100644 --- a/hibernate-osgi/src/test/resources/META-INF/persistence.xml +++ b/hibernate-osgi/src/test/resources/META-INF/persistence.xml @@ -10,6 +10,7 @@ version="1.0"> org.hibernate.osgi.test.client.DataPoint + org.hibernate.osgi.test.client.AuditedDataPoint true diff --git a/hibernate-osgi/src/test/resources/hibernate.cfg.xml b/hibernate-osgi/src/test/resources/hibernate.cfg.xml index c3d7a1f584..a8897b49f3 100644 --- a/hibernate-osgi/src/test/resources/hibernate.cfg.xml +++ b/hibernate-osgi/src/test/resources/hibernate.cfg.xml @@ -18,5 +18,6 @@ create-drop false +