Skip to content

Commit a0f623e

Browse files
dwalluckjeremylong
andauthored
fix: correct issues with PackageURLTest (#156)
* 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 * fix: spelling --------- Co-authored-by: Jeremy Long <jeremy.long@gmail.com>
1 parent ed3a681 commit a0f623e

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

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

Lines changed: 35 additions & 32 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.HashMap;
2728
import java.util.Map;
2829
import java.util.Locale;
@@ -41,7 +42,8 @@
4142
/**
4243
* Test cases for PackageURL parsing
4344
* <p>
44-
* Original test cases retrieved from: https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json
45+
* Original test cases retrieved from:
46+
* <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>
4547
*
4648
* @author Steve Springett
4749
*/
@@ -56,7 +58,8 @@ public class PackageURLTest {
5658
@BeforeClass
5759
public static void setup() throws IOException {
5860
InputStream is = PackageURLTest.class.getResourceAsStream("/test-suite-data.json");
59-
String jsonTxt = IOUtils.toString(is, "UTF-8");
61+
Assert.assertNotNull(is);
62+
String jsonTxt = IOUtils.toString(is, StandardCharsets.UTF_8);
6063
json = new JSONArray(jsonTxt);
6164
defaultLocale = Locale.getDefault();
6265
Locale.setDefault(new Locale("tr"));
@@ -89,7 +92,7 @@ public void testConstructorParsing() throws Exception {
8992
if (invalid) {
9093
try {
9194
PackageURL purl = new PackageURL(purlString);
92-
Assert.fail("Inavlid purl should have caused an exception: " + purl.toString());
95+
Assert.fail("Invalid purl should have caused an exception: " + purl);
9396
} catch (MalformedPackageURLException e) {
9497
Assert.assertNotNull(e.getMessage());
9598
}
@@ -109,7 +112,7 @@ public void testConstructorParsing() throws Exception {
109112
} else {
110113
Assert.assertNotNull(purl.getQualifiers());
111114
Assert.assertEquals(qualifiers.length(), purl.getQualifiers().size());
112-
qualifiers.keySet().forEach((key) -> {
115+
qualifiers.keySet().forEach(key -> {
113116
String value = qualifiers.getString(key);
114117
Assert.assertTrue(purl.getQualifiers().containsKey(key));
115118
Assert.assertEquals(value, purl.getQualifiers().get(key));
@@ -143,9 +146,9 @@ public void testConstructorParameters() throws MalformedPackageURLException {
143146
Map<String, String> hashMap = null;
144147
if (qualifiers != null) {
145148
map = qualifiers.toMap().entrySet().stream().collect(
146-
TreeMap<String, String>::new,
149+
TreeMap::new,
147150
(qmap, entry) -> qmap.put(entry.getKey(), (String) entry.getValue()),
148-
TreeMap<String, String>::putAll
151+
TreeMap::putAll
149152
);
150153
hashMap = new HashMap<>(map);
151154
}
@@ -155,7 +158,7 @@ public void testConstructorParameters() throws MalformedPackageURLException {
155158
if (invalid) {
156159
try {
157160
PackageURL purl = new PackageURL(type, namespace, name, version, map, subpath);
158-
Assert.fail("Invalid package url components should have caused an exception: " + purl.toString());
161+
Assert.fail("Invalid package url components should have caused an exception: " + purl);
159162
} catch (MalformedPackageURLException e) {
160163
Assert.assertNotNull(e.getMessage());
161164
}
@@ -174,7 +177,7 @@ public void testConstructorParameters() throws MalformedPackageURLException {
174177
if (qualifiers != null) {
175178
Assert.assertNotNull(purl.getQualifiers());
176179
Assert.assertEquals(qualifiers.length(), purl.getQualifiers().size());
177-
qualifiers.keySet().forEach((key) -> {
180+
qualifiers.keySet().forEach(key -> {
178181
String value = qualifiers.getString(key);
179182
Assert.assertTrue(purl.getQualifiers().containsKey(key));
180183
Assert.assertEquals(value, purl.getQualifiers().get(key));
@@ -195,6 +198,7 @@ public void testConstructor() throws MalformedPackageURLException {
195198

196199
purl = new PackageURL("pkg:generic/namespace/name@1.0.0?key=value==");
197200
Assert.assertEquals("generic", purl.getType());
201+
Assert.assertNotNull(purl.getQualifiers());
198202
Assert.assertEquals(1, purl.getQualifiers().size());
199203
Assert.assertTrue(purl.getQualifiers().containsValue("value=="));
200204

@@ -318,24 +322,23 @@ public void testConstructorWithEmptyKey() throws MalformedPackageURLException {
318322

319323
@Test
320324
public void testStandardTypes() {
321-
exception = ExpectedException.none();
322-
Assert.assertEquals(PackageURL.StandardTypes.BITBUCKET, "bitbucket");
323-
Assert.assertEquals(PackageURL.StandardTypes.CARGO, "cargo");
324-
Assert.assertEquals(PackageURL.StandardTypes.COMPOSER, "composer");
325-
Assert.assertEquals(PackageURL.StandardTypes.DEBIAN, "deb");
326-
Assert.assertEquals(PackageURL.StandardTypes.DOCKER, "docker");
327-
Assert.assertEquals(PackageURL.StandardTypes.GEM, "gem");
328-
Assert.assertEquals(PackageURL.StandardTypes.GENERIC, "generic");
329-
Assert.assertEquals(PackageURL.StandardTypes.GITHUB, "github");
330-
Assert.assertEquals(PackageURL.StandardTypes.GOLANG, "golang");
331-
Assert.assertEquals(PackageURL.StandardTypes.HEX, "hex");
332-
Assert.assertEquals(PackageURL.StandardTypes.MAVEN, "maven");
333-
Assert.assertEquals(PackageURL.StandardTypes.NPM, "npm");
334-
Assert.assertEquals(PackageURL.StandardTypes.NUGET, "nuget");
335-
Assert.assertEquals(PackageURL.StandardTypes.PYPI, "pypi");
336-
Assert.assertEquals(PackageURL.StandardTypes.RPM, "rpm");
337-
Assert.assertEquals(PackageURL.StandardTypes.NIXPKGS, "nixpkgs");
338-
Assert.assertEquals(PackageURL.StandardTypes.HACKAGE, "hackage");
325+
Assert.assertEquals("bitbucket", PackageURL.StandardTypes.BITBUCKET);
326+
Assert.assertEquals("cargo", PackageURL.StandardTypes.CARGO);
327+
Assert.assertEquals("composer", PackageURL.StandardTypes.COMPOSER);
328+
Assert.assertEquals("deb", PackageURL.StandardTypes.DEBIAN);
329+
Assert.assertEquals("docker", PackageURL.StandardTypes.DOCKER);
330+
Assert.assertEquals("gem", PackageURL.StandardTypes.GEM);
331+
Assert.assertEquals("generic", PackageURL.StandardTypes.GENERIC);
332+
Assert.assertEquals("github", PackageURL.StandardTypes.GITHUB);
333+
Assert.assertEquals("golang", PackageURL.StandardTypes.GOLANG);
334+
Assert.assertEquals("hex", PackageURL.StandardTypes.HEX);
335+
Assert.assertEquals("maven", PackageURL.StandardTypes.MAVEN);
336+
Assert.assertEquals("npm", PackageURL.StandardTypes.NPM);
337+
Assert.assertEquals("nuget", PackageURL.StandardTypes.NUGET);
338+
Assert.assertEquals("pypi", PackageURL.StandardTypes.PYPI);
339+
Assert.assertEquals("rpm", PackageURL.StandardTypes.RPM);
340+
Assert.assertEquals("nixpkgs", PackageURL.StandardTypes.NIXPKGS);
341+
Assert.assertEquals("hackage", PackageURL.StandardTypes.HACKAGE);
339342
}
340343

341344
@Test
@@ -369,14 +372,14 @@ public void testGetCoordinatesNoCacheIssue89() throws Exception {
369372
public void testNpmCaseSensitive() throws Exception {
370373
// e.g. https://www.npmjs.com/package/base64/v/1.0.0
371374
PackageURL base64Lowercase = new PackageURL("pkg:npm/base64@1.0.0");
372-
Assert.assertEquals(base64Lowercase.getType(), "npm");
373-
Assert.assertEquals(base64Lowercase.getName(), "base64");
374-
Assert.assertEquals(base64Lowercase.getVersion(), "1.0.0");
375+
Assert.assertEquals("npm", base64Lowercase.getType());
376+
Assert.assertEquals("base64", base64Lowercase.getName());
377+
Assert.assertEquals("1.0.0", base64Lowercase.getVersion());
375378

376379
// e.g. https://www.npmjs.com/package/Base64/v/1.0.0
377380
PackageURL base64Uppercase = new PackageURL("pkg:npm/Base64@1.0.0");
378-
Assert.assertEquals(base64Uppercase.getType(), "npm");
379-
Assert.assertEquals(base64Uppercase.getName(), "Base64");
380-
Assert.assertEquals(base64Uppercase.getVersion(), "1.0.0");
381+
Assert.assertEquals("npm", base64Uppercase.getType());
382+
Assert.assertEquals("Base64", base64Uppercase.getName());
383+
Assert.assertEquals("1.0.0", base64Uppercase.getVersion());
381384
}
382385
}

0 commit comments

Comments
 (0)