Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<!-- Maven Plugins -->
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class GenerateJavaMojo extends AbstractGenerationMojo {

/** Code will contain JPA features, e.g. using annotations from jakarta.persistence
* and org.hibernate.annotations. */
@Parameter(defaultValue = "false")
private boolean ejb3;
@Parameter(defaultValue = "true")
private boolean ejb3 = true;

/** Code will contain JDK 5 constructs such as generics and static imports. */
@Parameter(defaultValue = "false")
Expand Down
118 changes: 118 additions & 0 deletions maven/src/test/java/org/hibernate/tool/maven/GenerateJavaMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.hibernate.tool.maven;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.sql.DriverManager;

import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class GenerateJavaMojoTest {

private static final String JDBC_CONNECTION = "jdbc:h2:mem:test";
private static final String CREATE_PERSON_TABLE =
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))";
private static final String DROP_PERSON_TABLE =
"drop table PERSON";

@TempDir
private File tempDir;

private File outputDirectory;
private GenerateJavaMojo generateJavaMojo;

@BeforeEach
public void beforeEach() throws Exception {
createDatabase();
createPropertiesFile();
createOutputDirectory();
createGenerateJavaMojo();
}

@AfterEach
public void afterEach() throws Exception {
dropDatabase();
}

@Test
public void testGenerateAnnotatedJava() throws Exception {
File personJavaFile = new File(outputDirectory, "Person.java");
// Person.java should not exist
assertFalse(personJavaFile.exists());
// Execute mojo with default value of 'ejb3' field which is 'true'
generateJavaMojo.execute();
// Person.java should exist
assertTrue(personJavaFile.exists());
// Person.java should be an annotated entity
byte[] raw = Files.readAllBytes(personJavaFile.toPath());
assertTrue(new String(raw).contains("import jakarta.persistence.Entity;"));
}

@Test
public void testGenerateNonAnnotatedJava() throws Exception {
File personJavaFile = new File(outputDirectory, "Person.java");
// Person.java should not exist
assertFalse(personJavaFile.exists());
// Set value of field 'ejb3' to 'false' and execute mojo
Field ejb3Field = GenerateJavaMojo.class.getDeclaredField("ejb3");
ejb3Field.setAccessible(true);
ejb3Field.set(generateJavaMojo, false);
generateJavaMojo.execute();
// Person.java should exist
assertTrue(personJavaFile.exists());
// Person.java should be an annotated entity
byte[] raw = Files.readAllBytes(personJavaFile.toPath());
assertFalse(new String(raw).contains("import jakarta.persistence.Entity;"));
}

private void createDatabase() throws Exception {
DriverManager
.getConnection(JDBC_CONNECTION)
.createStatement()
.execute(CREATE_PERSON_TABLE);
}

private void dropDatabase() throws Exception {
DriverManager
.getConnection(JDBC_CONNECTION)
.createStatement()
.execute(DROP_PERSON_TABLE);
}

private void createPropertiesFile() throws Exception {
File propertiesFile = new File(tempDir, "hibernate.properties");
try (FileWriter fileWriter = new FileWriter(propertiesFile)) {
fileWriter.write("hibernate.connection.url=" + JDBC_CONNECTION + '\n');
fileWriter.write("hibernate.default_catalog=TEST\n");
fileWriter.write("hibernate.default_schema=PUBLIC\n");
}
}

private void createGenerateJavaMojo() throws Exception {
generateJavaMojo = new GenerateJavaMojo();
Field projectField = AbstractGenerationMojo.class.getDeclaredField("project");
projectField.setAccessible(true);
projectField.set(generateJavaMojo, new MavenProject());
Field propertyFileField = AbstractGenerationMojo.class.getDeclaredField("propertyFile");
propertyFileField.setAccessible(true);
propertyFileField.set(generateJavaMojo, new File(tempDir, "hibernate.properties"));
Field outputDirectoryField = GenerateJavaMojo.class.getDeclaredField("outputDirectory");
outputDirectoryField.setAccessible(true);
outputDirectoryField.set(generateJavaMojo, outputDirectory);
}

private void createOutputDirectory() {
outputDirectory = new File(tempDir, "generated");
outputDirectory.mkdir();
}


}