-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix javadoc link * Guard against null pointers * Use `StandardCharsets.UTF_8` * Remove redundant `toString()` calls * Swap argument order in assert statements
- Loading branch information
Showing
1 changed file
with
35 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.TreeMap; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
@@ -37,7 +38,8 @@ | |
/** | ||
* Test cases for PackageURL parsing | ||
* <p> | ||
* Original test cases retrieved from: https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json | ||
* Original test cases retrieved from: | ||
* <a href="https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json">https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json</a> | ||
* | ||
* @author Steve Springett | ||
*/ | ||
|
@@ -51,7 +53,8 @@ public class PackageURLTest { | |
@BeforeClass | ||
public static void setup() throws IOException { | ||
InputStream is = PackageURLTest.class.getResourceAsStream("/test-suite-data.json"); | ||
String jsonTxt = IOUtils.toString(is, "UTF-8"); | ||
Assert.assertNotNull(is); | ||
String jsonTxt = IOUtils.toString(is, StandardCharsets.UTF_8); | ||
json = new JSONArray(jsonTxt); | ||
} | ||
|
||
|
@@ -77,7 +80,7 @@ public void testConstructorParsing() throws Exception { | |
if (invalid) { | ||
try { | ||
PackageURL purl = new PackageURL(purlString); | ||
Assert.fail("Inavlid purl should have caused an exception: " + purl.toString()); | ||
Assert.fail("Inavlid purl should have caused an exception: " + purl); | ||
} catch (MalformedPackageURLException e) { | ||
Assert.assertNotNull(e.getMessage()); | ||
} | ||
|
@@ -97,7 +100,7 @@ public void testConstructorParsing() throws Exception { | |
} else { | ||
Assert.assertNotNull(purl.getQualifiers()); | ||
Assert.assertEquals(qualifiers.length(), purl.getQualifiers().size()); | ||
qualifiers.keySet().forEach((key) -> { | ||
qualifiers.keySet().forEach(key -> { | ||
String value = qualifiers.getString(key); | ||
Assert.assertTrue(purl.getQualifiers().containsKey(key)); | ||
Assert.assertEquals(value, purl.getQualifiers().get(key)); | ||
|
@@ -130,16 +133,16 @@ public void testConstructorParameters() throws MalformedPackageURLException { | |
TreeMap<String, String> map = null; | ||
if (qualifiers != null) { | ||
map = qualifiers.toMap().entrySet().stream().collect( | ||
TreeMap<String, String>::new, | ||
TreeMap::new, | ||
(qmap, entry) -> qmap.put(entry.getKey(), (String) entry.getValue()), | ||
TreeMap<String, String>::putAll | ||
TreeMap::putAll | ||
); | ||
} | ||
|
||
if (invalid) { | ||
try { | ||
PackageURL purl = new PackageURL(type, namespace, name, version, map, subpath); | ||
Assert.fail("Invalid package url components should have caused an exception: " + purl.toString()); | ||
Assert.fail("Invalid package url components should have caused an exception: " + purl); | ||
} catch (MalformedPackageURLException e) { | ||
Assert.assertNotNull(e.getMessage()); | ||
} | ||
|
@@ -158,7 +161,7 @@ public void testConstructorParameters() throws MalformedPackageURLException { | |
if (qualifiers != null) { | ||
Assert.assertNotNull(purl.getQualifiers()); | ||
Assert.assertEquals(qualifiers.length(), purl.getQualifiers().size()); | ||
qualifiers.keySet().forEach((key) -> { | ||
qualifiers.keySet().forEach(key -> { | ||
String value = qualifiers.getString(key); | ||
Assert.assertTrue(purl.getQualifiers().containsKey(key)); | ||
Assert.assertEquals(value, purl.getQualifiers().get(key)); | ||
|
@@ -177,6 +180,7 @@ public void testConstructor() throws MalformedPackageURLException { | |
|
||
purl = new PackageURL("pkg:generic/namespace/[email protected]?key=value=="); | ||
Assert.assertEquals("generic", purl.getType()); | ||
Assert.assertNotNull(purl.getQualifiers()); | ||
Assert.assertEquals(1, purl.getQualifiers().size()); | ||
Assert.assertTrue(purl.getQualifiers().containsValue("value==")); | ||
|
||
|
@@ -269,23 +273,23 @@ public void testConstructorWithDuplicateQualifiers() throws MalformedPackageURLE | |
@Test | ||
public void testStandardTypes() { | ||
exception = ExpectedException.none(); | ||
Assert.assertEquals(PackageURL.StandardTypes.BITBUCKET, "bitbucket"); | ||
Assert.assertEquals(PackageURL.StandardTypes.CARGO, "cargo"); | ||
Assert.assertEquals(PackageURL.StandardTypes.COMPOSER, "composer"); | ||
Assert.assertEquals(PackageURL.StandardTypes.DEBIAN, "deb"); | ||
Assert.assertEquals(PackageURL.StandardTypes.DOCKER, "docker"); | ||
Assert.assertEquals(PackageURL.StandardTypes.GEM, "gem"); | ||
Assert.assertEquals(PackageURL.StandardTypes.GENERIC, "generic"); | ||
Assert.assertEquals(PackageURL.StandardTypes.GITHUB, "github"); | ||
Assert.assertEquals(PackageURL.StandardTypes.GOLANG, "golang"); | ||
Assert.assertEquals(PackageURL.StandardTypes.HEX, "hex"); | ||
Assert.assertEquals(PackageURL.StandardTypes.MAVEN, "maven"); | ||
Assert.assertEquals(PackageURL.StandardTypes.NPM, "npm"); | ||
Assert.assertEquals(PackageURL.StandardTypes.NUGET, "nuget"); | ||
Assert.assertEquals(PackageURL.StandardTypes.PYPI, "pypi"); | ||
Assert.assertEquals(PackageURL.StandardTypes.RPM, "rpm"); | ||
Assert.assertEquals(PackageURL.StandardTypes.NIXPKGS, "nixpkgs"); | ||
Assert.assertEquals(PackageURL.StandardTypes.HACKAGE, "hackage"); | ||
Assert.assertEquals("bitbucket", PackageURL.StandardTypes.BITBUCKET); | ||
Assert.assertEquals("cargo", PackageURL.StandardTypes.CARGO); | ||
Assert.assertEquals("composer", PackageURL.StandardTypes.COMPOSER); | ||
Assert.assertEquals("deb", PackageURL.StandardTypes.DEBIAN); | ||
Assert.assertEquals("docker", PackageURL.StandardTypes.DOCKER); | ||
Assert.assertEquals("gem", PackageURL.StandardTypes.GEM); | ||
Assert.assertEquals("generic", PackageURL.StandardTypes.GENERIC); | ||
Assert.assertEquals("github", PackageURL.StandardTypes.GITHUB); | ||
Assert.assertEquals("golang", PackageURL.StandardTypes.GOLANG); | ||
Assert.assertEquals("hex", PackageURL.StandardTypes.HEX); | ||
Assert.assertEquals("maven", PackageURL.StandardTypes.MAVEN); | ||
Assert.assertEquals("npm", PackageURL.StandardTypes.NPM); | ||
Assert.assertEquals("nuget", PackageURL.StandardTypes.NUGET); | ||
Assert.assertEquals("pypi", PackageURL.StandardTypes.PYPI); | ||
Assert.assertEquals("rpm", PackageURL.StandardTypes.RPM); | ||
Assert.assertEquals("nixpkgs", PackageURL.StandardTypes.NIXPKGS); | ||
Assert.assertEquals("hackage", PackageURL.StandardTypes.HACKAGE); | ||
} | ||
|
||
@Test | ||
|
@@ -319,14 +323,14 @@ public void testGetCoordinatesNoCacheIssue89() throws Exception { | |
public void testNpmCaseSensitive() throws Exception { | ||
// e.g. https://www.npmjs.com/package/base64/v/1.0.0 | ||
PackageURL base64Lowercase = new PackageURL("pkg:npm/[email protected]"); | ||
Assert.assertEquals(base64Lowercase.getType(), "npm"); | ||
Assert.assertEquals(base64Lowercase.getName(), "base64"); | ||
Assert.assertEquals(base64Lowercase.getVersion(), "1.0.0"); | ||
Assert.assertEquals("npm", base64Lowercase.getType()); | ||
Assert.assertEquals("base64", base64Lowercase.getName()); | ||
Assert.assertEquals("1.0.0", base64Lowercase.getVersion()); | ||
|
||
// e.g. https://www.npmjs.com/package/Base64/v/1.0.0 | ||
PackageURL base64Uppercase = new PackageURL("pkg:npm/[email protected]"); | ||
Assert.assertEquals(base64Uppercase.getType(), "npm"); | ||
Assert.assertEquals(base64Uppercase.getName(), "Base64"); | ||
Assert.assertEquals(base64Uppercase.getVersion(), "1.0.0"); | ||
Assert.assertEquals("npm", base64Uppercase.getType()); | ||
Assert.assertEquals("Base64", base64Uppercase.getName()); | ||
Assert.assertEquals("1.0.0", base64Uppercase.getVersion()); | ||
} | ||
} |