Skip to content

Commit 9de1b87

Browse files
Merge pull request #111 from veselov/editable-builder
Allow builder to be fully editable
2 parents 941c84b + 2e99d20 commit 9de1b87

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

src/main/java/com/github/packageurl/PackageURL.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ public PackageURL(final String type, final String namespace, final String name,
146146
*/
147147
private String subpath;
148148

149+
public PackageURLBuilder toBuilder() {
150+
151+
PackageURLBuilder builder = PackageURLBuilder.aPackageURL()
152+
.withType(getType())
153+
.withNamespace(getNamespace())
154+
.withName(getName())
155+
.withVersion(getVersion())
156+
.withSubpath(getSubpath());
157+
158+
if (qualifiers != null) {
159+
qualifiers.forEach(builder::withQualifier);
160+
}
161+
162+
return builder;
163+
164+
}
165+
149166
/**
150167
* Returns the package url scheme.
151168
*

src/main/java/com/github/packageurl/PackageURLBuilder.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,88 @@ public PackageURLBuilder withQualifier(final String key, final String value) {
124124
return this;
125125
}
126126

127+
/**
128+
* Removes a package qualifier. This is a no-op if the qualifier is not present.
129+
* @param key the package qualifier key to remove
130+
* @return a reference to the builder
131+
*/
132+
public PackageURLBuilder withoutQualifier(final String key) {
133+
if (qualifiers != null) {
134+
qualifiers.remove(key);
135+
if (qualifiers.isEmpty()) { qualifiers = null; }
136+
}
137+
return this;
138+
}
139+
140+
/**
141+
* Removes all qualifiers, if any.
142+
* @return a reference to this builder.
143+
*/
144+
public PackageURLBuilder withNoQualifiers() {
145+
qualifiers = null;
146+
return this;
147+
}
148+
149+
/**
150+
* Returns current type value set in the builder.
151+
* @return type set in this builder
152+
*/
153+
public String getType() {
154+
return type;
155+
}
156+
157+
/**
158+
* Returns current namespace value set in the builder.
159+
* @return namespace set in this builder
160+
*/
161+
public String getNamespace() {
162+
return namespace;
163+
}
164+
165+
/**
166+
* Returns current name value set in the builder.
167+
* @return name set in this builder
168+
*/
169+
public String getName() {
170+
return name;
171+
}
172+
173+
/**
174+
* Returns current version value set in the builder.
175+
* @return version set in this builder
176+
*/
177+
public String getVersion() {
178+
return version;
179+
}
180+
181+
/**
182+
* Returns current subpath value set in the builder.
183+
* @return subpath set in this builder
184+
*/
185+
public String getSubpath() {
186+
return subpath;
187+
}
188+
189+
/**
190+
* Returns sorted map containing all qualifiers set in this builder.
191+
* An empty map is returned if no qualifiers is set.
192+
* @return all qualifiers set in this builder, or an empty map if none are set.
193+
*/
194+
public TreeMap<String, String> getQualifiers() {
195+
if (qualifiers == null) { return new TreeMap<>(); }
196+
return new TreeMap<>(qualifiers);
197+
}
198+
199+
/**
200+
* Returns a currently set qualifier value set in the builder for the specified key.
201+
* @param key qualifier key
202+
* @return qualifier value or {@code null} if one is not set.
203+
*/
204+
public String getQualifier(String key) {
205+
if (qualifiers == null) { return null; }
206+
return qualifiers.get(key);
207+
}
208+
127209
/**
128210
* Builds the new PackageURL object.
129211
*

src/test/java/com/github/packageurl/PackageURLBuilderTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.junit.Test;
2727
import org.junit.rules.ExpectedException;
2828

29+
import java.util.Map;
30+
2931
import static org.junit.Assert.*;
3032

3133
public class PackageURLBuilderTest {
@@ -94,6 +96,7 @@ public void testPackageURLBuilder() throws MalformedPackageURLException {
9496
@Test
9597
public void testPackageURLBuilderException1() throws MalformedPackageURLException {
9698
exception.expect(MalformedPackageURLException.class);
99+
exception.expectMessage("contains a qualifier key with an empty or null");
97100
PackageURL purl = PackageURLBuilder.aPackageURL()
98101
.withType("type")
99102
.withName("name")
@@ -102,6 +105,18 @@ public void testPackageURLBuilderException1() throws MalformedPackageURLExceptio
102105
Assert.fail("Build should fail due to invalid qualifier (empty value)");
103106
}
104107

108+
@Test
109+
public void testPackageURLBuilderException1Null() throws MalformedPackageURLException {
110+
exception.expect(MalformedPackageURLException.class);
111+
exception.expectMessage("contains a qualifier key with an empty or null");
112+
PackageURLBuilder.aPackageURL()
113+
.withType("type")
114+
.withName("name")
115+
.withQualifier("key",null)
116+
.build();
117+
Assert.fail("Build should fail due to invalid qualifier (null value)");
118+
}
119+
105120
@Test
106121
public void testPackageURLBuilderException2() throws MalformedPackageURLException {
107122
exception.expect(MalformedPackageURLException.class);
@@ -156,4 +171,49 @@ public void testPackageURLBuilderException6() throws MalformedPackageURLExceptio
156171
Assert.fail("Build should fail due to invalid qualifier key");
157172
}
158173

174+
@Test
175+
public void testEditBuilder1() throws MalformedPackageURLException {
176+
177+
PackageURL p = new PackageURL("pkg:generic/namespace/name@1.0.0?k=v#s");
178+
PackageURLBuilder b = p.toBuilder();
179+
assertBuilderMatch(p, b);
180+
181+
assertBuilderMatch(new PackageURL("pkg:generic/namespace/name@1.0.0#s"), b.withNoQualifiers());
182+
b.withType("maven")
183+
.withNamespace("org.junit")
184+
.withName("junit5")
185+
.withVersion("3.1.2")
186+
.withSubpath("sub")
187+
.withQualifier("repo", "maven")
188+
.withQualifier("dark", "matter")
189+
.withQualifier("ping", "pong")
190+
.withoutQualifier("dark");
191+
192+
assertBuilderMatch(new PackageURL("pkg:maven/org.junit/junit5@3.1.2?repo=maven&ping=pong#sub"), b);
193+
194+
}
195+
196+
private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {
197+
198+
Assert.assertEquals(expected.toString(), actual.build().toString());
199+
Assert.assertEquals(expected.getType(), actual.getType());
200+
Assert.assertEquals(expected.getNamespace(), actual.getNamespace());
201+
Assert.assertEquals(expected.getName(), actual.getName());
202+
Assert.assertEquals(expected.getVersion(), actual.getVersion());
203+
Assert.assertEquals(expected.getSubpath(), actual.getSubpath());
204+
205+
Map<String, String> eQualifiers = expected.getQualifiers();
206+
Map<String, String> aQualifiers = actual.getQualifiers();
207+
208+
if (eQualifiers != null) {
209+
eQualifiers.forEach((k,v)-> {
210+
Assert.assertEquals(v, aQualifiers.remove(k));
211+
Assert.assertEquals(v, actual.getQualifier(k));
212+
});
213+
}
214+
215+
Assert.assertTrue(aQualifiers.isEmpty());
216+
217+
}
218+
159219
}

0 commit comments

Comments
 (0)