|
| 1 | +package org.hibernate.tool.gradle.java; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.DriverManager; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import org.gradle.testkit.runner.BuildResult; |
| 16 | +import org.gradle.testkit.runner.GradleRunner; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.io.TempDir; |
| 19 | + |
| 20 | +public class NoAnnotationsTest { |
| 21 | + |
| 22 | + private static final List<String> GRADLE_INIT_PROJECT_ARGUMENTS = List.of( |
| 23 | + "init", "--type", "java-application", "--dsl", "groovy", "--test-framework", "junit-jupiter", "--java-version", "17"); |
| 24 | + |
| 25 | + @TempDir |
| 26 | + private File projectDir; |
| 27 | + |
| 28 | + private File gradlePropertiesFile; |
| 29 | + private File gradleBuildFile; |
| 30 | + private File databaseFile; |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testTutorial() throws Exception { |
| 34 | + assertTrue(projectDir.exists()); |
| 35 | + createGradleProject(); |
| 36 | + editGradleBuildFile(); |
| 37 | + editGradlePropertiesFile(); |
| 38 | + createDatabase(); |
| 39 | + createHibernatePropertiesFile(); |
| 40 | + verifyDatabase(); |
| 41 | + executeGenerateJavaTask(); |
| 42 | + verifyProject(); |
| 43 | + } |
| 44 | + |
| 45 | + private void verifyDatabase() throws Exception { |
| 46 | + Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); |
| 47 | + ResultSet resultSet = connection.createStatement().executeQuery("select * from PERSON"); |
| 48 | + assertTrue(resultSet.next()); |
| 49 | + assertEquals(1, resultSet.getInt(1)); |
| 50 | + assertEquals("foo", resultSet.getString(2)); |
| 51 | + } |
| 52 | + |
| 53 | + private void createGradleProject() throws Exception { |
| 54 | + GradleRunner runner = GradleRunner.create(); |
| 55 | + runner.withArguments(GRADLE_INIT_PROJECT_ARGUMENTS); |
| 56 | + runner.forwardOutput(); |
| 57 | + runner.withProjectDir(projectDir); |
| 58 | + BuildResult buildResult = runner.build(); |
| 59 | + assertTrue(buildResult.getOutput().contains("BUILD SUCCESSFUL")); |
| 60 | + gradlePropertiesFile = new File(projectDir, "gradle.properties"); |
| 61 | + assertTrue(gradlePropertiesFile.exists()); |
| 62 | + assertTrue(gradlePropertiesFile.isFile()); |
| 63 | + File appDir = new File(projectDir, "app"); |
| 64 | + assertTrue(appDir.exists()); |
| 65 | + assertTrue(appDir.isDirectory()); |
| 66 | + gradleBuildFile = new File(appDir, "build.gradle"); |
| 67 | + assertTrue(gradleBuildFile.exists()); |
| 68 | + assertTrue(gradleBuildFile.isFile()); |
| 69 | + databaseFile = new File(projectDir, "database/test.mv.db"); |
| 70 | + assertFalse(databaseFile.exists()); |
| 71 | + } |
| 72 | + |
| 73 | + private void editGradleBuildFile() throws Exception { |
| 74 | + StringBuffer gradleBuildFileContents = new StringBuffer( |
| 75 | + new String(Files.readAllBytes(gradleBuildFile.toPath()))); |
| 76 | + addHibernateToolsPluginLine(gradleBuildFileContents); |
| 77 | + addH2DatabaseDependencyLine(gradleBuildFileContents); |
| 78 | + addHibernateToolsExtension(gradleBuildFileContents); |
| 79 | + Files.writeString(gradleBuildFile.toPath(), gradleBuildFileContents.toString()); |
| 80 | + } |
| 81 | + |
| 82 | + private void editGradlePropertiesFile() throws Exception { |
| 83 | + // The Hibernate Tools Gradle plugin does not support the configuration cache. |
| 84 | + // As this is enabled by default when initializing a new Gradle project, the setting needs to be commented out |
| 85 | + // in the gradle.properties file. |
| 86 | + StringBuffer gradlePropertiesFileContents = new StringBuffer( |
| 87 | + new String(Files.readAllBytes(gradlePropertiesFile.toPath()))); |
| 88 | + int pos = gradlePropertiesFileContents.indexOf("org.gradle.configuration-cache=true"); |
| 89 | + gradlePropertiesFileContents.insert(pos, "#"); |
| 90 | + Files.writeString(gradlePropertiesFile.toPath(), gradlePropertiesFileContents.toString()); |
| 91 | + } |
| 92 | + |
| 93 | + private void createDatabase() throws Exception { |
| 94 | + String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; |
| 95 | + Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); |
| 96 | + Statement statement = connection.createStatement(); |
| 97 | + statement.execute(CREATE_PERSON_TABLE); |
| 98 | + statement.execute("insert into PERSON values (1, 'foo')"); |
| 99 | + statement.close(); |
| 100 | + connection.close(); |
| 101 | + assertTrue(databaseFile.exists()); |
| 102 | + assertTrue(databaseFile.isFile()); |
| 103 | + } |
| 104 | + |
| 105 | + private void createHibernatePropertiesFile() throws Exception { |
| 106 | + File hibernatePropertiesFile = new File(projectDir, "app/src/main/resources/hibernate.properties"); |
| 107 | + StringBuffer hibernatePropertiesFileContents = new StringBuffer(); |
| 108 | + hibernatePropertiesFileContents |
| 109 | + .append("hibernate.connection.driver_class=org.h2.Driver\n") |
| 110 | + .append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n") |
| 111 | + .append("hibernate.connection.username=\n") |
| 112 | + .append("hibernate.connection.password=\n") |
| 113 | + .append("hibernate.default_catalog=TEST\n") |
| 114 | + .append("hibernate.default_schema=PUBLIC\n"); |
| 115 | + Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString()); |
| 116 | + assertTrue(hibernatePropertiesFile.exists()); |
| 117 | + } |
| 118 | + |
| 119 | + private void executeGenerateJavaTask() throws Exception { |
| 120 | + GradleRunner gradleRunner = GradleRunner.create(); |
| 121 | + gradleRunner.forwardOutput(); |
| 122 | + gradleRunner.withProjectDir(projectDir); |
| 123 | + gradleRunner.withPluginClasspath(); |
| 124 | + gradleRunner.withArguments("generateJava"); |
| 125 | + BuildResult buildResult = gradleRunner.build(); |
| 126 | + assertTrue(buildResult.getOutput().contains("BUILD SUCCESSFUL")); |
| 127 | + } |
| 128 | + |
| 129 | + private void verifyProject() throws Exception { |
| 130 | + File generatedOutputFolder = new File(projectDir, "app/generated-sources"); |
| 131 | + assertTrue(generatedOutputFolder.exists()); |
| 132 | + assertTrue(generatedOutputFolder.isDirectory()); |
| 133 | + assertEquals(1, generatedOutputFolder.list().length); |
| 134 | + File generatedPersonJavaFile = new File(generatedOutputFolder, "Person.java"); |
| 135 | + assertTrue(generatedPersonJavaFile.exists()); |
| 136 | + assertTrue(generatedPersonJavaFile.isFile()); |
| 137 | + String generatedPersonJavaFileContents = new String( |
| 138 | + Files.readAllBytes(generatedPersonJavaFile.toPath())); |
| 139 | + assertFalse(generatedPersonJavaFileContents.contains("import jakarta.persistence.Entity;")); |
| 140 | + } |
| 141 | + |
| 142 | + private void addHibernateToolsPluginLine(StringBuffer gradleBuildFileContents) { |
| 143 | + int pos = gradleBuildFileContents.indexOf("plugins {"); |
| 144 | + pos = gradleBuildFileContents.indexOf("}", pos); |
| 145 | + gradleBuildFileContents.insert(pos, constructHibernateToolsPluginLine() + "\n"); |
| 146 | + } |
| 147 | + |
| 148 | + private void addH2DatabaseDependencyLine(StringBuffer gradleBuildFileContents) { |
| 149 | + int pos = gradleBuildFileContents.indexOf("dependencies {"); |
| 150 | + pos = gradleBuildFileContents.indexOf("}", pos); |
| 151 | + gradleBuildFileContents.insert(pos, constructH2DatabaseDependencyLine() + "\n"); |
| 152 | + } |
| 153 | + |
| 154 | + private void addHibernateToolsExtension(StringBuffer gradleBuildFileContents) { |
| 155 | + int pos = gradleBuildFileContents.indexOf("dependencies {"); |
| 156 | + pos = gradleBuildFileContents.indexOf("}", pos); |
| 157 | + StringBuffer hibernateToolsExtension = new StringBuffer(); |
| 158 | + hibernateToolsExtension |
| 159 | + .append("\n") |
| 160 | + .append("\n") |
| 161 | + .append("hibernateTools { \n") |
| 162 | + .append(" generateAnnotations=false \n") |
| 163 | + .append("}"); |
| 164 | + gradleBuildFileContents.insert(pos + 1, hibernateToolsExtension.toString()); |
| 165 | + } |
| 166 | + |
| 167 | + private String constructJdbcConnectionString() { |
| 168 | + return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; |
| 169 | + } |
| 170 | + |
| 171 | + private String constructHibernateToolsPluginLine() { |
| 172 | + return " id 'org.hibernate.tool.hibernate-tools-gradle' version '" |
| 173 | + + System.getenv("HIBERNATE_TOOLS_VERSION") + "'"; |
| 174 | + } |
| 175 | + |
| 176 | + private String constructH2DatabaseDependencyLine() { |
| 177 | + return " implementation 'com.h2database:h2:" + System.getenv("H2_VERSION") + "'"; |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments