|
1 | 1 | package org.hibernate.tool.ant; |
2 | 2 |
|
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; |
5 | 9 | import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.io.TempDir; |
6 | 11 |
|
| 12 | +import java.io.File; |
| 13 | +import java.net.URISyntaxException; |
7 | 14 | 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; |
8 | 22 |
|
9 | 23 | public class ExamplesTestIT { |
10 | 24 |
|
| 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 | + |
11 | 35 | @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(); |
15 | 109 | } |
16 | 110 |
|
17 | 111 | } |
0 commit comments