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