diff --git a/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java b/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java index e2e723c9fa..1344f78efa 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/sequence/HiLoSequenceMismatchStrategyTest.java @@ -8,7 +8,15 @@ package org.hibernate.test.idgen.enhanced.sequence; import java.sql.Connection; import java.sql.SQLException; +import java.sql.SQLSyntaxErrorException; import java.sql.Statement; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; @@ -17,33 +25,36 @@ import org.hibernate.id.SequenceMismatchStrategy; import org.hibernate.id.enhanced.HiLoOptimizer; import org.hibernate.id.enhanced.SequenceStyleGenerator; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.AfterClassOnce; + import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Assert; import org.junit.Test; + import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability; import static org.junit.Assert.fail; /** * @author Nathan Xu */ -@TestForIssue( jiraKey = "HHH-13783" ) -@RequiresDialectFeature( DialectChecks.SupportsSequences.class ) +@TestForIssue(jiraKey = "HHH-13783") +@RequiresDialectFeature(DialectChecks.SupportsSequences.class) public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase { - private String sequenceName = "ID_SEQ_HILO_SEQ"; + public final static String sequenceName = "ID_SEQ_HILO_SEQ"; @Override - protected String[] getMappings() { - return new String[] { "idgen/enhanced/sequence/HiLo.hbm.xml" }; + protected Class[] getAnnotatedClasses() { + return new Class[] { TestEntity.class }; } - @Override protected void configure(Configuration configuration) { - configuration.setProperty(AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, SequenceMismatchStrategy.EXCEPTION.toString()); + configuration.setProperty( + AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, + SequenceMismatchStrategy.EXCEPTION.toString() + ); } @Override @@ -54,10 +65,15 @@ public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase String[] dropSequenceStatements = getDialect().getDropSequenceStrings( sequenceName ); String[] createSequenceStatements = getDialect().getCreateSequenceStrings( sequenceName, 1, 1 ); - try ( Connection connection = connectionProvider.getConnection(); - Statement statement = connection.createStatement() ) { - for ( String dropSequenceStatement : dropSequenceStatements ) { - statement.execute( dropSequenceStatement ); + try (Connection connection = connectionProvider.getConnection(); + Statement statement = connection.createStatement()) { + try { + for ( String dropSequenceStatement : dropSequenceStatements ) { + statement.execute( dropSequenceStatement ); + } + } + catch (SQLSyntaxErrorException e) { + //ignore } for ( String createSequenceStatement : createSequenceStatements ) { statement.execute( createSequenceStatement ); @@ -68,38 +84,38 @@ public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase } } - @AfterClassOnce - @SuppressWarnings( {"UnusedDeclaration"}) - protected void releaseSessionFactory() { - DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl(); - connectionProvider.configure( Environment.getProperties() ); - - String[] dropSequenceStatements = getDialect().getDropSequenceStrings( sequenceName ); - try ( Connection connection = connectionProvider.getConnection(); - Statement statement = connection.createStatement() ) { - for ( String dropSequenceStatement : dropSequenceStatements ) { - statement.execute( dropSequenceStatement ); - } - } - catch (SQLException e) { - fail( e.getMessage() ); - } - super.releaseSessionFactory(); - } @Test public void testSequenceMismatchStrategyNotApplied() { - EntityPersister persister = sessionFactory().getEntityPersister( Entity.class.getName() ); + EntityPersister persister = sessionFactory().getEntityPersister( TestEntity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); - SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); + SequenceStyleGenerator generator = (SequenceStyleGenerator) persister.getIdentifierGenerator(); assertClassAssignability( HiLoOptimizer.class, generator.getOptimizer().getClass() ); String sequenceName = generator.getDatabaseStructure().getName(); - Assert.assertEquals(this.sequenceName, sequenceName); + Assert.assertEquals( this.sequenceName, sequenceName ); int incrementSize = generator.getOptimizer().getIncrementSize(); - Assert.assertNotEquals(1, incrementSize); + Assert.assertNotEquals( 1, incrementSize ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_sequence_generator") + @GenericGenerator( + name = "hilo_sequence_generator", + strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", + parameters = { + @Parameter(name = "sequence_name", value = sequenceName), + @Parameter(name = "initial_value", value = "1"), + @Parameter(name = "increment_size", value = "10"), + @Parameter(name = "optimizer", value = "hilo") + }) + private Long id; + + private String aString; } }