-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from veselov/editable-builder
Allow builder to be fully editable
- Loading branch information
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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") | ||
|
@@ -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); | ||
|
@@ -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()); | ||
|
||
} | ||
|
||
} |