|
| 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