Skip to content

Commit 1b7e7d4

Browse files
committed
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 <[email protected]>
1 parent acd5bb5 commit 1b7e7d4

File tree

6 files changed

+97
-167
lines changed

6 files changed

+97
-167
lines changed

ant/src/it/java/org/hibernate/tool/ant/hbm2java/JpaDefaultTestIT.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,24 @@ public class JpaDefaultTestIT extends TestTemplate {
1313

1414
@Test
1515
public void testJpaDefault() throws Exception {
16+
setHibernateToolTaskXml(
17+
"""
18+
<hibernatetool destdir='generated'> \s
19+
<jdbcconfiguration propertyfile='hibernate.properties'/>\s
20+
<hbm2java/> \s
21+
</hibernatetool> \s
22+
"""
23+
);
24+
setDatabaseCreationScript(new String[] {
25+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
26+
});
1627
createBuildXmlFile();
1728
createDatabase();
1829
createHibernatePropertiesFile();
1930
runAntBuild();
2031
verifyResult();
2132
}
2233

23-
protected String hibernateToolTaskXml() {
24-
return hibernateToolTaskXml;
25-
}
26-
27-
protected String[] createDatabaseScript() {
28-
return new String[] {
29-
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
30-
};
31-
}
32-
33-
private void createHibernatePropertiesFile() throws Exception {
34-
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
35-
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
36-
hibernatePropertiesFileContents
37-
.append("hibernate.connection.driver_class=org.h2.Driver\n")
38-
.append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n")
39-
.append("hibernate.connection.username=\n")
40-
.append("hibernate.connection.password=\n")
41-
.append("hibernate.default_catalog=TEST\n")
42-
.append("hibernate.default_schema=PUBLIC\n");
43-
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString());
44-
assertTrue(hibernatePropertiesFile.exists());
45-
}
46-
4734
private void verifyResult() throws Exception {
4835
File generatedOutputFolder = new File(getProjectDir(), "generated");
4936
assertTrue(generatedOutputFolder.exists());
@@ -58,10 +45,4 @@ private void verifyResult() throws Exception {
5845
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
5946
}
6047

61-
private static final String hibernateToolTaskXml =
62-
" <hibernatetool destdir='generated'> \n" +
63-
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
64-
" <hbm2java/> \n" +
65-
" </hibernatetool> \n" ;
66-
6748
}

ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoAnnotationsTestIT.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,24 @@ public class NoAnnotationsTestIT extends TestTemplate {
1414

1515
@Test
1616
public void testNoAnnotations() throws Exception {
17+
setHibernateToolTaskXml(
18+
"""
19+
<hibernatetool destdir='generated'> \s
20+
<jdbcconfiguration propertyfile='hibernate.properties'/>\s
21+
<hbm2java ejb3='false'/> \s
22+
</hibernatetool> \s
23+
"""
24+
);
25+
setDatabaseCreationScript(new String[] {
26+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
27+
});
1728
createBuildXmlFile();
1829
createDatabase();
1930
createHibernatePropertiesFile();
2031
runAntBuild();
2132
verifyResult();
2233
}
2334

24-
protected String hibernateToolTaskXml() {
25-
return hibernateToolTaskXml;
26-
}
27-
28-
protected String[] createDatabaseScript() {
29-
return new String[] {
30-
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
31-
};
32-
}
33-
34-
private void createHibernatePropertiesFile() throws Exception {
35-
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
36-
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
37-
hibernatePropertiesFileContents
38-
.append("hibernate.connection.driver_class=org.h2.Driver\n")
39-
.append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n")
40-
.append("hibernate.connection.username=\n")
41-
.append("hibernate.connection.password=\n")
42-
.append("hibernate.default_catalog=TEST\n")
43-
.append("hibernate.default_schema=PUBLIC\n");
44-
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString());
45-
assertTrue(hibernatePropertiesFile.exists());
46-
}
47-
4835
private void verifyResult() throws Exception {
4936
File generatedOutputFolder = new File(getProjectDir(), "generated");
5037
assertTrue(generatedOutputFolder.exists());
@@ -59,9 +46,4 @@ private void verifyResult() throws Exception {
5946
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
6047
}
6148

62-
private static final String hibernateToolTaskXml =
63-
" <hibernatetool destdir='generated'> \n" +
64-
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
65-
" <hbm2java ejb3='false'/> \n" +
66-
" </hibernatetool> \n" ;
6749
}

