HHH-9467 : Cannot enable NamingStrategyDelegator implementations using standalone schema tools

This commit is contained in:
Gail Badner 2015-01-05 12:04:44 -08:00
parent 1b9cd19ca7
commit 4bf3b162ad
9 changed files with 377 additions and 2 deletions

View File

@ -46,6 +46,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.internal.FormatStyle;
@ -504,6 +505,8 @@ public class SchemaExport {
String propFile = null;
boolean format = false;
String delim = null;
boolean hasNaming = false;
boolean hasNamingDelegator = false;
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].startsWith( "--" ) ) {
@ -541,11 +544,21 @@ public class SchemaExport {
cfg.configure( args[i].substring( 9 ) );
}
else if ( args[i].startsWith( "--naming=" ) ) {
hasNaming = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategy(
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) )
.newInstance()
);
}
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
hasNamingDelegator = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
.newInstance()
);
}
}
else {
String filename = args[i];
@ -592,6 +605,12 @@ public class SchemaExport {
}
}
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
if ( namingSpecified && namingDelegatorSpecified ) {
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
}
}
/**
* Returns a List of all Exceptions which occured during the export.
*

View File

@ -36,6 +36,7 @@ import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
@ -82,6 +83,7 @@ public class SchemaExportTask extends MatchingTask {
private boolean haltOnError;
private String delimiter;
private String namingStrategy;
private String namingStrategyDelegator;
public void addFileset(FileSet set) {
fileSets.add(set);
@ -207,11 +209,20 @@ public class SchemaExportTask extends MatchingTask {
private Configuration getConfiguration() throws Exception {
Configuration cfg = new Configuration();
if ( namingStrategy != null && namingStrategyDelegator != null ) {
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
}
if (namingStrategy!=null) {
cfg.setNamingStrategy(
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
);
}
else if ( namingStrategyDelegator != null) {
cfg.setNamingStrategyDelegator(
( NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
);
}
if (configurationFile != null) {
cfg.configure( configurationFile );
}
@ -249,6 +260,10 @@ public class SchemaExportTask extends MatchingTask {
this.namingStrategy = namingStrategy;
}
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
this.namingStrategyDelegator = namingStrategyDelegator;
}
public void setHaltonerror(boolean haltOnError) {
this.haltOnError = haltOnError;
}

View File

@ -40,6 +40,7 @@ import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.engine.jdbc.internal.Formatter;
@ -121,6 +122,9 @@ public class SchemaUpdate {
// If true then execute db updates, otherwise just generate and display updates
boolean doUpdate = true;
String propFile = null;
String outFile = null;
boolean hasNaming = false;
boolean hasNamingDelegator = false;
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].startsWith( "--" ) ) {
@ -136,11 +140,24 @@ public class SchemaUpdate {
else if ( args[i].startsWith( "--text" ) ) {
doUpdate = false;
}
else if ( args[i].startsWith( "--output=" ) ) {
outFile = args[i].substring( 9 );
}
else if ( args[i].startsWith( "--naming=" ) ) {
hasNaming = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategy(
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
);
}
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
hasNamingDelegator = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
.newInstance()
);
}
}
else {
cfg.addFile( args[i] );
@ -157,7 +174,9 @@ public class SchemaUpdate {
StandardServiceRegistryImpl serviceRegistry = createServiceRegistry( cfg.getProperties() );
try {
new SchemaUpdate( serviceRegistry, cfg ).execute( script, doUpdate );
final SchemaUpdate schemaUpdate = new SchemaUpdate( serviceRegistry, cfg );
schemaUpdate.setOutputFile( outFile );
schemaUpdate.execute( script, doUpdate );
}
finally {
serviceRegistry.destroy();
@ -169,6 +188,12 @@ public class SchemaUpdate {
}
}
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
if ( namingSpecified && namingDelegatorSpecified ) {
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
}
}
/**
* Execute the schema updates
*

View File

@ -36,6 +36,7 @@ import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
@ -76,7 +77,7 @@ public class SchemaUpdateTask extends MatchingTask {
private boolean haltOnError;
private String delimiter;
private String namingStrategy;
private String namingStrategyDelegator;
public void addFileset(FileSet set) {
fileSets.add(set);
@ -170,11 +171,20 @@ public class SchemaUpdateTask extends MatchingTask {
private Configuration getConfiguration() throws Exception {
Configuration cfg = new Configuration();
if ( namingStrategy != null && namingStrategyDelegator != null ) {
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
}
if (namingStrategy!=null) {
cfg.setNamingStrategy(
(NamingStrategy) ReflectHelper.classForName( namingStrategy ).newInstance()
);
}
else if ( namingStrategyDelegator != null) {
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
);
}
if (configurationFile!=null) {
cfg.configure( configurationFile );
}
@ -213,6 +223,10 @@ public class SchemaUpdateTask extends MatchingTask {
this.namingStrategy = namingStrategy;
}
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
this.namingStrategyDelegator = namingStrategyDelegator;
}
public File getOutputFile() {
return outputFile;
}

View File

@ -34,6 +34,7 @@ import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.internal.CoreMessageLogger;
@ -87,6 +88,8 @@ public class SchemaValidator {
Configuration cfg = new Configuration();
String propFile = null;
boolean hasNaming = false;
boolean hasNamingDelegator = false;
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].startsWith( "--" ) ) {
@ -97,10 +100,21 @@ public class SchemaValidator {
cfg.configure( args[i].substring( 9 ) );
}
else if ( args[i].startsWith( "--naming=" ) ) {
hasNaming = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategy(
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
);
}
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
hasNamingDelegator = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
.newInstance()
);
}
}
else {
cfg.addFile( args[i] );
@ -129,6 +143,12 @@ public class SchemaValidator {
}
}
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
if ( namingSpecified && namingDelegatorSpecified ) {
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
}
}
/**
* Perform the validations.
*/

