Skip to content

Commit b051aa6

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 ebd7c6a commit b051aa6

File tree

6 files changed

+77
-159
lines changed

6 files changed

+77
-159
lines changed

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

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

1414
@Test
1515
public void testJpaDefault() throws Exception {
16+
setHibernateToolTaskXml(
17+
" <hibernatetool destdir='generated'> \n" +
18+
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
19+
" <hbm2java/> \n" +
20+
" </hibernatetool> \n"
21+
);
22+
setDatabaseCreationScript(new String[] {
23+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
24+
});
1625
createBuildXmlFile();
1726
createDatabase();
1827
createHibernatePropertiesFile();
1928
runAntBuild();
2029
verifyResult();
2130
}
2231

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-
4732
private void verifyResult() throws Exception {
4833
File generatedOutputFolder = new File(getProjectDir(), "generated");
4934
assertTrue(generatedOutputFolder.exists());
@@ -58,10 +43,4 @@ private void verifyResult() throws Exception {
5843
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
5944
}
6045

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

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

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

1515
@Test
1616
public void testNoAnnotations() throws Exception {
17+
setHibernateToolTaskXml(
18+
" <hibernatetool destdir='generated'> \n" +
19+
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
20+
" <hbm2java ejb3='false'/> \n" +
21+
" </hibernatetool> \n"
22+
);
23+
setDatabaseCreationScript(new String[] {
24+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
25+
});
1726
createBuildXmlFile();
1827
createDatabase();
1928
createHibernatePropertiesFile();
2029
runAntBuild();
2130
verifyResult();
2231
}
2332

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-
4833
private void verifyResult() throws Exception {
4934
File generatedOutputFolder = new File(getProjectDir(), "generated");
5035
assertTrue(generatedOutputFolder.exists());
@@ -59,9 +44,4 @@ private void verifyResult() throws Exception {
5944
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
6045
}
6146

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" ;
6747
}

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

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

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

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-
5136
private void verifyResult() throws Exception {
5237
File generatedOutputFolder = new File(getProjectDir(), "generated");
5338
assertTrue(generatedOutputFolder.exists());
@@ -68,10 +53,4 @@ private void verifyResult() throws Exception {
6853
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
6954
}
7055

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-
7756
}

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

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

1414
@Test
1515
public void testUseGenerics() throws Exception {
16+
setHibernateToolTaskXml(
17+
" <hibernatetool destdir='generated'> \n" +
18+
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
19+
" <hbm2java/> \n" +
20+
" </hibernatetool> \n"
21+
);
22+
setDatabaseCreationScript(new String[] {
23+
"create table PERSON (ID int not null, NAME varchar(20), " +
24+
"primary key (ID))",
25+
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
26+
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
27+
});
1628
createBuildXmlFile();
1729
createDatabase();
1830
createHibernatePropertiesFile();
1931
runAntBuild();
2032
verifyResult();
2133
}
2234

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-
5035
private void verifyResult() throws Exception {
5136
File generatedOutputFolder = new File(getProjectDir(), "generated");
5237
assertTrue(generatedOutputFolder.exists());
@@ -67,9 +52,4 @@ private void verifyResult() throws Exception {
6752
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
6853
}
6954

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

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

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

1414
@Test
1515
public void testTutorial() throws Exception {
16+
setHibernateToolTaskXml(
17+
" <hibernatetool destdir='generated'> \n" +
18+
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
19+
" <hbm2java/> \n" +
20+
" </hibernatetool> \n"
21+
);
22+
setDatabaseCreationScript(new String[] {
23+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
24+
});
1625
createBuildXmlFile();
1726
createDatabase();
1827
createHibernatePropertiesFile();
1928
runAntBuild();
2029
verifyResult();
2130
}
2231

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-
4732
private void verifyResult() {
4833
File generatedOutputFolder = new File(getProjectDir(), "generated");
4934
assertTrue(generatedOutputFolder.exists());
@@ -54,9 +39,4 @@ private void verifyResult() {
5439
assertTrue(generatedPersonJavaFile.isFile());
5540
}
5641

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

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

Lines changed: 26 additions & 6 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);

0 commit comments

Comments
 (0)