Skip to content

Commit 715810b

Browse files
committed
HBX-3080: Refactor the Gradle integration tests to factor out common code
- Create an instance variable 'hibernateToolsExtensionSection' in TestTemplate along with setter/getter - Pull up method 'TestTemplate#addHibernateToolsExtension(StringBuffer)' making use of the above instance variable - Set the instance variable above in the test method of the 'NoGenerics' and 'NoAnnotationsTest' test classes Signed-off-by: Koen Aers <[email protected]>
1 parent 6dcf578 commit 715810b

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

gradle/plugin/src/functionalTest/java/org/hibernate/tool/gradle/java/NoAnnotationsTest.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public void testTutorial() throws Exception {
2626
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))",
2727
"insert into PERSON values (1, 'foo')"
2828
});
29+
setHibernateToolsExtensionSection(
30+
"hibernateTools { \n" +
31+
" generateAnnotations=false \n" +
32+
"}"
33+
);
2934
createProject();
3035
executeGradleCommand("generateJava");
3136
verifyProject();
@@ -45,17 +50,4 @@ private void verifyProject() throws Exception {
4550
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
4651
}
4752

48-
protected void addHibernateToolsExtension(StringBuffer gradleBuildFileContents) {
49-
int pos = gradleBuildFileContents.indexOf("dependencies {");
50-
pos = gradleBuildFileContents.indexOf("}", pos);
51-
StringBuffer hibernateToolsExtension = new StringBuffer();
52-
hibernateToolsExtension
53-
.append("\n")
54-
.append("\n")
55-
.append("hibernateTools { \n")
56-
.append(" generateAnnotations=false \n")
57-
.append("}");
58-
gradleBuildFileContents.insert(pos + 1, hibernateToolsExtension.toString());
59-
}
60-
6153
}

gradle/plugin/src/functionalTest/java/org/hibernate/tool/gradle/java/NoGenerics.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public void testTutorial() throws Exception {
2626
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
2727
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
2828
});
29+
setHibernateToolsExtensionSection(
30+
"hibernateTools { \n" +
31+
" useGenerics=false \n" +
32+
"}"
33+
);
2934
createProject();
3035
executeGradleCommand("generateJava");
3136
verifyProject();
@@ -51,17 +56,4 @@ private void verifyProject() throws Exception {
5156
assertTrue(generatedItemJavaFileContents.contains("public class Item "));
5257
}
5358

54-
protected void addHibernateToolsExtension(StringBuffer gradleBuildFileContents) {
55-
int pos = gradleBuildFileContents.indexOf("dependencies {");
56-
pos = gradleBuildFileContents.indexOf("}", pos);
57-
StringBuffer hibernateToolsExtension = new StringBuffer();
58-
hibernateToolsExtension
59-
.append("\n")
60-
.append("\n")
61-
.append("hibernateTools { \n")
62-
.append(" useGenerics=false \n")
63-
.append("}");
64-
gradleBuildFileContents.insert(pos + 1, hibernateToolsExtension.toString());
65-
}
66-
6759
}

gradle/plugin/src/functionalTest/java/org/hibernate/tool/it/gradle/TestTemplate.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class TestTemplate {
2828
private File databaseFile;
2929

3030
private String[] databaseCreationScript;
31+
private String hibernateToolsExtensionSection;
3132

3233
protected File getProjectDir() { return projectDir; }
3334
protected File getGradlePropertiesFile() { return gradlePropertiesFile; }
@@ -38,6 +39,8 @@ public class TestTemplate {
3839
protected void setDatabaseFile(File f) { databaseFile = f; }
3940
protected String[] getDatabaseCreationScript() { return databaseCreationScript; }
4041
protected void setDatabaseCreationScript(String[] script) { databaseCreationScript = script; }
42+
protected String getHibernateToolsExtensionSection() { return hibernateToolsExtensionSection; }
43+
protected void setHibernateToolsExtensionSection(String s) { hibernateToolsExtensionSection = s; }
4144

4245
protected void executeGradleCommand(String ... gradleCommandLine) {
4346
GradleRunner runner = GradleRunner.create();
@@ -143,6 +146,14 @@ protected void addHibernateToolsPluginLine(StringBuffer gradleBuildFileContents)
143146
gradleBuildFileContents.insert(pos, constructHibernateToolsPluginLine() + "\n");
144147
}
145148

146-
protected void addHibernateToolsExtension(StringBuffer gradleBuildFileContents) {}
147-
149+
protected void addHibernateToolsExtension(StringBuffer gradleBuildFileContents) {
150+
String extension = getHibernateToolsExtensionSection();
151+
if (extension != null) {
152+
int pos = gradleBuildFileContents.indexOf("dependencies {");
153+
pos = gradleBuildFileContents.indexOf("}", pos);
154+
gradleBuildFileContents.insert(pos + 1, "\n\n" + extension);
155+
}
156+
}
148157
}
158+
159+

0 commit comments

Comments
 (0)