44import static org .junit .jupiter .api .Assertions .assertFalse ;
55import static org .junit .jupiter .api .Assertions .assertTrue ;
66
7+ import org .hibernate .tool .api .version .Version ;
78import org .junit .jupiter .api .BeforeAll ;
89import org .junit .jupiter .api .Test ;
910import org .junit .jupiter .api .io .TempDir ;
1213import org .apache .maven .cli .MavenCli ;
1314
1415import java .io .File ;
16+ import java .net .URL ;
1517import java .nio .file .Files ;
1618import java .sql .Connection ;
1719import java .sql .DriverManager ;
1820import java .sql .Statement ;
21+ import java .util .Objects ;
1922
2023public class ExamplesTestIT {
2124
@@ -24,7 +27,6 @@ public class ExamplesTestIT {
2427 private static File localRepo ;
2528
2629 private File projectFolder ;
27- private MavenCli mavenCli ;
2830
2931 @ TempDir
3032 private File tempFolder ;
@@ -94,7 +96,7 @@ public void testOutputDirectory() throws Exception {
9496 assertFalse (outputDirectory .exists ());
9597 assertFalse (personFile .exists ());
9698 runGenerateSources ();
97- assertEquals (1 , outputDirectory .list ().length ); // 1 file is generated in 'generated-classes'
99+ assertEquals (1 , Objects . requireNonNull ( outputDirectory .list () ).length ); // 1 file is generated in 'generated-classes'
98100 assertTrue (personFile .exists ()); // The Person.java file should have been generated
99101 }
100102
@@ -122,6 +124,17 @@ public void testUseGenerics() throws Exception {
122124 assertGeneratedContains ("Person.java" , "Set<Item>" );
123125 }
124126
127+ @ Test
128+ public void testHbm2OrmSimpleDefault () throws Exception {
129+ projectFolder = new File (baseFolder , "hbm2orm/simple-default" );
130+ File ormXmlFile = new File (projectFolder , "src/main/resources/simple.mapping.xml" );
131+ assertFalse (ormXmlFile .exists ());
132+ runMavenCommand ("org.hibernate.tool:hibernate-tools-maven:" + Version .versionString () + ":hbm2orm" );
133+ assertTrue (ormXmlFile .exists ());
134+ String ormXmlContents = Files .readString ( ormXmlFile .toPath () );
135+ assertTrue (ormXmlContents .contains ("entity-mappings" ));
136+ }
137+
125138 private void prepareProject (String projectName ) throws Exception {
126139 projectFolder = new File (baseFolder , projectName );
127140 assertTrue (projectFolder .exists ());
@@ -132,7 +145,7 @@ private void prepareProject(String projectName) throws Exception {
132145
133146 private void createHibernatePropertiesFile (File projectFolder ) throws Exception {
134147 File projectResourcesFolder = new File (projectFolder , "src/main/resources" );
135- projectResourcesFolder .mkdirs ();
148+ assertTrue ( projectResourcesFolder .mkdirs () );
136149 File hibernatePropertiesFile = new File (projectResourcesFolder , "hibernate.properties" );
137150 assertFalse (hibernatePropertiesFile .exists ());
138151 String hibernatePropertiesFileContents =
@@ -147,8 +160,14 @@ private void createHibernatePropertiesFile(File projectFolder) throws Exception
147160 }
148161
149162 private void runGenerateSources () {
163+ runMavenCommand ("generate-sources" );
164+ }
165+
166+ private void runMavenCommand (String command ) {
150167 new MavenCli ().doMain (
151- new String []{"-Dmaven.repo.local=" + localRepo .getAbsolutePath (), "generate-sources" },
168+ new String []{
169+ "-Dmaven.repo.local=" + localRepo .getAbsolutePath (),
170+ command },
152171 projectFolder .getAbsolutePath (),
153172 null ,
154173 null );
@@ -166,8 +185,11 @@ private void assertGeneratedDoesNotContain(String fileName, String contents) thr
166185 assertFalse (readGeneratedContents (fileName ).contains (contents ));
167186 }
168187
169- private void assertNumberOfGeneratedFiles (int amount ) throws Exception {
170- assertEquals (amount , new File (projectFolder , "target/generated-sources" ).list ().length );
188+ private void assertNumberOfGeneratedFiles (int amount ) {
189+ assertEquals (
190+ amount ,
191+ Objects .requireNonNull (
192+ new File (projectFolder , "target/generated-sources" ).list ()).length );
171193 }
172194
173195 private String readGeneratedContents (String fileName ) throws Exception {
@@ -177,8 +199,14 @@ private String readGeneratedContents(String fileName) throws Exception {
177199 }
178200
179201 private static File determineBaseFolder () throws Exception {
180- return new File (ExamplesTestIT .class .getClassLoader ().getResource ("5-minute-tutorial/pom.xml" ).toURI ())
181- .getParentFile ().getParentFile ();
202+ Class <?> thisClass = ExamplesTestIT .class ;
203+ URL classUrl = thisClass .getResource ("/" + thisClass .getName ().replace ("." , "/" ) + ".class" );
204+ assert classUrl != null ;
205+ File result = new File (classUrl .toURI ());
206+ for (int i = 0 ; i < thisClass .getName ().chars ().filter (ch -> ch == '.' ).count () + 1 ; i ++) {
207+ result = result .getParentFile ();
208+ }
209+ return result ;
182210 }
183211
184212 private void createDatabase () throws Exception {
0 commit comments