HHH-8127 OutOfMemoryError when running tests

This commit is contained in:
Strong Liu 2013-04-02 18:25:08 +08:00
parent b10f6c5d24
commit 3551bfa58f
25 changed files with 141 additions and 54 deletions

View File

@ -33,9 +33,12 @@ import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
@ -60,66 +63,77 @@ import static junit.framework.Assert.assertEquals;
public class ApplySchemaConstraintTest { public class ApplySchemaConstraintTest {
private StandardServiceRegistryImpl serviceRegistry; private StandardServiceRegistryImpl serviceRegistry;
@Before @BeforeClass
public void setUp() { public void setUp() {
serviceRegistry = createServiceRegistry(); serviceRegistry = createServiceRegistry();
} }
@AfterClass
public void tearDown(){
serviceRegistry.destroy();
serviceRegistry = null;
}
@Test @Test
public void testLengthConstraintApplied() throws Exception { public void testLengthConstraintApplied() throws Exception {
MetadataImplementor metadata = buildMetadata( serviceRegistry, Foo.class ); MetadataImplementor metadata = buildMetadata( serviceRegistry, Foo.class );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( Foo.class.getName() ), "s" ); Column column = getColumnForAttribute( metadata.getEntityBinding( Foo.class.getName() ), "s" );
assertEquals( "@Length constraint should have been applied", 10, column.getSize().getLength() ); assertEquals( "@Length constraint should have been applied", 10, column.getSize().getLength() );
sf.close();
} }
@Test @Test
public void testDigitsConstraintApplied() throws Exception { public void testDigitsConstraintApplied() throws Exception {
MetadataImplementor metadata = buildMetadata( serviceRegistry, Fubar.class ); MetadataImplementor metadata = buildMetadata( serviceRegistry, Fubar.class );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( Fubar.class.getName() ), "f" ); Column column = getColumnForAttribute( metadata.getEntityBinding( Fubar.class.getName() ), "f" );
Size size = column.getSize(); Size size = column.getSize();
assertEquals( "@Digits should have been applied", 1, size.getScale() ); assertEquals( "@Digits should have been applied", 1, size.getScale() );
assertEquals( "@Digits should have been applied", 2, size.getPrecision() ); assertEquals( "@Digits should have been applied", 2, size.getPrecision() );
sf.close();
} }
@Test @Test
public void testMinConstraintApplied() throws Exception { public void testMinConstraintApplied() throws Exception {
MetadataImplementor metadata = buildMetadata( serviceRegistry, Foobar.class ); MetadataImplementor metadata = buildMetadata( serviceRegistry, Foobar.class );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( Foobar.class.getName() ), "i" ); Column column = getColumnForAttribute( metadata.getEntityBinding( Foobar.class.getName() ), "i" );
assertEquals( "@Min constraint should have been applied", "i>=42", column.getCheckCondition() ); assertEquals( "@Min constraint should have been applied", "i>=42", column.getCheckCondition() );
sf.close();
} }
@Test @Test
public void testMaxConstraintApplied() throws Exception { public void testMaxConstraintApplied() throws Exception {
MetadataImplementor metadata = buildMetadata( serviceRegistry, Snafu.class ); MetadataImplementor metadata = buildMetadata( serviceRegistry, Snafu.class );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( Snafu.class.getName() ), "i" ); Column column = getColumnForAttribute( metadata.getEntityBinding( Snafu.class.getName() ), "i" );
assertEquals( "@Max constraint should have been applied", "i<=42", column.getCheckCondition() ); assertEquals( "@Max constraint should have been applied", "i<=42", column.getCheckCondition() );
sf.close();
} }
@Test @Test
public void testSizeConstraintApplied() throws Exception { public void testSizeConstraintApplied() throws Exception {
MetadataImplementor metadata = buildMetadata( serviceRegistry, Tarfu.class ); MetadataImplementor metadata = buildMetadata( serviceRegistry, Tarfu.class );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( Tarfu.class.getName() ), "s" ); Column column = getColumnForAttribute( metadata.getEntityBinding( Tarfu.class.getName() ), "s" );
Size size = column.getSize(); Size size = column.getSize();
assertEquals( "@Size constraint should have been applied", 42, size.getLength() ); assertEquals( "@Size constraint should have been applied", 42, size.getLength() );
sf.close();
} }
@Test @Test
public void testNotNullConstraintApplied() throws Exception { public void testNotNullConstraintApplied() throws Exception {
MetadataImplementor metadata = buildMetadata( serviceRegistry, Bohica.class ); MetadataImplementor metadata = buildMetadata( serviceRegistry, Bohica.class );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( Bohica.class.getName() ), "s" ); Column column = getColumnForAttribute( metadata.getEntityBinding( Bohica.class.getName() ), "s" );
assertEquals( "@NotNull constraint should have been applied", false, column.isNullable() ); assertEquals( "@NotNull constraint should have been applied", false, column.isNullable() );
sf.close();
} }
@Test @Test
@ -129,10 +143,11 @@ public class ApplySchemaConstraintTest {
BaseClassSingleTable.class, BaseClassSingleTable.class,
SubClassSingleTable.class SubClassSingleTable.class
); );
metadata.buildSessionFactory(); SessionFactory sf= metadata.buildSessionFactory();
Column column = getColumnForAttribute( metadata.getEntityBinding( SubClassSingleTable.class.getName() ), "s" ); Column column = getColumnForAttribute( metadata.getEntityBinding( SubClassSingleTable.class.getName() ), "s" );
assertEquals( "@NotNull constraint should not have been applied", true, column.isNullable() ); assertEquals( "@NotNull constraint should not have been applied", true, column.isNullable() );
sf.close();
} }
// TODO - Requires JoinedSubclassEntityPersister to be wired up" // TODO - Requires JoinedSubclassEntityPersister to be wired up"

