1717package com .google .cloud .spanner .connection ;
1818
1919import static com .google .cloud .spanner .connection .ConnectionOptions .Builder .SPANNER_URI_PATTERN ;
20+ import static com .google .cloud .spanner .connection .ConnectionOptions .DEFAULT_ENDPOINT ;
2021import static com .google .cloud .spanner .connection .ConnectionOptions .determineHost ;
2122import static com .google .common .truth .Truth .assertThat ;
2223import static org .junit .Assert .assertEquals ;
@@ -172,41 +173,47 @@ public void testDetermineHost() {
172173 DEFAULT_HOST ,
173174 determineHost (
174175 matcherWithoutHost ,
176+ DEFAULT_ENDPOINT ,
175177 /* autoConfigEmulator= */ false ,
176178 /* usePlainText= */ false ,
177179 ImmutableMap .of ()));
178180 assertEquals (
179181 DEFAULT_HOST ,
180182 determineHost (
181183 matcherWithoutHost ,
184+ DEFAULT_ENDPOINT ,
182185 /* autoConfigEmulator= */ false ,
183186 /* usePlainText= */ false ,
184187 ImmutableMap .of ("FOO" , "bar" )));
185188 assertEquals (
186189 "http://localhost:9010" ,
187190 determineHost (
188191 matcherWithoutHost ,
192+ DEFAULT_ENDPOINT ,
189193 /* autoConfigEmulator= */ true ,
190194 /* usePlainText= */ false ,
191195 ImmutableMap .of ()));
192196 assertEquals (
193197 "http://localhost:9011" ,
194198 determineHost (
195199 matcherWithoutHost ,
200+ DEFAULT_ENDPOINT ,
196201 /* autoConfigEmulator= */ true ,
197202 /* usePlainText= */ false ,
198203 ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
199204 assertEquals (
200205 "http://localhost:9010" ,
201206 determineHost (
202207 matcherWithoutHost ,
208+ DEFAULT_ENDPOINT ,
203209 /* autoConfigEmulator= */ true ,
204210 /* usePlainText= */ true ,
205211 ImmutableMap .of ()));
206212 assertEquals (
207213 "http://localhost:9011" ,
208214 determineHost (
209215 matcherWithoutHost ,
216+ DEFAULT_ENDPOINT ,
210217 /* autoConfigEmulator= */ true ,
211218 /* usePlainText= */ true ,
212219 ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
@@ -216,44 +223,80 @@ public void testDetermineHost() {
216223 "https://custom.host.domain:1234" ,
217224 determineHost (
218225 matcherWithHost ,
226+ DEFAULT_ENDPOINT ,
219227 /* autoConfigEmulator= */ false ,
220228 /* usePlainText= */ false ,
221229 ImmutableMap .of ()));
222230 assertEquals (
223231 "http://custom.host.domain:1234" ,
224232 determineHost (
225233 matcherWithHost ,
234+ DEFAULT_ENDPOINT ,
226235 /* autoConfigEmulator= */ false ,
227236 /* usePlainText= */ true ,
228237 ImmutableMap .of ()));
229238 assertEquals (
230239 "http://custom.host.domain:1234" ,
231240 determineHost (
232241 matcherWithHost ,
242+ DEFAULT_ENDPOINT ,
233243 /* autoConfigEmulator= */ false ,
234244 /* usePlainText= */ true ,
235245 ImmutableMap .of ()));
236246 assertEquals (
237247 "https://custom.host.domain:1234" ,
238248 determineHost (
239249 matcherWithHost ,
250+ DEFAULT_ENDPOINT ,
240251 /* autoConfigEmulator= */ true ,
241252 /* usePlainText= */ false ,
242253 ImmutableMap .of ()));
243254 assertEquals (
244255 "http://custom.host.domain:1234" ,
245256 determineHost (
246257 matcherWithHost ,
258+ DEFAULT_ENDPOINT ,
247259 /* autoConfigEmulator= */ false ,
248260 /* usePlainText= */ true ,
249261 ImmutableMap .of ("SPANNER_EMULATOR_HOST" , "localhost:9011" )));
250262 assertEquals (
251263 "https://custom.host.domain:1234" ,
252264 determineHost (
253265 matcherWithHost ,
266+ DEFAULT_ENDPOINT ,
254267 /* autoConfigEmulator= */ true ,
255268 /* usePlainText= */ false ,
256269 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 ()));
257300 }
258301
259302 @ Test
@@ -291,6 +334,20 @@ public void testBuildWithAutoConfigEmulatorAndHost() {
291334 assertTrue (options .isUsePlainText ());
292335 }
293336
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+
294351 @ Test
295352 public void testBuildWithDefaultProjectPlaceholder () {
296353 ConnectionOptions .Builder builder = ConnectionOptions .newBuilder ();
0 commit comments