Skip to content

Commit 60cad09

Browse files
committed
HBX-3001: Maven GenerateJava Mojo should use generics and static imports by default
- Modify setup of GenerateJavaMojoTest to have a second table with foreign key constraint - Add unit test GenerateJavaMojoTest#testGenerateJavaWithGenerics() - Add unit test GenerateJavaMojoTest#testGenerateJavaWithoutGenerics() Signed-off-by: Koen Aers <[email protected]>
1 parent 577949a commit 60cad09

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

maven/src/test/java/org/hibernate/tool/maven/GenerateJavaMojoTest.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import java.io.File;
77
import java.lang.reflect.Field;
88
import java.nio.file.Files;
9+
import java.sql.Connection;
910
import java.sql.DriverManager;
11+
import java.sql.Statement;
1012
import java.util.Properties;
1113

1214
import org.apache.maven.project.MavenProject;
@@ -22,6 +24,10 @@ public class GenerateJavaMojoTest {
2224
private static final String JDBC_CONNECTION = "jdbc:h2:mem:test";
2325
private static final String CREATE_PERSON_TABLE =
2426
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
27+
private static final String CREATE_ITEM_TABLE =
28+
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, primary key (ID), foreign key (OWNER_ID) references PERSON(ID))";
29+
private static final String DROP_ITEM_TABLE =
30+
"drop table ITEM";
2531
private static final String DROP_PERSON_TABLE =
2632
"drop table PERSON";
2733

@@ -79,18 +85,54 @@ public void testGenerateNonAnnotatedJava() throws Exception {
7985
assertFalse(new String(raw).contains("import jakarta.persistence.Entity;"));
8086
}
8187

88+
@Test
89+
public void testGenerateJavaWithGenerics() throws Exception {
90+
File personJavaFile = new File(outputDirectory, "Person.java");
91+
// Person.java should not exist
92+
assertFalse(personJavaFile.exists());
93+
// Set value of field 'jdk5' to 'true' and execute mojo
94+
Field jdk5Field = GenerateJavaMojo.class.getDeclaredField("jdk5");
95+
jdk5Field.setAccessible(true);
96+
jdk5Field.set(generateJavaMojo, true);
97+
// Execute mojo
98+
generateJavaMojo.executeExporter(createMetadataDescriptor());
99+
// Person.java should exist
100+
assertTrue(personJavaFile.exists());
101+
// Person.java should be an annotated entity
102+
byte[] raw = Files.readAllBytes(personJavaFile.toPath());
103+
assertTrue(new String(raw).contains("Set<Item>"));
104+
}
105+
106+
@Test
107+
public void testGenerateJavaWithoutGenerics() throws Exception {
108+
File personJavaFile = new File(outputDirectory, "Person.java");
109+
// Person.java should not exist
110+
assertFalse(personJavaFile.exists());
111+
// Set value of field 'jdk5' to 'true' and execute mojo
112+
Field jdk5Field = GenerateJavaMojo.class.getDeclaredField("jdk5");
113+
jdk5Field.setAccessible(true);
114+
jdk5Field.set(generateJavaMojo, false);
115+
// Execute mojo
116+
generateJavaMojo.executeExporter(createMetadataDescriptor());
117+
// Person.java should exist
118+
assertTrue(personJavaFile.exists());
119+
// Person.java should be an annotated entity
120+
byte[] raw = Files.readAllBytes(personJavaFile.toPath());
121+
assertFalse(new String(raw).contains("Set<Item>"));
122+
}
123+
82124
private void createDatabase() throws Exception {
83-
DriverManager
84-
.getConnection(JDBC_CONNECTION)
85-
.createStatement()
86-
.execute(CREATE_PERSON_TABLE);
125+
Connection connection = DriverManager.getConnection(JDBC_CONNECTION);
126+
Statement statement = connection.createStatement();
127+
statement.execute(CREATE_PERSON_TABLE);
128+
statement.execute(CREATE_ITEM_TABLE);
87129
}
88130

89131
private void dropDatabase() throws Exception {
90-
DriverManager
91-
.getConnection(JDBC_CONNECTION)
92-
.createStatement()
93-
.execute(DROP_PERSON_TABLE);
132+
Connection connection = DriverManager.getConnection(JDBC_CONNECTION);
133+
Statement statement = connection.createStatement();
134+
statement.execute(DROP_ITEM_TABLE);
135+
statement.execute(DROP_PERSON_TABLE);
94136
}
95137

96138
private void createGenerateJavaMojo() throws Exception {

0 commit comments

Comments
 (0)