View File

@ -120,6 +120,7 @@ public class TemplateTest extends BaseUnitTestCase {
public static void closeSessionFactory() { public static void closeSessionFactory() {
if ( SESSION_FACTORY != null ) { if ( SESSION_FACTORY != null ) {
SESSION_FACTORY.close(); SESSION_FACTORY.close();
SESSION_FACTORY = null;
} }
} }

View File

@ -3,6 +3,7 @@ package org.hibernate.test.annotations;
import org.junit.Test; import org.junit.Test;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -20,15 +21,19 @@ public class SafeMappingTest {
cfg.addAnnotatedClass( IncorrectEntity.class ); cfg.addAnnotatedClass( IncorrectEntity.class );
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
ServiceRegistry serviceRegistry = null; ServiceRegistry serviceRegistry = null;
SessionFactory sessionFactory = null;
try { try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ); serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
cfg.buildSessionFactory( serviceRegistry ); sessionFactory = cfg.buildSessionFactory( serviceRegistry );
fail( "Entity wo id should fail" ); fail( "Entity wo id should fail" );
} }
catch (AnnotationException e) { catch (AnnotationException e) {
//success //success
} }
finally { finally {
if( sessionFactory !=null){
sessionFactory.close();
}
if ( serviceRegistry != null ) { if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry ); ServiceRegistryBuilder.destroy( serviceRegistry );
} }

View File