View File

@ -36,6 +36,7 @@ import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
@ -70,6 +71,7 @@ public class SchemaValidatorTask extends MatchingTask {
private File propertiesFile;
private File configurationFile;
private String namingStrategy;
private String namingStrategyDelegator;
public void addFileset(FileSet set) {
fileSets.add(set);
@ -143,11 +145,20 @@ public class SchemaValidatorTask extends MatchingTask {
private Configuration getConfiguration() throws Exception {
Configuration cfg = new Configuration();
if ( namingStrategy != null && namingStrategyDelegator != null ) {
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
}
if (namingStrategy!=null) {
cfg.setNamingStrategy(
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
);
}
else if ( namingStrategyDelegator != null) {
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
);
}
if (configurationFile!=null) {
cfg.configure( configurationFile );
}
@ -182,4 +193,7 @@ public class SchemaValidatorTask extends MatchingTask {
this.namingStrategy = namingStrategy;
}
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
this.namingStrategyDelegator = namingStrategyDelegator;
}
}

View File

@ -0,0 +1,40 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, 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.schematoolsnaming;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name="Mtx")
public class Matrix {
@Id
private Integer id;
@ElementCollection
private Set<Integer> mvalues = new HashSet<Integer>();
}

View File

@ -0,0 +1,218 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, 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.schematoolsnaming;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Test;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2ddl.SchemaValidator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests that using {@code --namingdelegator=<naming_delegator_class_name>} works when
* using standalone schema tools.
*
* @author Gail Badner
*/
@TestForIssue(jiraKey = "HHH-9467")
public class StandaloneSchemaToolsNamingTest extends BaseUnitTestCase {
private final java.io.File OUTFILE = new java.io.File( "StandaloneSchemaToolsNamingTest.out" );
@Test
public void testDefaultNamingStrategyDelegator() {
// using the default NamingStrategyDelegator, the collection table name should be "Matrix_mvalues"
doValidTest(
new String[] {
"--output=" + OUTFILE.getName(),
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml"
},
"Matrix_mvalues"
);
}
@Test
public void testImprovedNamingStrategyDelegator() {
// using ImprovedNamingStrategyDelegator, the collection table name should be "Mtx_mvalues"
doValidTest(
new String[] {
"--output=" + OUTFILE.getName(),
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
},
"Mtx_mvalues"
);
}
@Test
public void testEJB3NamingStrategy() {
// using EJB3NamingStrategy should be the same as using the default NamingStrategyDelegator, so
// the collection table name should be "Matrix_mvalues"
doValidTest(
new String[] {
"--output=" + OUTFILE.getName(),
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
},
"Matrix_mvalues"
);
}
@Test
public void testSchemaExportNamingAndNamingDelegatorSpecified() {
OUTFILE.delete();
// --naming and --namingdelegator cannot be used together.
// when SchemaExport is used standalone, an exception should be logged and OUTFILE should not exist.
SchemaExport.main(
new String[] {
"--output=" + OUTFILE.getName(),
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
}
);
assertFalse( OUTFILE.exists() );
}
@Test
public void testSchemaUpdateNamingAndNamingDelegatorSpecified() {
OUTFILE.delete();
// --naming and --namingdelegator cannot be used together.
// when SchemaUpdate is used standalone, an exception should be logged and OUTFILE should not exist.
SchemaUpdate.main(
new String[] {
"--output=" + OUTFILE.getName(),
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
}
);
assertFalse( OUTFILE.exists() );
}
@Test
public void testSchemaValidatorNamingAndNamingDelegatorSpecified() {
// --naming and --namingdelegator cannot be used together.
// when SchemaValidator is used standalone, an exception should be logged.
SchemaValidator.main(
new String[] {
"--output=" + OUTFILE.getName(),
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
}
);
}
private void doValidTest(String args[], String collectionTableNameExpected) {
OUTFILE.delete();
SchemaExport.main( args );
assertTrue( OUTFILE.exists() );
assertFileContainsTrimmedLineStartingWith( OUTFILE, collectionTableNameExpected );
SchemaValidator.main( args );
OUTFILE.delete();
// SchemaUpdate should result in an empty file because there should be nothing to update.
SchemaUpdate.main( args );
assertFileIsEmpty( OUTFILE );
dropSchema( args );
OUTFILE.delete();
// since schema was dropped, OUTFILE should now contain references to collectionTableNameExpected.
SchemaUpdate.main( args );
assertTrue( OUTFILE.exists() );
assertFileContainsTrimmedLineStartingWith( OUTFILE, collectionTableNameExpected );
SchemaValidator.main( args );
dropSchema( args );
}
private void assertFileContainsTrimmedLineStartingWith(java.io.File file, String text) {
try {
BufferedReader input = new BufferedReader( new FileReader( file ) );
String line;
/*
* readLine is a bit quirky :
* it returns the content of a line MINUS the newline.
* it returns null only for the END of the stream.
* it returns an empty String if two newlines appear in a row.
*/
while ( ( line = input.readLine() ) != null ) {
final String trimmedLine = line.trim();
if ( trimmedLine.contains( text ) ) {
return;
}
}
}
catch ( IOException e ) {
fail(
String.format(
"Failed due to IOException checking file [%s] for a line containing: [%s]",
file.getName(),
text
)
);
}
fail(
String.format(
"File [%s] does not contain a line containing: [%s]",
file.getName(),
text
)
);
}
private void assertFileIsEmpty(java.io.File file) {
try {
FileReader fileReader = new FileReader( file );
assertEquals( -1, fileReader.read() );
}
catch (IOException ex) {
fail( ex.getMessage() );
}
}
private void dropSchema(String[] args) {
final String[] argsNew = Arrays.copyOf( args, args.length + 1 );
argsNew[ args.length ] = "--drop";
SchemaExport.main( argsNew );
}
}

View File

@ -0,0 +1,10 @@
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping package="org.hibernate.test.schematoolsnaming"/>
<mapping class="org.hibernate.test.schematoolsnaming.Matrix"/>
</session-factory>
</hibernate-configuration>