HHH-10365 - Add Envers native and JPA test cases.
This commit is contained in:
parent
60b3ce796b
commit
ab0b44c569
|
@ -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() );
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
version="1.0">
|
||||
<persistence-unit name="hibernate-osgi-test">
|
||||
<class>org.hibernate.osgi.test.client.DataPoint</class>
|
||||
<class>org.hibernate.osgi.test.client.AuditedDataPoint</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
|
||||
|
|
|
@ -18,5 +18,6 @@
|
|||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
||||
<property name="hibernate.cache.use_second_level_cache">false</property>
|
||||
<mapping class="org.hibernate.osgi.test.client.DataPoint"/>
|
||||
<mapping class="org.hibernate.osgi.test.client.AuditedDataPoint"/>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
|
|
Loading…
Reference in New Issue