|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 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 | +package io.fabric8.deps.compatibility.tests; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.lang.module.ModuleFinder; |
| 23 | +import java.lang.module.ModuleReference; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.Comparator; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.jar.JarFile; |
| 31 | +import java.util.jar.Manifest; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +class HttpClientAutomaticModuleNameTest { |
| 37 | + |
| 38 | + private static final Map<String, String> EXPECTED_MODULE_NAMES = new LinkedHashMap<>(); |
| 39 | + |
| 40 | + static { |
| 41 | + EXPECTED_MODULE_NAMES.put("kubernetes-httpclient-jdk", "io.fabric8.kubernetes.client.jdkhttp"); |
| 42 | + EXPECTED_MODULE_NAMES.put("kubernetes-httpclient-jetty", "io.fabric8.kubernetes.client.jetty"); |
| 43 | + EXPECTED_MODULE_NAMES.put("kubernetes-httpclient-okhttp", "io.fabric8.kubernetes.client.okhttp"); |
| 44 | + EXPECTED_MODULE_NAMES.put("kubernetes-httpclient-vertx-5", "io.fabric8.kubernetes.client.vertx5"); |
| 45 | + EXPECTED_MODULE_NAMES.put("kubernetes-httpclient-vertx", "io.fabric8.kubernetes.client.vertx"); |
| 46 | + } |
| 47 | + |
| 48 | + private static Path jarsDir; |
| 49 | + |
| 50 | + @BeforeAll |
| 51 | + static void setUp() { |
| 52 | + String jarsDirProperty = System.getProperty("httpclient.jars.dir"); |
| 53 | + assertThat(jarsDirProperty) |
| 54 | + .as("System property 'httpclient.jars.dir' must be set") |
| 55 | + .isNotNull(); |
| 56 | + jarsDir = Paths.get(jarsDirProperty); |
| 57 | + assertThat(jarsDir).isDirectory(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void httpClientModulesHaveCorrectAutomaticModuleName() throws IOException { |
| 62 | + try (Stream<Path> jarFiles = Files.list(jarsDir)) { |
| 63 | + Map<String, String> actualModuleNames = new LinkedHashMap<>(); |
| 64 | + jarFiles.filter(p -> p.toString().endsWith(".jar")).forEach(jarPath -> { |
| 65 | + String fileName = jarPath.getFileName().toString(); |
| 66 | + Map.Entry<String, String> match = findMatchingArtifact(fileName); |
| 67 | + if (match != null) { |
| 68 | + try (JarFile jarFile = new JarFile(jarPath.toFile())) { |
| 69 | + Manifest manifest = jarFile.getManifest(); |
| 70 | + assertThat(manifest) |
| 71 | + .as("Manifest for %s", fileName) |
| 72 | + .isNotNull(); |
| 73 | + String actualModuleName = manifest.getMainAttributes().getValue("Automatic-Module-Name"); |
| 74 | + assertThat(actualModuleName) |
| 75 | + .as("Automatic-Module-Name in %s", fileName) |
| 76 | + .isEqualTo(match.getValue()); |
| 77 | + actualModuleNames.put(match.getKey(), actualModuleName); |
| 78 | + } catch (IOException e) { |
| 79 | + throw new RuntimeException("Failed to read JAR: " + jarPath, e); |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + assertThat(actualModuleNames) |
| 84 | + .as("All httpclient modules should have been verified") |
| 85 | + .hasSize(EXPECTED_MODULE_NAMES.size()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void httpClientModulesHaveValidJpmsModuleNames() throws IOException { |
| 91 | + try (Stream<Path> jarFiles = Files.list(jarsDir)) { |
| 92 | + Map<String, String> resolvedModuleNames = new LinkedHashMap<>(); |
| 93 | + jarFiles.filter(p -> p.toString().endsWith(".jar")).forEach(jarPath -> { |
| 94 | + String fileName = jarPath.getFileName().toString(); |
| 95 | + Map.Entry<String, String> match = findMatchingArtifact(fileName); |
| 96 | + if (match != null) { |
| 97 | + ModuleFinder finder = ModuleFinder.of(jarPath); |
| 98 | + assertThat(finder.findAll()) |
| 99 | + .as("ModuleFinder should resolve module from %s", fileName) |
| 100 | + .hasSize(1); |
| 101 | + ModuleReference moduleRef = finder.findAll().iterator().next(); |
| 102 | + String resolvedName = moduleRef.descriptor().name(); |
| 103 | + assertThat(resolvedName) |
| 104 | + .as("JPMS module name resolved from %s", fileName) |
| 105 | + .isEqualTo(match.getValue()); |
| 106 | + resolvedModuleNames.put(match.getKey(), resolvedName); |
| 107 | + } |
| 108 | + }); |
| 109 | + assertThat(resolvedModuleNames) |
| 110 | + .as("All httpclient modules should have been verified via ModuleFinder") |
| 111 | + .hasSize(EXPECTED_MODULE_NAMES.size()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Finds the best matching artifact prefix for the given filename. |
| 117 | + * Uses longest-prefix matching to distinguish e.g. "kubernetes-httpclient-vertx-5" from "kubernetes-httpclient-vertx". |
| 118 | + */ |
| 119 | + private static Map.Entry<String, String> findMatchingArtifact(String fileName) { |
| 120 | + return EXPECTED_MODULE_NAMES.entrySet().stream() |
| 121 | + .filter(entry -> fileName.startsWith(entry.getKey() + "-")) |
| 122 | + .max(Comparator.comparingInt(entry -> entry.getKey().length())) |
| 123 | + .orElse(null); |
| 124 | + } |
| 125 | +} |
0 commit comments