@ -31,7 +31,7 @@ public class SecuredBindingTest {
p.put( "hibernate.show_sql", "true" ); p.put( "hibernate.show_sql", "true" );
ac.setProperties( p ); ac.setProperties( p );
ac.addAnnotatedClass( Plane.class ); ac.addAnnotatedClass( Plane.class );
SessionFactory sf; SessionFactory sf=null;
ServiceRegistry serviceRegistry = null; ServiceRegistry serviceRegistry = null;
try { try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( p ); serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( p );
@ -47,6 +47,9 @@ public class SecuredBindingTest {
//success //success
} }
finally { finally {
if(sf!=null){
sf.close();
}
if ( serviceRegistry != null ) { if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry ); ServiceRegistryBuilder.destroy( serviceRegistry );
} }

View File

@ -28,6 +28,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -68,12 +69,17 @@ public class AccessMappingTest {
AnnotationConfiguration cfg = new AnnotationConfiguration(); AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass( Course1.class ); cfg.addAnnotatedClass( Course1.class );
cfg.addAnnotatedClass( Student.class ); cfg.addAnnotatedClass( Student.class );
SessionFactory sf = null;
try { try {
cfg.buildSessionFactory( serviceRegistry ); sf= cfg.buildSessionFactory( serviceRegistry );
fail( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." ); fail( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
// success // success
} finally {
if(sf!=null){
sf.close();
}
} }
} }
@ -91,6 +97,7 @@ public class AccessMappingTest {
"Field access should be used.", "Field access should be used.",
tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter
); );
factory.close();
} }
@Test @Test
@ -107,6 +114,7 @@ public class AccessMappingTest {
"Property access should be used.", "Property access should be used.",
tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter
); );
factory.close();
} }
@Test @Test
@ -123,6 +131,7 @@ public class AccessMappingTest {
"Property access should be used.", "Property access should be used.",
tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter tuplizer.getIdentifierGetter() instanceof BasicPropertyAccessor.BasicGetter
); );
factory.close();
} }
@Test @Test
@ -130,12 +139,17 @@ public class AccessMappingTest {
AnnotationConfiguration cfg = new AnnotationConfiguration(); AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass( Course4.class ); cfg.addAnnotatedClass( Course4.class );
cfg.addAnnotatedClass( Student.class ); cfg.addAnnotatedClass( Student.class );
SessionFactory sf= null;
try { try {
cfg.buildSessionFactory( serviceRegistry ); sf = cfg.buildSessionFactory( serviceRegistry );
fail( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." ); fail( "@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail." );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
// success // success
} finally {
if(sf!=null){
sf.close();
}
} }
} }
@ -158,6 +172,7 @@ public class AccessMappingTest {
"Property access should be used.", "Property access should be used.",
tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter
); );
factory.close();
} }
@Test @Test
@ -179,6 +194,7 @@ public class AccessMappingTest {
"Property access should be used.", "Property access should be used.",
tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter tuplizer.getGetter( 0 ) instanceof BasicPropertyAccessor.BasicGetter
); );
factory.close();
} }
@Test @Test
@ -196,6 +212,7 @@ public class AccessMappingTest {
"Field access should be used since the default access mode gets inherited", "Field access should be used since the default access mode gets inherited",
tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter tuplizer.getIdentifierGetter() instanceof DirectPropertyAccessor.DirectGetter
); );
factory.close();
} }
@Test @Test
@ -220,6 +237,7 @@ public class AccessMappingTest {
"Field access should be used since the default access mode gets inherited", "Field access should be used since the default access mode gets inherited",
tuplizer.getGetter( 0 ) instanceof DirectPropertyAccessor.DirectGetter tuplizer.getGetter( 0 ) instanceof DirectPropertyAccessor.DirectGetter
); );
factory.close();
} }
@TestForIssue(jiraKey = "HHH-5004") @TestForIssue(jiraKey = "HHH-5004")
@ -228,6 +246,6 @@ public class AccessMappingTest {
AnnotationConfiguration cfg = new AnnotationConfiguration(); AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass( Course8.class ); cfg.addAnnotatedClass( Course8.class );
cfg.addAnnotatedClass( Student.class ); cfg.addAnnotatedClass( Student.class );
cfg.buildSessionFactory( serviceRegistry ); cfg.buildSessionFactory( serviceRegistry ).close();
} }
} }

View File

