-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: run MyBatis sample on the Emulator and add insert-or-update sam…
…ple (#1737) * docs: run MyBatis sample on the Emulator Adds an option to the MyBatis sample to also run on the Spanner Emulator, and add a test run that uses this option. * chore: remove commented code * docs: add insert-or-update example * fix: set spanner.emulator property in test * fix: update copy-paste error
- Loading branch information
Showing
12 changed files
with
323 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...pring-data-mybatis/src/main/java/com/google/cloud/spanner/sample/EmulatorInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.sample; | ||
|
||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.images.PullPolicy; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
public class EmulatorInitializer | ||
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { | ||
private GenericContainer<?> emulator; | ||
|
||
@Override | ||
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { | ||
ConfigurableEnvironment environment = event.getEnvironment(); | ||
boolean useEmulator = | ||
Boolean.TRUE.equals(environment.getProperty("spanner.emulator", Boolean.class)); | ||
if (!useEmulator) { | ||
return; | ||
} | ||
|
||
emulator = | ||
new GenericContainer<>(DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator")); | ||
emulator.withImagePullPolicy(PullPolicy.alwaysPull()); | ||
emulator.addExposedPort(9010); | ||
emulator.setWaitStrategy(Wait.forListeningPorts(9010)); | ||
emulator.start(); | ||
|
||
System.setProperty("spanner.endpoint", "//localhost:" + emulator.getMappedPort(9010)); | ||
} | ||
|
||
public void stopEmulator() { | ||
if (this.emulator != null) { | ||
this.emulator.stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13
samples/spring-data-mybatis/src/main/resources/application-cs.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
|
||
# This profile uses a Cloud Spanner PostgreSQL database. | ||
|
||
# The sample by default uses the Spanner emulator. | ||
# Disable this flag to run the sample on a real Spanner instance. | ||
spanner.emulator=true | ||
# This property is automatically set to point to the Spanner emulator that is automatically | ||
# started together with the application. It remains empty if the application is executed | ||
# against a real Spanner instance. | ||
spanner.endpoint= | ||
# Used for testing | ||
spanner.additional_properties= | ||
|
||
# Update these properties to match your project, instance, and database. | ||
spanner.project=my-project | ||
spanner.instance=my-instance | ||
spanner.database=mybatis-sample | ||
|
||
spring.datasource.url=jdbc:cloudspanner:/projects/${spanner.project}/instances/${spanner.instance}/databases/${spanner.database} | ||
spring.datasource.url=jdbc:cloudspanner:${spanner.endpoint}/projects/${spanner.project}/instances/${spanner.instance}/databases/${spanner.database};dialect=POSTGRESQL;autoConfigEmulator=${spanner.emulator};${spanner.additional_properties} | ||
spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver |
31 changes: 31 additions & 0 deletions
31
...g-data-mybatis/src/test/java/com/google/cloud/spanner/sample/ApplicationEmulatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.sample; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ApplicationEmulatorTest { | ||
|
||
@Test | ||
public void testRunApplicationOnEmulator() { | ||
System.setProperty("spanner.emulator", "true"); | ||
Application.main(new String[] {}); | ||
} | ||
} |
Oops, something went wrong.