From 0def0abcac06309c7ce5dde349458624116727e0 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Mon, 5 Jan 2015 12:04:44 -0800 Subject: [PATCH] HHH-9467 : Cannot enable NamingStrategyDelegator implementations using standalone schema tools (cherry picked from commit 4bf3b162ad00ee977d65c02ef0f6db6fe29bde85) Conflicts: hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExportTask.java hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidatorTask.java --- .../hibernate/tool/hbm2ddl/SchemaExport.java | 19 ++ .../tool/hbm2ddl/SchemaExportTask.java | 15 ++ .../hibernate/tool/hbm2ddl/SchemaUpdate.java | 27 ++- .../tool/hbm2ddl/SchemaUpdateTask.java | 16 +- .../tool/hbm2ddl/SchemaValidator.java | 20 ++ .../tool/hbm2ddl/SchemaValidatorTask.java | 14 ++ .../test/schematoolsnaming/Matrix.java | 40 ++++ .../StandaloneSchemaToolsNamingTest.java | 218 ++++++++++++++++++ .../test/schematoolsnaming/hibernate.cfg.xml | 10 + 9 files changed, 377 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/Matrix.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/StandaloneSchemaToolsNamingTest.java create mode 100644 hibernate-core/src/test/resources/org/hibernate/test/schematoolsnaming/hibernate.cfg.xml diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java index 17e7b3f20c..8141d179c9 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java @@ -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.internal.FormatStyle; import org.hibernate.engine.jdbc.internal.Formatter; @@ -527,6 +528,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( "--" ) ) { @@ -564,11 +567,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]; @@ -615,6 +628,12 @@ public class SchemaExport { } } + private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) { + if ( namingSpecified && namingDelegatorSpecified ) { + throw new HibernateException( "--naming= and --namingdelegator= cannot be used together." ); + } + } + /** * Returns a List of all Exceptions which occured during the export. * diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExportTask.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExportTask.java index 7722c2c388..94a64f3368 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExportTask.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExportTask.java @@ -42,6 +42,7 @@ import org.apache.tools.ant.types.FileSet; 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 = false; private String delimiter = null; private String namingStrategy = null; + private String namingStrategyDelegator = null; 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; } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java index 0f3d887ee8..20449c2b95 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java @@ -40,6 +40,7 @@ import org.hibernate.JDBCException; 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= and --namingdelegator= cannot be used together." ); + } + } + /** * Execute the schema updates * diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java index 72e44ea351..38de26cd40 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java @@ -42,6 +42,7 @@ import org.apache.tools.ant.types.FileSet; 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 = false; private String delimiter = null; private String namingStrategy = null; - + private String namingStrategyDelegator = null; 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; } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java index 299029ffff..305b6480f0 100755 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidator.java @@ -34,6 +34,7 @@ import org.hibernate.HibernateException; 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= and --namingdelegator= cannot be used together." ); + } + } + /** * Perform the validations. */ diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidatorTask.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidatorTask.java index e74b6d8c97..9b89db5606 100755 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidatorTask.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaValidatorTask.java @@ -42,6 +42,7 @@ import org.apache.tools.ant.types.FileSet; 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 = null; private File configurationFile = null; private String namingStrategy = null; + private String namingStrategyDelegator = null; 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; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/Matrix.java b/hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/Matrix.java new file mode 100644 index 0000000000..a19b2ef0bd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/Matrix.java @@ -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 mvalues = new HashSet(); +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/StandaloneSchemaToolsNamingTest.java b/hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/StandaloneSchemaToolsNamingTest.java new file mode 100644 index 0000000000..6b8b43483a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schematoolsnaming/StandaloneSchemaToolsNamingTest.java @@ -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=} 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 ); + + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/test/schematoolsnaming/hibernate.cfg.xml b/hibernate-core/src/test/resources/org/hibernate/test/schematoolsnaming/hibernate.cfg.xml new file mode 100644 index 0000000000..ebdc67171d --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/schematoolsnaming/hibernate.cfg.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file