@ -56,12 +56,13 @@ public class XmlAccessTest extends BaseUnitTestCase {
// without any xml configuration we have field access // without any xml configuration we have field access
assertAccessType( factory, classUnderTest, AccessType.FIELD ); assertAccessType( factory, classUnderTest, AccessType.FIELD );
factory.close();
// now with an additional xml configuration file changing the default access type for Tourist using basic // now with an additional xml configuration file changing the default access type for Tourist using basic
configFiles = new ArrayList<String>(); configFiles = new ArrayList<String>();
configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist.xml" );
factory = buildSessionFactory( classes, configFiles ); factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
@Test @Test
@ -74,12 +75,13 @@ public class XmlAccessTest extends BaseUnitTestCase {
// without any xml configuration we have field access // without any xml configuration we have field access
assertAccessType( factory, classUnderTest, AccessType.FIELD ); assertAccessType( factory, classUnderTest, AccessType.FIELD );
factory.close();
// now with an additional xml configuration file changing the default access type for Tourist using persitence unit defaults // now with an additional xml configuration file changing the default access type for Tourist using persitence unit defaults
configFiles = new ArrayList<String>(); configFiles = new ArrayList<String>();
configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist2.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist2.xml" );
factory = buildSessionFactory( classes, configFiles ); factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
@Test @Test
@ -92,12 +94,13 @@ public class XmlAccessTest extends BaseUnitTestCase {
// without any xml configuration we have field access // without any xml configuration we have field access
assertAccessType( factory, classUnderTest, AccessType.FIELD ); assertAccessType( factory, classUnderTest, AccessType.FIELD );
factory.close();
// now with an additional xml configuration file changing the default access type for Tourist using default in entity-mappings // now with an additional xml configuration file changing the default access type for Tourist using default in entity-mappings
configFiles = new ArrayList<String>(); configFiles = new ArrayList<String>();
configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist3.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist3.xml" );
factory = buildSessionFactory( classes, configFiles ); factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
@Test @Test
@ -110,12 +113,13 @@ public class XmlAccessTest extends BaseUnitTestCase {
// without any xml configuration we have field access // without any xml configuration we have field access
assertAccessType( factory, classUnderTest, AccessType.FIELD ); assertAccessType( factory, classUnderTest, AccessType.FIELD );
factory.close();
// now with an additional xml configuration file changing the default access type for Tourist using entity level config // now with an additional xml configuration file changing the default access type for Tourist using entity level config
configFiles = new ArrayList<String>(); configFiles = new ArrayList<String>();
configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist4.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Tourist4.xml" );
factory = buildSessionFactory( classes, configFiles ); factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
@Test @Test
@ -128,6 +132,7 @@ public class XmlAccessTest extends BaseUnitTestCase {
configFiles.add( "org/hibernate/test/annotations/access/xml/Crew.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Crew.xml" );
SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.FIELD ); assertAccessType( factory, classUnderTest, AccessType.FIELD );
factory.close();
} }
@Test @Test
@ -140,6 +145,7 @@ public class XmlAccessTest extends BaseUnitTestCase {
configFiles.add( "org/hibernate/test/annotations/access/xml/RentalCar.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/RentalCar.xml" );
SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
@Test @Test
@ -152,6 +158,7 @@ public class XmlAccessTest extends BaseUnitTestCase {
configFiles.add( "org/hibernate/test/annotations/access/xml/Cook.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Cook.xml" );
SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
@Test @Test
@ -163,6 +170,7 @@ public class XmlAccessTest extends BaseUnitTestCase {
configFiles.add( "org/hibernate/test/annotations/access/xml/Boy.xml" ); configFiles.add( "org/hibernate/test/annotations/access/xml/Boy.xml" );
SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles );
assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); assertAccessType( factory, classUnderTest, AccessType.PROPERTY );
factory.close();
} }
private SessionFactoryImplementor buildSessionFactory(List<Class<?>> classesUnderTest, List<String> configFiles) { private SessionFactoryImplementor buildSessionFactory(List<Class<?>> classesUnderTest, List<String> configFiles) {

View File

@ -41,6 +41,6 @@ public class DeepCollectionElementTest extends BaseUnitTestCase {
configuration.addAnnotatedClass( A.class ); configuration.addAnnotatedClass( A.class );
configuration.addAnnotatedClass( B.class ); configuration.addAnnotatedClass( B.class );
configuration.addAnnotatedClass( C.class ); configuration.addAnnotatedClass( C.class );
configuration.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( configuration.getProperties() ) ); configuration.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( configuration.getProperties() ) ).close();
} }
} }

View File

@ -4,6 +4,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -18,19 +19,23 @@ public class DuplicateTest {
AnnotationConfiguration cfg = new AnnotationConfiguration(); AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
ServiceRegistry serviceRegistry = null; ServiceRegistry serviceRegistry = null;
SessionFactory sf = null;
try { try {
cfg.addAnnotatedClass( Flight.class ); cfg.addAnnotatedClass( Flight.class );
cfg.addAnnotatedClass( org.hibernate.test.annotations.Flight.class ); cfg.addAnnotatedClass( org.hibernate.test.annotations.Flight.class );
cfg.addResource( "org/hibernate/test/annotations/orm.xml" ); cfg.addResource( "org/hibernate/test/annotations/orm.xml" );
cfg.addResource( "org/hibernate/test/annotations/duplicatedgenerator/orm.xml" ); cfg.addResource( "org/hibernate/test/annotations/duplicatedgenerator/orm.xml" );
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ); serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
cfg.buildSessionFactory( serviceRegistry ); sf = cfg.buildSessionFactory( serviceRegistry );
Assert.fail( "Should not be able to map the same entity name twice" ); Assert.fail( "Should not be able to map the same entity name twice" );
} }
catch (AnnotationException ae) { catch (AnnotationException ae) {
//success //success
} }
finally { finally {
if (sf != null){
sf.close();
}
if ( serviceRegistry != null ) { if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry ); ServiceRegistryBuilder.destroy( serviceRegistry );
} }

