|
| 1 | +package tools.jackson.dataformat.smile; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import tools.jackson.databind.ObjectMapper; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +// [dataformats-binary#700]: SPI file referenced wrong class name |
| 15 | +// (`...smile.databind.SmileMapper` instead of `...smile.SmileMapper`), |
| 16 | +// causing `ServiceConfigurationError` from `ServiceLoader` for classpath |
| 17 | +// (non-modular) usage. |
| 18 | +// |
| 19 | +// NOTE: the SPI file only governs classpath usage; modular usage is covered by |
| 20 | +// the `provides` directive in `module-info.java`. Since the test harness runs on |
| 21 | +// the module path (where the module's own resources are not reachable), this test |
| 22 | +// validates the source SPI file content directly instead of via `ServiceLoader`. |
| 23 | +public class ServiceLoader700Test extends BaseTestForSmile |
| 24 | +{ |
| 25 | + private final static File SERVICE_FILE = new File( |
| 26 | + "src/main/resources/META-INF/services/tools.jackson.databind.ObjectMapper"); |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testServiceFileClassNamesResolve() throws Exception |
| 30 | + { |
| 31 | + assertTrue(SERVICE_FILE.exists(), "Missing SPI file: " + SERVICE_FILE.getAbsolutePath()); |
| 32 | + boolean foundSmileMapper = false; |
| 33 | + List<String> lines = Files.readAllLines(SERVICE_FILE.toPath(), StandardCharsets.UTF_8); |
| 34 | + for (String line : lines) { |
| 35 | + line = line.trim(); |
| 36 | + if (line.isEmpty() || line.startsWith("#")) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + // Class named in SPI file must actually exist and be an `ObjectMapper`... |
| 40 | + Class<?> cls = Class.forName(line); |
| 41 | + assertTrue(ObjectMapper.class.isAssignableFrom(cls), |
| 42 | + "Class `" + line + "` is not an `ObjectMapper` subtype"); |
| 43 | + if (cls == SmileMapper.class) { |
| 44 | + foundSmileMapper = true; |
| 45 | + } |
| 46 | + } |
| 47 | + assertTrue(foundSmileMapper, |
| 48 | + "SPI file should list `" + SmileMapper.class.getName() + "`"); |
| 49 | + } |
| 50 | +} |
0 commit comments