From ebd7c6a0b0421933d672f35a5168ef88ee4e2793 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Thu, 31 Jul 2025 17:52:44 +0300 Subject: [PATCH] HBX-3068: Refactor the Ant integration tests to factor out common code - Create an abstract method 'createDatabaseScript()' to return an array of SQL statements for the database creation - Implement this method in the integration test classes - Modify the 'createDatabase()' method to use the above script and pull up to TestTemplate - Pull up the methods 'constructJdbcConnectionString()' and createBuildXmlFile() to TestTemplate - Get rid of the local variables and '@BeforeEach' method for all the integration test classes Signed-off-by: Koen Aers --- .../tool/ant/hbm2java/JpaDefaultTestIT.java | 50 ++++------------- .../ant/hbm2java/NoAnnotationsTestIT.java | 49 ++++------------- .../tool/ant/hbm2java/NoGenericsTestIT.java | 54 +++++-------------- .../tool/ant/hbm2java/UseGenericsTestIT.java | 53 ++++-------------- .../tool/ant/tutorial/TutorialTestIT.java | 42 ++------------- .../hibernate/tool/it/ant/TestTemplate.java | 36 ++++++++++++- 6 files changed, 83 insertions(+), 201 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 36b9cf01d5..8086d82a49 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 @@ -1,31 +1,16 @@ package org.hibernate.tool.ant.hbm2java; -import org.hibernate.tool.it.ant.TestTemplate; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import java.nio.file.Files; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.Statement; -import static org.junit.jupiter.api.Assertions.*; +import org.hibernate.tool.it.ant.TestTemplate; +import org.junit.jupiter.api.Test; public class JpaDefaultTestIT extends TestTemplate { - - private File buildXmlFile; - private File databaseFile; - private File personFile; - - @BeforeEach - public void beforeEach() { - databaseFile = new File(getProjectDir(), "database/test.mv.db"); - assertFalse(databaseFile.exists()); - personFile = new File(getProjectDir(), "generated/Person.java"); - assertFalse(personFile.exists()); - } - + @Test public void testJpaDefault() throws Exception { createBuildXmlFile(); @@ -39,23 +24,12 @@ protected String hibernateToolTaskXml() { return hibernateToolTaskXml; } - private void createBuildXmlFile() throws Exception { - buildXmlFile = new File(getProjectDir(), "build.xml"); - assertFalse(buildXmlFile.exists()); - Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); - } - - private void createDatabase() throws Exception { - String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; - Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); - Statement statement = connection.createStatement(); - statement.execute(CREATE_PERSON_TABLE); - statement.close(); - connection.close(); - assertTrue(databaseFile.exists()); - assertTrue(databaseFile.isFile()); + 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(); @@ -84,10 +58,6 @@ private void verifyResult() throws Exception { assertTrue(generatedPersonJavaFileContents.contains("public class Person ")); } - private String constructJdbcConnectionString() { - return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; - } - private static final String hibernateToolTaskXml = " \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 0b1e107918..570c3ea486 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 @@ -1,31 +1,17 @@ package org.hibernate.tool.ant.hbm2java; -import org.hibernate.tool.it.ant.TestTemplate; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import java.nio.file.Files; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.Statement; -import static org.junit.jupiter.api.Assertions.*; +import org.hibernate.tool.it.ant.TestTemplate; +import org.junit.jupiter.api.Test; public class NoAnnotationsTestIT extends TestTemplate { - private File buildXmlFile; - private File databaseFile; - private File personFile; - - @BeforeEach - public void beforeEach() { - databaseFile = new File(getProjectDir(), "database/test.mv.db"); - assertFalse(databaseFile.exists()); - personFile = new File(getProjectDir(), "generated/Person.java"); - assertFalse(personFile.exists()); - } - @Test public void testNoAnnotations() throws Exception { createBuildXmlFile(); @@ -39,23 +25,12 @@ protected String hibernateToolTaskXml() { return hibernateToolTaskXml; } - private void createBuildXmlFile() throws Exception { - buildXmlFile = new File(getProjectDir(), "build.xml"); - assertFalse(buildXmlFile.exists()); - Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); - } - - private void createDatabase() throws Exception { - String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; - Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); - Statement statement = connection.createStatement(); - statement.execute(CREATE_PERSON_TABLE); - statement.close(); - connection.close(); - assertTrue(databaseFile.exists()); - assertTrue(databaseFile.isFile()); + 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(); @@ -84,10 +59,6 @@ private void verifyResult() throws Exception { assertTrue(generatedPersonJavaFileContents.contains("public class Person ")); } - private String constructJdbcConnectionString() { - return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; - } - private static final String hibernateToolTaskXml = " \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 f15f470aed..77a4f0f4b1 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 @@ -1,31 +1,17 @@ package org.hibernate.tool.ant.hbm2java; -import org.hibernate.tool.it.ant.TestTemplate; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import java.nio.file.Files; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.Statement; -import static org.junit.jupiter.api.Assertions.*; +import org.hibernate.tool.it.ant.TestTemplate; +import org.junit.jupiter.api.Test; public class NoGenericsTestIT extends TestTemplate { - private File buildXmlFile; - private File databaseFile; - private File personFile; - - @BeforeEach - public void beforeEach() { - databaseFile = new File(getProjectDir(), "database/test.mv.db"); - assertFalse(databaseFile.exists()); - personFile = new File(getProjectDir(), "generated/Person.java"); - assertFalse(personFile.exists()); - } - @Test public void testUseGenerics() throws Exception { createBuildXmlFile(); @@ -39,27 +25,15 @@ protected String hibernateToolTaskXml() { return hibernateToolTaskXml; } - private void createBuildXmlFile() throws Exception { - buildXmlFile = new File(getProjectDir(), "build.xml"); - assertFalse(buildXmlFile.exists()); - Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); - } - - 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 = + 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))"; - 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()); + "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(); @@ -94,10 +68,6 @@ private void verifyResult() throws Exception { assertTrue(generatedItemJavaFileContents.contains("public class Item ")); } - private String constructJdbcConnectionString() { - return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; - } - private static final String hibernateToolTaskXml = " \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 80243e0655..e4b18066b2 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 @@ -1,31 +1,16 @@ package org.hibernate.tool.ant.hbm2java; -import org.hibernate.tool.it.ant.TestTemplate; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import java.nio.file.Files; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.Statement; -import static org.junit.jupiter.api.Assertions.*; +import org.hibernate.tool.it.ant.TestTemplate; +import org.junit.jupiter.api.Test; public class UseGenericsTestIT extends TestTemplate { - private File buildXmlFile; - private File databaseFile; - private File personFile; - - @BeforeEach - public void beforeEach() { - databaseFile = new File(getProjectDir(), "database/test.mv.db"); - assertFalse(databaseFile.exists()); - personFile = new File(getProjectDir(), "generated/Person.java"); - assertFalse(personFile.exists()); - } - @Test public void testUseGenerics() throws Exception { createBuildXmlFile(); @@ -39,27 +24,15 @@ protected String hibernateToolTaskXml() { return hibernateToolTaskXml; } - private void createBuildXmlFile() throws Exception { - buildXmlFile = new File(getProjectDir(), "build.xml"); - assertFalse(buildXmlFile.exists()); - Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); - } - - 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 = + 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))"; - 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()); + "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(); @@ -94,10 +67,6 @@ private void verifyResult() throws Exception { assertTrue(generatedItemJavaFileContents.contains("public class Item ")); } - private String constructJdbcConnectionString() { - return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; - } - private static final String hibernateToolTaskXml = " \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 6e1cb14fcc..503c17c8e4 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 @@ -1,33 +1,16 @@ package org.hibernate.tool.ant.tutorial; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.nio.file.Files; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.Statement; import org.hibernate.tool.it.ant.TestTemplate; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class TutorialTestIT extends TestTemplate { - private File buildXmlFile; - private File databaseFile; - private File personFile; - - @BeforeEach - public void beforeEach() { - databaseFile = new File(getProjectDir(), "database/test.mv.db"); - assertFalse(databaseFile.exists()); - personFile = new File(getProjectDir(), "generated/Person.java"); - assertFalse(personFile.exists()); - } - @Test public void testTutorial() throws Exception { createBuildXmlFile(); @@ -41,23 +24,12 @@ protected String hibernateToolTaskXml() { return hibernateToolTaskXml; } - private void createBuildXmlFile() throws Exception { - buildXmlFile = new File(getProjectDir(), "build.xml"); - assertFalse(buildXmlFile.exists()); - Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); - } - - private void createDatabase() throws Exception { - String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; - Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); - Statement statement = connection.createStatement(); - statement.execute(CREATE_PERSON_TABLE); - statement.close(); - connection.close(); - assertTrue(databaseFile.exists()); - assertTrue(databaseFile.isFile()); + 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(); @@ -82,10 +54,6 @@ private void verifyResult() { assertTrue(generatedPersonJavaFile.isFile()); } - private String constructJdbcConnectionString() { - return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; - } - private static final String hibernateToolTaskXml = " \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 1865b94475..7cbfbb9289 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 @@ -6,7 +6,13 @@ import org.junit.jupiter.api.io.TempDir; 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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class TestTemplate { @@ -19,6 +25,8 @@ protected File getProjectDir() { protected abstract String hibernateToolTaskXml(); + protected abstract String[] createDatabaseScript(); + protected String constructBuildXmlFileContents() { return buildXmlFileContents.replace("@hibernateToolTaskXml@", hibernateToolTaskXml()); } @@ -32,6 +40,32 @@ protected void runAntBuild() { project.executeTarget(project.getDefaultTarget()); } + protected void createBuildXmlFile() throws Exception { + File buildXmlFile = new File(getProjectDir(), "build.xml"); + assertFalse(buildXmlFile.exists()); + Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); + assertTrue(buildXmlFile.exists()); + } + + protected String constructJdbcConnectionString() { + return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; + } + + protected void createDatabase() throws Exception { + File databaseFile = new File(getProjectDir(), "database/test.mv.db"); + assertFalse(databaseFile.exists()); + assertFalse(databaseFile.isFile()); + Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); + Statement statement = connection.createStatement(); + for (String sql : createDatabaseScript()) { + statement.execute(sql); + } + statement.close(); + connection.close(); + assertTrue(databaseFile.exists()); + assertTrue(databaseFile.isFile()); + } + private DefaultLogger getConsoleLogger() { DefaultLogger consoleLogger = new DefaultLogger(); consoleLogger.setErrorPrintStream(System.err);