Skip to content

Commit 667771c

Browse files
authored
HBX-3110: Add a functional test to guard the sanity of the Ant examples (#5465)
1 parent 2222d81 commit 667771c

File tree

4 files changed

+268
-15
lines changed

4 files changed

+268
-15
lines changed

ant/docs/examples/common/included.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
-->
1616
<project name="common" xmlns:ivy="antlib:org.apache.ivy.ant">
1717

18-
<property name="hibernate-tools.version" value="7.0.11.Final"/>
19-
<property name="javaee-api.version" value="8.0.1"/>
18+
<property name="hibernate-tools.version" value="@project.version@"/>
19+
<property name="javaee-api.version" value="@javaee-api.version@"/>
2020
<property name="jdbc-driver.org" value="com.h2database"/>
2121
<property name="jdbc-driver.module" value="h2"/>
22-
<property name="jdbc-driver.version" value="2.3.232"/>
22+
<property name="jdbc-driver.version" value="@h2.version@"/>
2323

2424
<ivy:cachepath
2525
organisation="org.hibernate.tool"

ant/docs/examples/properties/build.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
<property name="thingOne" value="Bar"/>
2424
<echo message="First try without any property set"/>
2525
<hibernatetool destdir="dummy">
26+
<!-- The classpath to be used by the 'hibernatetool' task to look up additional classes or resources -->
27+
<classpath location="."/>
2628
<!-- Dummy configuration, as it is needed for any 'hibernatetool' task -->
2729
<configuration />
2830
<!-- Custom exporter 'HelloExporter' will say 'Hello, World!' -->
2931
<hbmtemplate exporterclass="HelloExporter"/>
3032
</hibernatetool>
3133
<echo message="Now try with a property element: 'greetedOne' is set to 'Foo'"/>
3234
<hibernatetool destdir="dummy">
35+
<!-- The classpath to be used by the 'hibernatetool' task to look up additional classes or resources -->
36+
<classpath location="."/>
3337
<!-- <property> element to be used by the HelloExporter below -->
3438
<property key="greetedOne" value="Foo"/>
3539
<!-- Dummy configuration, as it is needed for any 'hibernatetool' task -->
@@ -39,6 +43,8 @@
3943
</hibernatetool>
4044
<echo message="Lastly try with a propertySet element: 'greetedOne' is set to 'Bar'"/>
4145
<hibernatetool destdir="dummy">
46+
<!-- The classpath to be used by the 'hibernatetool' task to look up additional classes or resources -->
47+
<classpath location="."/>
4248
<!-- <propertySet> element to be used by the HelloExporter below -->
4349
<!-- The property 'thingOne' defined above is mapped to 'greetedOne' and subsequently used -->
4450
<propertySet>

ant/pom.xml

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,76 @@
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>
6470
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-resources-plugin</artifactId>
74+
<executions>
75+
<execution>
76+
<id>filter-test-resources</id>
77+
<phase>process-test-resources</phase>
78+
<goals>
79+
<goal>testResources</goal>
80+
</goals>
81+
<configuration>
82+
<resources>
83+
<resource>
84+
<directory>${project.build.testOutputDirectory}</directory>
85+
<filtering>true</filtering>
86+
<includes>
87+
<include>common/included.xml</include>
88+
</includes>
89+
</resource>
90+
</resources>
91+
<delimiters>
92+
<delimiter>@*@</delimiter>
93+
</delimiters>
94+
</configuration>
95+
</execution>
96+
</executions>
97+
</plugin>
6598
<plugin>
6699
<groupId>org.codehaus.mojo</groupId>
67100
<artifactId>build-helper-maven-plugin</artifactId>
68101
<executions>
69-
<execution>
70-
<id>add-test-source</id>
71-
<phase>generate-test-sources</phase>
72-
<goals>
73-
<goal>add-test-source</goal>
74-
</goals>
75-
<configuration>
76-
<sources>
77-
<source>src/it/java</source>
78-
</sources>
79-
</configuration>
80-
</execution>
102+
<execution>
103+
<id>add-test-source</id>
104+
<phase>generate-test-sources</phase>
105+
<goals>
106+
<goal>add-test-source</goal>
107+
</goals>
108+
<configuration>
109+
<sources>
110+
<source>src/it/java</source>
111+
</sources>
112+
</configuration>
113+
</execution>
114+
<execution>
115+
<id>add-test-resource</id>
116+
<phase>generate-test-resources</phase>
117+
<goals>
118+
<goal>add-test-resource</goal>
119+
</goals>
120+
<configuration>
121+
<resources>
122+
<resource>
123+
<directory>docs/examples</directory>
124+
<excludes>
125+
<exclude>**/README.md</exclude>
126+
</excludes>
127+
</resource>
128+
</resources>
129+
</configuration>
130+
</execution>
81131
</executions>
82132
</plugin>
83133
<plugin>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package org.hibernate.tool.ant;
2+
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.Test;
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+
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
21+
public class ExamplesTestIT {
22+
23+
private static File baseFolder;
24+
25+
@BeforeAll
26+
public static void beforeAll() throws Exception {
27+
// The needed resource for this test are put in place
28+
// in the 'baseFolder' (normally 'target/test-classes')
29+
// by the 'build-helper-maven-plugin' execution.
30+
// See the 'pom.xml'
31+
baseFolder = determineBaseFolder();
32+
editIncludedXml();
33+
overwriteHibernateProperties();
34+
createDatabase();
35+
}
36+
37+
@Test
38+
public void test5MinuteTutorial() throws Exception {
39+
File buildFile = new File(baseFolder, "5-minute-tutorial/build.xml");
40+
Project project = createProject(buildFile);
41+
assertNotNull(project);
42+
File personFile = new File(baseFolder, "5-minute-tutorial/generated/Person.java");
43+
assertFalse(personFile.exists());
44+
project.executeTarget("reveng");
45+
assertTrue(personFile.exists());
46+
}
47+
48+
@Test
49+
public void testClasspath() throws Exception {
50+
PrintStream savedOut = System.out;
51+
try {
52+
File buildFile = new File(baseFolder, "classpath/build.xml");
53+
Project project = createProject(buildFile);
54+
assertNotNull(project);
55+
ByteArrayOutputStream out = new ByteArrayOutputStream();
56+
assertFalse(out.toString().contains("Hello from Exporter!"));
57+
System.setOut(new PrintStream(out));
58+
project.executeTarget("reveng");
59+
assertTrue(out.toString().contains("Hello from Exporter!"));
60+
} finally {
61+
System.setOut(savedOut);
62+
}
63+
}
64+
65+
@Test
66+
public void testConfiguration() throws Exception {
67+
File buildFile = new File(baseFolder, "configuration/build.xml");
68+
Project project = createProject(buildFile);
69+
assertNotNull(project);
70+
File cfgXmlFile = new File(baseFolder, "configuration/generated/hibernate.cfg.xml");
71+
assertFalse(cfgXmlFile.exists());
72+
project.executeTarget("reveng");
73+
assertTrue(cfgXmlFile.exists());
74+
}
75+
76+
@Test
77+
public void testJpa() throws Exception {
78+
File buildFile = new File(baseFolder, "jpa/build.xml");
79+
Project project = createProject(buildFile);
80+
assertNotNull(project);
81+
File barSqlFile = new File(baseFolder, "jpa/generated/bar.sql");
82+
assertFalse(barSqlFile.exists());
83+
project.executeTarget("reveng");
84+
assertTrue(barSqlFile.exists());
85+
}
86+
87+
@Test
88+
public void testNative() throws Exception {
89+
File buildFile = new File(baseFolder, "native/build.xml");
90+
Project project = createProject(buildFile);
91+
assertNotNull(project);
92+
File fooSqlFile = new File(baseFolder, "native/generated/foo.sql");
93+
assertFalse(fooSqlFile.exists());
94+
project.executeTarget("reveng");
95+
assertTrue(fooSqlFile.exists());
96+
}
97+
98+
@Test
99+
public void testProperties() throws Exception {
100+
PrintStream savedOut = System.out;
101+
try {
102+
File buildFile = new File(baseFolder, "properties/build.xml");
103+
Project project = createProject(buildFile);
104+
assertNotNull(project);
105+
ByteArrayOutputStream out = new ByteArrayOutputStream();
106+
System.setOut(new PrintStream(out));
107+
assertFalse(out.toString().contains("Hello, World!"));
108+
assertFalse(out.toString().contains("Hello, Foo!"));
109+
assertFalse(out.toString().contains("Hello, Bar!"));
110+
project.executeTarget("reveng");
111+
assertTrue(out.toString().contains("Hello, World!"));
112+
assertTrue(out.toString().contains("Hello, Foo!"));
113+
assertTrue(out.toString().contains("Hello, Bar!"));
114+
} finally {
115+
System.setOut(savedOut);
116+
}
117+
}
118+
119+
@Test
120+
public void testTemplatePath() throws Exception {
121+
File buildFile = new File(baseFolder, "templatepath/build.xml");
122+
Project project = createProject(buildFile);
123+
assertNotNull(project);
124+
File personFile = new File(baseFolder, "templatepath/generated/Person.java");
125+
assertFalse(personFile.exists());
126+
project.executeTarget("reveng");
127+
assertTrue(personFile.exists());
128+
String personFileContents = new String(Files.readAllBytes(personFile.toPath()));
129+
assertTrue(personFileContents.contains("// This is just an example of a custom template"));
130+
}
131+
132+
private Project createProject(File buildXmlFile) throws Exception {
133+
Project result = new Project();
134+
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
135+
result.addReference(MagicNames.REFID_PROJECT_HELPER, projectHelper);
136+
result.setBaseDir(buildXmlFile.getParentFile());
137+
result.addBuildListener(createConsoleLogger());
138+
projectHelper.parse(result, buildXmlFile);
139+
return result;
140+
}
141+
142+
private DefaultLogger createConsoleLogger() {
143+
DefaultLogger consoleLogger = new DefaultLogger();
144+
consoleLogger.setErrorPrintStream(System.err);
145+
consoleLogger.setOutputPrintStream(System.out);
146+
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
147+
return consoleLogger;
148+
}
149+
150+
private static void editIncludedXml() throws Exception {
151+
File xmlFile = new File(baseFolder, "common/included.xml");
152+
StringBuffer xmlFileContents = new StringBuffer(
153+
new String(Files.readAllBytes(xmlFile.toPath())));
154+
int start = xmlFileContents.indexOf("<ivy:cachepath");
155+
int end = xmlFileContents.indexOf("<ivy:cachepath", start + 1);
156+
xmlFileContents.replace(start, end, "");
157+
start = xmlFileContents.indexOf("<path refid=\"hibernate-tools.path\"/>");
158+
end = xmlFileContents.indexOf("<path refid=", start + 1);
159+
xmlFileContents.replace(start, end, "");
160+
Files.writeString(xmlFile.toPath(), xmlFileContents.toString());
161+
}
162+
163+
private static void overwriteHibernateProperties() throws Exception {
164+
File hibernatePropertiesFile = new File(baseFolder, "common/hibernate.properties");
165+
String hibernatePropertiesFileContents =
166+
"hibernate.connection.driver_class=org.h2.Driver\n" +
167+
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
168+
"hibernate.connection.username=\n" +
169+
"hibernate.connection.password=\n" +
170+
"hibernate.default_catalog=TEST\n" +
171+
"hibernate.default_schema=PUBLIC\n";
172+
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
173+
}
174+
175+
private static void createDatabase() throws Exception {
176+
File databaseFile = new File(baseFolder, "database/test.mv.db");
177+
assertFalse(databaseFile.exists());
178+
assertFalse(databaseFile.isFile());
179+
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
180+
Statement statement = connection.createStatement();
181+
statement.execute("create table PERSON (ID int not null, NAME varchar(20), primary key (ID))");
182+
statement.close();
183+
connection.close();
184+
assertTrue(databaseFile.exists());
185+
assertTrue(databaseFile.isFile());
186+
}
187+
188+
private static String constructJdbcConnectionString() {
189+
return "jdbc:h2:" + baseFolder.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
190+
}
191+
192+
private static File determineBaseFolder() throws Exception {
193+
return new File(ExamplesTestIT.class.getClassLoader().getResource("common/included.xml").toURI())
194+
.getParentFile().getParentFile();
195+
}
196+
197+
}

0 commit comments

Comments
 (0)