From 1df6c45f8a1a16d16e9a33938787bd2a0c8f1b8c Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 19 Aug 2014 03:12:41 +0200 Subject: [PATCH] Added regression test for HHH-9327 (cherry picked from commit 3da903f064e9c4963efd1f2c21d69af70e4aa1f1) --- .../regression/LocalizedEntity.java | 110 ++++++++++++++++++ .../regression/MyNamingStrategy.java | 47 ++++++++ .../regression/NamingStrategyTest.java | 43 +++++++ .../namingstrategy/regression/Workflow.java | 95 +++++++++++++++ 4 files changed, 295 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/LocalizedEntity.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/MyNamingStrategy.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/NamingStrategyTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/Workflow.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/LocalizedEntity.java b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/LocalizedEntity.java new file mode 100644 index 0000000000..d51387983a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/LocalizedEntity.java @@ -0,0 +1,110 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.namingstrategy.regression; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class LocalizedEntity implements Serializable, Cloneable { + + private static final long serialVersionUID = -8539302606114365372L; + + private String name; + private String description; + + /** + * Empty Constructor + */ + public LocalizedEntity() { + super(); + } + + /** + * @param name + * @param description + */ + public LocalizedEntity(String name, String description) { + super(); + this.name = name; + this.description = description; + } + + @Column(name = "NAME", nullable = false, length = 255) + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + @Column(name = "DESCRIPTION", nullable = false, length = 2500) + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public LocalizedEntity clone() { + return new LocalizedEntity( this.name, this.description ); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ( ( description == null ) ? 0 : description.hashCode() ); + result = prime * result + ( ( name == null ) ? 0 : name.hashCode() ); + return result; + } + + @Override + public boolean equals(Object obj) { + if ( this == obj ) + return true; + if ( obj == null ) + return false; + if ( getClass() != obj.getClass() ) + return false; + LocalizedEntity other = (LocalizedEntity) obj; + if ( description == null ) { + if ( other.description != null ) + return false; + } + else if ( !description.equals( other.description ) ) + return false; + if ( name == null ) { + if ( other.name != null ) + return false; + } + else if ( !name.equals( other.name ) ) + return false; + return true; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/MyNamingStrategy.java b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/MyNamingStrategy.java new file mode 100644 index 0000000000..37066f9fd5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/MyNamingStrategy.java @@ -0,0 +1,47 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.namingstrategy.regression; + +import org.hibernate.cfg.EJB3NamingStrategy; + +public class MyNamingStrategy extends EJB3NamingStrategy { + + private static final long serialVersionUID = -5713413771290957530L; + + @Override + public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) { + String name = null; + + if ( "localized".equals( propertyName ) ) { + // NOTE: The problem is that ownerEntity is not the fully qualified class name since 4.2.15 anymore + name = ownerEntity.replaceAll( "\\.", "_" ).toUpperCase() + "_LOCALIZED"; + } + + if ( name == null ) { + name = super.collectionTableName( ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName ); + } + + return name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/NamingStrategyTest.java b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/NamingStrategyTest.java new file mode 100644 index 0000000000..7fa9fc25a1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/NamingStrategyTest.java @@ -0,0 +1,43 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package org.hibernate.test.namingstrategy.regression; + +import static org.junit.Assert.assertEquals; + +import org.hibernate.cfg.Configuration; +import org.hibernate.mapping.Map; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class NamingStrategyTest extends BaseCoreFunctionalTestCase { + + @Override + public void configure(Configuration cfg) { + super.configure( cfg ); + cfg.setNamingStrategy( new MyNamingStrategy() ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Workflow.class }; + } + + @Test + @TestForIssue(jiraKey = "HHH-9327") + public void testNamingStrategyRegression() throws Exception { + String expectedName = Workflow.class.getName().replaceAll( "\\.", "_" ).toUpperCase(); + expectedName += "_LOCALIZED"; + + PersistentClass classMapping = configuration().getClassMapping( Workflow.class.getName() ); + Property property = classMapping.getProperty( "localized" ); + Map map = (Map) property.getValue(); + assertEquals( expectedName, map.getCollectionTable().getName() ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/Workflow.java b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/Workflow.java new file mode 100644 index 0000000000..7054d1b774 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/namingstrategy/regression/Workflow.java @@ -0,0 +1,95 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.namingstrategy.regression; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import javax.persistence.Basic; +import javax.persistence.CollectionTable; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.MapKeyColumn; + +@Entity +public class Workflow implements Serializable { + + private static final long serialVersionUID = 7504955999101475681L; + + private Long id; + private Locale defaultLanguage; + private Set supportedLocales = new HashSet(); + private Map localized = new HashMap(); + + public Workflow() { + } + + @Id + @GeneratedValue + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Basic(optional = false) + public Locale getDefaultLanguage() { + return defaultLanguage; + } + + public void setDefaultLanguage(Locale defaultLanguage) { + this.defaultLanguage = defaultLanguage; + } + + @ElementCollection + @CollectionTable(joinColumns = { @JoinColumn(name = "ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false) }) + public Set getSupportedLocales() { + return supportedLocales; + } + + public void setSupportedLocales(Set supportedLocales) { + this.supportedLocales = supportedLocales; + } + + @ElementCollection + @CollectionTable(joinColumns = { @JoinColumn(name = "ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false) }) + @MapKeyColumn(name = "LANGUAGE_CODE", nullable = false, insertable = false, updatable = false) + public Map getLocalized() { + return localized; + } + + public void setLocalized(Map localized) { + this.localized = localized; + } + +}