|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.tool.ant; |
| 6 | + |
| 7 | +import org.apache.tools.ant.DefaultLogger; |
| 8 | +import org.apache.tools.ant.Project; |
| 9 | +import org.apache.tools.ant.ProjectHelper; |
| 10 | +import org.junit.jupiter.api.io.TempDir; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.sql.Connection; |
| 15 | +import java.sql.DriverManager; |
| 16 | +import java.sql.Statement; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +public abstract class TestTemplate { |
| 25 | + |
| 26 | + @TempDir |
| 27 | + private File projectDir; |
| 28 | + |
| 29 | + private String hibernateToolTaskXml; |
| 30 | + private String[] databaseCreationScript; |
| 31 | + |
| 32 | + protected File getProjectDir() { |
| 33 | + return projectDir; |
| 34 | + } |
| 35 | + |
| 36 | + protected void setHibernateToolTaskXml(String xml) { |
| 37 | + this.hibernateToolTaskXml = xml; |
| 38 | + } |
| 39 | + |
| 40 | + protected void setDatabaseCreationScript(String[] sqls) { |
| 41 | + this.databaseCreationScript = sqls; |
| 42 | + } |
| 43 | + |
| 44 | + protected String constructBuildXmlFileContents() { |
| 45 | + assertNotNull(hibernateToolTaskXml); |
| 46 | + return buildXmlFileContents.replace("@hibernateToolTaskXml@", hibernateToolTaskXml); |
| 47 | + } |
| 48 | + |
| 49 | + protected String getFileContents(String path) throws Exception { |
| 50 | + return new String( |
| 51 | + Files.readAllBytes( |
| 52 | + new File(getProjectDir(), path).toPath())); |
| 53 | + } |
| 54 | + |
| 55 | + protected void createProjectAndBuild() throws Exception { |
| 56 | + createBuildXmlFile(); |
| 57 | + createDatabase(); |
| 58 | + createHibernatePropertiesFile(); |
| 59 | + runAntBuild(); |
| 60 | + } |
| 61 | + |
| 62 | + protected void assertFolderExists(String path, int amountOfChildren) { |
| 63 | + File generatedOutputFolder = new File(getProjectDir(), path); |
| 64 | + assertTrue(generatedOutputFolder.exists()); |
| 65 | + assertTrue(generatedOutputFolder.isDirectory()); |
| 66 | + assertEquals(amountOfChildren, Objects.requireNonNull(generatedOutputFolder.list()).length); |
| 67 | + } |
| 68 | + |
| 69 | + protected void assertFileExists(String path) { |
| 70 | + File generatedPersonJavaFile = new File(getProjectDir(), path); |
| 71 | + assertTrue(generatedPersonJavaFile.exists()); |
| 72 | + assertTrue(generatedPersonJavaFile.isFile()); |
| 73 | + } |
| 74 | + |
| 75 | + private void runAntBuild() { |
| 76 | + File buildXmlFile = new File(getProjectDir(), "build.xml"); |
| 77 | + Project project = new Project(); |
| 78 | + project.setBaseDir(getProjectDir()); |
| 79 | + project.addBuildListener(getConsoleLogger()); |
| 80 | + ProjectHelper.getProjectHelper().parse(project, buildXmlFile); |
| 81 | + project.executeTarget(project.getDefaultTarget()); |
| 82 | + } |
| 83 | + |
| 84 | + private void createBuildXmlFile() throws Exception { |
| 85 | + File buildXmlFile = new File(getProjectDir(), "build.xml"); |
| 86 | + assertFalse(buildXmlFile.exists()); |
| 87 | + Files.writeString(buildXmlFile.toPath(), constructBuildXmlFileContents()); |
| 88 | + assertTrue(buildXmlFile.exists()); |
| 89 | + } |
| 90 | + |
| 91 | + private String constructJdbcConnectionString() { |
| 92 | + return "jdbc:h2:" + getProjectDir().getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; |
| 93 | + } |
| 94 | + |
| 95 | + private void createDatabase() throws Exception { |
| 96 | + File databaseFile = new File(getProjectDir(), "database/test.mv.db"); |
| 97 | + assertFalse(databaseFile.exists()); |
| 98 | + assertFalse(databaseFile.isFile()); |
| 99 | + Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); |
| 100 | + Statement statement = connection.createStatement(); |
| 101 | + for (String sql : databaseCreationScript) { |
| 102 | + statement.execute(sql); |
| 103 | + } |
| 104 | + statement.close(); |
| 105 | + connection.close(); |
| 106 | + assertTrue(databaseFile.exists()); |
| 107 | + assertTrue(databaseFile.isFile()); |
| 108 | + } |
| 109 | + |
| 110 | + private void createHibernatePropertiesFile() throws Exception { |
| 111 | + File hibernatePropertiesFile = new File(getProjectDir(), "hibernate.properties"); |
| 112 | + String hibernatePropertiesFileContents = |
| 113 | + "hibernate.connection.driver_class=org.h2.Driver\n" + |
| 114 | + "hibernate.connection.url=" + constructJdbcConnectionString() + "\n" + |
| 115 | + "hibernate.connection.username=\n" + |
| 116 | + "hibernate.connection.password=\n" + |
| 117 | + "hibernate.default_catalog=TEST\n" + |
| 118 | + "hibernate.default_schema=PUBLIC\n"; |
| 119 | + Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents); |
| 120 | + assertTrue(hibernatePropertiesFile.exists()); |
| 121 | + } |
| 122 | + |
| 123 | + private DefaultLogger getConsoleLogger() { |
| 124 | + DefaultLogger consoleLogger = new DefaultLogger(); |
| 125 | + consoleLogger.setErrorPrintStream(System.err); |
| 126 | + consoleLogger.setOutputPrintStream(System.out); |
| 127 | + consoleLogger.setMessageOutputLevel(Project.MSG_INFO); |
| 128 | + return consoleLogger; |
| 129 | + } |
| 130 | + |
| 131 | + private static final String buildXmlFileContents = |
| 132 | + """ |
| 133 | + <project name='tutorial' default='reveng'> \s |
| 134 | + <taskdef \s |
| 135 | + name='hibernatetool' \s |
| 136 | + classname='org.hibernate.tool.ant.HibernateToolTask'/> \s |
| 137 | + <target name='reveng'> \s |
| 138 | + @hibernateToolTaskXml@\ |
| 139 | + </target> \s |
| 140 | + </project> \s |
| 141 | + """; |
| 142 | + |
| 143 | +} |
0 commit comments