28
28
import org .hibernate .testing .TestForIssue ;
29
29
30
30
import static org .junit .Assert .assertTrue ;
31
+ import static org .junit .Assert .fail ;
31
32
32
33
/**
33
34
* @author Andrea Boriero
36
37
public class ImplicitCompositeKeyJoinTest {
37
38
private static final Logger LOGGER = Logger .getLogger ( ImplicitCompositeKeyJoinTest .class );
38
39
39
- private final static String EXPECTED_SQL = "create table Employee " +
40
- "(age varchar(15) not null" +
41
- ", birthday varchar(255) not null" +
42
- ", name varchar(20) not null" +
43
- ", manager_age varchar(15)" +
44
- ", manager_birthday varchar(255)" +
45
- ", manager_name varchar(20)" +
46
- ", primary key (age, birthday, name))" ;
47
-
48
40
@ Test
49
- public void testImplicitCompositeJoin () throws Exception {
50
-
51
- StandardServiceRegistry ssr = new StandardServiceRegistryBuilder ().build ();
41
+ public void testSchemaCreationSQLCommandIsGeneratedWithTheCorrectColumnSizeValues () throws Exception {
42
+ final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder ().build ();
52
43
try {
53
- org .hibernate .boot .Metadata metadata = new MetadataSources ( ssr )
44
+ final org .hibernate .boot .Metadata metadata = new MetadataSources ( ssr )
54
45
.addAnnotatedClass ( Employee .class )
55
46
.buildMetadata ();
56
47
57
- boolean passed = false ;
48
+ boolean createTableEmployeeFound = false ;
58
49
59
- List <String > commands = new SchemaCreatorImpl ().generateCreationCommands (
50
+ final List <String > commands = new SchemaCreatorImpl ().generateCreationCommands (
60
51
metadata ,
61
52
false
62
53
);
54
+
63
55
for ( String command : commands ) {
64
56
LOGGER .info ( command );
57
+ if ( command .toLowerCase ().contains ( "create table employee" ) ) {
58
+ final String [] columnsDefinition = getColumnsDefinition ( command );
65
59
66
- if ( EXPECTED_SQL .equals ( command ) ) {
67
- passed = true ;
60
+ for ( int i = 0 ; i < columnsDefinition .length ; i ++ ) {
61
+ checkColumnSize ( columnsDefinition [i ] );
62
+ }
63
+ createTableEmployeeFound = true ;
68
64
}
69
65
}
70
66
assertTrue (
71
67
"Expected create table command for Employee entity not found" ,
72
- passed
68
+ createTableEmployeeFound
73
69
);
74
70
}
75
71
finally {
76
72
StandardServiceRegistryBuilder .destroy ( ssr );
77
73
}
78
74
}
79
75
76
+ private String [] getColumnsDefinition (String command ) {
77
+ String substring = command .toLowerCase ().replaceAll ( "create table employee " , "" );
78
+ substring = substring .substring ( 0 , substring .toLowerCase ().indexOf ( "primary key" ) );
79
+ return substring .split ( "\\ ," );
80
+ }
81
+
82
+ private void checkColumnSize (String s ) {
83
+ if ( s .toLowerCase ().contains ( "manager_age" ) ) {
84
+ if ( !s .contains ( "15" ) ) {
85
+ fail ( expectedMessage ( "manager_age" , 15 , s ) );
86
+ }
87
+ }
88
+ else if ( s .toLowerCase ().contains ( "manager_birthday" ) ) {
89
+ if ( !s .contains ( "255" ) ) {
90
+ fail ( expectedMessage ( "manager_birthday" , 255 , s ) );
91
+ }
92
+ }
93
+ else if ( s .toLowerCase ().contains ( "manager_name" ) ) {
94
+ if ( !s .contains ( "20" ) ) {
95
+ fail ( expectedMessage ( "manager_name" , 20 , s ) );
96
+ }
97
+ }
98
+ else if ( s .toLowerCase ().contains ( "age" ) ) {
99
+ if ( !s .contains ( "15" ) ) {
100
+ fail ( expectedMessage ( "age" , 15 , s ) );
101
+ }
102
+ }
103
+ else if ( s .toLowerCase ().contains ( "birthday" ) ) {
104
+ if ( !s .contains ( "255" ) ) {
105
+ fail ( expectedMessage ( "birthday" , 255 , s ) );
106
+ }
107
+ }
108
+ else if ( s .toLowerCase ().contains ( "name" ) ) {
109
+ if ( !s .contains ( "20" ) ) {
110
+ fail ( expectedMessage ( "name" , 20 , s ) );
111
+ }
112
+ }
113
+ }
114
+
115
+ private String expectedMessage (String column_name , int size , String actual ) {
116
+ return "Expected " + column_name + " " + size + " but was " + actual ;
117
+ }
118
+
80
119
@ Entity
81
120
@ Table (name = "Employee" )
82
121
public class Employee {
@@ -87,95 +126,16 @@ public class Employee {
87
126
@ ManyToOne (optional = true )
88
127
@ ForeignKey (name = "none" )
89
128
private Employee manager ;
90
-
91
- public void setId (EmployeeId id ) {
92
- this .id = id ;
93
- }
94
-
95
- public EmployeeId getId () {
96
- return id ;
97
- }
98
-
99
- public void setManager (Employee manager ) {
100
- this .manager = manager ;
101
- }
102
-
103
- public Employee getManager () {
104
- return manager ;
105
- }
106
129
}
107
130
108
131
@ Embeddable
109
132
public class EmployeeId implements Serializable {
110
- private static final long serialVersionUID = 1L ;
111
-
112
- public EmployeeId (String name , String birthday , String age ) {
113
- this .name = name ;
114
- this .birthday = birthday ;
115
- this .age = age ;
116
- }
117
-
118
133
@ Column (length = 15 )
119
134
public String age ;
120
135
121
136
@ Column (length = 20 )
122
137
private String name ;
123
138
124
139
private String birthday ;
125
-
126
- @ Override
127
- public int hashCode () {
128
- int hash = 1 ;
129
- hash = hash * 31 + (name != null ? name .hashCode () : 0 );
130
- hash = hash * 31 + (age != null ? age .hashCode () : 0 );
131
- return hash * 31 + (birthday != null ? birthday .hashCode () : 0 );
132
- }
133
-
134
- @ Override
135
- public boolean equals (Object obj ) {
136
- if ( obj == this ) {
137
- return true ;
138
- }
139
-
140
- if ( !(obj instanceof EmployeeId ) ) {
141
- return false ;
142
- }
143
- EmployeeId that = (EmployeeId ) obj ;
144
- if ( age != that .age ) {
145
- return false ;
146
- }
147
- if ( birthday != that .birthday ) {
148
- return false ;
149
- }
150
- if ( name != null && !name .equals ( that .name ) ) {
151
- return false ;
152
- }
153
- return true ;
154
- }
155
-
156
- public void setAge (String age ) {
157
- this .age = age ;
158
- }
159
-
160
- public void setName (String name ) {
161
- this .name = name ;
162
- }
163
-
164
-
165
- public void setBirthday (String birthday ) {
166
- this .birthday = birthday ;
167
- }
168
-
169
- public String getAge () {
170
- return age ;
171
- }
172
-
173
- public String getName () {
174
- return name ;
175
- }
176
-
177
- public String getBirthday () {
178
- return birthday ;
179
- }
180
140
}
181
141
}
0 commit comments