17
17
package com .google .cloud .spanner .connection ;
18
18
19
19
import static com .google .cloud .spanner .connection .ConnectionOptions .Builder .SPANNER_URI_PATTERN ;
20
+ import static com .google .cloud .spanner .connection .ConnectionOptions .DEFAULT_ENDPOINT ;
20
21
import static com .google .cloud .spanner .connection .ConnectionOptions .determineHost ;
21
22
import static com .google .common .truth .Truth .assertThat ;
22
23
import static org .junit .Assert .assertEquals ;
@@ -172,41 +173,47 @@ public void testDetermineHost() {
172
173
DEFAULT_HOST ,
173
174
determineHost (
174
175
matcherWithoutHost ,
176
+ DEFAULT_ENDPOINT ,
175
177
/* autoConfigEmulator= */ false ,
176
178
/* usePlainText= */ false ,
177
179
ImmutableMap .of ()));
178
180
assertEquals (
179
181
DEFAULT_HOST ,
180
182
determineHost (
181
183
matcherWithoutHost ,
184
+ DEFAULT_ENDPOINT ,
182
185
/* autoConfigEmulator= */ false ,
183
186
/* usePlainText= */ false ,
184
187
ImmutableMap .of ("FOO" , "bar" )));
185
188
assertEquals (
186
189
"http://localhost:9010" ,
187
190
determineHost (
188
191
matcherWithoutHost ,
192
+ DEFAULT_ENDPOINT ,
189
193
/* autoConfigEmulator= */ true ,
190
194
/* usePlainText= */ false ,
191
195
ImmutableMap .of ()));
192
196
assertEquals (
193
197
"http://localhost:9011" ,
194
198
determineHost (
195
199
matcherWithoutHost ,
200
+ DEFAULT_ENDPOINT ,
196
201
/* autoConfigEmulator= */ true ,
197
202
/* usePlainText= */ false ,
198
203
ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
199
204
assertEquals (
200
205
"http://localhost:9010" ,
201
206
determineHost (
202
207
matcherWithoutHost ,
208
+ DEFAULT_ENDPOINT ,
203
209
/* autoConfigEmulator= */ true ,
204
210
/* usePlainText= */ true ,
205
211
ImmutableMap .of ()));
206
212
assertEquals (
207
213
"http://localhost:9011" ,
208
214
determineHost (
209
215
matcherWithoutHost ,
216
+ DEFAULT_ENDPOINT ,
210
217
/* autoConfigEmulator= */ true ,
211
218
/* usePlainText= */ true ,
212
219
ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
@@ -216,44 +223,80 @@ public void testDetermineHost() {
216
223
"https://custom.host.domain:1234" ,
217
224
determineHost (
218
225
matcherWithHost ,
226
+ DEFAULT_ENDPOINT ,
219
227
/* autoConfigEmulator= */ false ,
220
228
/* usePlainText= */ false ,
221
229
ImmutableMap .of ()));
222
230
assertEquals (
223
231
"http://custom.host.domain:1234" ,
224
232
determineHost (
225
233
matcherWithHost ,
234
+ DEFAULT_ENDPOINT ,
226
235
/* autoConfigEmulator= */ false ,
227
236
/* usePlainText= */ true ,
228
237
ImmutableMap .of ()));
229
238
assertEquals (
230
239
"http://custom.host.domain:1234" ,
231
240
determineHost (
232
241
matcherWithHost ,
242
+ DEFAULT_ENDPOINT ,
233
243
/* autoConfigEmulator= */ false ,
234
244
/* usePlainText= */ true ,
235
245
ImmutableMap .of ()));
236
246
assertEquals (
237
247
"https://custom.host.domain:1234" ,
238
248
determineHost (
239
249
matcherWithHost ,
250
+ DEFAULT_ENDPOINT ,
240
251
/* autoConfigEmulator= */ true ,
241
252
/* usePlainText= */ false ,
242
253
ImmutableMap .of ()));
243
254
assertEquals (
244
255
"http://custom.host.domain:1234" ,
245
256
determineHost (
246
257
matcherWithHost ,
258
+ DEFAULT_ENDPOINT ,
247
259
/* autoConfigEmulator= */ false ,
248
260
/* usePlainText= */ true ,
249
261
ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
250
262
assertEquals (
251
263
"https://custom.host.domain:1234" ,
252
264
determineHost (
253
265
matcherWithHost ,
266
+ DEFAULT_ENDPOINT ,
254
267
/* autoConfigEmulator= */ true ,
255
268
/* usePlainText= */ false ,
256
269
ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
270
+
271
+ // The 'endpoint' connection URL property can also be used to connect to the emulator.
272
+ // Using this property is sometimes easier than adding the URL to the host part of the
273
+ // connection string, for example because it can be added to the Properties object that
274
+ // is used by JDBC.
275
+ assertEquals (
276
+ "http://localhost:9010" ,
277
+ determineHost (
278
+ matcherWithoutHost ,
279
+ "localhost:9010" ,
280
+ /* autoConfigEmulator= */ false ,
281
+ /* usePlainText= */ true ,
282
+ ImmutableMap .of ()));
283
+ // A value for the 'endpoint' connection property overrides any value in the host group.
284
+ assertEquals (
285
+ "https://my.endpoint:1234" ,
286
+ determineHost (
287
+ matcherWithHost ,
288
+ "my.endpoint:1234" ,
289
+ /* autoConfigEmulator= */ false ,
290
+ /* usePlainText= */ false ,
291
+ ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
292
+ assertEquals (
293
+ "http://my.endpoint.local:1234" ,
294
+ determineHost (
295
+ matcherWithHost ,
296
+ "my.endpoint.local:1234" ,
297
+ /* autoConfigEmulator= */ false ,
298
+ /* usePlainText= */ true ,
299
+ ImmutableMap .of ()));
257
300
}
258
301
259
302
@ Test
@@ -291,6 +334,20 @@ public void testBuildWithAutoConfigEmulatorAndHost() {
291
334
assertTrue (options .isUsePlainText ());
292
335
}
293
336
337
+ @ Test
338
+ public void testBuildWithAutoConfigEmulatorAndEndpoint () {
339
+ ConnectionOptions .Builder builder = ConnectionOptions .newBuilder ();
340
+ builder .setUri (
341
+ "cloudspanner:/projects/test-project-123/instances/test-instance-123/databases/test-database-123?autoConfigEmulator=true;endpoint=central-emulator.local:8080" );
342
+ ConnectionOptions options = builder .build ();
343
+ assertEquals ("http://central-emulator.local:8080" , options .getHost ());
344
+ assertEquals ("test-project-123" , options .getProjectId ());
345
+ assertEquals ("test-instance-123" , options .getInstanceId ());
346
+ assertEquals ("test-database-123" , options .getDatabaseName ());
347
+ assertEquals (NoCredentials .getInstance (), options .getCredentials ());
348
+ assertTrue (options .isUsePlainText ());
349
+ }
350
+
294
351
@ Test
295
352
public void testBuildWithDefaultProjectPlaceholder () {
296
353
ConnectionOptions .Builder builder = ConnectionOptions .newBuilder ();
0 commit comments