ant/src/it/java/org/hibernate/tool/ant/hbm2java/NoGenericsTestIT.java

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,27 @@ public class NoGenericsTestIT extends TestTemplate {
1414

1515
@Test
1616
public void testUseGenerics() throws Exception {
17+
setHibernateToolTaskXml(
18+
"""
19+
<hibernatetool destdir='generated'> \s
20+
<jdbcconfiguration propertyfile='hibernate.properties'/>\s
21+
<hbm2java jdk5='false'/> \s
22+
</hibernatetool> \s
23+
"""
24+
);
25+
setDatabaseCreationScript(new String[] {
26+
"create table PERSON (ID int not null, NAME varchar(20), " +
27+
"primary key (ID))",
28+
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
29+
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
30+
});
1731
createBuildXmlFile();
1832
createDatabase();
1933
createHibernatePropertiesFile();
2034
runAntBuild();
2135
verifyResult();
2236
}
2337

24-
protected String hibernateToolTaskXml() {
25-
return hibernateToolTaskXml;
26-
}
27-
28-
protected String[] createDatabaseScript() {
29-
return new String[] {
30-
"create table PERSON (ID int not null, NAME varchar(20), " +
31-
"primary key (ID))",
32-
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
33-
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
34-
};
35-
}
36-
37-
private void createHibernatePropertiesFile() throws Exception {
38-
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
39-
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
40-
hibernatePropertiesFileContents
41-
.append("hibernate.connection.driver_class=org.h2.Driver\n")
42-
.append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n")
43-
.append("hibernate.connection.username=\n")
44-
.append("hibernate.connection.password=\n")
45-
.append("hibernate.default_catalog=TEST\n")
46-
.append("hibernate.default_schema=PUBLIC\n");
47-
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString());
48-
assertTrue(hibernatePropertiesFile.exists());
49-
}
50-
5138
private void verifyResult() throws Exception {
5239
File generatedOutputFolder = new File(getProjectDir(), "generated");
5340
assertTrue(generatedOutputFolder.exists());
@@ -68,10 +55,4 @@ private void verifyResult() throws Exception {
6855
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
6956
}
7057

71-
private static final String hibernateToolTaskXml =
72-
" <hibernatetool destdir='generated'> \n" +
73-
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
74-
" <hbm2java jdk5='false'/> \n" +
75-
" </hibernatetool> \n" ;
76-
7758
}

ant/src/it/java/org/hibernate/tool/ant/hbm2java/UseGenericsTestIT.java

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,27 @@ public class UseGenericsTestIT extends TestTemplate {
1313

1414
@Test
1515
public void testUseGenerics() throws Exception {
16+
setHibernateToolTaskXml(
17+
"""
18+
<hibernatetool destdir='generated'> \s
19+
<jdbcconfiguration propertyfile='hibernate.properties'/>\s
20+
<hbm2java/> \s
21+
</hibernatetool> \s
22+
"""
23+
);
24+
setDatabaseCreationScript(new String[] {
25+
"create table PERSON (ID int not null, NAME varchar(20), " +
26+
"primary key (ID))",
27+
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
28+
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
29+
});
1630
createBuildXmlFile();
1731
createDatabase();
1832
createHibernatePropertiesFile();
1933
runAntBuild();
2034
verifyResult();
2135
}
2236

23-
protected String hibernateToolTaskXml() {
24-
return hibernateToolTaskXml;
25-
}
26-
27-
protected String[] createDatabaseScript() {
28-
return new String[] {
29-
"create table PERSON (ID int not null, NAME varchar(20), " +
30-
"primary key (ID))",
31-
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
32-
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
33-
};
34-
}
35-
36-
private void createHibernatePropertiesFile() throws Exception {
37-
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
38-
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
39-
hibernatePropertiesFileContents
40-
.append("hibernate.connection.driver_class=org.h2.Driver\n")
41-
.append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n")
42-
.append("hibernate.connection.username=\n")
43-
.append("hibernate.connection.password=\n")
44-
.append("hibernate.default_catalog=TEST\n")
45-
.append("hibernate.default_schema=PUBLIC\n");
46-
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString());
47-
assertTrue(hibernatePropertiesFile.exists());
48-
}
49-
5037
private void verifyResult() throws Exception {
5138
File generatedOutputFolder = new File(getProjectDir(), "generated");
5239
assertTrue(generatedOutputFolder.exists());
@@ -67,9 +54,4 @@ private void verifyResult() throws Exception {
6754
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
6855
}
6956

70-
private static final String hibernateToolTaskXml =
71-
" <hibernatetool destdir='generated'> \n" +
72-
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
73-
" <hbm2java/> \n" +
74-
" </hibernatetool> \n" ;
7557
}

