1
1
package org .hibernate .tool .maven ;
2
2
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
3
4
import static org .junit .jupiter .api .Assertions .assertFalse ;
4
5
import static org .junit .jupiter .api .Assertions .assertTrue ;
5
- import static org .junit .jupiter .api .Assertions .fail ;
6
6
7
7
import org .junit .jupiter .api .BeforeAll ;
8
- import org .junit .jupiter .api .BeforeEach ;
9
8
import org .junit .jupiter .api .Test ;
10
9
import org .junit .jupiter .api .io .TempDir ;
11
10
@@ -45,39 +44,31 @@ public static void beforeAll() throws Exception {
45
44
localRepo = new File (baseFolder .getParentFile (), "local-repo" );
46
45
}
47
46
48
- @ BeforeEach
49
- public void beforeEach () throws Exception {
50
- createDatabase ();
51
- }
52
-
53
47
@ Test
54
48
public void test5MinuteTutorial () throws Exception {
55
49
prepareProject ("5-minute-tutorial" );
56
- assertNotGeneratedYet ();
50
+ assertNotGeneratedYet ("Person.java" );
57
51
runGenerateSources ();
58
- assertGeneratedContains ("public class Person" );
52
+ assertNumberOfGeneratedFiles (1 );
53
+ assertGeneratedContains ("Person.java" , "public class Person" );
59
54
}
60
55
61
56
@ Test
62
57
public void testJpaDefault () throws Exception {
63
58
prepareProject ("hbm2java/jpa-default" );
64
- assertNotGeneratedYet ();
59
+ assertNotGeneratedYet ("Person.java" );
65
60
runGenerateSources ();
66
- assertGeneratedContains ("import jakarta.persistence.Entity;" );
61
+ assertNumberOfGeneratedFiles (1 );
62
+ assertGeneratedContains ("Person.java" ,"import jakarta.persistence.Entity;" );
67
63
}
68
64
69
65
@ Test
70
66
public void testNoAnnotations () throws Exception {
71
67
prepareProject ("hbm2java/no-annotations" );
72
- assertNotGeneratedYet ();
68
+ assertNotGeneratedYet ("Person.java" );
73
69
runGenerateSources ();
74
- assertGeneratedDoesNotContain ("import jakarta.persistence.Entity;" );
75
- }
76
- private void prepareProject (String projectName ) throws Exception {
77
- projectFolder = new File (baseFolder , projectName );
78
- assertTrue (projectFolder .exists ());
79
- System .setProperty (MVN_HOME , projectFolder .getAbsolutePath ());
80
- createHibernatePropertiesFile (projectFolder );
70
+ assertNumberOfGeneratedFiles (1 );
71
+ assertGeneratedDoesNotContain ("Person.java" , "import jakarta.persistence.Entity;" );
81
72
}
82
73
83
74
@ Test
@@ -88,9 +79,32 @@ public void testNoGenerics() throws Exception {
88
79
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
89
80
};
90
81
prepareProject ("hbm2java/no-generics" );
91
- assertNotGeneratedYet ();
82
+ assertNotGeneratedYet ("Person.java" );
83
+ runGenerateSources ();
84
+ assertNumberOfGeneratedFiles (2 );
85
+ assertGeneratedDoesNotContain ("Person.java" , "Set<Item>" );
86
+ }
87
+
88
+ @ Test
89
+ public void testUseGenerics () throws Exception {
90
+ databaseCreationScript = new String [] {
91
+ "create table PERSON (ID int not null, NAME varchar(20), primary key (ID))" ,
92
+ "create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
93
+ " primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
94
+ };
95
+ prepareProject ("hbm2java/use-generics" );
96
+ assertNotGeneratedYet ("Person.java" );
92
97
runGenerateSources ();
93
- assertGeneratedDoesNotContain ("Set<Item>" );
98
+ assertNumberOfGeneratedFiles (2 );
99
+ assertGeneratedContains ("Person.java" , "Set<Item>" );
100
+ }
101
+
102
+ private void prepareProject (String projectName ) throws Exception {
103
+ projectFolder = new File (baseFolder , projectName );
104
+ assertTrue (projectFolder .exists ());
105
+ System .setProperty (MVN_HOME , projectFolder .getAbsolutePath ());
106
+ createHibernatePropertiesFile (projectFolder );
107
+ createDatabase ();
94
108
}
95
109
96
110
private void createHibernatePropertiesFile (File projectFolder ) throws Exception {
@@ -117,20 +131,24 @@ private void runGenerateSources() {
117
131
null );
118
132
}
119
133
120
- private void assertNotGeneratedYet () {
121
- assertFalse (new File (projectFolder , "target/generated-sources/Person.java" ).exists ());
134
+ private void assertNotGeneratedYet (String fileName ) {
135
+ assertFalse (new File (projectFolder , "target/generated-sources/" + fileName ).exists ());
136
+ }
137
+
138
+ private void assertGeneratedContains (String fileName , String contents ) throws Exception {
139
+ assertTrue (readGeneratedContents (fileName ).contains (contents ));
122
140
}
123
141
124
- private void assertGeneratedContains ( String contents ) throws Exception {
125
- assertTrue (readGeneratedContents ().contains (contents ));
142
+ private void assertGeneratedDoesNotContain ( String fileName , String contents ) throws Exception {
143
+ assertFalse (readGeneratedContents (fileName ).contains (contents ));
126
144
}
127
145
128
- private void assertGeneratedDoesNotContain ( String contents ) throws Exception {
129
- assertFalse ( readGeneratedContents (). contains ( contents ) );
146
+ private void assertNumberOfGeneratedFiles ( int amount ) throws Exception {
147
+ assertEquals ( amount , new File ( projectFolder , "target/generated-sources" ). list (). length );
130
148
}
131
149
132
- private String readGeneratedContents () throws Exception {
133
- File generatedPersonFile = new File (projectFolder , "target/generated-sources/Person.java" );
150
+ private String readGeneratedContents (String fileName ) throws Exception {
151
+ File generatedPersonFile = new File (projectFolder , "target/generated-sources/" + fileName );
134
152
assertTrue (generatedPersonFile .exists ());
135
153
return new String (Files .readAllBytes (generatedPersonFile .toPath ()));
136
154
}
0 commit comments