Skip to content

Commit b8dadbb

Browse files
authored
test: add a test for using the single-jar-with-dependencies (googleapis#1692)
* test: add a test for using the single-jar-with-dependencies The single-jar-with-dependencies is prone to dependency issues, due to the fact that the build can only place one version of each file in the jar, if multiple dependencies define the same file. This change adds a test for using the single-jar with a very simple test application. Updates googleapis#1687 * fix: make the test work on the emulator * fix: only add slashes if host is actually set * test: add test for shaded jar
1 parent b6bbd8f commit b8dadbb

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,32 @@
426426
</plugins>
427427
</build>
428428
</profile>
429+
<profile>
430+
<!-- This profile is used for testing the single-jar-with-dependencies. -->
431+
<id>alt_build_dir</id>
432+
<activation>
433+
<property>
434+
<name>alt.build.dir</name>
435+
</property>
436+
</activation>
437+
<build>
438+
<directory>${alt.build.dir}</directory>
439+
<plugins>
440+
<plugin>
441+
<groupId>org.apache.maven.plugins</groupId>
442+
<artifactId>maven-shade-plugin</artifactId>
443+
<version>3.6.0</version>
444+
<executions>
445+
<execution>
446+
<configuration>
447+
<outputFile>${alt.build.dir}/single.jar</outputFile>
448+
</configuration>
449+
</execution>
450+
</executions>
451+
</plugin>
452+
</plugins>
453+
</build>
454+
</profile>
429455
</profiles>
430456

431457
<reporting>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner.jdbc;
18+
19+
import java.sql.Connection;
20+
import java.sql.DriverManager;
21+
import java.sql.ResultSet;
22+
23+
/**
24+
* Simple Java application that is used to verify the working of the single-jar-with-dependencies.
25+
*/
26+
public class SingleJarTestApplication {
27+
28+
public static void main(String[] args) throws Exception {
29+
if (args.length != 3) {
30+
throw new IllegalArgumentException("expected 3 arguments");
31+
}
32+
String project = args[0];
33+
String instance = args[1];
34+
String database = args[2];
35+
String extraOptions = "";
36+
String host = "";
37+
if (System.getenv("SPANNER_EMULATOR_HOST") != null) {
38+
extraOptions = "?autoConfigEmulator=true";
39+
host = "//" + System.getenv("SPANNER_EMULATOR_HOST");
40+
}
41+
42+
try (Connection connection =
43+
DriverManager.getConnection(
44+
String.format(
45+
"jdbc:cloudspanner:%s/projects/%s/instances/%s/databases/%s%s",
46+
host, project, instance, database, extraOptions))) {
47+
try (ResultSet resultSet =
48+
connection.createStatement().executeQuery("select 'Hello World from Real Spanner!'")) {
49+
while (resultSet.next()) {
50+
System.out.println(resultSet.getString(1));
51+
}
52+
}
53+
}
54+
}
55+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner.jdbc.it;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.cloud.spanner.Database;
22+
import com.google.cloud.spanner.DatabaseId;
23+
import com.google.cloud.spanner.ParallelIntegrationTest;
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.common.io.CharStreams;
26+
import java.io.InputStreamReader;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
29+
import org.junit.Before;
30+
import org.junit.ClassRule;
31+
import org.junit.Test;
32+
import org.junit.experimental.categories.Category;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/**
37+
* Tests that the following works:
38+
*
39+
* <ol>
40+
* <li>Build a single-jar-with-dependencies
41+
* <li>Compile a simple Java application consisting of a single file and no dependencies to a
42+
* class file
43+
* <li>Run the simple Java application with only itself + the single-jar-with-dependencies on the
44+
* class path
45+
* </ol>
46+
*/
47+
@Category(ParallelIntegrationTest.class)
48+
@RunWith(JUnit4.class)
49+
public class ITSingleJarTest extends ITAbstractJdbcTest {
50+
@ClassRule public static JdbcIntegrationTestEnv env = new JdbcIntegrationTestEnv();
51+
52+
private Database database;
53+
54+
@Before
55+
public void setup() {
56+
database =
57+
env.getOrCreateDatabase(
58+
getDialect(),
59+
ImmutableList.of("create table test (id int64, value string(max)) primary key (id)"));
60+
}
61+
62+
@Test
63+
public void testUseSingleJar() throws Exception {
64+
buildSingleJar();
65+
buildMainClass();
66+
runTestApplication();
67+
}
68+
69+
@Test
70+
public void testUseShadedJar() throws Exception {
71+
buildShadedJar();
72+
buildMainClass();
73+
runTestApplication();
74+
}
75+
76+
private void runTestApplication() throws Exception {
77+
DatabaseId id = database.getId();
78+
ProcessBuilder builder = new ProcessBuilder();
79+
if (System.getenv("SPANNER_EMULATOR_HOST") != null) {
80+
builder.environment().put("SPANNER_EMULATOR_HOST", System.getenv("SPANNER_EMULATOR_HOST"));
81+
}
82+
// This runs the simple test application with only the shaded jar on the classpath.
83+
builder.command(
84+
"java",
85+
"-cp",
86+
"./target/single/test/:target/single/single.jar",
87+
"com/google/cloud/spanner/jdbc/SingleJarTestApplication",
88+
id.getInstanceId().getProject(),
89+
id.getInstanceId().getInstance(),
90+
id.getDatabase());
91+
execute(builder);
92+
}
93+
94+
private void buildSingleJar() throws Exception {
95+
ProcessBuilder builder = new ProcessBuilder();
96+
builder.command("mvn", "clean", "package", "-DskipTests", "-Dalt.build.dir=./target/single");
97+
execute(builder);
98+
}
99+
100+
private void buildShadedJar() throws Exception {
101+
ProcessBuilder builder = new ProcessBuilder();
102+
builder.command(
103+
"mvn", "clean", "-Pshade", "package", "-DskipTests", "-Dalt.build.dir=./target/single");
104+
execute(builder);
105+
}
106+
107+
private void buildMainClass() throws Exception {
108+
Files.createDirectories(Paths.get("target", "single", "test"));
109+
ProcessBuilder builder = new ProcessBuilder();
110+
builder.command(
111+
"javac",
112+
"src/test/java/com/google/cloud/spanner/jdbc/SingleJarTestApplication.java",
113+
"-d",
114+
"./target/single/test");
115+
execute(builder);
116+
}
117+
118+
private void execute(ProcessBuilder builder) throws Exception {
119+
Process process = builder.start();
120+
String errors;
121+
try (InputStreamReader reader = new InputStreamReader(process.getErrorStream())) {
122+
errors = CharStreams.toString(reader);
123+
}
124+
assertEquals(errors, 0, process.waitFor());
125+
}
126+
}

0 commit comments

Comments
 (0)