|
22 | 22 | package com.github.packageurl; |
23 | 23 |
|
24 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
25 | 26 | import static org.junit.jupiter.api.Assertions.assertThrowsExactly; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
26 | 28 |
|
| 29 | +import java.io.IOException; |
27 | 30 | import java.util.Collections; |
28 | 31 | import java.util.HashMap; |
29 | 32 | import java.util.Map; |
| 33 | +import java.util.stream.Stream; |
| 34 | +import org.jspecify.annotations.Nullable; |
30 | 35 | import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.params.ParameterizedTest; |
| 37 | +import org.junit.jupiter.params.provider.Arguments; |
| 38 | +import org.junit.jupiter.params.provider.MethodSource; |
31 | 39 |
|
32 | 40 | class PackageURLBuilderTest { |
33 | 41 |
|
34 | | - @Test |
35 | | - void packageURLBuilder() throws MalformedPackageURLException { |
36 | | - PackageURL purl = PackageURLBuilder.aPackageURL() |
37 | | - .withType("my.type-9+") |
38 | | - .withName("name") |
39 | | - .build(); |
40 | | - |
41 | | - assertEquals("pkg:my.type-9+/name", purl.toString()); |
42 | | - assertEquals("pkg:my.type-9+/name", purl.canonicalize()); |
43 | | - |
44 | | - purl = PackageURLBuilder.aPackageURL() |
45 | | - .withType("type") |
46 | | - .withNamespace("namespace") |
47 | | - .withName("name") |
48 | | - .withVersion("version") |
49 | | - .withQualifier("key","value") |
50 | | - .withSubpath("subpath") |
51 | | - .build(); |
52 | | - |
53 | | - assertEquals("pkg:type/namespace/name@version?key=value#subpath", purl.toString()); |
54 | | - |
55 | | - purl = PackageURLBuilder.aPackageURL() |
56 | | - .withType(PackageURL.StandardTypes.GENERIC) |
57 | | - .withNamespace("namespace") |
58 | | - .withName("name") |
59 | | - .withVersion("version") |
60 | | - .withQualifier("key_1.1-","value") |
61 | | - .withSubpath("subpath") |
62 | | - .build(); |
63 | | - |
64 | | - assertEquals("pkg:generic/namespace/name@version?key_1.1-=value#subpath", purl.toString()); |
65 | | - |
66 | | - purl = PackageURLBuilder.aPackageURL() |
67 | | - .withType(PackageURL.StandardTypes.GENERIC) |
68 | | - .withNamespace("/////") |
69 | | - .withName("name") |
70 | | - .withVersion("version") |
71 | | - .withQualifier("key","value") |
72 | | - .withSubpath("/////") |
73 | | - .build(); |
74 | | - |
75 | | - assertEquals("pkg:generic/name@version?key=value", purl.toString()); |
76 | | - |
77 | | - purl = PackageURLBuilder.aPackageURL() |
78 | | - .withType(PackageURL.StandardTypes.GENERIC) |
79 | | - .withNamespace("") |
80 | | - .withName("name") |
81 | | - .withVersion("version") |
82 | | - .withQualifier("key","value") |
83 | | - .withQualifier("next","value") |
84 | | - .withSubpath("") |
85 | | - .build(); |
| 42 | + static Stream<Arguments> packageURLBuilder() throws IOException { |
| 43 | + return PurlParameters.getTestDataFromFiles("test-suite-data.json", "components-constructor-only.json"); |
| 44 | + } |
86 | 45 |
|
87 | | - assertEquals("pkg:generic/name@version?key=value&next=value", purl.toString()); |
| 46 | + @ParameterizedTest(name = "{0}: {1}") |
| 47 | + @MethodSource |
| 48 | + void packageURLBuilder(String description, |
| 49 | + @Nullable String ignoredPurl, |
| 50 | + PurlParameters parameters, |
| 51 | + String canonicalPurl, |
| 52 | + boolean invalid) throws MalformedPackageURLException { |
| 53 | + if (parameters.getType() == null || parameters.getName() == null) { |
| 54 | + assertTrue(invalid, "valid test case with type or name `null`"); |
| 55 | + return; |
| 56 | + } |
| 57 | + PackageURLBuilder builder = PackageURLBuilder.aPackageURL().withType(parameters.getType()).withName(parameters.getName()); |
| 58 | + String namespace = parameters.getNamespace(); |
| 59 | + if (namespace != null) { |
| 60 | + builder.withNamespace(namespace); |
| 61 | + } |
| 62 | + String version = parameters.getVersion(); |
| 63 | + if (version != null) { |
| 64 | + builder.withVersion(version); |
| 65 | + } |
| 66 | + parameters.getQualifiers().forEach(builder::withQualifier); |
| 67 | + String subpath = parameters.getSubpath(); |
| 68 | + if (subpath != null) { |
| 69 | + builder.withSubpath(subpath); |
| 70 | + } |
| 71 | + if (invalid) { |
| 72 | + assertThrows(MalformedPackageURLException.class, builder::build); |
| 73 | + } else { |
| 74 | + assertEquals(parameters.getType(), builder.getType(), "type"); |
| 75 | + assertEquals(parameters.getNamespace(), builder.getNamespace(), "namespace"); |
| 76 | + assertEquals(parameters.getName(), builder.getName(), "name"); |
| 77 | + assertEquals(parameters.getVersion(), builder.getVersion(), "version"); |
| 78 | + assertEquals(parameters.getQualifiers(), builder.getQualifiers(), "qualifiers"); |
| 79 | + assertEquals(parameters.getSubpath(), builder.getSubpath(), "subpath"); |
| 80 | + PackageURL actual = builder.build(); |
| 81 | + assertEquals(canonicalPurl, actual.canonicalize(), "canonical PURL"); |
| 82 | + } |
88 | 83 | } |
89 | 84 |
|
90 | 85 | @Test |
@@ -229,10 +224,8 @@ private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) t |
229 | 224 |
|
230 | 225 | assertEquals(eQualifiers, aQualifiers); |
231 | 226 |
|
232 | | - if (eQualifiers != null && aQualifiers != null) { |
233 | | - eQualifiers.forEach((k,v) -> |
234 | | - assertEquals(v, actual.getQualifier(k))); |
235 | | - } |
| 227 | + eQualifiers.forEach((k, v) -> |
| 228 | + assertEquals(v, actual.getQualifier(k))); |
236 | 229 | } |
237 | 230 |
|
238 | 231 | } |
0 commit comments