From abdd70e4023786a507d391fc24708f520b3b1ee3 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Fri, 18 Jul 2025 12:30:37 +0300 Subject: [PATCH] HBX-3048: Ant task hbm2java should generate classes using generics by default - Add a new integration test 'UseGenericsTestIT' to guard this new default behavior - Change the default value of the 'jdk5' field in 'Hbm2JavaExporterTask' - Some cleanup in other ant integration tests Signed-off-by: Koen Aers --- .../tool/ant/hbm2java/JpaDefaultTestIT.java | 3 +- .../ant/hbm2java/NoAnnotationsTestIT.java | 3 +- .../tool/ant/hbm2java/UseGenericsTestIT.java | 137 ++++++++++++++++++ .../tool/ant/tutorial/TutorialTestIT.java | 1 - .../tool/ant/Hbm2JavaExporterTask.java | 2 +- 5 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java diff --git a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/JpaDefaultTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/JpaDefaultTestIT.java index ff0bed501b..d6f58eda18 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/JpaDefaultTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/JpaDefaultTestIT.java @@ -37,7 +37,7 @@ public void beforeEach() { } @Test - public void testTutorial() throws Exception { + public void testJpaDefault() throws Exception { createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -56,7 +56,6 @@ private void createDatabase() throws Exception { Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); Statement statement = connection.createStatement(); statement.execute(CREATE_PERSON_TABLE); - statement.execute("insert into PERSON values (1, 'foo')"); statement.close(); connection.close(); assertTrue(databaseFile.exists()); diff --git a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoAnnotationsTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoAnnotationsTestIT.java index be2192f767..e1cd5cbcb5 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoAnnotationsTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoAnnotationsTestIT.java @@ -37,7 +37,7 @@ public void beforeEach() { } @Test - public void testTutorial() throws Exception { + public void testNoAnnotations() throws Exception { createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -56,7 +56,6 @@ private void createDatabase() throws Exception { Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); Statement statement = connection.createStatement(); statement.execute(CREATE_PERSON_TABLE); - statement.execute("insert into PERSON values (1, 'foo')"); statement.close(); connection.close(); assertTrue(databaseFile.exists()); diff --git a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java new file mode 100644 index 0000000000..5f9f317bac --- /dev/null +++ b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java @@ -0,0 +1,137 @@ +package org.hibernate.tool.ant.hbm2java; + +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.nio.file.Files; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.*; + +public class UseGenericsTestIT { + + @TempDir + private File projectDir; + + private File buildXmlFile; + private ByteArrayOutputStream output; + private File databaseFile; + private File personFile; + + @BeforeEach + public void beforeEach() { + output = new ByteArrayOutputStream(); + databaseFile = new File(projectDir, "database/test.mv.db"); + assertFalse(databaseFile.exists()); + personFile = new File(projectDir, "generated/Person.java"); + assertFalse(personFile.exists()); + } + + @Test + public void testUseGenerics() throws Exception { + createBuildXmlFile(); + createDatabase(); + createHibernatePropertiesFile(); + runAntBuild(); + verifyResult(); + } + + private void createBuildXmlFile() throws Exception { + buildXmlFile = new File(projectDir, "build.xml"); + assertFalse(buildXmlFile.exists()); + Files.writeString(buildXmlFile.toPath(), buildXmlFileContents); + } + + private void createDatabase() throws Exception { + String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; + String CREATE_ITEM_TABLE = + "create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " + + " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"; + Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); + Statement statement = connection.createStatement(); + statement.execute(CREATE_PERSON_TABLE); + statement.execute(CREATE_ITEM_TABLE); + statement.close(); + connection.close(); + assertTrue(databaseFile.exists()); + assertTrue(databaseFile.isFile()); + } + + private void createHibernatePropertiesFile() throws Exception { + File hibernatePropertiesFile = new File(projectDir, "hibernate.properties"); + StringBuffer hibernatePropertiesFileContents = new StringBuffer(); + hibernatePropertiesFileContents + .append("hibernate.connection.driver_class=org.h2.Driver\n") + .append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n") + .append("hibernate.connection.username=\n") + .append("hibernate.connection.password=\n") + .append("hibernate.default_catalog=TEST\n") + .append("hibernate.default_schema=PUBLIC\n"); + Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString()); + assertTrue(hibernatePropertiesFile.exists()); + } + + private void runAntBuild() { + Project project = new Project(); + project.setBaseDir(projectDir); + project.addBuildListener(getConsoleLogger()); + ProjectHelper.getProjectHelper().parse(project, buildXmlFile); + project.executeTarget(project.getDefaultTarget()); + } + + private void verifyResult() throws Exception { + File generatedOutputFolder = new File(projectDir, "generated"); + assertTrue(generatedOutputFolder.exists()); + assertTrue(generatedOutputFolder.isDirectory()); + assertEquals(2, generatedOutputFolder.list().length); + File generatedPersonJavaFile = new File(generatedOutputFolder, "Person.java"); + assertTrue(generatedPersonJavaFile.exists()); + assertTrue(generatedPersonJavaFile.isFile()); + String generatedPersonJavaFileContents = new String( + Files.readAllBytes(generatedPersonJavaFile.toPath())); + assertTrue(generatedPersonJavaFileContents.contains("public class Person ")); + assertTrue(generatedPersonJavaFileContents.contains("Set")); + File generatedItemJavaFile = new File(generatedOutputFolder, "Item.java"); + assertTrue(generatedItemJavaFile.exists()); + assertTrue(generatedItemJavaFile.isFile()); + String generatedItemJavaFileContents = new String( + Files.readAllBytes(generatedItemJavaFile.toPath())); + assertTrue(generatedItemJavaFileContents.contains("public class Item ")); + } + + private DefaultLogger getConsoleLogger() { + DefaultLogger consoleLogger = new DefaultLogger(); + consoleLogger.setErrorPrintStream(System.err); + consoleLogger.setOutputPrintStream(new PrintStream(output, true)); + consoleLogger.setMessageOutputLevel(Project.MSG_INFO); + return consoleLogger; + } + + private String constructJdbcConnectionString() { + return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; + } + + private static final String buildXmlFileContents = + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" ; + +} diff --git a/ant/src/it/java/org/hibernate/tool/ant/tutorial/TutorialTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/tutorial/TutorialTestIT.java index a359e98f9b..257d3d3f4e 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/tutorial/TutorialTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/tutorial/TutorialTestIT.java @@ -58,7 +58,6 @@ private void createDatabase() throws Exception { Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); Statement statement = connection.createStatement(); statement.execute(CREATE_PERSON_TABLE); - statement.execute("insert into PERSON values (1, 'foo')"); statement.close(); connection.close(); assertTrue(databaseFile.exists()); diff --git a/ant/src/main/java/org/hibernate/tool/ant/Hbm2JavaExporterTask.java b/ant/src/main/java/org/hibernate/tool/ant/Hbm2JavaExporterTask.java index 9415b36f6a..a5ec33d494 100644 --- a/ant/src/main/java/org/hibernate/tool/ant/Hbm2JavaExporterTask.java +++ b/ant/src/main/java/org/hibernate/tool/ant/Hbm2JavaExporterTask.java @@ -29,7 +29,7 @@ public class Hbm2JavaExporterTask extends ExporterTask { boolean ejb3 = true; - boolean jdk5 = false; + boolean jdk5 = true; public Hbm2JavaExporterTask(HibernateToolTask parent) { super( parent );