|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
4 | 4 |
|
5 | 5 | import java.io.File; |
| 6 | +import java.io.StringWriter; |
| 7 | +import java.net.URLClassLoader; |
6 | 8 | import java.nio.charset.StandardCharsets; |
7 | 9 | import java.nio.file.Files; |
8 | 10 | import java.nio.file.Path; |
9 | 11 | import java.util.concurrent.TimeUnit; |
10 | 12 | import java.util.jar.Attributes; |
11 | 13 | import java.util.jar.JarFile; |
12 | 14 |
|
| 15 | +import javax.tools.ToolProvider; |
| 16 | + |
13 | 17 | import org.junit.jupiter.api.Test; |
14 | 18 | import org.junit.jupiter.api.io.TempDir; |
15 | 19 |
|
|
19 | 23 | public class FullArtifactTest { |
20 | 24 | @TempDir Path temporary; |
21 | 25 |
|
| 26 | + @Test void legacyRedisConsumerCompilesAndLinksAgainstOnlyTheFullJar() throws Exception { |
| 27 | + Path full = fullJar(); |
| 28 | + Path source = temporary.resolve("LegacyRedisConsumer.java"); |
| 29 | + Files.writeString(source, """ |
| 30 | + import java.util.Set; |
| 31 | + import redis.clients.jedis.HostAndPort; |
| 32 | + import redis.clients.jedis.JedisClientConfig; |
| 33 | + import redis.clients.jedis.JedisPooled; |
| 34 | + import redis.clients.jedis.JedisSentineled; |
| 35 | + import redis.clients.jedis.UnifiedJedis; |
| 36 | +
|
| 37 | + public final class LegacyRedisConsumer { |
| 38 | + // Compile the established constructor calls without opening a connection. |
| 39 | + public static Object[] construct(HostAndPort endpoint, JedisClientConfig config, |
| 40 | + Set<HostAndPort> sentinels) { |
| 41 | + return new Object[] { |
| 42 | + new JedisPooled(endpoint, config), |
| 43 | + new JedisSentineled("synthetic-master", config, sentinels, config), |
| 44 | + new UnifiedJedis(endpoint, config) |
| 45 | + }; |
| 46 | + } |
| 47 | +
|
| 48 | + public static int link() throws Exception { |
| 49 | + JedisPooled.class.getConstructor(HostAndPort.class, JedisClientConfig.class); |
| 50 | + JedisSentineled.class.getConstructor(String.class, JedisClientConfig.class, |
| 51 | + Set.class, JedisClientConfig.class); |
| 52 | + UnifiedJedis.class.getConstructor(HostAndPort.class, JedisClientConfig.class); |
| 53 | + return 3; |
| 54 | + } |
| 55 | + } |
| 56 | + """); |
| 57 | + var compiler = ToolProvider.getSystemJavaCompiler(); |
| 58 | + assertNotNull(compiler, "Packaged API verification requires the build JDK"); |
| 59 | + StringWriter diagnostics = new StringWriter(); |
| 60 | + try (var files = compiler.getStandardFileManager(null, null, StandardCharsets.UTF_8)) { |
| 61 | + boolean compiled = compiler.getTask(diagnostics, files, null, |
| 62 | + java.util.List.of("--release", "21", "-proc:none", "-classpath", full.toString(), |
| 63 | + "-d", temporary.toString()), null, |
| 64 | + files.getJavaFileObjects(source.toFile())).call(); |
| 65 | + assertTrue(compiled, "Legacy Redis consumer must compile using only the packaged JAR: " + diagnostics); |
| 66 | + } |
| 67 | + // Platform parent excludes every Maven dependency, including any older Jedis on the test classpath. |
| 68 | + try (var loader = new URLClassLoader(new java.net.URL[] { |
| 69 | + temporary.toUri().toURL(), full.toUri().toURL() }, ClassLoader.getPlatformClassLoader())) { |
| 70 | + Class<?> consumer = Class.forName("LegacyRedisConsumer", true, loader); |
| 71 | + assertEquals(3, consumer.getMethod("link").invoke(null)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
22 | 75 | @Test void omitsExternalCryptoProvider() throws Exception { |
23 | 76 | Path full = fullJar(); |
24 | 77 | try (JarFile output = new JarFile(full.toFile())) { |
|
0 commit comments