Skip to content

Commit 32a46c8

Browse files
committed
HBX-3128: Avoid use of Maven invoker plugin while performing functional testing
- Add a test for use-generics - Make sure database is correctly created - Add assertion for the amount of generated files - Modify some utility methods to accept file name parameter Signed-off-by: Koen Aers <[email protected]>
1 parent ac8a4d3 commit 32a46c8

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

maven/src/functionalTest/java/org/hibernate/tool/maven/ExamplesTestIT.java

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.hibernate.tool.maven;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
import static org.junit.jupiter.api.Assertions.fail;
66

77
import org.junit.jupiter.api.BeforeAll;
8-
import org.junit.jupiter.api.BeforeEach;
98
import org.junit.jupiter.api.Test;
109
import org.junit.jupiter.api.io.TempDir;
1110

@@ -45,39 +44,31 @@ public static void beforeAll() throws Exception {
4544
localRepo = new File(baseFolder.getParentFile(), "local-repo");
4645
}
4746

48-
@BeforeEach
49-
public void beforeEach() throws Exception {
50-
createDatabase();
51-
}
52-
5347
@Test
5448
public void test5MinuteTutorial() throws Exception {
5549
prepareProject("5-minute-tutorial");
56-
assertNotGeneratedYet();
50+
assertNotGeneratedYet("Person.java");
5751
runGenerateSources();
58-
assertGeneratedContains("public class Person");
52+
assertNumberOfGeneratedFiles(1);
53+
assertGeneratedContains("Person.java", "public class Person");
5954
}
6055

6156
@Test
6257
public void testJpaDefault() throws Exception {
6358
prepareProject("hbm2java/jpa-default");
64-
assertNotGeneratedYet();
59+
assertNotGeneratedYet("Person.java");
6560
runGenerateSources();
66-
assertGeneratedContains("import jakarta.persistence.Entity;");
61+
assertNumberOfGeneratedFiles(1);
62+
assertGeneratedContains("Person.java","import jakarta.persistence.Entity;");
6763
}
6864

6965
@Test
7066
public void testNoAnnotations() throws Exception {
7167
prepareProject("hbm2java/no-annotations");
72-
assertNotGeneratedYet();
68+
assertNotGeneratedYet("Person.java");
7369
runGenerateSources();
74-
assertGeneratedDoesNotContain("import jakarta.persistence.Entity;");
75-
}
76-
private void prepareProject(String projectName) throws Exception {
77-
projectFolder = new File(baseFolder, projectName);
78-
assertTrue(projectFolder.exists());
79-
System.setProperty(MVN_HOME, projectFolder.getAbsolutePath());
80-
createHibernatePropertiesFile(projectFolder);
70+
assertNumberOfGeneratedFiles(1);
71+
assertGeneratedDoesNotContain("Person.java", "import jakarta.persistence.Entity;");
8172
}
8273

8374
@Test
@@ -88,9 +79,32 @@ public void testNoGenerics() throws Exception {
8879
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
8980
};
9081
prepareProject("hbm2java/no-generics");
91-
assertNotGeneratedYet();
82+
assertNotGeneratedYet("Person.java");
83+
runGenerateSources();
84+
assertNumberOfGeneratedFiles(2);
85+
assertGeneratedDoesNotContain("Person.java", "Set<Item>");
86+
}
87+
88+
@Test
89+
public void testUseGenerics() throws Exception {
90+
databaseCreationScript = new String[] {
91+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))",
92+
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
93+
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
94+
};
95+
prepareProject("hbm2java/use-generics");
96+
assertNotGeneratedYet("Person.java");
9297
runGenerateSources();
93-
assertGeneratedDoesNotContain("Set<Item>");
98+
assertNumberOfGeneratedFiles(2);
99+
assertGeneratedContains("Person.java", "Set<Item>");
100+
}
101+
102+
private void prepareProject(String projectName) throws Exception {
103+
projectFolder = new File(baseFolder, projectName);
104+
assertTrue(projectFolder.exists());
105+
System.setProperty(MVN_HOME, projectFolder.getAbsolutePath());
106+
createHibernatePropertiesFile(projectFolder);
107+
createDatabase();
94108
}
95109

96110
private void createHibernatePropertiesFile(File projectFolder) throws Exception {
@@ -117,20 +131,24 @@ private void runGenerateSources() {
117131
null);
118132
}
119133

120-
private void assertNotGeneratedYet() {
121-
assertFalse(new File(projectFolder, "target/generated-sources/Person.java").exists());
134+
private void assertNotGeneratedYet(String fileName) {
135+
assertFalse(new File(projectFolder, "target/generated-sources/" + fileName).exists());
136+
}
137+
138+
private void assertGeneratedContains(String fileName, String contents) throws Exception {
139+
assertTrue(readGeneratedContents(fileName).contains(contents));
122140
}
123141

124-
private void assertGeneratedContains(String contents) throws Exception {
125-
assertTrue(readGeneratedContents().contains(contents));
142+
private void assertGeneratedDoesNotContain(String fileName, String contents) throws Exception {
143+
assertFalse(readGeneratedContents(fileName).contains(contents));
126144
}
127145

128-
private void assertGeneratedDoesNotContain(String contents) throws Exception {
129-
assertFalse(readGeneratedContents().contains(contents));
146+
private void assertNumberOfGeneratedFiles(int amount) throws Exception {
147+
assertEquals(amount, new File(projectFolder, "target/generated-sources").list().length);
130148
}
131149

132-
private String readGeneratedContents() throws Exception {
133-
File generatedPersonFile = new File(projectFolder, "target/generated-sources/Person.java");
150+
private String readGeneratedContents(String fileName) throws Exception {
151+
File generatedPersonFile = new File(projectFolder, "target/generated-sources/" + fileName);
134152
assertTrue(generatedPersonFile.exists());
135153
return new String(Files.readAllBytes(generatedPersonFile.toPath()));
136154
}

0 commit comments

Comments
 (0)