|
| 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