From 1b7e7d4348a8c0046fcf41d9fc7c4e1c6b5cbf34 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Thu, 31 Jul 2025 18:53:17 +0300 Subject: [PATCH] HBX-3068: Refactor the Ant integration tests to factor out common code - Pull up method 'createHibernatePropertiesFile()' to TestTemplate - Add 'hibernateToolTaskXml' and 'databaseCreationScript' instance variables to TestTemplate along with respective setter methods - Remove the methods 'createDatabaseScript()' and 'hibernateToolTaskXml()' from TestTemplate and instead use the instance variables above - Set the instance variables in the respective integration test classes appropriately and get rid of the inherited methods from above Signed-off-by: Koen Aers --- .../tool/ant/hbm2java/JpaDefaultTestIT.java | 41 ++++----------- .../ant/hbm2java/NoAnnotationsTestIT.java | 40 ++++----------- .../tool/ant/hbm2java/NoGenericsTestIT.java | 47 ++++++----------- .../tool/ant/hbm2java/UseGenericsTestIT.java | 46 ++++++----------- .../tool/ant/tutorial/TutorialTestIT.java | 40 ++++----------- .../hibernate/tool/it/ant/TestTemplate.java | 50 +++++++++++++------ 6 files changed, 97 insertions(+), 167 deletions(-) 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 8086d82a49..cbfe168ff5 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 @@ -13,6 +13,17 @@ public class JpaDefaultTestIT extends TestTemplate { @Test public void testJpaDefault() throws Exception { + setHibernateToolTaskXml( + """ + \s + \s + \s + \s + """ + ); + setDatabaseCreationScript(new String[] { + "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" + }); createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -20,30 +31,6 @@ public void testJpaDefault() throws Exception { verifyResult(); } - protected String hibernateToolTaskXml() { - return hibernateToolTaskXml; - } - - protected String[] createDatabaseScript() { - return new String[] { - "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" - }; - } - - private void createHibernatePropertiesFile() throws Exception { - File hibernatePropertiesFile = new File(getProjectDir(), "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 verifyResult() throws Exception { File generatedOutputFolder = new File(getProjectDir(), "generated"); assertTrue(generatedOutputFolder.exists()); @@ -58,10 +45,4 @@ private void verifyResult() throws Exception { assertTrue(generatedPersonJavaFileContents.contains("public class Person ")); } - private static final String hibernateToolTaskXml = - " \n" + - " \n" + - " \n" + - " \n" ; - } 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 570c3ea486..a3e4fee508 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 @@ -14,6 +14,17 @@ public class NoAnnotationsTestIT extends TestTemplate { @Test public void testNoAnnotations() throws Exception { + setHibernateToolTaskXml( + """ + \s + \s + \s + \s + """ + ); + setDatabaseCreationScript(new String[] { + "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" + }); createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -21,30 +32,6 @@ public void testNoAnnotations() throws Exception { verifyResult(); } - protected String hibernateToolTaskXml() { - return hibernateToolTaskXml; - } - - protected String[] createDatabaseScript() { - return new String[] { - "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" - }; - } - - private void createHibernatePropertiesFile() throws Exception { - File hibernatePropertiesFile = new File(getProjectDir(), "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 verifyResult() throws Exception { File generatedOutputFolder = new File(getProjectDir(), "generated"); assertTrue(generatedOutputFolder.exists()); @@ -59,9 +46,4 @@ private void verifyResult() throws Exception { assertTrue(generatedPersonJavaFileContents.contains("public class Person ")); } - private static final String hibernateToolTaskXml = - " \n" + - " \n" + - " \n" + - " \n" ; } diff --git a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoGenericsTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoGenericsTestIT.java index 77a4f0f4b1..49d69bd8c5 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoGenericsTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoGenericsTestIT.java @@ -14,6 +14,20 @@ public class NoGenericsTestIT extends TestTemplate { @Test public void testUseGenerics() throws Exception { + setHibernateToolTaskXml( + """ + \s + \s + \s + \s + """ + ); + setDatabaseCreationScript(new String[] { + "create table PERSON (ID int not null, NAME varchar(20), " + + "primary key (ID))", + "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))" + }); createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -21,33 +35,6 @@ public void testUseGenerics() throws Exception { verifyResult(); } - protected String hibernateToolTaskXml() { - return hibernateToolTaskXml; - } - - protected String[] createDatabaseScript() { - return new String[] { - "create table PERSON (ID int not null, NAME varchar(20), " + - "primary key (ID))", - "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))" - }; - } - - private void createHibernatePropertiesFile() throws Exception { - File hibernatePropertiesFile = new File(getProjectDir(), "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 verifyResult() throws Exception { File generatedOutputFolder = new File(getProjectDir(), "generated"); assertTrue(generatedOutputFolder.exists()); @@ -68,10 +55,4 @@ private void verifyResult() throws Exception { assertTrue(generatedItemJavaFileContents.contains("public class Item ")); } - private static final String hibernateToolTaskXml = - " \n" + - " \n" + - " \n" + - " \n" ; - } 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 index e4b18066b2..e96a08baed 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java @@ -13,6 +13,20 @@ public class UseGenericsTestIT extends TestTemplate { @Test public void testUseGenerics() throws Exception { + setHibernateToolTaskXml( + """ + \s + \s + \s + \s + """ + ); + setDatabaseCreationScript(new String[] { + "create table PERSON (ID int not null, NAME varchar(20), " + + "primary key (ID))", + "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))" + }); createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -20,33 +34,6 @@ public void testUseGenerics() throws Exception { verifyResult(); } - protected String hibernateToolTaskXml() { - return hibernateToolTaskXml; - } - - protected String[] createDatabaseScript() { - return new String[] { - "create table PERSON (ID int not null, NAME varchar(20), " + - "primary key (ID))", - "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))" - }; - } - - private void createHibernatePropertiesFile() throws Exception { - File hibernatePropertiesFile = new File(getProjectDir(), "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 verifyResult() throws Exception { File generatedOutputFolder = new File(getProjectDir(), "generated"); assertTrue(generatedOutputFolder.exists()); @@ -67,9 +54,4 @@ private void verifyResult() throws Exception { assertTrue(generatedItemJavaFileContents.contains("public class Item ")); } - private static final String hibernateToolTaskXml = - " \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 503c17c8e4..5704d995bc 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 @@ -13,6 +13,17 @@ public class TutorialTestIT extends TestTemplate { @Test public void testTutorial() throws Exception { + setHibernateToolTaskXml( + """ + \s + \s + \s + \s + """ + ); + setDatabaseCreationScript(new String[] { + "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" + }); createBuildXmlFile(); createDatabase(); createHibernatePropertiesFile(); @@ -20,30 +31,6 @@ public void testTutorial() throws Exception { verifyResult(); } - protected String hibernateToolTaskXml() { - return hibernateToolTaskXml; - } - - protected String[] createDatabaseScript() { - return new String[] { - "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" - }; - } - - private void createHibernatePropertiesFile() throws Exception { - File hibernatePropertiesFile = new File(getProjectDir(), "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 verifyResult() { File generatedOutputFolder = new File(getProjectDir(), "generated"); assertTrue(generatedOutputFolder.exists()); @@ -54,9 +41,4 @@ private void verifyResult() { assertTrue(generatedPersonJavaFile.isFile()); } - private static final String hibernateToolTaskXml = - " \n" + - " \n" + - " \n" + - " \n" ; } diff --git a/ant/src/it/java/org/hibernate/tool/it/ant/TestTemplate.java b/ant/src/it/java/org/hibernate/tool/it/ant/TestTemplate.java index 7cbfbb9289..0a2e43a0cf 100644 --- a/ant/src/it/java/org/hibernate/tool/it/ant/TestTemplate.java +++ b/ant/src/it/java/org/hibernate/tool/it/ant/TestTemplate.java @@ -11,24 +11,31 @@ import java.sql.DriverManager; import java.sql.Statement; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public abstract class TestTemplate { @TempDir private File projectDir; + private String hibernateToolTaskXml; + private String[] databaseCreationScript; + protected File getProjectDir() { return projectDir; } - protected abstract String hibernateToolTaskXml(); + protected void setHibernateToolTaskXml(String xml) { + this.hibernateToolTaskXml = xml; + } - protected abstract String[] createDatabaseScript(); + protected void setDatabaseCreationScript(String[] sqls) { + this.databaseCreationScript = sqls; + } protected String constructBuildXmlFileContents() { - return buildXmlFileContents.replace("@hibernateToolTaskXml@", hibernateToolTaskXml()); + assertNotNull(hibernateToolTaskXml); + return buildXmlFileContents.replace("@hibernateToolTaskXml@", hibernateToolTaskXml); } protected void runAntBuild() { @@ -57,7 +64,7 @@ protected void createDatabase() throws Exception { assertFalse(databaseFile.isFile()); Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); Statement statement = connection.createStatement(); - for (String sql : createDatabaseScript()) { + for (String sql : databaseCreationScript) { statement.execute(sql); } statement.close(); @@ -66,6 +73,19 @@ protected void createDatabase() throws Exception { assertTrue(databaseFile.isFile()); } + protected void createHibernatePropertiesFile() throws Exception { + File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties"); + String hibernatePropertiesFileContents = + "hibernate.connection.driver_class=org.h2.Driver\n" + + "hibernate.connection.url=" + constructJdbcConnectionString() + "\n" + + "hibernate.connection.username=\n" + + "hibernate.connection.password=\n" + + "hibernate.default_catalog=TEST\n" + + "hibernate.default_schema=PUBLIC\n"; + Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents); + assertTrue(hibernatePropertiesFile.exists()); + } + private DefaultLogger getConsoleLogger() { DefaultLogger consoleLogger = new DefaultLogger(); consoleLogger.setErrorPrintStream(System.err); @@ -75,13 +95,15 @@ private DefaultLogger getConsoleLogger() { } private static final String buildXmlFileContents = - " \n" + - " \n" + - " \n" + - "@hibernateToolTaskXml@" + - " \n" + - " \n" ; + """ + \s + \s + \s + @hibernateToolTaskXml@\ + \s + \s + """; }