Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow builder to be fully editable #111

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/com/github/packageurl/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ public PackageURL(final String type, final String namespace, final String name,
*/
private String subpath;

public PackageURLBuilder toBuilder() {

PackageURLBuilder builder = PackageURLBuilder.aPackageURL()
.withType(getType())
.withNamespace(getNamespace())
.withName(getName())
.withVersion(getVersion())
.withSubpath(getSubpath());

if (qualifiers != null) {
qualifiers.forEach(builder::withQualifier);
}

return builder;

}

/**
* Returns the package url scheme.
*
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/github/packageurl/PackageURLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,88 @@ public PackageURLBuilder withQualifier(final String key, final String value) {
return this;
}

/**
* Removes a package qualifier. This is a no-op if the qualifier is not present.
* @param key the package qualifier key to remove
* @return a reference to the builder
*/
public PackageURLBuilder withoutQualifier(final String key) {
if (qualifiers != null) {
qualifiers.remove(key);
if (qualifiers.isEmpty()) { qualifiers = null; }
}
return this;
}

/**
* Removes all qualifiers, if any.
* @return a reference to this builder.
*/
public PackageURLBuilder withNoQualifiers() {
qualifiers = null;
return this;
}

/**
* Returns current type value set in the builder.
* @return type set in this builder
*/
public String getType() {
return type;
}

/**
* Returns current namespace value set in the builder.
* @return namespace set in this builder
*/
public String getNamespace() {
return namespace;
}

/**
* Returns current name value set in the builder.
* @return name set in this builder
*/
public String getName() {
return name;
}

/**
* Returns current version value set in the builder.
* @return version set in this builder
*/
public String getVersion() {
return version;
}

/**
* Returns current subpath value set in the builder.
* @return subpath set in this builder
*/
public String getSubpath() {
return subpath;
}

/**
* Returns sorted map containing all qualifiers set in this builder.
* An empty map is returned if no qualifiers is set.
* @return all qualifiers set in this builder, or an empty map if none are set.
*/
public TreeMap<String, String> getQualifiers() {
if (qualifiers == null) { return new TreeMap<>(); }
return new TreeMap<>(qualifiers);
}

/**
* Returns a currently set qualifier value set in the builder for the specified key.
* @param key qualifier key
* @return qualifier value or {@code null} if one is not set.
*/
public String getQualifier(String key) {
if (qualifiers == null) { return null; }
return qualifiers.get(key);
}

/**
* Builds the new PackageURL object.
*
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/github/packageurl/PackageURLBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Map;

import static org.junit.Assert.*;

public class PackageURLBuilderTest {
Expand Down Expand Up @@ -94,6 +96,7 @@ public void testPackageURLBuilder() throws MalformedPackageURLException {
@Test
public void testPackageURLBuilderException1() throws MalformedPackageURLException {
exception.expect(MalformedPackageURLException.class);
exception.expectMessage("contains a qualifier key with an empty or null");
PackageURL purl = PackageURLBuilder.aPackageURL()
.withType("type")
.withName("name")
Expand All @@ -102,6 +105,18 @@ public void testPackageURLBuilderException1() throws MalformedPackageURLExceptio
Assert.fail("Build should fail due to invalid qualifier (empty value)");
}

@Test
public void testPackageURLBuilderException1Null() throws MalformedPackageURLException {
exception.expect(MalformedPackageURLException.class);
exception.expectMessage("contains a qualifier key with an empty or null");
PackageURLBuilder.aPackageURL()
.withType("type")
.withName("name")
.withQualifier("key",null)
.build();
Assert.fail("Build should fail due to invalid qualifier (null value)");
}

@Test
public void testPackageURLBuilderException2() throws MalformedPackageURLException {
exception.expect(MalformedPackageURLException.class);
Expand Down Expand Up @@ -156,4 +171,49 @@ public void testPackageURLBuilderException6() throws MalformedPackageURLExceptio
Assert.fail("Build should fail due to invalid qualifier key");
}

@Test
public void testEditBuilder1() throws MalformedPackageURLException {

PackageURL p = new PackageURL("pkg:generic/namespace/[email protected]?k=v#s");
PackageURLBuilder b = p.toBuilder();
assertBuilderMatch(p, b);

assertBuilderMatch(new PackageURL("pkg:generic/namespace/[email protected]#s"), b.withNoQualifiers());
b.withType("maven")
.withNamespace("org.junit")
.withName("junit5")
.withVersion("3.1.2")
.withSubpath("sub")
.withQualifier("repo", "maven")
.withQualifier("dark", "matter")
.withQualifier("ping", "pong")
.withoutQualifier("dark");

assertBuilderMatch(new PackageURL("pkg:maven/org.junit/[email protected]?repo=maven&ping=pong#sub"), b);

}

private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {

Assert.assertEquals(expected.toString(), actual.build().toString());
Assert.assertEquals(expected.getType(), actual.getType());
Assert.assertEquals(expected.getNamespace(), actual.getNamespace());
Assert.assertEquals(expected.getName(), actual.getName());
Assert.assertEquals(expected.getVersion(), actual.getVersion());
Assert.assertEquals(expected.getSubpath(), actual.getSubpath());

Map<String, String> eQualifiers = expected.getQualifiers();
Map<String, String> aQualifiers = actual.getQualifiers();

if (eQualifiers != null) {
eQualifiers.forEach((k,v)-> {
Assert.assertEquals(v, aQualifiers.remove(k));
Assert.assertEquals(v, actual.getQualifier(k));
});
}

Assert.assertTrue(aQualifiers.isEmpty());

}

}
Loading