|
6 | 6 | package org.elasticsearch.xpack.sql.client;
|
7 | 7 |
|
8 | 8 | import org.elasticsearch.test.ESTestCase;
|
9 |
| -import org.elasticsearch.xpack.sql.client.Version; |
| 9 | + |
| 10 | +import java.io.BufferedInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URL; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.StandardOpenOption; |
| 16 | +import java.util.jar.Attributes; |
| 17 | +import java.util.jar.JarEntry; |
| 18 | +import java.util.jar.JarOutputStream; |
| 19 | +import java.util.jar.Manifest; |
10 | 20 |
|
11 | 21 | public class VersionTests extends ESTestCase {
|
12 | 22 | public void test70Version() {
|
@@ -43,4 +53,47 @@ public void testInvalidVersion() {
|
43 | 53 | IllegalArgumentException err = expectThrows(IllegalArgumentException.class, () -> Version.from("7.1"));
|
44 | 54 | assertEquals("Invalid version 7.1", err.getMessage());
|
45 | 55 | }
|
| 56 | + |
| 57 | + public void testVersionFromJarInJar() throws IOException { |
| 58 | + final String JDBC_JAR_NAME = "es-sql-jdbc.jar"; |
| 59 | + final String JAR_PATH_SEPARATOR = "!/"; |
| 60 | + |
| 61 | + Path dir = createTempDir(); |
| 62 | + Path jarPath = dir.resolve("uberjar.jar"); // simulated uberjar containing the jdbc driver |
| 63 | + Path innerJarPath = dir.resolve(JDBC_JAR_NAME); // simulated ES JDBC driver file |
| 64 | + |
| 65 | + Manifest jdbcJarManifest = new Manifest(); |
| 66 | + Attributes attributes = jdbcJarManifest.getMainAttributes(); |
| 67 | + attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); |
| 68 | + attributes.put(new Attributes.Name("Change"), "abc"); |
| 69 | + attributes.put(new Attributes.Name("X-Compile-Elasticsearch-Version"), "1.2.3"); |
| 70 | + |
| 71 | + // create the jdbc driver file |
| 72 | + try (JarOutputStream jdbc = new JarOutputStream(Files.newOutputStream(innerJarPath, StandardOpenOption.CREATE), jdbcJarManifest)) {} |
| 73 | + |
| 74 | + // create the uberjar and embed the jdbc driver one into it |
| 75 | + try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(innerJarPath)); |
| 76 | + JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarPath, StandardOpenOption.CREATE), new Manifest())) { |
| 77 | + JarEntry entry = new JarEntry(JDBC_JAR_NAME + JAR_PATH_SEPARATOR); |
| 78 | + out.putNextEntry(entry); |
| 79 | + |
| 80 | + byte[] buffer = new byte[1024]; |
| 81 | + while (true) { |
| 82 | + int count = in.read(buffer); |
| 83 | + if (count == -1) { |
| 84 | + break; |
| 85 | + } |
| 86 | + out.write(buffer, 0, count); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + URL jarInJar = new URL("jar:" + jarPath.toUri().toURL().toString() + JAR_PATH_SEPARATOR + JDBC_JAR_NAME + JAR_PATH_SEPARATOR); |
| 91 | + |
| 92 | + Version version = Version.extractVersion(jarInJar); |
| 93 | + assertEquals(1, version.major); |
| 94 | + assertEquals(2, version.minor); |
| 95 | + assertEquals(3, version.revision); |
| 96 | + assertEquals("abc", version.hash); |
| 97 | + assertEquals("1.2.3", version.version); |
| 98 | + } |
46 | 99 | }
|
0 commit comments