ant/src/it/java/org/hibernate/tool/ant/tutorial/TutorialTestIT.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,24 @@ public class TutorialTestIT extends TestTemplate {
1313

1414
@Test
1515
public void testTutorial() throws Exception {
16+
setHibernateToolTaskXml(
17+
"""
18+
<hibernatetool destdir='generated'> \s
19+
<jdbcconfiguration propertyfile='hibernate.properties'/>\s
20+
<hbm2java/> \s
21+
</hibernatetool> \s
22+
"""
23+
);
24+
setDatabaseCreationScript(new String[] {
25+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
26+
});
1627
createBuildXmlFile();
1728
createDatabase();
1829
createHibernatePropertiesFile();
1930
runAntBuild();
2031
verifyResult();
2132
}
2233

23-
protected String hibernateToolTaskXml() {
24-
return hibernateToolTaskXml;
25-
}
26-
27-
protected String[] createDatabaseScript() {
28-
return new String[] {
29-
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
30-
};
31-
}
32-
33-
private void createHibernatePropertiesFile() throws Exception {
34-
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
35-
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
36-
hibernatePropertiesFileContents
37-
.append("hibernate.connection.driver_class=org.h2.Driver\n")
38-
.append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n")
39-
.append("hibernate.connection.username=\n")
40-
.append("hibernate.connection.password=\n")
41-
.append("hibernate.default_catalog=TEST\n")
42-
.append("hibernate.default_schema=PUBLIC\n");
43-
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString());
44-
assertTrue(hibernatePropertiesFile.exists());
45-
}
46-
4734
private void verifyResult() {
4835
File generatedOutputFolder = new File(getProjectDir(), "generated");
4936
assertTrue(generatedOutputFolder.exists());
@@ -54,9 +41,4 @@ private void verifyResult() {
5441
assertTrue(generatedPersonJavaFile.isFile());
5542
}
5643

57-
private static final String hibernateToolTaskXml =
58-
" <hibernatetool destdir='generated'> \n" +
59-
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
60-
" <hbm2java/> \n" +
61-
" </hibernatetool> \n" ;
6244
}

ant/src/it/java/org/hibernate/tool/it/ant/TestTemplate.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@
1111
import java.sql.DriverManager;
1212
import java.sql.Statement;
1313

14-
import static org.junit.jupiter.api.Assertions.assertFalse;
15-
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
import static org.junit.jupiter.api.Assertions.*;
1615

1716
public abstract class TestTemplate {
1817

1918
@TempDir
2019
private File projectDir;
2120

21+
private String hibernateToolTaskXml;
22+
private String[] databaseCreationScript;
23+
2224
protected File getProjectDir() {
2325
return projectDir;
2426
}
2527

26-
protected abstract String hibernateToolTaskXml();
28+
protected void setHibernateToolTaskXml(String xml) {
29+
this.hibernateToolTaskXml = xml;
30+
}
2731

28-
protected abstract String[] createDatabaseScript();
32+
protected void setDatabaseCreationScript(String[] sqls) {
33+
this.databaseCreationScript = sqls;
34+
}
2935

3036
protected String constructBuildXmlFileContents() {
31-
return buildXmlFileContents.replace("@hibernateToolTaskXml@", hibernateToolTaskXml());
37+
assertNotNull(hibernateToolTaskXml);
38+
return buildXmlFileContents.replace("@hibernateToolTaskXml@", hibernateToolTaskXml);
3239
}
3340

3441
protected void runAntBuild() {
@@ -57,7 +64,7 @@ protected void createDatabase() throws Exception {
5764
assertFalse(databaseFile.isFile());
5865
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
5966
Statement statement = connection.createStatement();
60-
for (String sql : createDatabaseScript()) {
67+
for (String sql : databaseCreationScript) {
6168
statement.execute(sql);
6269
}
6370
statement.close();
@@ -66,6 +73,19 @@ protected void createDatabase() throws Exception {
6673
assertTrue(databaseFile.isFile());
6774
}
6875

76+
protected void createHibernatePropertiesFile() throws Exception {
77+
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
78+
String hibernatePropertiesFileContents =
79+
"hibernate.connection.driver_class=org.h2.Driver\n" +
80+
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
81+
"hibernate.connection.username=\n" +
82+
"hibernate.connection.password=\n" +
83+
"hibernate.default_catalog=TEST\n" +
84+
"hibernate.default_schema=PUBLIC\n";
85+
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
86+
assertTrue(hibernatePropertiesFile.exists());
87+
}
88+
6989
private DefaultLogger getConsoleLogger() {
7090
DefaultLogger consoleLogger = new DefaultLogger();
7191
consoleLogger.setErrorPrintStream(System.err);
@@ -75,13 +95,15 @@ private DefaultLogger getConsoleLogger() {
7595
}
7696

7797
private static final String buildXmlFileContents =
78-
"<project name='tutorial' default='reveng'> \n" +
79-
" <taskdef \n" +
80-
" name='hibernatetool' \n" +
81-
" classname='org.hibernate.tool.ant.HibernateToolTask'/> \n" +
82-
" <target name='reveng'> \n" +
83-
"@hibernateToolTaskXml@" +
84-
" </target> \n" +
85-
"</project> \n" ;
98+
"""
99+
<project name='tutorial' default='reveng'> \s
100+
<taskdef \s
101+
name='hibernatetool' \s
102+
classname='org.hibernate.tool.ant.HibernateToolTask'/> \s
103+
<target name='reveng'> \s
104+
@hibernateToolTaskXml@\
105+
</target> \s
106+
</project> \s
107+
""";
86108

87109
}

0 commit comments

Comments
 (0)