|
| 1 | +package org.hibernate.tool.ant.hbm2java; |
| 2 | + |
| 3 | +import org.apache.tools.ant.DefaultLogger; |
| 4 | +import org.apache.tools.ant.Project; |
| 5 | +import org.apache.tools.ant.ProjectHelper; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.io.TempDir; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.File; |
| 12 | +import java.io.PrintStream; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.sql.Connection; |
| 15 | +import java.sql.DriverManager; |
| 16 | +import java.sql.Statement; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +public class NoGenericsTestIT { |
| 21 | + |
| 22 | + @TempDir |
| 23 | + private File projectDir; |
| 24 | + |
| 25 | + private File buildXmlFile; |
| 26 | + private ByteArrayOutputStream output; |
| 27 | + private File databaseFile; |
| 28 | + private File personFile; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + public void beforeEach() { |
| 32 | + output = new ByteArrayOutputStream(); |
| 33 | + databaseFile = new File(projectDir, "database/test.mv.db"); |
| 34 | + assertFalse(databaseFile.exists()); |
| 35 | + personFile = new File(projectDir, "generated/Person.java"); |
| 36 | + assertFalse(personFile.exists()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testUseGenerics() throws Exception { |
| 41 | + createBuildXmlFile(); |
| 42 | + createDatabase(); |
| 43 | + createHibernatePropertiesFile(); |
| 44 | + runAntBuild(); |
| 45 | + verifyResult(); |
| 46 | + } |
| 47 | + |
| 48 | + private void createBuildXmlFile() throws Exception { |
| 49 | + buildXmlFile = new File(projectDir, "build.xml"); |
| 50 | + assertFalse(buildXmlFile.exists()); |
| 51 | + Files.writeString(buildXmlFile.toPath(), buildXmlFileContents); |
| 52 | + } |
| 53 | + |
| 54 | + private void createDatabase() throws Exception { |
| 55 | + String CREATE_PERSON_TABLE = "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"; |
| 56 | + String CREATE_ITEM_TABLE = |
| 57 | + "create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " + |
| 58 | + " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"; |
| 59 | + Connection connection = DriverManager.getConnection(constructJdbcConnectionString()); |
| 60 | + Statement statement = connection.createStatement(); |
| 61 | + statement.execute(CREATE_PERSON_TABLE); |
| 62 | + statement.execute(CREATE_ITEM_TABLE); |
| 63 | + statement.close(); |
| 64 | + connection.close(); |
| 65 | + assertTrue(databaseFile.exists()); |
| 66 | + assertTrue(databaseFile.isFile()); |
| 67 | + } |
| 68 | + |
| 69 | + private void createHibernatePropertiesFile() throws Exception { |
| 70 | + File hibernatePropertiesFile = new File(projectDir, "hibernate.properties"); |
| 71 | + StringBuffer hibernatePropertiesFileContents = new StringBuffer(); |
| 72 | + hibernatePropertiesFileContents |
| 73 | + .append("hibernate.connection.driver_class=org.h2.Driver\n") |
| 74 | + .append("hibernate.connection.url=" + constructJdbcConnectionString() + "\n") |
| 75 | + .append("hibernate.connection.username=\n") |
| 76 | + .append("hibernate.connection.password=\n") |
| 77 | + .append("hibernate.default_catalog=TEST\n") |
| 78 | + .append("hibernate.default_schema=PUBLIC\n"); |
| 79 | + Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.toString()); |
| 80 | + assertTrue(hibernatePropertiesFile.exists()); |
| 81 | + } |
| 82 | + |
| 83 | + private void runAntBuild() { |
| 84 | + Project project = new Project(); |
| 85 | + project.setBaseDir(projectDir); |
| 86 | + project.addBuildListener(getConsoleLogger()); |
| 87 | + ProjectHelper.getProjectHelper().parse(project, buildXmlFile); |
| 88 | + project.executeTarget(project.getDefaultTarget()); |
| 89 | + } |
| 90 | + |
| 91 | + private void verifyResult() throws Exception { |
| 92 | + File generatedOutputFolder = new File(projectDir, "generated"); |
| 93 | + assertTrue(generatedOutputFolder.exists()); |
| 94 | + assertTrue(generatedOutputFolder.isDirectory()); |
| 95 | + assertEquals(2, generatedOutputFolder.list().length); |
| 96 | + File generatedPersonJavaFile = new File(generatedOutputFolder, "Person.java"); |
| 97 | + assertTrue(generatedPersonJavaFile.exists()); |
| 98 | + assertTrue(generatedPersonJavaFile.isFile()); |
| 99 | + String generatedPersonJavaFileContents = new String( |
| 100 | + Files.readAllBytes(generatedPersonJavaFile.toPath())); |
| 101 | + assertTrue(generatedPersonJavaFileContents.contains("public class Person ")); |
| 102 | + assertFalse(generatedPersonJavaFileContents.contains("Set<Item>")); |
| 103 | + File generatedItemJavaFile = new File(generatedOutputFolder, "Item.java"); |
| 104 | + assertTrue(generatedItemJavaFile.exists()); |
| 105 | + assertTrue(generatedItemJavaFile.isFile()); |
| 106 | + String generatedItemJavaFileContents = new String( |
| 107 | + Files.readAllBytes(generatedItemJavaFile.toPath())); |
| 108 | + assertTrue(generatedItemJavaFileContents.contains("public class Item ")); |
| 109 | + } |
| 110 | + |
| 111 | + private DefaultLogger getConsoleLogger() { |
| 112 | + DefaultLogger consoleLogger = new DefaultLogger(); |
| 113 | + consoleLogger.setErrorPrintStream(System.err); |
| 114 | + consoleLogger.setOutputPrintStream(new PrintStream(output, true)); |
| 115 | + consoleLogger.setMessageOutputLevel(Project.MSG_INFO); |
| 116 | + return consoleLogger; |
| 117 | + } |
| 118 | + |
| 119 | + private String constructJdbcConnectionString() { |
| 120 | + return "jdbc:h2:" + projectDir.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE"; |
| 121 | + } |
| 122 | + |
| 123 | + private static final String buildXmlFileContents = |
| 124 | + |
| 125 | + "<project name='tutorial' default='reveng'> \n" + |
| 126 | + " <taskdef \n" + |
| 127 | + " name='hibernatetool' \n" + |
| 128 | + " classname='org.hibernate.tool.ant.HibernateToolTask'/> \n" + |
| 129 | + " <target name='reveng'> \n" + |
| 130 | + " <hibernatetool destdir='generated'> \n" + |
| 131 | + " <jdbcconfiguration propertyfile='hibernate.properties'/> \n" + |
| 132 | + " <hbm2java jdk5='false'/> \n" + |
| 133 | + " </hibernatetool> \n" + |
| 134 | + " </target> \n" + |
| 135 | + "</project> \n" ; |
| 136 | + |
| 137 | +} |
0 commit comments