diff --git a/src/main/java/com/github/packageurl/PackageURL.java b/src/main/java/com/github/packageurl/PackageURL.java index 2ffe903..3fd8452 100644 --- a/src/main/java/com/github/packageurl/PackageURL.java +++ b/src/main/java/com/github/packageurl/PackageURL.java @@ -105,7 +105,25 @@ public PackageURL(final String type, final String namespace, final String name, } /** - * The PackageURL scheme constant. + * Constructs a new PackageURL object. + * + * @param type the type of package (i.e. maven, npm, gem, etc) + * @param namespace the name prefix (i.e. group, owner, organization) + * @param name the name of the package + * @param version the version of the package + * @param qualifiers an array of key/value pair qualifiers + * @param subpath the subpath string + * @throws MalformedPackageURLException if parsing fails + * @since 1.6.0 + */ + public PackageURL(final String type, final String namespace, final String name, final String version, + final Map qualifiers, final String subpath) + throws MalformedPackageURLException { + this(type, namespace, name, version, (qualifiers != null) ? new TreeMap<>(qualifiers) : null, subpath); + } + + /** + * The PackageURL scheme constant */ public static final String SCHEME = "pkg"; diff --git a/src/test/java/com/github/packageurl/PackageURLTest.java b/src/test/java/com/github/packageurl/PackageURLTest.java index b462bfc..bf32ead 100644 --- a/src/test/java/com/github/packageurl/PackageURLTest.java +++ b/src/test/java/com/github/packageurl/PackageURLTest.java @@ -23,6 +23,8 @@ import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; import java.util.Locale; import java.util.TreeMap; @@ -138,14 +140,18 @@ public void testConstructorParameters() throws MalformedPackageURLException { final String subpath = testDefinition.optString("subpath", null); TreeMap map = null; + Map hashMap = null; if (qualifiers != null) { map = qualifiers.toMap().entrySet().stream().collect( TreeMap::new, (qmap, entry) -> qmap.put(entry.getKey(), (String) entry.getValue()), TreeMap::putAll ); + hashMap = new HashMap<>(map); } + + if (invalid) { try { PackageURL purl = new PackageURL(type, namespace, name, version, map, subpath); @@ -173,6 +179,8 @@ public void testConstructorParameters() throws MalformedPackageURLException { Assert.assertTrue(purl.getQualifiers().containsKey(key)); Assert.assertEquals(value, purl.getQualifiers().get(key)); }); + PackageURL purl2 = new PackageURL(type, namespace, name, version, hashMap, subpath); + Assert.assertEquals(purl.getQualifiers(), purl2.getQualifiers()); } } }