55import static org .junit .jupiter .api .Assertions .fail ;
66
77import org .junit .jupiter .api .BeforeAll ;
8+ import org .junit .jupiter .api .BeforeEach ;
89import org .junit .jupiter .api .Test ;
910
1011import org .apache .maven .cli .MavenCli ;
@@ -21,6 +22,14 @@ public class ExamplesTestIT {
2122 private static File baseFolder ;
2223 private static File localRepo ;
2324
25+ private File projectFolder ;
26+ private MavenCli mavenCli ;
27+
28+ private String [] databaseCreationScript = new String [] {
29+ // This is the default database which can be overridden per test
30+ "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
31+ };
32+
2433 @ BeforeAll
2534 public static void beforeAll () throws Exception {
2635 // The needed resource for this test are put in place
@@ -29,32 +38,26 @@ public static void beforeAll() throws Exception {
2938 // See the 'pom.xml'
3039 baseFolder = determineBaseFolder ();
3140 localRepo = new File (baseFolder .getParentFile (), "local-repo" );
32- createDatabase ();
3341 }
3442
35- private MavenCli mavenCli ;
43+ @ BeforeEach
44+ public void beforeEach () throws Exception {
45+ createDatabase ();
46+ }
3647
3748 @ Test
3849 public void test5MinuteTutorial () throws Exception {
39- File projectFolder = prepareProjectFolder ("5-minute-tutorial" );
40- File generatedPersonFile = new File (projectFolder , "target/generated-sources/Person.java" );
41- assertFalse (generatedPersonFile .exists ());
42- new MavenCli ().doMain (
43- new String []{"-Dmaven.repo.local=" + localRepo .getAbsolutePath (), "generate-sources" },
44- projectFolder .getAbsolutePath (),
45- null ,
46- null );
47- assertTrue (generatedPersonFile .exists ());
48- String personFileContents = new String (Files .readAllBytes (generatedPersonFile .toPath ()));
49- assertTrue (personFileContents .contains ("public class Person" ));
50+ prepareProject ("5-minute-tutorial" );
51+ assertNotGeneratedYet ();
52+ runGenerateSources ();
53+ assertGeneratedContains ("public class Person" );
5054 }
5155
52- private File prepareProjectFolder (String projectName ) throws Exception {
53- File projectFolder = new File (baseFolder , projectName );
56+ private void prepareProject (String projectName ) throws Exception {
57+ projectFolder = new File (baseFolder , projectName );
5458 assertTrue (projectFolder .exists ());
5559 System .setProperty (MVN_HOME , projectFolder .getAbsolutePath ());
5660 createHibernatePropertiesFile (projectFolder );
57- return projectFolder ;
5861 }
5962
6063 private void createHibernatePropertiesFile (File projectFolder ) throws Exception {
@@ -73,18 +76,46 @@ private void createHibernatePropertiesFile(File projectFolder) throws Exception
7376 assertTrue (hibernatePropertiesFile .exists ());
7477 }
7578
79+ private void runGenerateSources () {
80+ new MavenCli ().doMain (
81+ new String []{"-Dmaven.repo.local=" + localRepo .getAbsolutePath (), "generate-sources" },
82+ projectFolder .getAbsolutePath (),
83+ null ,
84+ null );
85+ }
86+
87+ private void assertNotGeneratedYet () {
88+ assertFalse (new File (projectFolder , "target/generated-sources/Person.java" ).exists ());
89+ }
90+
91+ private void assertGeneratedContains (String contents ) throws Exception {
92+ assertTrue (readGeneratedContents ().contains (contents ));
93+ }
94+
95+ private void assertGeneratedDoesNotContain (String contents ) throws Exception {
96+ assertFalse (readGeneratedContents ().contains (contents ));
97+ }
98+
99+ private String readGeneratedContents () throws Exception {
100+ File generatedPersonFile = new File (projectFolder , "target/generated-sources/Person.java" );
101+ assertTrue (generatedPersonFile .exists ());
102+ return new String (Files .readAllBytes (generatedPersonFile .toPath ()));
103+ }
104+
76105 private static File determineBaseFolder () throws Exception {
77106 return new File (ExamplesTestIT .class .getClassLoader ().getResource ("5-minute-tutorial/pom.xml" ).toURI ())
78107 .getParentFile ().getParentFile ();
79108 }
80109
81- private static void createDatabase () throws Exception {
110+ private void createDatabase () throws Exception {
82111 File databaseFile = new File (baseFolder , "database/test.mv.db" );
83112 assertFalse (databaseFile .exists ());
84113 assertFalse (databaseFile .isFile ());
85114 Connection connection = DriverManager .getConnection (constructJdbcConnectionString ());
86115 Statement statement = connection .createStatement ();
87- statement .execute ("create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" );
116+ for (String s : databaseCreationScript ) {
117+ statement .execute (s );
118+ }
88119 statement .close ();
89120 connection .close ();
90121 assertTrue (databaseFile .exists ());
0 commit comments