HHH-16551 - Re-designed test case similar to org.hibernate.orm.test.annotations.beanvalidation.DDLTest

This commit is contained in:
Cedomir Igaly 2023-05-08 19:42:10 +02:00 committed by Christian Beikov
parent f469c769f6
commit 43b54be622
1 changed files with 19 additions and 39 deletions

View File

@ -1,15 +1,11 @@
package org.hibernate.orm.test.schemaupdate;
import java.sql.Types;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.testing.orm.jpa.PersistenceUnitDescriptorAdapter;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorColumn;
@ -21,51 +17,35 @@ import jakarta.persistence.Table;
import static jakarta.persistence.DiscriminatorType.CHAR;
import static jakarta.persistence.InheritanceType.SINGLE_TABLE;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@JiraKey("HHH-16551")
public class CreateCharDiscriminatorTest {
public class CreateCharDiscriminatorTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
@org.junit.Test
@JiraKey("HHH-16551")
public void testCreateDiscriminatorCharColumnSize() {
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
PersistentClass classMapping = metadata().getEntityBinding( Parent.class.getName() );
final var discriminatorColumn = classMapping.getDiscriminator().getColumns().get( 0 );
assertEquals( discriminatorColumn.getLength(), 1L );
}
@Override
public List<String> getManagedClassNames() {
return List.of( Parent.class.getName() );
protected void addSettings(Map<String, Object> settings) {
settings.put( "jakarta.persistence.validation.mode", "ddl" );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Parent.class
};
final var settings = Map.of( HBM2DDL_AUTO, "create-drop" );
try (var factory = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build();
var manager = factory.createEntityManager()) {
manager.unwrap( Session.class ).doWork( conn -> {
try (var rs = conn.getMetaData().getColumns( null, null, null, null )) {
if ( rs.next() ) {
do {
if ( "parent".equalsIgnoreCase( rs.getString( "TABLE_NAME" ) ) &&
"discr".equalsIgnoreCase( rs.getString( "COLUMN_NAME" ) ) ) {
assertEquals( Types.CHAR, rs.getInt( "DATA_TYPE" ) );
assertEquals( 1, rs.getInt( "COLUMN_SIZE" ) );
}
} while ( rs.next() );
}
else {
fail( "Table and/or columns has not been created, why?!?" );
}
}
} );
}
}
@Entity
@Table(name = "parent")
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "discr", discriminatorType = CHAR)
@DiscriminatorColumn(name = "discr", discriminatorType = CHAR, length = 2)
@DiscriminatorValue("*")
public static class Parent {