|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.gradle; |
| 10 | + |
| 11 | +import org.opensearch.gradle.test.GradleUnitTestCase; |
| 12 | +import org.gradle.api.Project; |
| 13 | +import org.gradle.api.internal.artifacts.repositories.DefaultIvyArtifactRepository; |
| 14 | +import org.gradle.api.internal.artifacts.repositories.descriptor.IvyRepositoryDescriptor; |
| 15 | +import org.gradle.api.internal.artifacts.repositories.layout.DefaultIvyPatternRepositoryLayout; |
| 16 | +import org.gradle.testfixtures.ProjectBuilder; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import java.net.URI; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 25 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 26 | +import static org.hamcrest.CoreMatchers.is; |
| 27 | + |
| 28 | +public class JdkDownloadPluginSetupRepositoryTests extends GradleUnitTestCase { |
| 29 | + |
| 30 | + private final Project project = ProjectBuilder.builder().build(); |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setup() { |
| 34 | + project.getPlugins().apply("opensearch.jdk-download"); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void shouldCreateRepositoryForAdoptiumJdk8() throws NoSuchFieldException, IllegalAccessException { |
| 39 | + // given |
| 40 | + Jdk jdk = createJdk("adoptium", "8u302-b08", "linux", "aarch64"); |
| 41 | + |
| 42 | + // when |
| 43 | + setupRepository(jdk); |
| 44 | + |
| 45 | + // then |
| 46 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_adoptium_8u302-b08"); |
| 47 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 48 | + assertThat(target.getUrl().toString(), equalTo("https://github.com/adoptium/temurin8-binaries/releases/download/")); |
| 49 | + assertThat(getPattern(target), is("jdk8u302-b08/OpenJDK8U-jdk_[classifier]_[module]_hotspot_8u302b08.[ext]")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void shouldCreateRepositoryForAdoptiumJdk17GaRelease() throws NoSuchFieldException, IllegalAccessException { |
| 54 | + // given - GA release (no dot in version) |
| 55 | + Jdk jdk = createJdk("adoptium", "17+35", "windows", "x64"); |
| 56 | + |
| 57 | + // when |
| 58 | + setupRepository(jdk); |
| 59 | + |
| 60 | + // then |
| 61 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_adoptium_17+35"); |
| 62 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 63 | + assertThat(target.getUrl().toString(), equalTo("https://github.com/adoptium/temurin17-binaries/releases/download/")); |
| 64 | + assertThat(getPattern(target), is("jdk-17+35/OpenJDK17-jdk_[classifier]_[module]_hotspot_17_35.[ext]")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void shouldCreateRepositoryForAdoptiumJdk17UpdateRelease() throws NoSuchFieldException, IllegalAccessException { |
| 69 | + // given - Update release (with dot in version) |
| 70 | + Jdk jdk = createJdk("adoptium", "17.0.1+12", "darwin", "x64"); |
| 71 | + |
| 72 | + // when |
| 73 | + setupRepository(jdk); |
| 74 | + |
| 75 | + // then |
| 76 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_adoptium_17.0.1+12"); |
| 77 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 78 | + assertThat(target.getUrl().toString(), equalTo("https://github.com/adoptium/temurin17-binaries/releases/download/")); |
| 79 | + assertThat(getPattern(target), is("jdk-17.0.1+12/OpenJDK17U-jdk_[classifier]_[module]_hotspot_17.0.1_12.[ext]")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void shouldCreateRepositoryForAdoptiumJdk21() throws NoSuchFieldException, IllegalAccessException { |
| 84 | + // given - JDK 20+ always has U suffix |
| 85 | + Jdk jdk = createJdk("adoptium", "21+35", "linux", "aarch64"); |
| 86 | + |
| 87 | + // when |
| 88 | + setupRepository(jdk); |
| 89 | + |
| 90 | + // then |
| 91 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_adoptium_21+35"); |
| 92 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 93 | + assertThat(target.getUrl().toString(), equalTo("https://github.com/adoptium/temurin21-binaries/releases/download/")); |
| 94 | + assertThat(getPattern(target), is("jdk-21+35/OpenJDK21U-jdk_[classifier]_[module]_hotspot_21_35.[ext]")); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void shouldCreateRepositoryForAdoptOpenJdkJdk8() throws NoSuchFieldException, IllegalAccessException { |
| 99 | + // given |
| 100 | + Jdk jdk = createJdk("adoptopenjdk", "8u302-b08", "linux", "x64"); |
| 101 | + |
| 102 | + // when |
| 103 | + setupRepository(jdk); |
| 104 | + |
| 105 | + // then |
| 106 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_adoptopenjdk_8u302-b08"); |
| 107 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 108 | + assertThat(target.getUrl().toString(), equalTo("https://api.adoptopenjdk.net/v3/binary/version/")); |
| 109 | + assertThat(getPattern(target), is("jdk8u302-b08/[module]/[classifier]/jdk/hotspot/normal/adoptopenjdk")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void shouldCreateRepositoryForAdoptOpenJdkJdk11() throws NoSuchFieldException, IllegalAccessException { |
| 114 | + // given |
| 115 | + Jdk jdk = createJdk("adoptopenjdk", "11.0.11+9", "windows", "x64"); |
| 116 | + |
| 117 | + // when |
| 118 | + setupRepository(jdk); |
| 119 | + |
| 120 | + // then |
| 121 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_adoptopenjdk_11.0.11+9"); |
| 122 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 123 | + assertThat(target.getUrl().toString(), equalTo("https://api.adoptopenjdk.net/v3/binary/version/")); |
| 124 | + assertThat(getPattern(target), is("jdk-11.0.11+9/[module]/[classifier]/jdk/hotspot/normal/adoptopenjdk")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void shouldCreateRepositoryForOpenJdkWithHash() throws NoSuchFieldException, IllegalAccessException { |
| 129 | + // given - hash is part of the version string |
| 130 | + Jdk jdk = createJdk("openjdk", "13.0.1+9@cec27d702aa74d5a8630c65ae61e4305", "darwin", "x64"); |
| 131 | + |
| 132 | + // when |
| 133 | + setupRepository(jdk); |
| 134 | + |
| 135 | + // then |
| 136 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_openjdk_13.0.1+9@cec27d702aa74d5a8630c65ae61e4305"); |
| 137 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 138 | + assertThat(target.getUrl().toString(), equalTo("https://download.oracle.com")); |
| 139 | + assertThat( |
| 140 | + getPattern(target), |
| 141 | + is("java/GA/jdk13.0.1/cec27d702aa74d5a8630c65ae61e4305/9/GPL/openjdk-[revision]_[module]-[classifier]_bin.[ext]") |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void shouldCreateRepositoryForOpenJdkWithoutHash() throws NoSuchFieldException, IllegalAccessException { |
| 147 | + // given |
| 148 | + Jdk jdk = createJdk("openjdk", "12+33", "linux", "x64"); |
| 149 | + |
| 150 | + // when |
| 151 | + setupRepository(jdk); |
| 152 | + |
| 153 | + // then |
| 154 | + DefaultIvyArtifactRepository target = findRepository("jdk_repo_openjdk_12+33"); |
| 155 | + assertThat(target, is(instanceOf(DefaultIvyArtifactRepository.class))); |
| 156 | + assertThat(target.getUrl().toString(), equalTo("https://download.oracle.com")); |
| 157 | + assertThat(getPattern(target), is("java/GA/jdk12/33/GPL/openjdk-[revision]_[module]-[classifier]_bin.[ext]")); |
| 158 | + } |
| 159 | + |
| 160 | + private Jdk createJdk(String vendor, String version, String platform, String architecture) { |
| 161 | + Jdk jdk = new Jdk("test", project.getConfigurations().create("test_" + System.nanoTime()), project.getObjects()); |
| 162 | + jdk.setVendor(vendor); |
| 163 | + jdk.setVersion(version); |
| 164 | + jdk.setPlatform(platform); |
| 165 | + jdk.setArchitecture(architecture); |
| 166 | + jdk.finalizeValues(); |
| 167 | + return jdk; |
| 168 | + } |
| 169 | + |
| 170 | + private void setupRepository(Jdk jdk) { |
| 171 | + JdkDownloadPlugin plugin = (JdkDownloadPlugin) project.getPlugins().getPlugin("opensearch.jdk-download"); |
| 172 | + plugin.setupRepository(project, jdk); |
| 173 | + } |
| 174 | + |
| 175 | + private DefaultIvyArtifactRepository findRepository(String name) { |
| 176 | + return (DefaultIvyArtifactRepository) project.getRepositories().findByName(name); |
| 177 | + } |
| 178 | + |
| 179 | + private String getPattern(DefaultIvyArtifactRepository input) throws NoSuchFieldException, IllegalAccessException { |
| 180 | + DefaultIvyPatternRepositoryLayout layout = (DefaultIvyPatternRepositoryLayout) input.getRepositoryLayout(); |
| 181 | + Set<String> patterns = new HashSet<>(); |
| 182 | + layout.apply(URI.create("test"), new IvyRepositoryDescriptor.Builder("test", URI.create("test")) { |
| 183 | + @Override |
| 184 | + public void addArtifactPattern(String declaredPattern) { |
| 185 | + patterns.add(declaredPattern); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void addArtifactResource(URI rootUri, String pattern) {} |
| 190 | + |
| 191 | + @Override |
| 192 | + public void addIvyPattern(String declaredPattern) {} |
| 193 | + |
| 194 | + @Override |
| 195 | + public void addIvyResource(URI baseUri, String pattern) {} |
| 196 | + }); |
| 197 | + assertThat(patterns.size(), is(1)); |
| 198 | + return patterns.stream().findFirst().get(); |
| 199 | + } |
| 200 | +} |
0 commit comments