Skip to content

Commit 2e9b6a6

Browse files
committed
HBX-3110: Add a functional test to guard the sanity of the Ant examples
- Add a test verifying the '5-minute-tutorial' example Signed-off-by: Koen Aers <[email protected]>
1 parent 60ec56e commit 2e9b6a6

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

ant/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<artifactId>h2</artifactId>
5959
<scope>test</scope>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.apache.ivy</groupId>
63+
<artifactId>ivy</artifactId>
64+
<version>2.5.2</version>
65+
<scope>test</scope>
66+
</dependency>
6167
</dependencies>
6268

6369
<build>
@@ -115,6 +121,9 @@
115121
<resources>
116122
<resource>
117123
<directory>docs/examples</directory>
124+
<excludes>
125+
<exclude>**/README.md</exclude>
126+
</excludes>
118127
</resource>
119128
</resources>
120129
</configuration>
Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,111 @@
11
package org.hibernate.tool.ant;
22

3-
import static org.junit.jupiter.api.Assertions.assertTrue;
4-
3+
import org.apache.tools.ant.DefaultLogger;
4+
import org.apache.tools.ant.MagicNames;
5+
import org.apache.tools.ant.Project;
6+
import org.apache.tools.ant.ProjectHelper;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.BeforeEach;
59
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.io.TempDir;
611

12+
import java.io.File;
13+
import java.net.URISyntaxException;
714
import java.net.URL;
15+
import java.nio.file.Files;
16+
import java.sql.Connection;
17+
import java.sql.DriverManager;
18+
import java.sql.Statement;
19+
20+
import static org.junit.jupiter.api.Assertions.*;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
822

923
public class ExamplesTestIT {
1024

25+
private static File baseFolder;
26+
27+
@BeforeAll
28+
public static void beforeAll() throws Exception {
29+
baseFolder = determineBaseFolder();
30+
editIncludedXml();
31+
overwriteHibernateProperties();
32+
createDatabase();
33+
}
34+
1135
@Test
12-
public void testSomethin() {
13-
URL url = getClass().getClassLoader().getResource("5-minute-tutorial/build.xml");
14-
System.out.println(url);
36+
public void test5MinuteTutorial() throws Exception {
37+
File buildFile = new File(baseFolder, "5-minute-tutorial/build.xml");
38+
Project project = createProject(buildFile);
39+
assertNotNull(project);
40+
File personFile = new File(baseFolder, "5-minute-tutorial/generated/Person.java");
41+
assertFalse(personFile.exists());
42+
project.executeTarget("reveng");
43+
assertTrue(personFile.exists());
44+
}
45+
46+
private Project createProject(File buildXmlFile) throws Exception {
47+
Project result = new Project();
48+
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
49+
result.addReference(MagicNames.REFID_PROJECT_HELPER, projectHelper);
50+
result.setBaseDir(buildXmlFile.getParentFile());
51+
result.addBuildListener(createConsoleLogger());
52+
projectHelper.parse(result, buildXmlFile);
53+
return result;
54+
}
55+
56+
private DefaultLogger createConsoleLogger() {
57+
DefaultLogger consoleLogger = new DefaultLogger();
58+
consoleLogger.setErrorPrintStream(System.err);
59+
consoleLogger.setOutputPrintStream(System.out);
60+
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
61+
return consoleLogger;
62+
}
63+
64+
private static void editIncludedXml() throws Exception {
65+
File xmlFile = new File(baseFolder, "common/included.xml");
66+
StringBuffer xmlFileContents = new StringBuffer(
67+
new String(Files.readAllBytes(xmlFile.toPath())));
68+
int start = xmlFileContents.indexOf("<ivy:cachepath");
69+
int end = xmlFileContents.indexOf("<ivy:cachepath", start + 1);
70+
xmlFileContents.replace(start, end, "");
71+
start = xmlFileContents.indexOf("<path refid=\"hibernate-tools.path\"/>");
72+
end = xmlFileContents.indexOf("<path refid=", start + 1);
73+
xmlFileContents.replace(start, end, "");
74+
Files.writeString(xmlFile.toPath(), xmlFileContents.toString());
75+
}
76+
77+
private static void overwriteHibernateProperties() throws Exception {
78+
File hibernatePropertiesFile = new File(baseFolder, "common/hibernate.properties");
79+
String hibernatePropertiesFileContents =
80+
"hibernate.connection.driver_class=org.h2.Driver\n" +
81+
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
82+
"hibernate.connection.username=\n" +
83+
"hibernate.connection.password=\n" +
84+
"hibernate.default_catalog=TEST\n" +
85+
"hibernate.default_schema=PUBLIC\n";
86+
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
87+
}
88+
89+
private static void createDatabase() throws Exception {
90+
File databaseFile = new File(baseFolder, "database/test.mv.db");
91+
assertFalse(databaseFile.exists());
92+
assertFalse(databaseFile.isFile());
93+
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
94+
Statement statement = connection.createStatement();
95+
statement.execute("create table PERSON (ID int not null, NAME varchar(20), primary key (ID))");
96+
statement.close();
97+
connection.close();
98+
assertTrue(databaseFile.exists());
99+
assertTrue(databaseFile.isFile());
100+
}
101+
102+
private static String constructJdbcConnectionString() {
103+
return "jdbc:h2:" + baseFolder.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
104+
}
105+
106+
private static File determineBaseFolder() throws Exception {
107+
return new File(ExamplesTestIT.class.getClassLoader().getResource("common/included.xml").toURI())
108+
.getParentFile().getParentFile();
15109
}
16110

17111
}

0 commit comments

Comments
 (0)