@@ -109,6 +109,117 @@ def initialize; end
109
109
assert config [ :adapter_class ]
110
110
end
111
111
112
+ test 'configuration attempts to load MySQL driver by default' do
113
+ load_jdbc_mysql
114
+
115
+ connection_handler = connection_handler_stub
116
+
117
+ config = { database : 'MyDB' }
118
+ connection_handler . expects ( :jdbc_connection )
119
+ ::Jdbc ::MySQL . expects ( :load_driver ) . with ( :require )
120
+ connection_handler . mysql_connection config
121
+ end
122
+
123
+ test 'configuration uses driver_name from Jdbc::MySQL' do
124
+ load_jdbc_mysql
125
+
126
+ connection_handler = connection_handler_stub
127
+
128
+ config = { database : 'MyDB' }
129
+ connection_handler . expects ( :jdbc_connection )
130
+ ::Jdbc ::MySQL . expects ( :driver_name ) . returns ( 'com.mysql.CustomDriver' )
131
+ connection_handler . mysql_connection config
132
+ assert_equal 'com.mysql.CustomDriver' , config [ :driver ]
133
+ end
134
+
135
+ test 'configuration sets up properties according to connector/j driver (>= 8.0)' do
136
+ load_jdbc_mysql
137
+
138
+ connection_handler = connection_handler_stub
139
+
140
+ config = { database : 'MyDB' }
141
+ connection_handler . expects ( :jdbc_connection )
142
+ ::Jdbc ::MySQL . expects ( :driver_name ) . returns ( 'com.mysql.cj.jdbc.Driver' )
143
+ connection_handler . mysql_connection config
144
+ assert_equal 'com.mysql.cj.jdbc.Driver' , config [ :driver ]
145
+ assert_equal 'CONVERT_TO_NULL' , config [ :properties ] [ 'zeroDateTimeBehavior' ]
146
+ assert_equal false , config [ :properties ] [ 'useLegacyDatetimeCode' ]
147
+ assert_equal false , config [ :properties ] [ 'jdbcCompliantTruncation' ]
148
+ assert_equal false , config [ :properties ] [ 'useSSL' ]
149
+ end
150
+
151
+ def load_jdbc_mysql
152
+ require 'jdbc/mysql'
153
+ end
154
+
155
+ end
156
+
157
+ context 'connection (Jdbc::MySQL missing)' do
158
+
159
+ module ::Jdbc ; end
160
+
161
+ @@jdbc_mysql = ::Jdbc ::MySQL rescue nil
162
+
163
+ def setup
164
+ ::Jdbc . send :remove_const , :MySQL if @@jdbc_mysql
165
+ end
166
+
167
+ def teardown
168
+ ::Jdbc . const_set :MySQL , @@jdbc_mysql if @@jdbc_mysql
169
+ end
170
+
171
+ test 'configuration sets url and properties assuming mysql driver (<= 5.1)' do
172
+ connection_handler = connection_handler_stub
173
+
174
+ config = { host : '127.0.0.1' , database : 'MyDB' }
175
+ connection_handler . expects ( :jdbc_connection )
176
+ connection_handler . mysql_connection config
177
+
178
+ # we do not complete username/database etc :
179
+ assert_equal 'root' , config [ :username ]
180
+ assert_equal 'com.mysql.jdbc.Driver' , config [ :driver ]
181
+ assert_equal 'jdbc:mysql://127.0.0.1/MyDB' , config [ :url ]
182
+ assert_equal 'UTF-8' , config [ :properties ] [ 'characterEncoding' ]
183
+ assert_equal 'convertToNull' , config [ :properties ] [ 'zeroDateTimeBehavior' ]
184
+ assert_equal false , config [ :properties ] [ 'useLegacyDatetimeCode' ]
185
+ assert_equal false , config [ :properties ] [ 'jdbcCompliantTruncation' ]
186
+ assert_equal false , config [ :properties ] [ 'useSSL' ]
187
+ end
188
+
189
+ test 'configuration attempts to load MySQL driver by default' do
190
+ connection_handler = connection_handler_stub
191
+
192
+ config = { database : 'MyDB' }
193
+ connection_handler . expects ( :jdbc_connection )
194
+ connection_handler . expects ( :require ) . with ( 'jdbc/mysql' )
195
+ connection_handler . mysql_connection config
196
+ end
197
+
198
+ test 'configuration allows to skip driver loading' do
199
+ connection_handler = connection_handler_stub
200
+
201
+ config = { database : 'MyDB' , driver : false }
202
+ connection_handler . expects ( :jdbc_connection )
203
+ connection_handler . expects ( :require ) . never
204
+ connection_handler . mysql_connection config
205
+ assert_not config [ :driver ] # allow Java's service discovery mechanism (with connector/j 8.0)
206
+ end
207
+
208
+ test 'configuration works with MariaDB driver specified' do
209
+ connection_handler = connection_handler_stub
210
+
211
+ config = { database : 'MyDB' , driver : 'org.mariadb.jdbc.Driver' }
212
+ connection_handler . expects ( :jdbc_connection )
213
+ connection_handler . mysql_connection config
214
+
215
+ # we do not complete username/database etc :
216
+ assert_equal 'root' , config [ :username ]
217
+ assert_equal 'org.mariadb.jdbc.Driver' , config [ :driver ]
218
+ assert_equal 'jdbc:mysql://localhost/MyDB' , config [ :url ]
219
+ assert_equal false , config [ :properties ] [ 'useLegacyDatetimeCode' ]
220
+ assert_equal false , config [ :properties ] [ 'useSsl' ]
221
+ end
222
+
112
223
end
113
224
114
225
end if defined? JRUBY_VERSION
0 commit comments