View File

@ -33,6 +33,7 @@ import org.junit.Test;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl; import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
@ -76,11 +77,13 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase {
assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount()); assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
sess.close(); sess.close();
sf.close();
StandardServiceRegistryBuilder.destroy( reg );
} }
@Test @Test
public void testWithIntegrator() { public void testWithIntegrator() {
ServiceRegistry reg = new StandardServiceRegistryBuilder( StandardServiceRegistry reg = new StandardServiceRegistryBuilder(
new BootstrapServiceRegistryBuilder().with( new InvestorIntegrator() ).build() new BootstrapServiceRegistryBuilder().with( new InvestorIntegrator() ).build()
).build(); ).build();
@ -102,6 +105,8 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase {
assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount()); assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
sess.close(); sess.close();
sf.close();
StandardServiceRegistryBuilder.destroy( reg );
} }
private Investor getInvestor() { private Investor getInvestor() {

View File

@ -37,6 +37,7 @@ import org.hibernate.Hibernate;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.testing.DialectChecks; import org.hibernate.testing.DialectChecks;
@ -654,16 +655,21 @@ public class BasicHibernateAnnotationsTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testTypeDefWithoutNameAndDefaultForTypeAttributes() { public void testTypeDefWithoutNameAndDefaultForTypeAttributes() {
SessionFactory sf=null;
try { try {
Configuration config = new Configuration(); Configuration config = new Configuration();
config.addAnnotatedClass(LocalContactDetails.class); config.addAnnotatedClass(LocalContactDetails.class);
config.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() ) ); sf = config.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() ) );
fail("Did not throw expected exception"); fail("Did not throw expected exception");
} }
catch( AnnotationException ex ) { catch( AnnotationException ex ) {
assertEquals( assertEquals(
"Either name or defaultForType (or both) attribute should be set in TypeDefinition having typeClass org.hibernate.test.annotations.entity.PhoneNumberType", "Either name or defaultForType (or both) attribute should be set in TypeDefinition having typeClass org.hibernate.test.annotations.entity.PhoneNumberType",
ex.getMessage()); ex.getMessage());
} finally {
if( sf != null){
sf.close();
}
} }
} }

View File

