HHH-13783 Fix test failing on Oracle

This commit is contained in:
Andrea Boriero 2020-01-27 11:30:11 +00:00
parent 0c8e3056dc
commit c5581e6759
1 changed files with 50 additions and 34 deletions

View File

@ -8,7 +8,15 @@ package org.hibernate.test.idgen.enhanced.sequence;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.sql.Statement; 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.AvailableSettings;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; 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.HiLoOptimizer;
import org.hibernate.id.enhanced.SequenceStyleGenerator; import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.AfterClassOnce;
import org.hibernate.testing.DialectChecks; import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability; import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
* @author Nathan Xu * @author Nathan Xu
*/ */
@TestForIssue( jiraKey = "HHH-13783" ) @TestForIssue(jiraKey = "HHH-13783")
@RequiresDialectFeature( DialectChecks.SupportsSequences.class ) @RequiresDialectFeature(DialectChecks.SupportsSequences.class)
public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase { public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase {
private String sequenceName = "ID_SEQ_HILO_SEQ"; public final static String sequenceName = "ID_SEQ_HILO_SEQ";
@Override @Override
protected String[] getMappings() { protected Class<?>[] getAnnotatedClasses() {
return new String[] { "idgen/enhanced/sequence/HiLo.hbm.xml" }; return new Class[] { TestEntity.class };
} }
@Override
protected void configure(Configuration configuration) { 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 @Override
@ -54,10 +65,15 @@ public class HiLoSequenceMismatchStrategyTest extends BaseCoreFunctionalTestCase
String[] dropSequenceStatements = getDialect().getDropSequenceStrings( sequenceName ); String[] dropSequenceStatements = getDialect().getDropSequenceStrings( sequenceName );
String[] createSequenceStatements = getDialect().getCreateSequenceStrings( sequenceName, 1, 1 ); String[] createSequenceStatements = getDialect().getCreateSequenceStrings( sequenceName, 1, 1 );
try ( Connection connection = connectionProvider.getConnection(); try (Connection connection = connectionProvider.getConnection();
Statement statement = connection.createStatement() ) { Statement statement = connection.createStatement()) {
for ( String dropSequenceStatement : dropSequenceStatements ) { try {
statement.execute( dropSequenceStatement ); for ( String dropSequenceStatement : dropSequenceStatements ) {
statement.execute( dropSequenceStatement );
}
}
catch (SQLSyntaxErrorException e) {
//ignore
} }
for ( String createSequenceStatement : createSequenceStatements ) { for ( String createSequenceStatement : createSequenceStatements ) {
statement.execute( createSequenceStatement ); 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 @Test
public void testSequenceMismatchStrategyNotApplied() { public void testSequenceMismatchStrategyNotApplied() {
EntityPersister persister = sessionFactory().getEntityPersister( Entity.class.getName() ); EntityPersister persister = sessionFactory().getEntityPersister( TestEntity.class.getName() );
assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() );
SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); SequenceStyleGenerator generator = (SequenceStyleGenerator) persister.getIdentifierGenerator();
assertClassAssignability( HiLoOptimizer.class, generator.getOptimizer().getClass() ); assertClassAssignability( HiLoOptimizer.class, generator.getOptimizer().getClass() );
String sequenceName = generator.getDatabaseStructure().getName(); String sequenceName = generator.getDatabaseStructure().getName();
Assert.assertEquals(this.sequenceName, sequenceName); Assert.assertEquals( this.sequenceName, sequenceName );
int incrementSize = generator.getOptimizer().getIncrementSize(); 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;
} }
} }