Skip to content

Commit 60498b4

Browse files
committed
HBX-3047: Ant task hbm2java should generate annotated classes by default
- Add a new integration test 'JpaDefaultTestIT' to guard this new default behavior - Change the default value of the 'ejb3' field in 'Hbm2JavaExporterTask' - Adapt 'org.hibernate.tool.ant.Hbm2JavaConfiguration.TestCase' to this new behavior Signed-off-by: Koen Aers <[email protected]>
1 parent 8124d8f commit 60498b4

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package org.hibernate.tool.ant.hbm2java;
2+
3+
import org.apache.tools.ant.DefaultLogger;
4+
import org.apache.tools.ant.Project;
5+
import org.apache.tools.ant.ProjectHelper;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.io.TempDir;
9+
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.File;
12+
import java.io.PrintStream;
13+
import java.nio.file.Files;
14+
import java.sql.Connection;
15+
import java.sql.DriverManager;
16+
import java.sql.Statement;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
public class JpaDefaultTestIT {
21+
22+
@TempDir
23+
private File projectDir;
24+
25+
private File buildXmlFile;
26+
private ByteArrayOutputStream output;
27+
private File databaseFile;
28+
private File personFile;
29+
30+
@BeforeEach
31+
public void beforeEach() {
32+
output = new ByteArrayOutputStream();
33+
databaseFile = new File(projectDir, "database/test.mv.db");
34+
assertFalse(databaseFile.exists());
35+
personFile = new File(projectDir, "generated/Person.java");
36+
assertFalse(personFile.exists());
37+
}
38+
39+
@Test
40+
public void testTutorial() throws Exception {
41+
createBuildXmlFile();
42+
createDatabase();
43+
createHibernatePropertiesFile();
44+
runAntBuild();
45+
verifyResult();
46+
}
47+
48+
private void createBuildXmlFile() throws Exception {
49+
buildXmlFile = new File(projectDir, "build.xml");
50+
assertFalse(buildXmlFile.exists());
51+
Files.writeString(buildXmlFile.toPath(), buildXmlFileContents);
52+
}
53+
54+
private void createDatabase() throws Exception {
55+
String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
56+
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
57+
Statement statement = connection.createStatement();
58+
statement.execute(CREATE_PERSON_TABLE);
59+
statement.execute("insert into PERSON values (1, 'foo')");
60+
statement.close();
61+
connection.close();
62+
assertTrue(databaseFile.exists());
63+
assertTrue(databaseFile.isFile());
64+
}
65+
66+
private void createHibernatePropertiesFile() throws Exception {
67+
File hibernatePropertiesFile = new File(projectDir, "hibernate.properties");
68+
StringBuffer hibernatePropertiesFileContents = new StringBuffer();
69+
hibernatePropertiesFileContents
70+
.append("hibernate.connection.driver_class=org.h2.Driver\n")
71+
.append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n")
72+
.append("hibernate.connection.username=\n")
73+
.append("hibernate.connection.password=\n")
74+
.append("hibernate.default_catalog=TEST\n")
75+
.append("hibernate.default_schema=PUBLIC\n");
76+
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString());
77+
assertTrue(hibernatePropertiesFile.exists());
78+
}
79+
80+
private void runAntBuild() {
81+
Project project = new Project();
82+
project.setBaseDir(projectDir);
83+
project.addBuildListener(getConsoleLogger());
84+
ProjectHelper.getProjectHelper().parse(project, buildXmlFile);
85+
project.executeTarget(project.getDefaultTarget());
86+
}
87+
88+
private void verifyResult() throws Exception {
89+
File generatedOutputFolder = new File(projectDir, "generated");
90+
assertTrue(generatedOutputFolder.exists());
91+
assertTrue(generatedOutputFolder.isDirectory());
92+
assertEquals(1, generatedOutputFolder.list().length);
93+
File generatedPersonJavaFile = new File(generatedOutputFolder, "Person.java");
94+
assertTrue(generatedPersonJavaFile.exists());
95+
assertTrue(generatedPersonJavaFile.isFile());
96+
String generatedPersonJavaFileContents = new String(
97+
Files.readAllBytes(generatedPersonJavaFile.toPath()));
98+
assertTrue(generatedPersonJavaFileContents.contains("import jakarta.persistence.Entity;"));
99+
assertTrue(generatedPersonJavaFileContents.contains("public class Person "));
100+
}
101+
102+
private DefaultLogger getConsoleLogger() {
103+
DefaultLogger consoleLogger = new DefaultLogger();
104+
consoleLogger.setErrorPrintStream(System.err);
105+
consoleLogger.setOutputPrintStream(new PrintStream(output, true));
106+
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
107+
return consoleLogger;
108+
}
109+
110+
private String constructJdbcConnectionString() {
111+
return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
112+
}
113+
114+
private static final String buildXmlFileContents =
115+
116+
"<project name='tutorial' default='reveng'> \n" +
117+
" <taskdef \n" +
118+
" name='hibernatetool' \n" +
119+
" classname='org.hibernate.tool.ant.HibernateToolTask'/> \n" +
120+
" <target name='reveng'> \n" +
121+
" <echo message='hello from hibernate tools' /> \n" +
122+
" <hibernatetool destdir='generated'> \n" +
123+
" <jdbcconfiguration propertyfile='hibernate.properties'/> \n" +
124+
" <hbm2java/> \n" +
125+
" </hibernatetool> \n" +
126+
" </target> \n" +
127+
"</project> \n" ;
128+
129+
}

ant/src/main/java/org/hibernate/tool/ant/Hbm2JavaExporterTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
public class Hbm2JavaExporterTask extends ExporterTask {
3131

32-
boolean ejb3 = false;
32+
boolean ejb3 = true;
3333

3434
boolean jdk5 = false;
3535

test/common/src/main/java/org/hibernate/tool/ant/Hbm2JavaConfiguration/TestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.hibernate.tool.ant.Hbm2JavaConfiguration;
2121

2222
import static org.junit.jupiter.api.Assertions.assertFalse;
23-
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
2525

2626
import java.io.File;
@@ -77,7 +77,7 @@ public void testHbm2JavaConfiguration() {
7777
assertTrue(FileUtil
7878
.findFirstString("SomeClass", someClass)
7979
.contains("SomeClass generated by hbm2java"));
80-
assertNull(FileUtil.findFirstString("@Entity", someClass));
80+
assertNotNull(FileUtil.findFirstString("@Entity", someClass));
8181

8282
}
8383

0 commit comments

Comments
 (0)