@ -83,6 +83,7 @@ public class FetchProfileTest extends BaseUnitTestCase {
"package info should not be parsed", "package info should not be parsed",
sessionImpl.containsFetchProfileDefinition( "package-profile-1" ) sessionImpl.containsFetchProfileDefinition( "package-profile-1" )
); );
sessionImpl.close();
} }
@Test @Test
@ -93,7 +94,7 @@ public class FetchProfileTest extends BaseUnitTestCase {
config.addAnnotatedClass( Country.class ); config.addAnnotatedClass( Country.class );
try { try {
config.buildSessionFactory( serviceRegistry ); config.buildMappings();
fail(); fail();
} }
catch ( MappingException e ) { catch ( MappingException e ) {
@ -109,7 +110,7 @@ public class FetchProfileTest extends BaseUnitTestCase {
config.addAnnotatedClass( Country.class ); config.addAnnotatedClass( Country.class );
try { try {
config.buildSessionFactory( serviceRegistry ); config.buildMappings();
fail(); fail();
} }
catch ( MappingException e ) { catch ( MappingException e ) {
@ -125,7 +126,7 @@ public class FetchProfileTest extends BaseUnitTestCase {
config.addAnnotatedClass( Country.class ); config.addAnnotatedClass( Country.class );
try { try {
config.buildSessionFactory( serviceRegistry ); config.buildMappings();
fail(); fail();
} }
catch ( MappingException e ) { catch ( MappingException e ) {
@ -151,6 +152,7 @@ public class FetchProfileTest extends BaseUnitTestCase {
"fetch profile not parsed properly", "fetch profile not parsed properly",
sessionImpl.containsFetchProfileDefinition( "orders-profile" ) sessionImpl.containsFetchProfileDefinition( "orders-profile" )
); );
sessionImpl.close();
// now the same with no xml // now the same with no xml
config = new Configuration(); config = new Configuration();
@ -158,7 +160,7 @@ public class FetchProfileTest extends BaseUnitTestCase {
config.addAnnotatedClass( Order.class ); config.addAnnotatedClass( Order.class );
config.addAnnotatedClass( Country.class ); config.addAnnotatedClass( Country.class );
try { try {
config.buildSessionFactory( serviceRegistry ); config.buildMappings();
fail(); fail();
} }
catch ( MappingException e ) { catch ( MappingException e ) {
@ -186,5 +188,6 @@ public class FetchProfileTest extends BaseUnitTestCase {
"fetch profile not parsed properly", "fetch profile not parsed properly",
sessionImpl.containsFetchProfileDefinition( "package-profile-2" ) sessionImpl.containsFetchProfileDefinition( "package-profile-2" )
); );
sessionImpl.close();
} }
} }

View File

@ -25,17 +25,7 @@ import org.hibernate.testing.ServiceRegistryBuilder;
public class FkCircularityTest { public class FkCircularityTest {
private static final Logger log = Logger.getLogger( FkCircularityTest.class ); private static final Logger log = Logger.getLogger( FkCircularityTest.class );
private ServiceRegistry serviceRegistry;
@Before
public void setUp() {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
}
@After
public void tearDown() {
if (serviceRegistry != null) ServiceRegistryBuilder.destroy(serviceRegistry);
}
@Test @Test
public void testJoinedSublcassesInPK() { public void testJoinedSublcassesInPK() {
try { try {
@ -44,7 +34,7 @@ public class FkCircularityTest {
config.addAnnotatedClass(B.class); config.addAnnotatedClass(B.class);
config.addAnnotatedClass(C.class); config.addAnnotatedClass(C.class);
config.addAnnotatedClass(D.class); config.addAnnotatedClass(D.class);
config.buildSessionFactory( serviceRegistry ); config.buildMappings( );
String[] schema = config String[] schema = config
.generateSchemaCreationScript(new SQLServerDialect()); .generateSchemaCreationScript(new SQLServerDialect());
for (String s : schema) { for (String s : schema) {
@ -66,7 +56,7 @@ public class FkCircularityTest {
config.addAnnotatedClass(ClassB.class); config.addAnnotatedClass(ClassB.class);
config.addAnnotatedClass(ClassC.class); config.addAnnotatedClass(ClassC.class);
config.addAnnotatedClass(ClassD.class); config.addAnnotatedClass(ClassD.class);
config.buildSessionFactory( serviceRegistry ); config.buildMappings( );
String[] schema = config String[] schema = config
.generateSchemaCreationScript(new HSQLDialect()); .generateSchemaCreationScript(new HSQLDialect());
for (String s : schema) { for (String s : schema) {

View File

@ -58,7 +58,7 @@ public class JoinColumnOverrideTest extends BaseUnitTestCase {
config.addAnnotatedClass(Bunny.class); config.addAnnotatedClass(Bunny.class);
config.addAnnotatedClass(PointyTooth.class); config.addAnnotatedClass(PointyTooth.class);
config.addAnnotatedClass(TwinkleToes.class); config.addAnnotatedClass(TwinkleToes.class);
config.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() ) ); config.buildMappings( );
String[] schema = config String[] schema = config
.generateSchemaCreationScript(new SQLServerDialect()); .generateSchemaCreationScript(new SQLServerDialect());
for (String s : schema) { for (String s : schema) {

View File

@ -36,7 +36,7 @@ public class JoinColumnOverrideTest extends BaseUnitTestCase {
config.addAnnotatedClass(Bunny.class); config.addAnnotatedClass(Bunny.class);
config.addAnnotatedClass(PointyTooth.class); config.addAnnotatedClass(PointyTooth.class);
config.addAnnotatedClass(TwinkleToes.class); config.addAnnotatedClass(TwinkleToes.class);
config.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() ) ); config.buildMappings( );
String[] schema = config.generateSchemaCreationScript( new SQLServerDialect() ); String[] schema = config.generateSchemaCreationScript( new SQLServerDialect() );
for (String s : schema) { for (String s : schema) {
log.debug(s); log.debug(s);

View File

@ -156,7 +156,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase {
try { try {
Configuration config = new Configuration(); Configuration config = new Configuration();
config.addAnnotatedClass(Foobar.class); config.addAnnotatedClass(Foobar.class);
config.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() ) ); config.buildMappings( );
fail(); fail();
} }
catch (AnnotationException ae) { catch (AnnotationException ae) {

View File

@ -10,6 +10,7 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.EJB3NamingStrategy; import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
@ -48,7 +49,7 @@ public class NamingStrategyTest {
config.setNamingStrategy(new DummyNamingStrategy()); config.setNamingStrategy(new DummyNamingStrategy());
config.addAnnotatedClass(Address.class); config.addAnnotatedClass(Address.class);
config.addAnnotatedClass(Person.class); config.addAnnotatedClass(Person.class);
config.buildSessionFactory( serviceRegistry ); config.buildMappings();
} }
catch( Exception e ) { catch( Exception e ) {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
@ -59,12 +60,13 @@ public class NamingStrategyTest {
} }
@Test @Test
public void testWithEJB3NamingStrategy() throws Exception { public void testWithEJB3NamingStrategy() throws Exception {
SessionFactory sf = null;
try { try {
AnnotationConfiguration config = new AnnotationConfiguration(); AnnotationConfiguration config = new AnnotationConfiguration();
config.setNamingStrategy(EJB3NamingStrategy.INSTANCE); config.setNamingStrategy(EJB3NamingStrategy.INSTANCE);
config.addAnnotatedClass(A.class); config.addAnnotatedClass(A.class);
config.addAnnotatedClass(AddressEntry.class); config.addAnnotatedClass(AddressEntry.class);
config.buildSessionFactory( serviceRegistry ); sf = config.buildSessionFactory( serviceRegistry );
Mappings mappings = config.createMappings(); Mappings mappings = config.createMappings();
boolean foundIt = false; boolean foundIt = false;
@ -84,6 +86,10 @@ public class NamingStrategyTest {
e.printStackTrace(new PrintWriter(writer)); e.printStackTrace(new PrintWriter(writer));
log.debug(writer.toString()); log.debug(writer.toString());
fail(e.getMessage()); fail(e.getMessage());
} finally {
if( sf != null ){
sf.close();
}
} }
} }
@Test @Test
@ -92,7 +98,7 @@ public class NamingStrategyTest {
AnnotationConfiguration config = new AnnotationConfiguration(); AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Address.class); config.addAnnotatedClass(Address.class);
config.addAnnotatedClass(Person.class); config.addAnnotatedClass(Person.class);
config.buildSessionFactory( serviceRegistry ); config.buildMappings();
} }
catch( Exception e ) { catch( Exception e ) {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();

View File

@ -5,6 +5,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -21,18 +22,23 @@ public class OneToOneErrorTest {
.addAnnotatedClass( ShowDescription.class ); .addAnnotatedClass( ShowDescription.class );
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
ServiceRegistry serviceRegistry = null; ServiceRegistry serviceRegistry = null;
SessionFactory sessionFactory = null;
try { try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() ); serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
cfg.buildSessionFactory( serviceRegistry ); sessionFactory = cfg.buildSessionFactory( serviceRegistry );
Assert.fail( "Wrong mappedBy does not fail property" ); Assert.fail( "Wrong mappedBy does not fail property" );
} }
catch (AnnotationException e) { catch (AnnotationException e) {
//success //success
} }
finally { finally {
if(sessionFactory!=null){
sessionFactory.close();
}
if ( serviceRegistry != null ) { if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry ); ServiceRegistryBuilder.destroy( serviceRegistry );
} }
} }
} }
} }

View File

@ -43,7 +43,8 @@ public class UniqueConstraintValidationTest extends BaseUnitTestCase {
cfg.buildMappings(); cfg.buildMappings();
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build(); .applySettings(cfg.getProperties()).build();
cfg.buildSessionFactory(serviceRegistry); cfg.buildSessionFactory(serviceRegistry).close();
serviceRegistry.destroy();
} }
@Entity @Entity

View File

@ -59,8 +59,8 @@ public class ExtendsTest extends BaseUnitTestCase {
cfg.addResource( getBaseForMappings() + "extendshbm/Person.hbm.xml" ); cfg.addResource( getBaseForMappings() + "extendshbm/Person.hbm.xml" );
cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" ); cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" );
cfg.buildSessionFactory( serviceRegistry ); // cfg.buildSessionFactory( serviceRegistry );
cfg.buildMappings();
assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) ); assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );
assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) ); assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) ); assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );
@ -117,8 +117,8 @@ public class ExtendsTest extends BaseUnitTestCase {
); );
cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" ); cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" );
cfg.buildSessionFactory( serviceRegistry ); // cfg.buildSessionFactory( serviceRegistry );
cfg.buildMappings();
fail( "Should not be able to build sessionFactory without a Person" ); fail( "Should not be able to build sessionFactory without a Person" );
} }
catch ( HibernateException e ) { catch ( HibernateException e ) {
@ -134,8 +134,9 @@ public class ExtendsTest extends BaseUnitTestCase {
try { try {
cfg.addResource( getBaseForMappings() + "extendshbm/allseparateinone.hbm.xml" ); cfg.addResource( getBaseForMappings() + "extendshbm/allseparateinone.hbm.xml" );
cfg.buildSessionFactory( serviceRegistry ); // cfg.buildSessionFactory( serviceRegistry );
cfg.buildMappings();
assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) ); assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );
assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) ); assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) ); assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );

