44 */
55package org .hibernate .tool .maven ;
66
7- import org .apache .maven .cli .MavenCli ;
7+ import static org .junit .jupiter .api .Assertions .assertEquals ;
8+ import static org .junit .jupiter .api .Assertions .assertFalse ;
9+ import static org .junit .jupiter .api .Assertions .assertTrue ;
10+
11+ import org .hibernate .tool .reveng .api .version .Version ;
812import org .junit .jupiter .api .BeforeAll ;
913import org .junit .jupiter .api .Test ;
1014import org .junit .jupiter .api .io .TempDir ;
1115
16+
17+ import org .apache .maven .cli .MavenCli ;
18+
1219import java .io .File ;
20+ import java .net .URL ;
1321import java .nio .file .Files ;
1422import java .sql .Connection ;
1523import java .sql .DriverManager ;
1624import java .sql .Statement ;
17-
18- import static org .junit .jupiter .api .Assertions .assertEquals ;
19- import static org .junit .jupiter .api .Assertions .assertFalse ;
20- import static org .junit .jupiter .api .Assertions .assertTrue ;
25+ import java .util .Objects ;
2126
2227public class ExamplesTestIT {
2328
2429 public static final String MVN_HOME = "maven.multiModuleProjectDirectory" ;
2530 private static File baseFolder ;
26- // private static File localRepo;
31+ private static File localRepo ;
2732
2833 private File projectFolder ;
29- private MavenCli mavenCli ;
3034
3135 @ TempDir
3236 private File tempFolder ;
@@ -43,7 +47,7 @@ public static void beforeAll() throws Exception {
4347 // by the 'build-helper-maven-plugin' execution.
4448 // See the 'pom.xml'
4549 baseFolder = determineBaseFolder ();
46- // localRepo = new File(baseFolder.getParentFile(), "local-repo");
50+ localRepo = new File (baseFolder .getParentFile (), "local-repo" );
4751 }
4852
4953 @ Test
@@ -78,7 +82,7 @@ public void testNoGenerics() throws Exception {
7882 databaseCreationScript = new String [] {
7983 "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" ,
8084 "create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
81- " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
85+ " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
8286 };
8387 prepareProject ("hbm2java/no-generics" );
8488 assertNotGeneratedYet ("Person.java" );
@@ -96,7 +100,7 @@ public void testOutputDirectory() throws Exception {
96100 assertFalse (outputDirectory .exists ());
97101 assertFalse (personFile .exists ());
98102 runGenerateSources ();
99- assertEquals (1 , outputDirectory .list ().length ); // 1 file is generated in 'generated-classes'
103+ assertEquals (1 , Objects . requireNonNull ( outputDirectory .list () ).length ); // 1 file is generated in 'generated-classes'
100104 assertTrue (personFile .exists ()); // The Person.java file should have been generated
101105 }
102106
@@ -115,7 +119,7 @@ public void testUseGenerics() throws Exception {
115119 databaseCreationScript = new String [] {
116120 "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" ,
117121 "create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
118- " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
122+ " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
119123 };
120124 prepareProject ("hbm2java/use-generics" );
121125 assertNotGeneratedYet ("Person.java" );
@@ -124,34 +128,50 @@ public void testUseGenerics() throws Exception {
124128 assertGeneratedContains ("Person.java" , "Set<Item>" );
125129 }
126130
131+ @ Test
132+ public void testHbm2OrmSimpleDefault () throws Exception {
133+ projectFolder = new File (baseFolder , "hbm2orm/simple-default" );
134+ File ormXmlFile = new File (projectFolder , "src/main/resources/simple.mapping.xml" );
135+ assertFalse (ormXmlFile .exists ());
136+ runMavenCommand ("org.hibernate.tool:hibernate-tools-maven:" + Version .versionString () + ":hbm2orm" );
137+ assertTrue (ormXmlFile .exists ());
138+ String ormXmlContents = Files .readString ( ormXmlFile .toPath () );
139+ assertTrue (ormXmlContents .contains ("entity-mappings" ));
140+ }
141+
127142 private void prepareProject (String projectName ) throws Exception {
128143 projectFolder = new File (baseFolder , projectName );
129144 assertTrue (projectFolder .exists ());
130145 System .setProperty (MVN_HOME , projectFolder .getAbsolutePath ());
131- editPomFile (projectFolder );
132146 createHibernatePropertiesFile (projectFolder );
133147 createDatabase ();
134148 }
135149
136150 private void createHibernatePropertiesFile (File projectFolder ) throws Exception {
137151 File projectResourcesFolder = new File (projectFolder , "src/main/resources" );
138- projectResourcesFolder .mkdirs ();
152+ assertTrue ( projectResourcesFolder .mkdirs () );
139153 File hibernatePropertiesFile = new File (projectResourcesFolder , "hibernate.properties" );
140- // assertFalse(hibernatePropertiesFile.exists());
154+ assertFalse (hibernatePropertiesFile .exists ());
141155 String hibernatePropertiesFileContents =
142156 "hibernate.connection.driver_class=org.h2.Driver\n " +
143- "hibernate.connection.url=" + constructJdbcConnectionString () + "\n " +
144- "hibernate.connection.username=\n " +
145- "hibernate.connection.password=\n " +
146- "hibernate.default_catalog=TEST\n " +
147- "hibernate.default_schema=PUBLIC\n " ;
157+ "hibernate.connection.url=" + constructJdbcConnectionString () + "\n " +
158+ "hibernate.connection.username=\n " +
159+ "hibernate.connection.password=\n " +
160+ "hibernate.default_catalog=TEST\n " +
161+ "hibernate.default_schema=PUBLIC\n " ;
148162 Files .writeString (hibernatePropertiesFile .toPath (), hibernatePropertiesFileContents );
149163 assertTrue (hibernatePropertiesFile .exists ());
150164 }
151165
152166 private void runGenerateSources () {
167+ runMavenCommand ("generate-sources" );
168+ }
169+
170+ private void runMavenCommand (String command ) {
153171 new MavenCli ().doMain (
154- new String []{"generate-sources" },
172+ new String []{
173+ "-Dmaven.repo.local=" + localRepo .getAbsolutePath (),
174+ command },
155175 projectFolder .getAbsolutePath (),
156176 null ,
157177 null );
@@ -169,8 +189,11 @@ private void assertGeneratedDoesNotContain(String fileName, String contents) thr
169189 assertFalse (readGeneratedContents (fileName ).contains (contents ));
170190 }
171191
172- private void assertNumberOfGeneratedFiles (int amount ) throws Exception {
173- assertEquals (amount , new File (projectFolder , "target/generated-sources" ).list ().length );
192+ private void assertNumberOfGeneratedFiles (int amount ) {
193+ assertEquals (
194+ amount ,
195+ Objects .requireNonNull (
196+ new File (projectFolder , "target/generated-sources" ).list ()).length );
174197 }
175198
176199 private String readGeneratedContents (String fileName ) throws Exception {
@@ -180,8 +203,14 @@ private String readGeneratedContents(String fileName) throws Exception {
180203 }
181204
182205 private static File determineBaseFolder () throws Exception {
183- return new File (ExamplesTestIT .class .getClassLoader ().getResource ("5-minute-tutorial/pom.xml" ).toURI ())
184- .getParentFile ().getParentFile ();
206+ Class <?> thisClass = ExamplesTestIT .class ;
207+ URL classUrl = thisClass .getResource ("/" + thisClass .getName ().replace ("." , "/" ) + ".class" );
208+ assert classUrl != null ;
209+ File result = new File (classUrl .toURI ());
210+ for (int i = 0 ; i < thisClass .getName ().chars ().filter (ch -> ch == '.' ).count () + 1 ; i ++) {
211+ result = result .getParentFile ();
212+ }
213+ return result ;
185214 }
186215
187216 private void createDatabase () throws Exception {
@@ -203,15 +232,4 @@ private String constructJdbcConnectionString() {
203232 return "jdbc:h2:" + tempFolder .getAbsolutePath () + "/database/test;AUTO_SERVER=TRUE" ;
204233 }
205234
206- private void editPomFile (File projectFolder ) throws Exception {
207- System .out .println ("Editing pom file" );
208- File pomFile = new File (projectFolder , "pom.xml" );
209- assertTrue (pomFile .exists ());
210- String pomFileContents = Files .readString ( pomFile .toPath () );
211- pomFileContents = pomFileContents
212- .replace ( "${h2.version}" , System .getenv ("h2Version" ) )
213- .replace ( "${hibernate.version}" , System .getenv ("hibernateVersion" ) );
214- Files .writeString ( pomFile .toPath (), pomFileContents );
215- }
216-
217235}
0 commit comments