|
| 1 | +package org.hibernate.tool.maven; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileWriter; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.sql.DriverManager; |
| 11 | + |
| 12 | +import org.apache.maven.project.MavenProject; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.io.TempDir; |
| 17 | + |
| 18 | +public class GenerateJavaMojoTest { |
| 19 | + |
| 20 | + private static final String JDBC_CONNECTION = "jdbc:h2:mem:test"; |
| 21 | + private static final String CREATE_PERSON_TABLE = |
| 22 | + "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; |
| 23 | + private static final String DROP_PERSON_TABLE = |
| 24 | + "drop table PERSON"; |
| 25 | + |
| 26 | + @TempDir |
| 27 | + private File tempDir; |
| 28 | + |
| 29 | + private File outputDirectory; |
| 30 | + private GenerateJavaMojo generateJavaMojo; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void beforeEach() throws Exception { |
| 34 | + createDatabase(); |
| 35 | + createPropertiesFile(); |
| 36 | + createOutputDirectory(); |
| 37 | + createGenerateJavaMojo(); |
| 38 | + } |
| 39 | + |
| 40 | + @AfterEach |
| 41 | + public void afterEach() throws Exception { |
| 42 | + dropDatabase(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testGenerateAnnotatedJava() throws Exception { |
| 47 | + File personJavaFile = new File(outputDirectory, "Person.java"); |
| 48 | + // Person.java should not exist |
| 49 | + assertFalse(personJavaFile.exists()); |
| 50 | + // Execute mojo with default value of 'ejb3' field which is 'true' |
| 51 | + generateJavaMojo.execute(); |
| 52 | + // Person.java should exist |
| 53 | + assertTrue(personJavaFile.exists()); |
| 54 | + // Person.java should be an annotated entity |
| 55 | + byte[] raw = Files.readAllBytes(personJavaFile.toPath()); |
| 56 | + assertTrue(new String(raw).contains("import jakarta.persistence.Entity;")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testGenerateNonAnnotatedJava() throws Exception { |
| 61 | + File personJavaFile = new File(outputDirectory, "Person.java"); |
| 62 | + // Person.java should not exist |
| 63 | + assertFalse(personJavaFile.exists()); |
| 64 | + // Set value of field 'ejb3' to 'false' and execute mojo |
| 65 | + Field ejb3Field = GenerateJavaMojo.class.getDeclaredField("ejb3"); |
| 66 | + ejb3Field.setAccessible(true); |
| 67 | + ejb3Field.set(generateJavaMojo, false); |
| 68 | + generateJavaMojo.execute(); |
| 69 | + // Person.java should exist |
| 70 | + assertTrue(personJavaFile.exists()); |
| 71 | + // Person.java should be an annotated entity |
| 72 | + byte[] raw = Files.readAllBytes(personJavaFile.toPath()); |
| 73 | + assertFalse(new String(raw).contains("import jakarta.persistence.Entity;")); |
| 74 | + } |
| 75 | + |
| 76 | + private void createDatabase() throws Exception { |
| 77 | + DriverManager |
| 78 | + .getConnection(JDBC_CONNECTION) |
| 79 | + .createStatement() |
| 80 | + .execute(CREATE_PERSON_TABLE); |
| 81 | + } |
| 82 | + |
| 83 | + private void dropDatabase() throws Exception { |
| 84 | + DriverManager |
| 85 | + .getConnection(JDBC_CONNECTION) |
| 86 | + .createStatement() |
| 87 | + .execute(DROP_PERSON_TABLE); |
| 88 | + } |
| 89 | + |
| 90 | + private void createPropertiesFile() throws Exception { |
| 91 | + File propertiesFile = new File(tempDir, "hibernate.properties"); |
| 92 | + try (FileWriter fileWriter = new FileWriter(propertiesFile)) { |
| 93 | + fileWriter.write("hibernate.connection.url=" + JDBC_CONNECTION + '\n'); |
| 94 | + fileWriter.write("hibernate.default_catalog=TEST\n"); |
| 95 | + fileWriter.write("hibernate.default_schema=PUBLIC\n"); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void createGenerateJavaMojo() throws Exception { |
| 100 | + generateJavaMojo = new GenerateJavaMojo(); |
| 101 | + Field projectField = AbstractGenerationMojo.class.getDeclaredField("project"); |
| 102 | + projectField.setAccessible(true); |
| 103 | + projectField.set(generateJavaMojo, new MavenProject()); |
| 104 | + Field propertyFileField = AbstractGenerationMojo.class.getDeclaredField("propertyFile"); |
| 105 | + propertyFileField.setAccessible(true); |
| 106 | + propertyFileField.set(generateJavaMojo, new File(tempDir, "hibernate.properties")); |
| 107 | + Field outputDirectoryField = GenerateJavaMojo.class.getDeclaredField("outputDirectory"); |
| 108 | + outputDirectoryField.setAccessible(true); |
| 109 | + outputDirectoryField.set(generateJavaMojo, outputDirectory); |
| 110 | + } |
| 111 | + |
| 112 | + private void createOutputDirectory() { |
| 113 | + outputDirectory = new File(tempDir, "generated"); |
| 114 | + outputDirectory.mkdir(); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +} |
0 commit comments