Author: anthonyHib
Date: 2008-06-19 04:32:30 -0400 (Thu, 19 Jun 2008)
New Revision: 14782
Modified:
branches/Branch_3_2/HibernateExt/tools/lib/testlibs/README.TXT
branches/Branch_3_2/HibernateExt/tools/lib/testlibs/hibernate3.jar
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/TestHelper.java
branches/Branch_3_2/HibernateExt/tools/src/testsupport/anttest-build.xml
Log:
HBX-757 fix
Modified: branches/Branch_3_2/HibernateExt/tools/lib/testlibs/README.TXT
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/lib/testlibs/README.TXT 2008-06-18 18:04:17 UTC
(rev 14781)
+++ branches/Branch_3_2/HibernateExt/tools/lib/testlibs/README.TXT 2008-06-19 08:32:30 UTC
(rev 14782)
@@ -3,4 +3,8 @@
Some of these libraries are only here for the purpose of verifying that the
generated code can compile.
-Jar files from JBoss 4.0.4.GA and jboss-seam-1.0.0.CR3
\ No newline at end of file
+Jar files from JBoss 4.0.4.GA and jboss-seam-1.0.0.CR3
+
+Warning: in order to run AntHibernateToolTest.testHbm2DDLExportExecution,testHbm2DDLLogic
and testHbm2DDLUpdateExecution
+you must have an up to date hibernate core jar in your classpath.
+In lib\testlibs, provided hibernate3.jar allows you to run these tests
\ No newline at end of file
Modified: branches/Branch_3_2/HibernateExt/tools/lib/testlibs/hibernate3.jar
===================================================================
(Binary files differ)
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java 2008-06-18
18:04:17 UTC (rev 14781)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java 2008-06-19
08:32:30 UTC (rev 14782)
@@ -11,11 +11,16 @@
package org.hibernate.tool.hbm2x;
import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.Iterator;
+import org.hibernate.HibernateException;
+import org.hibernate.JDBCException;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
+import org.hibernate.util.ReflectHelper;
/**
* Schema Export (.ddl) code generation.
@@ -73,7 +78,77 @@
final Configuration configuration = getConfiguration();
if (schemaUpdate) {
SchemaUpdate update = new SchemaUpdate(configuration);
- update.execute(scriptToConsole, exportToDatabase);
+
+ // classic schemaupdate execution, will work with all releases
+ if(outputFileName == null && delimiter == null && haltOnError
&& format)
+ update.execute(scriptToConsole, exportToDatabase);
+
+ // at least one of the parameter unmanaged by
+ // hibernate core prior versions is set
+ else {
+
+ /* working with reflection as no idea what hibernate core version is used */
+ try {
+ Class schemaUpdateClass = SchemaUpdate.class;
+
+ if (null != outputFileName) {
+ Method setOutputFile = schemaUpdateClass.getMethod("setOutputFile",
+ new Class[] {String.class});
+ setOutputFile.invoke(update, new Object[] {new File(getOutputDirectory(),
+ outputFileName).toString()});
+
+ log.debug("delimiter ='"+ delimiter + "'");
+ Method setDelimiter = schemaUpdateClass.getMethod("setDelimiter",
+ new Class[] {String.class});
+ setDelimiter.invoke(update, new Object[] {delimiter});
+
+ Method setFormat = schemaUpdateClass.getMethod("setFormat",
+ new Class[] {boolean.class});
+ setFormat.invoke(update, new Object[] {Boolean.valueOf(format)});
+
+ }
+
+ if (haltOnError) {
+ Method setHaltOnError = schemaUpdateClass.getMethod("setHaltOnError",
+ new Class[] {boolean.class});
+ setHaltOnError.invoke(update, new Object[] {Boolean.valueOf(haltOnError)});
+ }
+
+ update.execute(scriptToConsole, exportToDatabase);
+ if (!update.getExceptions().isEmpty()) {
+ int i = 1;
+ for (Iterator iterator = update.getExceptions().iterator(); iterator
+ .hasNext(); i++) {
+ Throwable element = (Throwable) iterator.next();
+ log.warn("Error #" + i + ": ", element);
+
+ }
+ log.error(i - 1 + " errors occurred while performing Hbm2DDLExporter.");
+ if (haltOnError) {
+ throw new ExporterException(
+ "Errors while performing Hbm2DDLExporter");
+ }
+ }
+
+ } catch (NoSuchMethodException e) {
+ log.error( "Error during DDL export, this version of hibernate doesn't
support following " +
+ "SchemaUpdate parameters: haltonerror = true, format= true, delimiter and
outputfilename" +
+ " either update hibernate3.jar or don't used the involved
parameters", e );
+ } catch (IllegalArgumentException e) {
+ log.error( "Error during DDL export, this version of hibernate doesn't
support following " +
+ "SchemaUpdate parameters: haltonerror = true, format= true, delimiter and
outputfilename" +
+ " either update hibernate3.jar or don't used the involved
parameters", e );
+ } catch (InvocationTargetException e) {
+ log.error( "Error during DDL export, this version of hibernate doesn't
support following " +
+ "SchemaUpdate parameters: haltonerror = true, format= true, delimiter and
outputfilename" +
+ " either update hibernate3.jar or don't used the involved
parameters", e );
+ } catch (IllegalAccessException e) {
+ log.error( "Error during DDL export, this version of hibernate doesn't
support following " +
+ "SchemaUpdate parameters: haltonerror = true, format= true, delimiter and
outputfilename" +
+ " either update hibernate3.jar or don't used the involved
parameters", e );
+ }
+ }
+
} else {
SchemaExport export = new SchemaExport(configuration);
if (null != outputFileName) {
@@ -90,22 +165,8 @@
} else {
export.execute(scriptToConsole, exportToDatabase, drop, create);
}
-
- if (!export.getExceptions().isEmpty()) {
- int i = 1;
- for (Iterator iterator = export.getExceptions().iterator(); iterator
- .hasNext(); i++) {
- Throwable element = (Throwable) iterator.next();
- log.warn("Error #" + i + ": ", element);
-
- }
- log.error(i - 1 + " errors occurred while performing Hbm2DDLExporter.");
- if (haltOnError) {
- throw new ExporterException(
- "Errors while performing Hbm2DDLExporter");
- }
- }
}
+
}
public void setExport(boolean export) {
Modified:
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java 2008-06-18
18:04:17 UTC (rev 14781)
+++
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java 2008-06-19
08:32:30 UTC (rev 14782)
@@ -10,6 +10,9 @@
import junit.framework.TestSuite;
import org.apache.tools.ant.BuildException;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.Session;
import org.hibernate.tool.test.TestHelper;
/**
@@ -34,36 +37,100 @@
}
public void testHbm2DDLLogic() {
+ cleanupOutputDir();
File baseDir = new File(project.getProperty("build.dir"),
"topdown");
File onlyCreate = new File(baseDir, "onlycreate.sql");
File onlyDrop = new File(baseDir, "onlydrop.sql");
File dropAndCreate = new File(baseDir, "dropandcreate.sql");
+ File update = new File(baseDir, "update.sql");
assertFalse(onlyCreate.exists());
assertFalse(onlyDrop.exists());
assertFalse(dropAndCreate.exists());
+ assertFalse(update.exists());
- executeTarget("testantcfg");
+ // allow to test creation of script file + delimiter
+ // + non execution (test will fail if executed because of crappy delimiter)
+ executeTarget("testScriptCreattion");
assertTrue(onlyCreate.exists());
assertTrue(onlyDrop.exists());
assertTrue(dropAndCreate.exists());
+ assertTrue(update.exists());
assertNotNull(TestHelper.findFirstString("drop", dropAndCreate));
assertNotNull(TestHelper.findFirstString("create", dropAndCreate));
-
+ assertNotNull(TestHelper.findFirstString("---", dropAndCreate));
+
assertEquals(null, TestHelper.findFirstString("create", onlyDrop));
assertNotNull(TestHelper.findFirstString("drop", onlyDrop));
-
+
assertEquals(null, TestHelper.findFirstString("drop", onlyCreate));
assertNotNull(TestHelper.findFirstString("create", onlyCreate));
+ assertNotNull(TestHelper.findFirstString("---", onlyCreate));
+
+ assertNotNull(TestHelper.findFirstString("create", update));
+ assertNotNull(TestHelper.findFirstString("---", update));
onlyCreate.delete();
onlyDrop.delete();
dropAndCreate.delete();
+ update.delete();
}
+
+ public void testHbm2DDLUpdateExecution() {
+ cleanupOutputDir();
+ File baseDir = new File(project.getProperty("build.dir"),
"topdown");
+ File update1 = new File(baseDir, "update1.sql");
+ File update2 = new File(baseDir, "update2.sql");
+ File onlydrop = new File(baseDir, "onlydrop.sql");
+
+ assertFalse(update1.exists());
+ assertFalse(update2.exists());
+ assertFalse(onlydrop.exists());
+
+ executeTarget("testantcfgUpdateExecuted");
+
+ assertTrue(update1.exists());
+ assertTrue(update2.exists());
+
+ assertNotNull(TestHelper.findFirstString("create", update1));
+ // if first update is executed, the second should be empty
+ assertEquals(0, update2.length());
+
+ update1.delete();
+ update2.delete();
+ onlydrop.delete();
+ }
+
+ public void testHbm2DDLExportExecution() {
+ cleanupOutputDir();
+ File baseDir = new File(project.getProperty("build.dir"),
"topdown");
+ File export = new File(baseDir, "export.sql");
+ File update = new File(baseDir, "update.sql");
+ File onlydrop = new File(baseDir, "onlydrop.sql");
+
+ assertFalse(export.exists());
+ assertFalse(update.exists());
+ assertFalse(onlydrop.exists());
+
+
+ executeTarget("testantcfgExportExecuted");
+
+ assertTrue(export.exists());
+ assertTrue(update.exists());
+
+ assertNotNull(TestHelper.findFirstString("create", export));
+ // if export is executed, update should be empty
+ assertEquals(0, update.length());
+
+ export.delete();
+ update.delete();
+ onlydrop.delete();
+ }
+
public void testJDBCConfiguration() {
executeTarget("testantjdbccfg");
}
Modified:
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/TestHelper.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/TestHelper.java 2008-06-18
18:04:17 UTC (rev 14781)
+++
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/TestHelper.java 2008-06-19
08:32:30 UTC (rev 14782)
@@ -255,21 +255,20 @@
}
static public String findFirstString(String string, File file) {
+ String str;
try {
BufferedReader in = new BufferedReader(new FileReader(file) );
- String str;
while ( (str = in.readLine() ) != null ) {
if(str.indexOf(string)>=0) {
- return str;
+ break;
}
}
- in.close();
-
+ in.close();
}
catch (IOException e) {
throw new RuntimeException("trouble with searching in " + file,e);
}
- return null;
+ return str;
}
}
Modified: branches/Branch_3_2/HibernateExt/tools/src/testsupport/anttest-build.xml
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/testsupport/anttest-build.xml 2008-06-18
18:04:17 UTC (rev 14781)
+++ branches/Branch_3_2/HibernateExt/tools/src/testsupport/anttest-build.xml 2008-06-19
08:32:30 UTC (rev 14782)
@@ -23,7 +23,7 @@
<target name="cleanup" description="task used for ensuring cleanup to
be done even in the case of test failure" depends="afterCfg2hbm"/>
- <target name="testantcfg">
+ <target name="testScriptCreattion">
<mkdir dir="build/testsupport" />
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask" />
@@ -39,14 +39,72 @@
</configuration>
<hbm2java />
- <hbm2ddl drop="false" create="true" export="false"
outputfilename="onlycreate.sql" format="true" />
+ <!-- this one is executed, security clean up-->
+ <hbm2ddl drop="true" create="false" export="true"
outputfilename="onlydrop.sql" format="true" />
+
+ <!-- allow to test creation of script file + delimiter + non execution (test will
fail if executed because of crappy delimiter) -->
+ <hbm2ddl drop="false" create="true" export="false"
outputfilename="onlycreate.sql" format="true"
delimiter="---"/>
<hbm2ddl drop="true" create="false" export="false"
outputfilename="onlydrop.sql" format="true" />
- <hbm2ddl drop="true" create="true" export="false"
outputfilename="dropandcreate.sql" format="true" />
+ <hbm2ddl drop="true" create="true" export="false"
outputfilename="dropandcreate.sql" format="true"
delimiter="---"/>
+ <hbm2ddl export="false" update="true"
outputfilename="update.sql" format="true"
delimiter="---"/>
+
+ <!-- this one is executed, security clean up, could be easily removed-->
+ <hbm2ddl drop="true" create="false" export="true"
outputfilename="onlydrop.sql" format="true" />
<hbm2doc />
</hibernatetool>
</target>
+
+ <target name="testantcfgUpdateExecuted">
+ <mkdir dir="build/testsupport" />
+ <taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask" />
+ <hibernatetool destdir="${build.dir}/topdown">
+ <classpath>
+ <path location="../../build/testsupport" />
+ </classpath>
+
+ <configuration namingstrategy="org.hibernate.cfg.ImprovedNamingStrategy"
entityresolver="DummyEntityResolver">
+ <fileset dir="../test" id="id">
+ <include name="**/*TopDown.hbm.xml" />
+ </fileset>
+ </configuration>
+
+ <hbm2java />
+ <hbm2ddl drop="true" create="false" export="true"
outputfilename="onlydrop.sql" format="true" />
+ <hbm2ddl export="true" update="true"
outputfilename="update1.sql" format="true" />
+ <hbm2ddl export="false" update="true"
outputfilename="update2.sql" format="true" />
+ <hbm2ddl drop="true" create="false" export="true"
/>
+ <hbm2doc />
+ </hibernatetool>
+
+ </target>
+
+ <target name="testantcfgExportExecuted">
+
+ <mkdir dir="build/testsupport" />
+ <taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask" />
+ <hibernatetool destdir="${build.dir}/topdown">
+ <classpath>
+ <path location="../../build/testsupport" />
+ </classpath>
+
+ <configuration namingstrategy="org.hibernate.cfg.ImprovedNamingStrategy"
entityresolver="DummyEntityResolver">
+ <fileset dir="../test" id="id">
+ <include name="**/*TopDown.hbm.xml" />
+ </fileset>
+ </configuration>
+
+ <hbm2java />
+ <hbm2ddl drop="true" create="false" export="true"
outputfilename="onlydrop.sql" format="true" />
+ <hbm2ddl export="true" update="false"
outputfilename="export.sql" format="true" />
+ <hbm2ddl export="false" update="true"
outputfilename="update.sql" format="true" />
+ <hbm2ddl drop="true" create="false" export="true"
/>
+ <hbm2doc />
+ </hibernatetool>
+
+ </target>
+
<target name="testantjdbccfg">
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="tasks.classpath" />
<property file="../etc/hibernate.properties" prefix="tools"
/>