View File

@ -39,12 +39,19 @@ private ServiceRegistry serviceRegistry;
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ); serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
} }
public void tearDown(){
if(serviceRegistry!=null){
ServiceRegistryBuilder.destroy( serviceRegistry );
}
}
@Test @Test
public void testFirstTypeThenEntity(){ public void testFirstTypeThenEntity(){
cfg.addResource("org/hibernate/test/mapping/usertypes/TestEnumType.hbm.xml") cfg.addResource("org/hibernate/test/mapping/usertypes/TestEnumType.hbm.xml")
.addResource("org/hibernate/test/mapping/usertypes/TestEntity.hbm.xml"); .addResource("org/hibernate/test/mapping/usertypes/TestEntity.hbm.xml");
SessionFactory sessions=cfg.buildSessionFactory(serviceRegistry); SessionFactory sessions=cfg.buildSessionFactory(serviceRegistry);
Assert.assertNotNull(sessions); Assert.assertNotNull(sessions);
sessions.close();
} }
@Test @Test
@ -54,6 +61,7 @@ private ServiceRegistry serviceRegistry;
SessionFactory sessions=cfg.buildSessionFactory(serviceRegistry); SessionFactory sessions=cfg.buildSessionFactory(serviceRegistry);
Assert.assertNotNull(sessions); Assert.assertNotNull(sessions);
sessions.close();
} }
} }

