Skip to content

Commit a9bd44a

Browse files
committed
HBX-3068: Refactor the Ant integration tests to factor out common code
- Create class 'org.hibernate.tool.it.ant.TestTemplate' - Make the integration test classes inherit from TestTemplate - Pull up the 'projectDir' instance variable to TestTemplate Signed-off-by: Koen Aers <[email protected]>
1 parent f9fb689 commit a9bd44a

File tree

6 files changed

+61
-55
lines changed

6 files changed

+61
-55
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.tools.ant.DefaultLogger;
44
import org.apache.tools.ant.Project;
55
import org.apache.tools.ant.ProjectHelper;
6+
import org.hibernate.tool.it.ant.TestTemplate;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.io.TempDir;
@@ -17,10 +18,7 @@
1718

1819
import static org.junit.jupiter.api.Assertions.*;
1920

20-
public class JpaDefaultTestIT {
21-
22-
@TempDir
23-
private File projectDir;
21+
public class JpaDefaultTestIT extends TestTemplate {
2422

2523
private File buildXmlFile;
2624
private ByteArrayOutputStream output;
@@ -30,9 +28,9 @@ public class JpaDefaultTestIT {
3028
@BeforeEach
3129
public void beforeEach() {
3230
output = new ByteArrayOutputStream();
33-
databaseFile = new File(projectDir, "database/test.mv.db");
31+
databaseFile = new File(getProjectDir(), "database/test.mv.db");
3432
assertFalse(databaseFile.exists());
35-
personFile = new File(projectDir, "generated/Person.java");
33+
personFile = new File(getProjectDir(), "generated/Person.java");
3634
assertFalse(personFile.exists());
3735
}
3836

@@ -46,7 +44,7 @@ public void testJpaDefault() throws Exception {
4644
}
4745

4846
private void createBuildXmlFile() throws Exception {
49-
buildXmlFile = new File(projectDir, "build.xml");
47+
buildXmlFile = new File(getProjectDir(), "build.xml");
5048
assertFalse(buildXmlFile.exists());
5149
Files.writeString(buildXmlFile.toPath(), buildXmlFileContents);
5250
}
@@ -63,7 +61,7 @@ private void createDatabase() throws Exception {
6361
}
6462

6563
private void createHibernatePropertiesFile() throws Exception {
66-
File hibernatePropertiesFile = new File(projectDir, "hibernate.properties");
64+
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6765
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
6866
hibernatePropertiesFileContents
6967
.append("hibernate.connection.driver_class=org.h2.Driver\n")
@@ -78,14 +76,14 @@ private void createHibernatePropertiesFile() throws Exception {
7876

7977
private void runAntBuild() {
8078
Project project = new Project();
81-
project.setBaseDir(projectDir);
79+
project.setBaseDir(getProjectDir());
8280
project.addBuildListener(getConsoleLogger());
8381
ProjectHelper.getProjectHelper().parse(project, buildXmlFile);
8482
project.executeTarget(project.getDefaultTarget());
8583
}
8684

8785
private void verifyResult() throws Exception {
88-
File generatedOutputFolder = new File(projectDir, "generated");
86+
File generatedOutputFolder = new File(getProjectDir(), "generated");
8987
assertTrue(generatedOutputFolder.exists());
9088
assertTrue(generatedOutputFolder.isDirectory());
9189
assertEquals(1, generatedOutputFolder.list().length);
@@ -107,7 +105,7 @@ private DefaultLogger getConsoleLogger() {
107105
}
108106

109107
private String constructJdbcConnectionString() {
110-
return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
108+
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
111109
}
112110

113111
private static final String buildXmlFileContents =

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.tools.ant.DefaultLogger;
44
import org.apache.tools.ant.Project;
55
import org.apache.tools.ant.ProjectHelper;
6+
import org.hibernate.tool.it.ant.TestTemplate;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.io.TempDir;
@@ -17,10 +18,7 @@
1718

1819
import static org.junit.jupiter.api.Assertions.*;
1920

20-
public class NoAnnotationsTestIT {
21-
22-
@TempDir
23-
private File projectDir;
21+
public class NoAnnotationsTestIT extends TestTemplate {
2422

2523
private File buildXmlFile;
2624
private ByteArrayOutputStream output;
@@ -30,9 +28,9 @@ public class NoAnnotationsTestIT {
3028
@BeforeEach
3129
public void beforeEach() {
3230
output = new ByteArrayOutputStream();
33-
databaseFile = new File(projectDir, "database/test.mv.db");
31+
databaseFile = new File(getProjectDir(), "database/test.mv.db");
3432
assertFalse(databaseFile.exists());
35-
personFile = new File(projectDir, "generated/Person.java");
33+
personFile = new File(getProjectDir(), "generated/Person.java");
3634
assertFalse(personFile.exists());
3735
}
3836

@@ -46,7 +44,7 @@ public void testNoAnnotations() throws Exception {
4644
}
4745

4846
private void createBuildXmlFile() throws Exception {
49-
buildXmlFile = new File(projectDir, "build.xml");
47+
buildXmlFile = new File(getProjectDir(), "build.xml");
5048
assertFalse(buildXmlFile.exists());
5149
Files.writeString(buildXmlFile.toPath(), buildXmlFileContents);
5250
}
@@ -63,7 +61,7 @@ private void createDatabase() throws Exception {
6361
}
6462

6563
private void createHibernatePropertiesFile() throws Exception {
66-
File hibernatePropertiesFile = new File(projectDir, "hibernate.properties");
64+
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6765
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
6866
hibernatePropertiesFileContents
6967
.append("hibernate.connection.driver_class=org.h2.Driver\n")
@@ -78,14 +76,14 @@ private void createHibernatePropertiesFile() throws Exception {
7876

7977
private void runAntBuild() {
8078
Project project = new Project();
81-
project.setBaseDir(projectDir);
79+
project.setBaseDir(getProjectDir());
8280
project.addBuildListener(getConsoleLogger());
8381
ProjectHelper.getProjectHelper().parse(project, buildXmlFile);
8482
project.executeTarget(project.getDefaultTarget());
8583
}
8684

8785
private void verifyResult() throws Exception {
88-
File generatedOutputFolder = new File(projectDir, "generated");
86+
File generatedOutputFolder = new File(getProjectDir(), "generated");
8987
assertTrue(generatedOutputFolder.exists());
9088
assertTrue(generatedOutputFolder.isDirectory());
9189
assertEquals(1, generatedOutputFolder.list().length);
@@ -107,7 +105,7 @@ private DefaultLogger getConsoleLogger() {
107105
}
108106

109107
private String constructJdbcConnectionString() {
110-
return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
108+
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
111109
}
112110

113111
private static final String buildXmlFileContents =

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.tools.ant.DefaultLogger;
44
import org.apache.tools.ant.Project;
55
import org.apache.tools.ant.ProjectHelper;
6+
import org.hibernate.tool.it.ant.TestTemplate;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.io.TempDir;
@@ -17,10 +18,7 @@
1718

1819
import static org.junit.jupiter.api.Assertions.*;
1920

20-
public class NoGenericsTestIT {
21-
22-
@TempDir
23-
private File projectDir;
21+
public class NoGenericsTestIT extends TestTemplate {
2422

2523
private File buildXmlFile;
2624
private ByteArrayOutputStream output;
@@ -30,9 +28,9 @@ public class NoGenericsTestIT {
3028
@BeforeEach
3129
public void beforeEach() {
3230
output = new ByteArrayOutputStream();
33-
databaseFile = new File(projectDir, "database/test.mv.db");
31+
databaseFile = new File(getProjectDir(), "database/test.mv.db");
3432
assertFalse(databaseFile.exists());
35-
personFile = new File(projectDir, "generated/Person.java");
33+
personFile = new File(getProjectDir(), "generated/Person.java");
3634
assertFalse(personFile.exists());
3735
}
3836

@@ -46,7 +44,7 @@ public void testUseGenerics() throws Exception {
4644
}
4745

4846
private void createBuildXmlFile() throws Exception {
49-
buildXmlFile = new File(projectDir, "build.xml");
47+
buildXmlFile = new File(getProjectDir(), "build.xml");
5048
assertFalse(buildXmlFile.exists());
5149
Files.writeString(buildXmlFile.toPath(), buildXmlFileContents);
5250
}
@@ -67,7 +65,7 @@ private void createDatabase() throws Exception {
6765
}
6866

6967
private void createHibernatePropertiesFile() throws Exception {
70-
File hibernatePropertiesFile = new File(projectDir, "hibernate.properties");
68+
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
7169
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
7270
hibernatePropertiesFileContents
7371
.append("hibernate.connection.driver_class=org.h2.Driver\n")
@@ -82,14 +80,14 @@ private void createHibernatePropertiesFile() throws Exception {
8280

8381
private void runAntBuild() {
8482
Project project = new Project();
85-
project.setBaseDir(projectDir);
83+
project.setBaseDir(getProjectDir());
8684
project.addBuildListener(getConsoleLogger());
8785
ProjectHelper.getProjectHelper().parse(project, buildXmlFile);
8886
project.executeTarget(project.getDefaultTarget());
8987
}
9088

9189
private void verifyResult() throws Exception {
92-
File generatedOutputFolder = new File(projectDir, "generated");
90+
File generatedOutputFolder = new File(getProjectDir(), "generated");
9391
assertTrue(generatedOutputFolder.exists());
9492
assertTrue(generatedOutputFolder.isDirectory());
9593
assertEquals(2, generatedOutputFolder.list().length);
@@ -117,7 +115,7 @@ private DefaultLogger getConsoleLogger() {
117115
}
118116

119117
private String constructJdbcConnectionString() {
120-
return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
118+
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
121119
}
122120

123121
private static final String buildXmlFileContents =

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.tools.ant.DefaultLogger;
44
import org.apache.tools.ant.Project;
55
import org.apache.tools.ant.ProjectHelper;
6+
import org.hibernate.tool.it.ant.TestTemplate;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.io.TempDir;
@@ -17,10 +18,7 @@
1718

1819
import static org.junit.jupiter.api.Assertions.*;
1920

20-
public class UseGenericsTestIT {
21-
22-
@TempDir
23-
private File projectDir;
21+
public class UseGenericsTestIT extends TestTemplate {
2422

2523
private File buildXmlFile;
2624
private ByteArrayOutputStream output;
@@ -30,9 +28,9 @@ public class UseGenericsTestIT {
3028
@BeforeEach
3129
public void beforeEach() {
3230
output = new ByteArrayOutputStream();
33-
databaseFile = new File(projectDir, "database/test.mv.db");
31+
databaseFile = new File(getProjectDir(), "database/test.mv.db");
3432
assertFalse(databaseFile.exists());
35-
personFile = new File(projectDir, "generated/Person.java");
33+
personFile = new File(getProjectDir(), "generated/Person.java");
3634
assertFalse(personFile.exists());
3735
}
3836

@@ -46,7 +44,7 @@ public void testUseGenerics() throws Exception {
4644
}
4745

4846
private void createBuildXmlFile() throws Exception {
49-
buildXmlFile = new File(projectDir, "build.xml");
47+
buildXmlFile = new File(getProjectDir(), "build.xml");
5048
assertFalse(buildXmlFile.exists());
5149
Files.writeString(buildXmlFile.toPath(), buildXmlFileContents);
5250
}
@@ -67,7 +65,7 @@ private void createDatabase() throws Exception {
6765
}
6866

6967
private void createHibernatePropertiesFile() throws Exception {
70-
File hibernatePropertiesFile = new File(projectDir, "hibernate.properties");
68+
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
7169
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
7270
hibernatePropertiesFileContents
7371
.append("hibernate.connection.driver_class=org.h2.Driver\n")
@@ -82,14 +80,14 @@ private void createHibernatePropertiesFile() throws Exception {
8280

8381
private void runAntBuild() {
8482
Project project = new Project();
85-
project.setBaseDir(projectDir);
83+
project.setBaseDir(getProjectDir());
8684
project.addBuildListener(getConsoleLogger());
8785
ProjectHelper.getProjectHelper().parse(project, buildXmlFile);
8886
project.executeTarget(project.getDefaultTarget());
8987
}
9088

9189
private void verifyResult() throws Exception {
92-
File generatedOutputFolder = new File(projectDir, "generated");
90+
File generatedOutputFolder = new File(getProjectDir(), "generated");
9391
assertTrue(generatedOutputFolder.exists());
9492
assertTrue(generatedOutputFolder.isDirectory());
9593
assertEquals(2, generatedOutputFolder.list().length);
@@ -117,7 +115,7 @@ private DefaultLogger getConsoleLogger() {
117115
}
118116

119117
private String constructJdbcConnectionString() {
120-
return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
118+
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
121119
}
122120

123121
private static final String buildXmlFileContents =

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
import org.apache.tools.ant.DefaultLogger;
1616
import org.apache.tools.ant.Project;
1717
import org.apache.tools.ant.ProjectHelper;
18+
import org.hibernate.tool.it.ant.TestTemplate;
1819
import org.junit.jupiter.api.BeforeEach;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.io.TempDir;
2122

22-
public class TutorialTestIT {
23-
24-
@TempDir
25-
private File projectDir;
23+
public class TutorialTestIT extends TestTemplate {
2624

2725
private File buildXmlFile;
2826
private ByteArrayOutputStream output;
@@ -32,9 +30,9 @@ public class TutorialTestIT {
3230
@BeforeEach
3331
public void beforeEach() {
3432
output = new ByteArrayOutputStream();
35-
databaseFile = new File(projectDir, "database/test.mv.db");
33+
databaseFile = new File(getProjectDir(), "database/test.mv.db");
3634
assertFalse(databaseFile.exists());
37-
personFile = new File(projectDir, "generated/Person.java");
35+
personFile = new File(getProjectDir(), "generated/Person.java");
3836
assertFalse(personFile.exists());
3937
}
4038

@@ -48,7 +46,7 @@ public void testTutorial() throws Exception {
4846
}
4947

5048
private void createBuildXmlFile() throws Exception {
51-
buildXmlFile = new File(projectDir, "build.xml");
49+
buildXmlFile = new File(getProjectDir(), "build.xml");
5250
assertFalse(buildXmlFile.exists());
5351
Files.writeString(buildXmlFile.toPath(), buildXmlFileContents);
5452
}
@@ -65,7 +63,7 @@ private void createDatabase() throws Exception {
6563
}
6664

6765
private void createHibernatePropertiesFile() throws Exception {
68-
File hibernatePropertiesFile = new File(projectDir, "hibernate.properties");
66+
File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties");
6967
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
7068
hibernatePropertiesFileContents
7169
.append("hibernate.connection.driver_class=org.h2.Driver\n")
@@ -80,14 +78,14 @@ private void createHibernatePropertiesFile() throws Exception {
8078

8179
private void runAntBuild() {
8280
Project project = new Project();
83-
project.setBaseDir(projectDir);
81+
project.setBaseDir(getProjectDir());
8482
project.addBuildListener(getConsoleLogger());
8583
ProjectHelper.getProjectHelper().parse(project, buildXmlFile);
8684
project.executeTarget(project.getDefaultTarget());
8785
}
8886

8987
private void verifyResult() {
90-
File generatedOutputFolder = new File(projectDir, "generated");
88+
File generatedOutputFolder = new File(getProjectDir(), "generated");
9189
assertTrue(generatedOutputFolder.exists());
9290
assertTrue(generatedOutputFolder.isDirectory());
9391
assertEquals(1, generatedOutputFolder.list().length);
@@ -105,7 +103,7 @@ private DefaultLogger getConsoleLogger() {
105103
}
106104

107105
private String constructJdbcConnectionString() {
108-
return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
106+
return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
109107
}
110108

111109
private static final String buildXmlFileContents =
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hibernate.tool.it.ant;
2+
3+
import org.junit.jupiter.api.io.TempDir;
4+
5+
import java.io.File;
6+
7+
public class TestTemplate {
8+
9+
@TempDir
10+
private File projectDir;
11+
12+
protected File getProjectDir() {
13+
return projectDir;
14+
}
15+
16+
}

0 commit comments

Comments
 (0)