Skip to content
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
1,028 changes: 541 additions & 487 deletions pom.xml

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/headers/java.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
22 changes: 22 additions & 0 deletions src/headers/xml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ MIT License
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public class MalformedPackageURLException extends Exception {
*
* @since 1.0.0
*/
public MalformedPackageURLException() {
}
public MalformedPackageURLException() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am against this change since braces are not supposed to be on a single line, empty or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a choice in the palantir-java-format, not modifiable.


/**
* Constructs a {@code MalformedPackageURLException} with the
Expand Down
140 changes: 91 additions & 49 deletions src/main/java/com/github/packageurl/PackageURL.java

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/main/java/com/github/packageurl/PackageURLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ public PackageURLBuilder withoutQualifiers(final Set<String> keys) {
return this;
}


/**
* Removes all qualifiers, if any.
* @return a reference to this builder.
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/github/packageurl/validator/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
* the JSR-303 compliant validator to validate the field to ensure it meets the Package URL specification.
* @since 1.3.0
*/
@Target({ ElementType.FIELD})
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PackageURLConstraintValidator.class)
public @interface PackageURL {

String message() default "The Package URL (purl) must be a valid URI and conform to https://github.com/package-url/purl-spec";
String message() default
"The Package URL (purl) must be a valid URI and conform to https://github.com/package-url/purl-spec";

Class<?>[] groups() default { };
Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default { };
Class<? extends Payload>[] payload() default {};
}
106 changes: 61 additions & 45 deletions src/test/java/com/github/packageurl/PackageURLBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ static Stream<Arguments> packageURLBuilder() throws IOException {

@ParameterizedTest(name = "{0}: {1}")
@MethodSource
void packageURLBuilder(String description,
@Nullable String ignoredPurl,
PurlParameters parameters,
String canonicalPurl,
boolean invalid) throws MalformedPackageURLException {
void packageURLBuilder(
String description,
@Nullable String ignoredPurl,
PurlParameters parameters,
String canonicalPurl,
boolean invalid)
throws MalformedPackageURLException {
if (parameters.getType() == null || parameters.getName() == null) {
assertTrue(invalid, "valid test case with type or name `null`");
return;
}
PackageURLBuilder builder = PackageURLBuilder.aPackageURL().withType(parameters.getType()).withName(parameters.getName());
PackageURLBuilder builder =
PackageURLBuilder.aPackageURL().withType(parameters.getType()).withName(parameters.getName());
String namespace = parameters.getNamespace();
if (namespace != null) {
builder.withNamespace(namespace);
Expand Down Expand Up @@ -87,7 +90,7 @@ void packageURLBuilderException1() throws MalformedPackageURLException {
PackageURL purl = PackageURLBuilder.aPackageURL()
.withType("type")
.withName("name")
.withQualifier("key","")
.withQualifier("key", "")
.build();
assertEquals(0, purl.getQualifiers().size(), "qualifier count");
}
Expand All @@ -97,63 +100,78 @@ void packageURLBuilderException1Null() throws MalformedPackageURLException {
PackageURL purl = PackageURLBuilder.aPackageURL()
.withType("type")
.withName("name")
.withQualifier("key",null)
.withQualifier("key", null)
.build();
assertEquals(0, purl.getQualifiers().size(), "qualifier count");
}

@Test
void packageURLBuilderException2() {
assertThrowsExactly(MalformedPackageURLException.class, () -> {
PackageURLBuilder.aPackageURL()
.withType("type")
.withNamespace("invalid//namespace")
.withName("name")
.build();
}, "Build should fail due to invalid namespace");
assertThrowsExactly(
MalformedPackageURLException.class,
() -> {
PackageURLBuilder.aPackageURL()
.withType("type")
.withNamespace("invalid//namespace")
.withName("name")
.build();
},
"Build should fail due to invalid namespace");
}

@Test
void packageURLBuilderException3() {
assertThrowsExactly(MalformedPackageURLException.class, () -> {
PackageURLBuilder.aPackageURL()
.withType("typ^e")
.withSubpath("invalid/name%2Fspace")
.withName("name")
.build();
}, "Build should fail due to invalid subpath");
assertThrowsExactly(
MalformedPackageURLException.class,
() -> {
PackageURLBuilder.aPackageURL()
.withType("typ^e")
.withSubpath("invalid/name%2Fspace")
.withName("name")
.build();
},
"Build should fail due to invalid subpath");
}

@Test
void packageURLBuilderException4() {
assertThrowsExactly(MalformedPackageURLException.class, () -> {
PackageURLBuilder.aPackageURL()
.withType("0_type")
.withName("name")
.build();
}, "Build should fail due to invalid type");
assertThrowsExactly(
MalformedPackageURLException.class,
() -> {
PackageURLBuilder.aPackageURL()
.withType("0_type")
.withName("name")
.build();
},
"Build should fail due to invalid type");
}

@Test
void packageURLBuilderException5() {
assertThrowsExactly(MalformedPackageURLException.class, () -> {
PackageURLBuilder.aPackageURL()
.withType("ype")
.withName("name")
.withQualifier("0_key", "value")
.build();
}, "Build should fail due to invalid qualifier key");
assertThrowsExactly(
MalformedPackageURLException.class,
() -> {
PackageURLBuilder.aPackageURL()
.withType("ype")
.withName("name")
.withQualifier("0_key", "value")
.build();
},
"Build should fail due to invalid qualifier key");
}

@Test
void packageURLBuilderException6() {
assertThrowsExactly(MalformedPackageURLException.class, () -> {
PackageURLBuilder.aPackageURL()
.withType("ype")
.withName("name")
.withQualifier("", "value")
.build();
}, "Build should fail due to invalid qualifier key");
assertThrowsExactly(
MalformedPackageURLException.class,
() -> {
PackageURLBuilder.aPackageURL()
.withType("ype")
.withName("name")
.withQualifier("", "value")
.build();
},
"Build should fail due to invalid qualifier key");
}

@Test
Expand Down Expand Up @@ -224,8 +242,6 @@ private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) t

assertEquals(eQualifiers, aQualifiers);

eQualifiers.forEach((k, v) ->
assertEquals(v, actual.getQualifier(k)));
eQualifiers.forEach((k, v) -> assertEquals(v, actual.getQualifier(k)));
}

}
89 changes: 53 additions & 36 deletions src/test/java/com/github/packageurl/PackageURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,22 @@ static void resetLocale() {
void validPercentEncoding() throws MalformedPackageURLException {
PackageURL purl = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null);
assertEquals("pkg:maven/com.google.summit/[email protected]%0A", purl.toString());
PackageURL purl2 = new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5");
PackageURL purl2 =
new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5");
assertEquals("Мicrosоft.ЕntitуFramеworkСоrе", purl2.getName());
assertEquals("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5", purl2.toString());
assertEquals(
"pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5", purl2.toString());
}

@SuppressWarnings("deprecation")
@Test
void invalidPercentEncoding() throws MalformedPackageURLException {
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg:maven/com.google.summit/[email protected]%"));
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg:maven/com.google.summit/[email protected]%0"));
assertThrowsExactly(
MalformedPackageURLException.class,
() -> new PackageURL("pkg:maven/com.google.summit/[email protected]%"));
assertThrowsExactly(
MalformedPackageURLException.class,
() -> new PackageURL("pkg:maven/com.google.summit/[email protected]%0"));
PackageURL purl = new PackageURL("pkg:maven/com.google.summit/[email protected]");
Throwable t1 = assertThrowsExactly(ValidationException.class, () -> purl.uriDecode("%"));
assertEquals("Incomplete percent encoding at offset 0 with value '%'", t1.getMessage());
Expand All @@ -86,19 +92,20 @@ void invalidPercentEncoding() throws MalformedPackageURLException {
}

static Stream<Arguments> constructorParsing() throws IOException {
return PurlParameters.getTestDataFromFiles("test-suite-data.json",
"custom-suite.json",
"string-constructor-only.json");
return PurlParameters.getTestDataFromFiles(
"test-suite-data.json", "custom-suite.json", "string-constructor-only.json");
}

@DisplayName("Test constructor parsing")
@ParameterizedTest(name = "{0}: ''{1}''")
@MethodSource
void constructorParsing(String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid) throws Exception {
void constructorParsing(
String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid)
throws Exception {
if (invalid) {
assertThrows(getExpectedException(purlString), () -> new PackageURL(purlString));
} else {
Expand All @@ -109,32 +116,38 @@ void constructorParsing(String description,
}

static Stream<Arguments> constructorParameters() throws IOException {
return PurlParameters.getTestDataFromFiles("test-suite-data.json", "custom-suite.json", "components-constructor-only.json");
return PurlParameters.getTestDataFromFiles(
"test-suite-data.json", "custom-suite.json", "components-constructor-only.json");
}

@DisplayName("Test constructor parameters")
@ParameterizedTest(name = "{0}: {2}")
@MethodSource
void constructorParameters(String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid) throws Exception {
void constructorParameters(
String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid)
throws Exception {
if (invalid) {
assertThrows(getExpectedException(parameters),
() -> new PackageURL(parameters.getType(),
assertThrows(
getExpectedException(parameters),
() -> new PackageURL(
parameters.getType(),
parameters.getNamespace(),
parameters.getName(),
parameters.getVersion(),
parameters.getQualifiers(),
parameters.getSubpath()));
} else {
PackageURL purl = new PackageURL(
parameters.getType(),
parameters.getNamespace(),
parameters.getName(),
parameters.getVersion(),
parameters.getQualifiers(),
parameters.getSubpath()));
} else {
PackageURL purl = new PackageURL(parameters.getType(),
parameters.getNamespace(),
parameters.getName(),
parameters.getVersion(),
parameters.getQualifiers(),
parameters.getSubpath());
parameters.getSubpath());
assertPurlEquals(parameters, purl);
assertEquals(canonicalPurl, purl.canonicalize(), "canonical PURL");
}
Expand All @@ -146,14 +159,16 @@ static Stream<Arguments> constructorTypeNameSpace() throws IOException {

@ParameterizedTest
@MethodSource
void constructorTypeNameSpace(String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid) throws Exception {
void constructorTypeNameSpace(
String description,
@Nullable String purlString,
PurlParameters parameters,
@Nullable String canonicalPurl,
boolean invalid)
throws Exception {
if (invalid) {
assertThrows(getExpectedException(parameters),
() -> new PackageURL(parameters.getType(), parameters.getName()));
assertThrows(
getExpectedException(parameters), () -> new PackageURL(parameters.getType(), parameters.getName()));
} else {
PackageURL purl = new PackageURL(parameters.getType(), parameters.getName());
assertPurlEquals(parameters, purl);
Expand All @@ -173,7 +188,9 @@ private static void assertPurlEquals(PurlParameters expected, PackageURL actual)
}

private static Class<? extends Exception> getExpectedException(PurlParameters json) {
return json.getType() == null || json.getName() == null ? NullPointerException.class : MalformedPackageURLException.class;
return json.getType() == null || json.getName() == null
? NullPointerException.class
: MalformedPackageURLException.class;
}

private static Class<? extends Exception> getExpectedException(@Nullable String purl) {
Expand Down
Loading