View File

@ -27,7 +27,8 @@ public class ConfigurationValidationTest extends BaseUnitTestCase {
cfg.buildMappings(); cfg.buildMappings();
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder() ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() ).build(); .applySettings( cfg.getProperties() ).build();
cfg.buildSessionFactory( serviceRegistry ); cfg.buildSessionFactory( serviceRegistry ).close();
serviceRegistry.destroy();
} }
@Test @Test
@ -50,6 +51,7 @@ public class ConfigurationValidationTest extends BaseUnitTestCase {
) )
.build(); .build();
cfg.buildSessionFactory( serviceRegistry ); cfg.buildSessionFactory( serviceRegistry ).close();
serviceRegistry.destroy();
} }
} }

View File

@ -131,6 +131,7 @@ public class TestExpectedUsage extends BaseUnitTestCase {
} }
finally { finally {
logicalConnection.close(); logicalConnection.close();
transactionContext.getTransactionEnvironment().getSessionFactory().close();
} }
} }

View File

@ -153,6 +153,7 @@ public class BasicDrivingTest extends BaseUnitTestCase {
} }
finally { finally {
logicalConnection.close(); logicalConnection.close();
transactionContext.getTransactionEnvironment().getSessionFactory().close();
} }
} }

View File

@ -171,6 +171,7 @@ public class ManagedDrivingTest extends BaseUnitTestCase {
} }
finally { finally {
logicalConnection.close(); logicalConnection.close();
transactionContext.getTransactionEnvironment().getSessionFactory().close();
} }
} }
} }

View File

@ -76,6 +76,7 @@ public class MetadataTest extends BaseEntityManagerFunctionalTestCase {
cfg.buildMappings(); cfg.buildMappings();
SessionFactoryImplementor sfi = (SessionFactoryImplementor) cfg.buildSessionFactory( serviceRegistry() ); SessionFactoryImplementor sfi = (SessionFactoryImplementor) cfg.buildSessionFactory( serviceRegistry() );
MetamodelImpl.buildMetamodel( cfg.getClassMappings(), sfi, true ); MetamodelImpl.buildMetamodel( cfg.getClassMappings(), sfi, true );
sfi.close();
} }
@Test @Test