Skip to content

Commit 60270be

Browse files
committed
Fix some issues with PackageURLTest
* Fix javadoc link * Guard against null pointers * Use StandardCharsets.UTF_8 * Remove redundant toString calls * Swap argument order in assert statements
1 parent ee6dda9 commit 60270be

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

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

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.TreeMap;
2728

2829
import org.apache.commons.io.IOUtils;
@@ -37,7 +38,8 @@
3738
/**
3839
* Test cases for PackageURL parsing
3940
* <p>
40-
* Original test cases retrieved from: https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json
41+
* Original test cases retrieved from:
42+
* <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>
4143
*
4244
* @author Steve Springett
4345
*/
@@ -51,7 +53,8 @@ public class PackageURLTest {
5153
@BeforeClass
5254
public static void setup() throws IOException {
5355
InputStream is = PackageURLTest.class.getResourceAsStream("/test-suite-data.json");
54-
String jsonTxt = IOUtils.toString(is, "UTF-8");
56+
Assert.assertNotNull(is);
57+
String jsonTxt = IOUtils.toString(is, StandardCharsets.UTF_8);
5558
json = new JSONArray(jsonTxt);
5659
}
5760

@@ -77,7 +80,7 @@ public void testConstructorParsing() throws Exception {
7780
if (invalid) {
7881
try {
7982
PackageURL purl = new PackageURL(purlString);
80-
Assert.fail("Inavlid purl should have caused an exception: " + purl.toString());
83+
Assert.fail("Inavlid purl should have caused an exception: " + purl);
8184
} catch (MalformedPackageURLException e) {
8285
Assert.assertNotNull(e.getMessage());
8386
}
@@ -97,7 +100,7 @@ public void testConstructorParsing() throws Exception {
97100
} else {
98101
Assert.assertNotNull(purl.getQualifiers());
99102
Assert.assertEquals(qualifiers.length(), purl.getQualifiers().size());
100-
qualifiers.keySet().forEach((key) -> {
103+
qualifiers.keySet().forEach(key -> {
101104
String value = qualifiers.getString(key);
102105
Assert.assertTrue(purl.getQualifiers().containsKey(key));
103106
Assert.assertEquals(value, purl.getQualifiers().get(key));
@@ -130,16 +133,16 @@ public void testConstructorParameters() throws MalformedPackageURLException {
130133
TreeMap<String, String> map = null;
131134
if (qualifiers != null) {
132135
map = qualifiers.toMap().entrySet().stream().collect(
133-
TreeMap<String, String>::new,
136+
TreeMap::new,
134137
(qmap, entry) -> qmap.put(entry.getKey(), (String) entry.getValue()),
135-
TreeMap<String, String>::putAll
138+
TreeMap::putAll
136139
);
137140
}
138141

139142
if (invalid) {
140143
try {
141144
PackageURL purl = new PackageURL(type, namespace, name, version, map, subpath);
142-
Assert.fail("Invalid package url components should have caused an exception: " + purl.toString());
145+
Assert.fail("Invalid package url components should have caused an exception: " + purl);
143146
} catch (MalformedPackageURLException e) {
144147
Assert.assertNotNull(e.getMessage());
145148
}
@@ -158,7 +161,7 @@ public void testConstructorParameters() throws MalformedPackageURLException {
158161
if (qualifiers != null) {
159162
Assert.assertNotNull(purl.getQualifiers());
160163
Assert.assertEquals(qualifiers.length(), purl.getQualifiers().size());
161-
qualifiers.keySet().forEach((key) -> {
164+
qualifiers.keySet().forEach(key -> {
162165
String value = qualifiers.getString(key);
163166
Assert.assertTrue(purl.getQualifiers().containsKey(key));
164167
Assert.assertEquals(value, purl.getQualifiers().get(key));
@@ -177,6 +180,7 @@ public void testConstructor() throws MalformedPackageURLException {
177180

178181
purl = new PackageURL("pkg:generic/namespace/[email protected]?key=value==");
179182
Assert.assertEquals("generic", purl.getType());
183+
Assert.assertNotNull(purl.getQualifiers());
180184
Assert.assertEquals(1, purl.getQualifiers().size());
181185
Assert.assertTrue(purl.getQualifiers().containsValue("value=="));
182186

@@ -269,23 +273,23 @@ public void testConstructorWithDuplicateQualifiers() throws MalformedPackageURLE
269273
@Test
270274
public void testStandardTypes() {
271275
exception = ExpectedException.none();
272-
Assert.assertEquals(PackageURL.StandardTypes.BITBUCKET, "bitbucket");
273-
Assert.assertEquals(PackageURL.StandardTypes.CARGO, "cargo");
274-
Assert.assertEquals(PackageURL.StandardTypes.COMPOSER, "composer");
275-
Assert.assertEquals(PackageURL.StandardTypes.DEBIAN, "deb");
276-
Assert.assertEquals(PackageURL.StandardTypes.DOCKER, "docker");
277-
Assert.assertEquals(PackageURL.StandardTypes.GEM, "gem");
278-
Assert.assertEquals(PackageURL.StandardTypes.GENERIC, "generic");
279-
Assert.assertEquals(PackageURL.StandardTypes.GITHUB, "github");
280-
Assert.assertEquals(PackageURL.StandardTypes.GOLANG, "golang");
281-
Assert.assertEquals(PackageURL.StandardTypes.HEX, "hex");
282-
Assert.assertEquals(PackageURL.StandardTypes.MAVEN, "maven");
283-
Assert.assertEquals(PackageURL.StandardTypes.NPM, "npm");
284-
Assert.assertEquals(PackageURL.StandardTypes.NUGET, "nuget");
285-
Assert.assertEquals(PackageURL.StandardTypes.PYPI, "pypi");
286-
Assert.assertEquals(PackageURL.StandardTypes.RPM, "rpm");
287-
Assert.assertEquals(PackageURL.StandardTypes.NIXPKGS, "nixpkgs");
288-
Assert.assertEquals(PackageURL.StandardTypes.HACKAGE, "hackage");
276+
Assert.assertEquals("bitbucket", PackageURL.StandardTypes.BITBUCKET);
277+
Assert.assertEquals("cargo", PackageURL.StandardTypes.CARGO);
278+
Assert.assertEquals("composer", PackageURL.StandardTypes.COMPOSER);
279+
Assert.assertEquals("deb", PackageURL.StandardTypes.DEBIAN);
280+
Assert.assertEquals("docker", PackageURL.StandardTypes.DOCKER);
281+
Assert.assertEquals("gem", PackageURL.StandardTypes.GEM);
282+
Assert.assertEquals("generic", PackageURL.StandardTypes.GENERIC);
283+
Assert.assertEquals("github", PackageURL.StandardTypes.GITHUB);
284+
Assert.assertEquals("golang", PackageURL.StandardTypes.GOLANG);
285+
Assert.assertEquals("hex", PackageURL.StandardTypes.HEX);
286+
Assert.assertEquals("maven", PackageURL.StandardTypes.MAVEN);
287+
Assert.assertEquals("npm", PackageURL.StandardTypes.NPM);
288+
Assert.assertEquals("nuget", PackageURL.StandardTypes.NUGET);
289+
Assert.assertEquals("pypi", PackageURL.StandardTypes.PYPI);
290+
Assert.assertEquals("rpm", PackageURL.StandardTypes.RPM);
291+
Assert.assertEquals("nixpkgs", PackageURL.StandardTypes.NIXPKGS);
292+
Assert.assertEquals("hackage", PackageURL.StandardTypes.HACKAGE);
289293
}
290294

291295
@Test
@@ -319,14 +323,14 @@ public void testGetCoordinatesNoCacheIssue89() throws Exception {
319323
public void testNpmCaseSensitive() throws Exception {
320324
// e.g. https://www.npmjs.com/package/base64/v/1.0.0
321325
PackageURL base64Lowercase = new PackageURL("pkg:npm/[email protected]");
322-
Assert.assertEquals(base64Lowercase.getType(), "npm");
323-
Assert.assertEquals(base64Lowercase.getName(), "base64");
324-
Assert.assertEquals(base64Lowercase.getVersion(), "1.0.0");
326+
Assert.assertEquals("npm", base64Lowercase.getType());
327+
Assert.assertEquals("base64", base64Lowercase.getName());
328+
Assert.assertEquals("1.0.0", base64Lowercase.getVersion());
325329

326330
// e.g. https://www.npmjs.com/package/Base64/v/1.0.0
327331
PackageURL base64Uppercase = new PackageURL("pkg:npm/[email protected]");
328-
Assert.assertEquals(base64Uppercase.getType(), "npm");
329-
Assert.assertEquals(base64Uppercase.getName(), "Base64");
330-
Assert.assertEquals(base64Uppercase.getVersion(), "1.0.0");
332+
Assert.assertEquals("npm", base64Uppercase.getType());
333+
Assert.assertEquals("Base64", base64Uppercase.getName());
334+
Assert.assertEquals("1.0.0", base64Uppercase.getVersion());
331335
}
332336
}

0 commit comments

Comments
 (0)