HHH-12454 - Add tests for issue

This commit is contained in:
Andrea Boriero 2018-04-06 12:27:19 +01:00 committed by Steve Ebersole
parent c7c919f54a
commit b463c809d6
4 changed files with 200 additions and 5 deletions

View File

@ -0,0 +1,88 @@
/*
* 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.test.idgen.namescope;
import java.util.Properties;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.junit.Test;
/**
* @author Andrea Boriero
*/
public class IdGeneratorNamesGlobalScopeTest {
@Test(expected = IllegalArgumentException.class)
public void testNoSequenceGenratorNameClash() {
buildSessionFactory();
}
@Entity(name = "FirstEntity")
@TableGenerator(
name = "table-generator",
table = "table_identifier_2",
pkColumnName = "identifier",
valueColumnName = "value",
allocationSize = 5
)
public static class FirstEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "table-generator")
private Long id;
}
@Entity(name = "SecondEntity")
@TableGenerator(
name = "table-generator",
table = "table_identifier",
pkColumnName = "identifier",
valueColumnName = "value",
allocationSize = 5
)
public static class SecondEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "table-generator")
private Long id;
}
private void buildSessionFactory() {
Configuration configuration = new Configuration();
final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
builder.applyClassLoader( getClass().getClassLoader() );
BootstrapServiceRegistry bootRegistry = builder.build();
StandardServiceRegistryImpl serviceRegistry = buildServiceRegistry( bootRegistry, configuration );
configuration.addAnnotatedClass( FirstEntity.class );
configuration.addAnnotatedClass( SecondEntity.class );
configuration.buildSessionFactory( serviceRegistry );
}
protected StandardServiceRegistryImpl buildServiceRegistry(BootstrapServiceRegistry bootRegistry, Configuration configuration) {
Properties properties = new Properties();
properties.putAll( configuration.getProperties() );
properties.put( AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, "true" );
ConfigurationHelper.resolvePlaceHolders( properties );
StandardServiceRegistryBuilder cfgRegistryBuilder = configuration.getStandardServiceRegistryBuilder();
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder( bootRegistry, cfgRegistryBuilder.getAggregatedCfgXml() )
.applySettings( properties );
return (StandardServiceRegistryImpl) registryBuilder.build();
}
}

View File

@ -0,0 +1,89 @@
/*
* 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>.
*/
/*
* 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.test.idgen.namescope;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertThat;
/**
* @author Andrea Boriero
*/
public class IdGeneratorNamesLocalScopeTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { FirstEntity.class, SecondEntity.class };
}
@Test
public void testNoSequenceGenratorNameClash() {
final FirstEntity first = new FirstEntity();
final SecondEntity second = new SecondEntity();
doInHibernate( this::sessionFactory, session -> {
session.persist( first );
session.persist( second );
} );
assertThat( first.getId(), is( 2L ) );
assertThat( second.getId(), is( 11L ) );
}
@Entity(name = "FirstEntity")
@TableGenerator(
name = "table-generator",
table = "table_identifier_2",
pkColumnName = "identifier",
valueColumnName = "value",
allocationSize = 5,
initialValue = 1
)
public static class FirstEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "table-generator")
private Long id;
public Long getId() {
return id;
}
}
@Entity(name = "SecondEntity")
@TableGenerator(
name = "table-generator",
table = "table_identifier",
pkColumnName = "identifier",
valueColumnName = "value",
allocationSize = 5,
initialValue = 10
)
public static class SecondEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "table-generator")
private Long id;
public Long getId() {
return id;
}
}
}

View File

@ -13,6 +13,9 @@ import javax.persistence.Id;
import javax.persistence.TableGenerator;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -26,11 +29,18 @@ public class TableGeneratorMultipleDefinitionTest extends BaseUnitTestCase {
@Test(expected = IllegalArgumentException.class)
public void testDuplicateGeneratorNamesDefinition() {
new MetadataSources().addAnnotatedClass( TestEntity1.class )
.addAnnotatedClass( TestEntity2.class )
.buildMetadata()
.buildSessionFactory().close();
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
.applySetting( AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, "true" )
.build();
try {
new MetadataSources( ssr )
.addAnnotatedClass( TestEntity2.class )
.addAnnotatedClass( TestEntity1.class )
.buildMetadata();
}
finally {
StandardServiceRegistryBuilder.destroy( ssr );
}
}
@Entity(name = "TestEntity1")

View File

@ -12,6 +12,9 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
@ -23,6 +26,11 @@ import org.junit.Test;
@TestForIssue(jiraKey = "HHH-12157")
public class TableGeneratorVisibilityTest extends BaseCoreFunctionalTestCase {
@Override
protected void configure(Configuration configuration) {
configuration.setProperty( AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, "true" );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {