Skip to content

Commit 5e627a2

Browse files
committed
HBX-3000: Maven GenerateJava Mojo should generate annotated entities by default
Signed-off-by: Koen Aers <[email protected]>
1 parent 4b37da0 commit 5e627a2

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed

maven/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@
9292
<groupId>jakarta.xml.bind</groupId>
9393
<artifactId>jakarta.xml.bind-api</artifactId>
9494
</dependency>
95+
<dependency>
96+
<groupId>org.junit.jupiter</groupId>
97+
<artifactId>junit-jupiter-engine</artifactId>
98+
<scope>test</scope>
99+
</dependency>
100+
<dependency>
101+
<groupId>com.h2database</groupId>
102+
<artifactId>h2</artifactId>
103+
<scope>test</scope>
104+
</dependency>
95105
<!-- Maven Plugins -->
96106
<dependency>
97107
<groupId>org.apache.maven</groupId>

maven/src/main/java/org/hibernate/tool/maven/GenerateJavaMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public class GenerateJavaMojo extends AbstractGenerationMojo {
4747

4848
/** Code will contain JPA features, e.g. using annotations from jakarta.persistence
4949
* and org.hibernate.annotations. */
50-
@Parameter(defaultValue = "false")
51-
private boolean ejb3;
50+
@Parameter(defaultValue = "true")
51+
private boolean ejb3 = true;
5252

5353
/** Code will contain JDK 5 constructs such as generics and static imports. */
5454
@Parameter(defaultValue = "false")
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.hibernate.tool.maven;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.File;
7+
import java.io.FileWriter;
8+
import java.lang.reflect.Field;
9+
import java.nio.file.Files;
10+
import java.sql.DriverManager;
11+
12+
import org.apache.maven.project.MavenProject;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.io.TempDir;
17+
18+
public class GenerateJavaMojoTest {
19+
20+
private static final String JDBC_CONNECTION = "jdbc:h2:mem:test";
21+
private static final String CREATE_PERSON_TABLE =
22+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
23+
private static final String DROP_PERSON_TABLE =
24+
"drop table PERSON";
25+
26+
@TempDir
27+
private File tempDir;
28+
29+
private File outputDirectory;
30+
private GenerateJavaMojo generateJavaMojo;
31+
32+
@BeforeEach
33+
public void beforeEach() throws Exception {
34+
createDatabase();
35+
createPropertiesFile();
36+
createOutputDirectory();
37+
createGenerateJavaMojo();
38+
}
39+
40+
@AfterEach
41+
public void afterEach() throws Exception {
42+
dropDatabase();
43+
}
44+
45+
@Test
46+
public void testGenerateAnnotatedJava() throws Exception {
47+
File personJavaFile = new File(outputDirectory, "Person.java");
48+
// Person.java should not exist
49+
assertFalse(personJavaFile.exists());
50+
// Execute mojo with default value of 'ejb3' field which is 'true'
51+
generateJavaMojo.execute();
52+
// Person.java should exist
53+
assertTrue(personJavaFile.exists());
54+
// Person.java should be an annotated entity
55+
byte[] raw = Files.readAllBytes(personJavaFile.toPath());
56+
assertTrue(new String(raw).contains("import jakarta.persistence.Entity;"));
57+
}
58+
59+
@Test
60+
public void testGenerateNonAnnotatedJava() throws Exception {
61+
File personJavaFile = new File(outputDirectory, "Person.java");
62+
// Person.java should not exist
63+
assertFalse(personJavaFile.exists());
64+
// Set value of field 'ejb3' to 'false' and execute mojo
65+
Field ejb3Field = GenerateJavaMojo.class.getDeclaredField("ejb3");
66+
ejb3Field.setAccessible(true);
67+
ejb3Field.set(generateJavaMojo, false);
68+
generateJavaMojo.execute();
69+
// Person.java should exist
70+
assertTrue(personJavaFile.exists());
71+
// Person.java should be an annotated entity
72+
byte[] raw = Files.readAllBytes(personJavaFile.toPath());
73+
assertFalse(new String(raw).contains("import jakarta.persistence.Entity;"));
74+
}
75+
76+
private void createDatabase() throws Exception {
77+
DriverManager
78+
.getConnection(JDBC_CONNECTION)
79+
.createStatement()
80+
.execute(CREATE_PERSON_TABLE);
81+
}
82+
83+
private void dropDatabase() throws Exception {
84+
DriverManager
85+
.getConnection(JDBC_CONNECTION)
86+
.createStatement()
87+
.execute(DROP_PERSON_TABLE);
88+
}
89+
90+
private void createPropertiesFile() throws Exception {
91+
File propertiesFile = new File(tempDir, "hibernate.properties");
92+
try (FileWriter fileWriter = new FileWriter(propertiesFile)) {
93+
fileWriter.write("hibernate.connection.url=" + JDBC_CONNECTION + '\n');
94+
fileWriter.write("hibernate.default_catalog=TEST\n");
95+
fileWriter.write("hibernate.default_schema=PUBLIC\n");
96+
}
97+
}
98+
99+
private void createGenerateJavaMojo() throws Exception {
100+
generateJavaMojo = new GenerateJavaMojo();
101+
Field projectField = AbstractGenerationMojo.class.getDeclaredField("project");
102+
projectField.setAccessible(true);
103+
projectField.set(generateJavaMojo, new MavenProject());
104+
Field propertyFileField = AbstractGenerationMojo.class.getDeclaredField("propertyFile");
105+
propertyFileField.setAccessible(true);
106+
propertyFileField.set(generateJavaMojo, new File(tempDir, "hibernate.properties"));
107+
Field outputDirectoryField = GenerateJavaMojo.class.getDeclaredField("outputDirectory");
108+
outputDirectoryField.setAccessible(true);
109+
outputDirectoryField.set(generateJavaMojo, outputDirectory);
110+
}
111+
112+
private void createOutputDirectory() {
113+
outputDirectory = new File(tempDir, "generated");
114+
outputDirectory.mkdir();
115+
}
116+
117+
118+
}

0 commit comments

Comments
 (0)