Skip to content

Commit acd5bb5

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

File tree

6 files changed

+83
-201
lines changed

6 files changed

+83
-201
lines changed

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

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
package org.hibernate.tool.ant.hbm2java;
22

3-
import org.hibernate.tool.it.ant.TestTemplate;
4-
import org.junit.jupiter.api.BeforeEach;
5-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
65

76
import java.io.File;
87
import java.nio.file.Files;
9-
import java.sql.Connection;
10-
import java.sql.DriverManager;
11-
import java.sql.Statement;
128

13-
import static org.junit.jupiter.api.Assertions.*;
9+
import org.hibernate.tool.it.ant.TestTemplate;
10+
import org.junit.jupiter.api.Test;
1411

1512
public class JpaDefaultTestIT extends TestTemplate {
16-
17-
private File buildXmlFile;
18-
private File databaseFile;
19-
private File personFile;
20-
21-
@BeforeEach
22-
public void beforeEach() {
23-
databaseFile = new File(getProjectDir(), "database/test.mv.db");
24-
assertFalse(databaseFile.exists());
25-
personFile = new File(getProjectDir(), "generated/Person.java");
26-
assertFalse(personFile.exists());
27-
}
28-
13+
2914
@Test
3015
public void testJpaDefault() throws Exception {
3116
createBuildXmlFile();
@@ -39,23 +24,12 @@ protected String hibernateToolTaskXml() {
3924
return hibernateToolTaskXml;
4025
}
4126

42-
private void createBuildXmlFile() throws Exception {
43-
buildXmlFile = new File(getProjectDir(), "build.xml");
44-
assertFalse(buildXmlFile.exists());
45-
Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents());
46-
}
47-
48-
private void createDatabase() throws Exception {
49-
String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
50-
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
51-
Statement statement = connection.createStatement();
52-
statement.execute(CREATE_PERSON_TABLE);
53-
statement.close();
54-
connection.close();
55-
assertTrue(databaseFile.exists());
56-
assertTrue(databaseFile.isFile());
27+
protected String[] createDatabaseScript() {
28+
return new String[] {
29+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
30+
};
5731
}
58-
32+
5933
private void createHibernatePropertiesFile() throws Exception {
6034
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6135
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
@@ -84,10 +58,6 @@ private void verifyResult() throws Exception {
8458
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
8559
}
8660

87-
private String constructJdbcConnectionString() {
88-
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
89-
}
90-
9161
private static final String hibernateToolTaskXml =
9262
" <hibernatetool destdir='generated'> \n" +
9363
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +

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

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
package org.hibernate.tool.ant.hbm2java;
22

3-
import org.hibernate.tool.it.ant.TestTemplate;
4-
import org.junit.jupiter.api.BeforeEach;
5-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
66

77
import java.io.File;
88
import java.nio.file.Files;
9-
import java.sql.Connection;
10-
import java.sql.DriverManager;
11-
import java.sql.Statement;
129

13-
import static org.junit.jupiter.api.Assertions.*;
10+
import org.hibernate.tool.it.ant.TestTemplate;
11+
import org.junit.jupiter.api.Test;
1412

1513
public class NoAnnotationsTestIT extends TestTemplate {
1614

17-
private File buildXmlFile;
18-
private File databaseFile;
19-
private File personFile;
20-
21-
@BeforeEach
22-
public void beforeEach() {
23-
databaseFile = new File(getProjectDir(), "database/test.mv.db");
24-
assertFalse(databaseFile.exists());
25-
personFile = new File(getProjectDir(), "generated/Person.java");
26-
assertFalse(personFile.exists());
27-
}
28-
2915
@Test
3016
public void testNoAnnotations() throws Exception {
3117
createBuildXmlFile();
@@ -39,23 +25,12 @@ protected String hibernateToolTaskXml() {
3925
return hibernateToolTaskXml;
4026
}
4127

42-
private void createBuildXmlFile() throws Exception {
43-
buildXmlFile = new File(getProjectDir(), "build.xml");
44-
assertFalse(buildXmlFile.exists());
45-
Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents());
46-
}
47-
48-
private void createDatabase() throws Exception {
49-
String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
50-
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
51-
Statement statement = connection.createStatement();
52-
statement.execute(CREATE_PERSON_TABLE);
53-
statement.close();
54-
connection.close();
55-
assertTrue(databaseFile.exists());
56-
assertTrue(databaseFile.isFile());
28+
protected String[] createDatabaseScript() {
29+
return new String[] {
30+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
31+
};
5732
}
58-
33+
5934
private void createHibernatePropertiesFile() throws Exception {
6035
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6136
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
@@ -84,10 +59,6 @@ private void verifyResult() throws Exception {
8459
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
8560
}
8661

87-
private String constructJdbcConnectionString() {
88-
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
89-
}
90-
9162
private static final String hibernateToolTaskXml =
9263
" <hibernatetool destdir='generated'> \n" +
9364
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +

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

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
package org.hibernate.tool.ant.hbm2java;
22

3-
import org.hibernate.tool.it.ant.TestTemplate;
4-
import org.junit.jupiter.api.BeforeEach;
5-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
66

77
import java.io.File;
88
import java.nio.file.Files;
9-
import java.sql.Connection;
10-
import java.sql.DriverManager;
11-
import java.sql.Statement;
129

13-
import static org.junit.jupiter.api.Assertions.*;
10+
import org.hibernate.tool.it.ant.TestTemplate;
11+
import org.junit.jupiter.api.Test;
1412

1513
public class NoGenericsTestIT extends TestTemplate {
1614

17-
private File buildXmlFile;
18-
private File databaseFile;
19-
private File personFile;
20-
21-
@BeforeEach
22-
public void beforeEach() {
23-
databaseFile = new File(getProjectDir(), "database/test.mv.db");
24-
assertFalse(databaseFile.exists());
25-
personFile = new File(getProjectDir(), "generated/Person.java");
26-
assertFalse(personFile.exists());
27-
}
28-
2915
@Test
3016
public void testUseGenerics() throws Exception {
3117
createBuildXmlFile();
@@ -39,27 +25,15 @@ protected String hibernateToolTaskXml() {
3925
return hibernateToolTaskXml;
4026
}
4127

42-
private void createBuildXmlFile() throws Exception {
43-
buildXmlFile = new File(getProjectDir(), "build.xml");
44-
assertFalse(buildXmlFile.exists());
45-
Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents());
46-
}
47-
48-
private void createDatabase() throws Exception {
49-
String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
50-
String CREATE_ITEM_TABLE =
28+
protected String[] createDatabaseScript() {
29+
return new String[] {
30+
"create table PERSON (ID int not null, NAME varchar(20), " +
31+
"primary key (ID))",
5132
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
52-
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))";
53-
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
54-
Statement statement = connection.createStatement();
55-
statement.execute(CREATE_PERSON_TABLE);
56-
statement.execute(CREATE_ITEM_TABLE);
57-
statement.close();
58-
connection.close();
59-
assertTrue(databaseFile.exists());
60-
assertTrue(databaseFile.isFile());
33+
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
34+
};
6135
}
62-
36+
6337
private void createHibernatePropertiesFile() throws Exception {
6438
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6539
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
@@ -94,10 +68,6 @@ private void verifyResult() throws Exception {
9468
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
9569
}
9670

97-
private String constructJdbcConnectionString() {
98-
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
99-
}
100-
10171
private static final String hibernateToolTaskXml =
10272
" <hibernatetool destdir='generated'> \n" +
10373
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +

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

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
package org.hibernate.tool.ant.hbm2java;
22

3-
import org.hibernate.tool.it.ant.TestTemplate;
4-
import org.junit.jupiter.api.BeforeEach;
5-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
65

76
import java.io.File;
87
import java.nio.file.Files;
9-
import java.sql.Connection;
10-
import java.sql.DriverManager;
11-
import java.sql.Statement;
128

13-
import static org.junit.jupiter.api.Assertions.*;
9+
import org.hibernate.tool.it.ant.TestTemplate;
10+
import org.junit.jupiter.api.Test;
1411

1512
public class UseGenericsTestIT extends TestTemplate {
1613

17-
private File buildXmlFile;
18-
private File databaseFile;
19-
private File personFile;
20-
21-
@BeforeEach
22-
public void beforeEach() {
23-
databaseFile = new File(getProjectDir(), "database/test.mv.db");
24-
assertFalse(databaseFile.exists());
25-
personFile = new File(getProjectDir(), "generated/Person.java");
26-
assertFalse(personFile.exists());
27-
}
28-
2914
@Test
3015
public void testUseGenerics() throws Exception {
3116
createBuildXmlFile();
@@ -39,27 +24,15 @@ protected String hibernateToolTaskXml() {
3924
return hibernateToolTaskXml;
4025
}
4126

42-
private void createBuildXmlFile() throws Exception {
43-
buildXmlFile = new File(getProjectDir(), "build.xml");
44-
assertFalse(buildXmlFile.exists());
45-
Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents());
46-
}
47-
48-
private void createDatabase() throws Exception {
49-
String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
50-
String CREATE_ITEM_TABLE =
27+
protected String[] createDatabaseScript() {
28+
return new String[] {
29+
"create table PERSON (ID int not null, NAME varchar(20), " +
30+
"primary key (ID))",
5131
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
52-
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))";
53-
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
54-
Statement statement = connection.createStatement();
55-
statement.execute(CREATE_PERSON_TABLE);
56-
statement.execute(CREATE_ITEM_TABLE);
57-
statement.close();
58-
connection.close();
59-
assertTrue(databaseFile.exists());
60-
assertTrue(databaseFile.isFile());
32+
"primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
33+
};
6134
}
62-
35+
6336
private void createHibernatePropertiesFile() throws Exception {
6437
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6538
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
@@ -94,10 +67,6 @@ private void verifyResult() throws Exception {
9467
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
9568
}
9669

97-
private String constructJdbcConnectionString() {
98-
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
99-
}
100-
10170
private static final String hibernateToolTaskXml =
10271
" <hibernatetool destdir='generated'> \n" +
10372
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +

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

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
package org.hibernate.tool.ant.tutorial;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertFalse;
54
import static org.junit.jupiter.api.Assertions.assertTrue;
65

76
import java.io.File;
87
import java.nio.file.Files;
9-
import java.sql.Connection;
10-
import java.sql.DriverManager;
11-
import java.sql.Statement;
128

139
import org.hibernate.tool.it.ant.TestTemplate;
14-
import org.junit.jupiter.api.BeforeEach;
1510
import org.junit.jupiter.api.Test;
1611

1712
public class TutorialTestIT extends TestTemplate {
1813

19-
private File buildXmlFile;
20-
private File databaseFile;
21-
private File personFile;
22-
23-
@BeforeEach
24-
public void beforeEach() {
25-
databaseFile = new File(getProjectDir(), "database/test.mv.db");
26-
assertFalse(databaseFile.exists());
27-
personFile = new File(getProjectDir(), "generated/Person.java");
28-
assertFalse(personFile.exists());
29-
}
30-
3114
@Test
3215
public void testTutorial() throws Exception {
3316
createBuildXmlFile();
@@ -41,23 +24,12 @@ protected String hibernateToolTaskXml() {
4124
return hibernateToolTaskXml;
4225
}
4326

44-
private void createBuildXmlFile() throws Exception {
45-
buildXmlFile = new File(getProjectDir(), "build.xml");
46-
assertFalse(buildXmlFile.exists());
47-
Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents());
48-
}
49-
50-
private void createDatabase() throws Exception {
51-
String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
52-
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
53-
Statement statement = connection.createStatement();
54-
statement.execute(CREATE_PERSON_TABLE);
55-
statement.close();
56-
connection.close();
57-
assertTrue(databaseFile.exists());
58-
assertTrue(databaseFile.isFile());
27+
protected String[] createDatabaseScript() {
28+
return new String[] {
29+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
30+
};
5931
}
60-
32+
6133
private void createHibernatePropertiesFile() throws Exception {
6234
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6335
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
@@ -82,10 +54,6 @@ private void verifyResult() {
8254
assertTrue(generatedPersonJavaFile.isFile());
8355
}
8456

85-
private String constructJdbcConnectionString() {
86-
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
87-
}
88-
8957
private static final String hibernateToolTaskXml =
9058
" <hibernatetool destdir='generated'> \n" +
9159
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +

0 commit comments

Comments
 (0)