Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<version>2.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -115,6 +121,9 @@
<resources>
<resource>
<directory>docs/examples</directory>
<excludes>
<exclude>**/README.md</exclude>
</excludes>
</resource>
</resources>
</configuration>
Expand Down
104 changes: 99 additions & 5 deletions ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,111 @@
package org.hibernate.tool.ant;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class ExamplesTestIT {

private static File baseFolder;

@BeforeAll
public static void beforeAll() throws Exception {
baseFolder = determineBaseFolder();
editIncludedXml();
overwriteHibernateProperties();
createDatabase();
}

@Test
public void testSomethin() {
URL url = getClass().getClassLoader().getResource("5-minute-tutorial/build.xml");
System.out.println(url);
public void test5MinuteTutorial() throws Exception {
File buildFile = new File(baseFolder, "5-minute-tutorial/build.xml");
Project project = createProject(buildFile);
assertNotNull(project);
File personFile = new File(baseFolder, "5-minute-tutorial/generated/Person.java");
assertFalse(personFile.exists());
project.executeTarget("reveng");
assertTrue(personFile.exists());
}

private Project createProject(File buildXmlFile) throws Exception {
Project result = new Project();
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
result.addReference(MagicNames.REFID_PROJECT_HELPER, projectHelper);
result.setBaseDir(buildXmlFile.getParentFile());
result.addBuildListener(createConsoleLogger());
projectHelper.parse(result, buildXmlFile);
return result;
}

private DefaultLogger createConsoleLogger() {
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
return consoleLogger;
}

private static void editIncludedXml() throws Exception {
File xmlFile = new File(baseFolder, "common/included.xml");
StringBuffer xmlFileContents = new StringBuffer(
new String(Files.readAllBytes(xmlFile.toPath())));
int start = xmlFileContents.indexOf("<ivy:cachepath");
int end = xmlFileContents.indexOf("<ivy:cachepath", start + 1);
xmlFileContents.replace(start, end, "");
start = xmlFileContents.indexOf("<path refid=\"hibernate-tools.path\"/>");
end = xmlFileContents.indexOf("<path refid=", start + 1);
xmlFileContents.replace(start, end, "");
Files.writeString(xmlFile.toPath(), xmlFileContents.toString());
}

private static void overwriteHibernateProperties() throws Exception {
File hibernatePropertiesFile = new File(baseFolder, "common/hibernate.properties");
String hibernatePropertiesFileContents =
"hibernate.connection.driver_class=org.h2.Driver\n" +
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
"hibernate.connection.username=\n" +
"hibernate.connection.password=\n" +
"hibernate.default_catalog=TEST\n" +
"hibernate.default_schema=PUBLIC\n";
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
}

private static void createDatabase() throws Exception {
File databaseFile = new File(baseFolder, "database/test.mv.db");
assertFalse(databaseFile.exists());
assertFalse(databaseFile.isFile());
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
Statement statement = connection.createStatement();
statement.execute("create table PERSON (ID int not null, NAME varchar(20), primary key (ID))");
statement.close();
connection.close();
assertTrue(databaseFile.exists());
assertTrue(databaseFile.isFile());
}

private static String constructJdbcConnectionString() {
return "jdbc:h2:" + baseFolder.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
}

private static File determineBaseFolder() throws Exception {
return new File(ExamplesTestIT.class.getClassLoader().getResource("common/included.xml").toURI())
.getParentFile().getParentFile();
}

}