HHH-8224 test case
This commit is contained in:
parent
ba59bbf119
commit
8cb6f073da
|
@ -1,12 +1,41 @@
|
|||
// $Id$
|
||||
package org.hibernate.test.annotations.namingstrategy;
|
||||
import org.hibernate.cfg.EJB3NamingStrategy;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.cfg.DefaultNamingStrategy;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class DummyNamingStrategy extends EJB3NamingStrategy {
|
||||
public class DummyNamingStrategy extends DefaultNamingStrategy {
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
@Override
|
||||
public String tableName(String tableName) {
|
||||
return "T" + tableName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String foreignKeyName(String sourceTableName, List<String> sourceColumnNames,
|
||||
String targetTableName, List<String> targetColumnNames) {
|
||||
return "F" + counter++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uniqueKeyName(String tableName, List<String> columnNames) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for ( String columnName : columnNames ) {
|
||||
sb.append( columnName );
|
||||
}
|
||||
return "U" + sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String indexName(String tableName, List<String> columnNames) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for ( String columnName : columnNames ) {
|
||||
sb.append( columnName );
|
||||
}
|
||||
return "I" + sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,34 +1,38 @@
|
|||
// $Id$
|
||||
package org.hibernate.test.annotations.namingstrategy;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.EJB3NamingStrategy;
|
||||
import org.hibernate.cfg.Mappings;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.mapping.Table;
|
||||
import org.hibernate.metamodel.MetadataBuilder;
|
||||
import org.hibernate.metamodel.MetadataSources;
|
||||
import org.hibernate.metamodel.spi.MetadataImplementor;
|
||||
import org.hibernate.metamodel.spi.relational.Database;
|
||||
import org.hibernate.metamodel.spi.relational.Schema;
|
||||
import org.hibernate.metamodel.spi.relational.TableSpecification;
|
||||
import org.hibernate.test.util.SchemaUtil;
|
||||
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestMethod;
|
||||
import org.hibernate.testing.junit4.TestSessionFactoryHelper;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test harness for ANN-716.
|
||||
*
|
||||
* @author Hardy Ferentschik
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class NamingStrategyTest extends BaseCoreFunctionalTestMethod {
|
||||
private static final Logger log = Logger.getLogger( NamingStrategyTest.class );
|
||||
|
@ -59,6 +63,36 @@ public class NamingStrategyTest extends BaseCoreFunctionalTestMethod {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstraintsCustomNamingStrategy() throws Exception {
|
||||
// ignoring Configuration here -- this is really specific to metamodel
|
||||
MetadataSources sources = new MetadataSources( new BootstrapServiceRegistryBuilder().build() );
|
||||
sources.addAnnotatedClass( Address.class );
|
||||
sources.addAnnotatedClass( Person.class );
|
||||
MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
|
||||
metadataBuilder.with( new DummyNamingStrategy() );
|
||||
MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();
|
||||
|
||||
TableSpecification table = SchemaUtil.getTable( Person.class, metadata );
|
||||
// UK, DummyNamingStrategy generated
|
||||
assertTrue( SchemaUtil.hasUniqueKey( table, "Uname" ) );
|
||||
// UK, DummyNamingStrategy generated
|
||||
assertTrue( SchemaUtil.hasUniqueKey( table, "Ubio" ) );
|
||||
// UK, explicit name
|
||||
assertTrue( SchemaUtil.hasUniqueKey( table, "uk_nickname" ) );
|
||||
// UK, explicit name, created by unique @Index
|
||||
assertTrue( SchemaUtil.hasUniqueKey( table, "uk_color" ) );
|
||||
// Index, DummyNamingStrategy generated
|
||||
assertTrue( SchemaUtil.hasIndex( table, "IfavoriteSong" ) );
|
||||
// Index, explicit name
|
||||
assertTrue( SchemaUtil.hasIndex( table, "idx_band" ) );
|
||||
|
||||
table = SchemaUtil.getTable( "person_address", metadata );
|
||||
// FKs in JoinTable, DummyNamingStrategy generated
|
||||
assertTrue( SchemaUtil.hasForeignKey( table, "F0" ) );
|
||||
assertTrue( SchemaUtil.hasForeignKey( table, "F1" ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
@FailureExpectedWithNewMetamodel
|
||||
public void testWithEJB3NamingStrategy() throws Exception {
|
||||
|
|
|
@ -2,15 +2,39 @@
|
|||
package org.hibernate.test.annotations.namingstrategy;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Index;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
uniqueConstraints = {@UniqueConstraint(columnNames = "bio"),
|
||||
@UniqueConstraint(name = "uk_nickname", columnNames = "nickname")},
|
||||
indexes = {@Index(columnList = "favoriteColor", unique = true, name = "uk_color"),
|
||||
@Index(columnList = "favoriteBand", name = "idx_band"),
|
||||
@Index(columnList = "favoriteSong")})
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@Column(unique = true)
|
||||
private String name;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String bio;
|
||||
|
||||
private String favoriteColor;
|
||||
|
||||
private String favoriteBand;
|
||||
|
||||
private String favoriteSong;
|
||||
|
||||
@OneToMany(mappedBy = "person")
|
||||
private Set<Address> addresses = new HashSet<Address>();
|
||||
|
@ -30,4 +54,44 @@ public class Person {
|
|||
public void setAddresses(Set<Address> addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
public void setNickname(String nickname) {
|
||||
this.nickname = nickname;
|
||||
}
|
||||
|
||||
public String getBio() {
|
||||
return bio;
|
||||
}
|
||||
|
||||
public void setBio(String bio) {
|
||||
this.bio = bio;
|
||||
}
|
||||
|
||||
public String getFavoriteColor() {
|
||||
return favoriteColor;
|
||||
}
|
||||
|
||||
public void setFavoriteColor(String favoriteColor) {
|
||||
this.favoriteColor = favoriteColor;
|
||||
}
|
||||
|
||||
public String getFavoriteBand() {
|
||||
return favoriteBand;
|
||||
}
|
||||
|
||||
public void setFavoriteBand(String favoriteBand) {
|
||||
this.favoriteBand = favoriteBand;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,10 +31,13 @@ import org.hibernate.metamodel.spi.MetadataImplementor;
|
|||
import org.hibernate.metamodel.spi.binding.EntityBinding;
|
||||
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
|
||||
import org.hibernate.metamodel.spi.relational.Column;
|
||||
import org.hibernate.metamodel.spi.relational.ForeignKey;
|
||||
import org.hibernate.metamodel.spi.relational.Identifier;
|
||||
import org.hibernate.metamodel.spi.relational.Index;
|
||||
import org.hibernate.metamodel.spi.relational.PrimaryKey;
|
||||
import org.hibernate.metamodel.spi.relational.Schema;
|
||||
import org.hibernate.metamodel.spi.relational.TableSpecification;
|
||||
import org.hibernate.metamodel.spi.relational.UniqueKey;
|
||||
|
||||
/**
|
||||
* Check that the Hibernate metamodel contains some database objects
|
||||
|
@ -120,4 +123,31 @@ public class SchemaUtil {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean hasUniqueKey(TableSpecification table, String keyName) {
|
||||
for ( UniqueKey uk : table.getUniqueKeys() ) {
|
||||
if ( uk.getName().equals( keyName ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasForeignKey(TableSpecification table, String keyName) {
|
||||
for ( ForeignKey fk : table.getForeignKeys() ) {
|
||||
if ( fk.getName().equals( keyName ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasIndex(TableSpecification table, String indexName) {
|
||||
for ( Index index : table.getIndexes() ) {
|
||||
if ( index.getName().equals( indexName ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue