Skip to content
Open
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
2 changes: 2 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:

- name: Checkout repository
uses: actions/checkout@v4.2.2
with:
submodules: true

- name: Set up JDK 8 and ${{ matrix.java-version }}
uses: actions/setup-java@v4
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ buildNumber.properties
.idea
*.iml
##########################################################################

#### ignore VSCODE config
.vscode/
##########################################################################
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/test/resources/purl-spec"]
path = src/test/resources/purl-spec
url = https://github.com/package-url/purl-spec
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<jmh.version>1.37</jmh.version>
<json.version>20250107</json.version>
<junit-bom.version>5.13.3</junit-bom.version>
<lib.jackson-databind.version>2.19.2</lib.jackson-databind.version>
<maven-surefire-junit5-tree-reporter.version>1.4.0</maven-surefire-junit5-tree-reporter.version>
</properties>

Expand Down Expand Up @@ -200,6 +201,11 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${lib.jackson-databind.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
216 changes: 216 additions & 0 deletions src/test/java/com/github/packageurl/PurlSpecRefTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* 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.
*
* SPDX-License-Identifier: MIT
* Copyright (c) AboutCode, and contributors. All Rights Reserved.
*/

package com.github.packageurl;

import java.net.URL;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.JsonNode;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.DeserializationContext;

import java.util.Map;

public class PurlSpecRefTest {

public static class TestSuite {
@JsonProperty("$schema")
public String schema;
public List<TestCase> tests;
}

public static class TestCase {
public String description;
public String test_group;
public String test_type;

public PurlOrComponent input;
public PurlOrComponent expected_output;

public boolean expected_failure;
public String expected_failure_reason;

public TestCase() {
}
}

@JsonDeserialize(using = PurlOrComponentDeserializer.class)
public static class PurlOrComponent {
public String purl;
public PurlComponents components;
}

public static class PurlComponents {
public String type;
public String namespace;
public String name;
public String version;
public Map<String, String> qualifiers;
public String subpath;
}

public static class PurlOrComponentDeserializer extends JsonDeserializer<PurlOrComponent> {
@Override
public PurlOrComponent deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
ObjectCodec codec = p.getCodec();
JsonNode node = codec.readTree(p);

PurlOrComponent value = new PurlOrComponent();

if (node.isTextual()) {
value.purl = node.asText();
} else if (node.isObject()) {
value.components = codec.treeToValue(node, PurlComponents.class);
}

return value;
}
}

static Stream<TestCase> collectTestCases() throws Exception {
ObjectMapper mapper = new ObjectMapper();

URL dirURL = PurlSpecRefTest.class.getClassLoader().getResource("purl-spec/tests/types/");
if (dirURL == null) {
throw new RuntimeException("Resource directory 'purl-spec/tests/types/' not found.");
}

Path testDataPath = Paths.get(dirURL.toURI());
List<Path> jsonFiles = Files.list(testDataPath)
.filter(p -> p.toString().endsWith(".json"))
.collect(Collectors.toList());

Stream.Builder<TestCase> builder = Stream.builder();

for (Path jsonFile : jsonFiles) {
try (InputStream is = Files.newInputStream(jsonFile)) {
TestSuite suite = mapper.readValue(is, TestSuite.class);
suite.tests.forEach(builder::add);
}
}

return builder.build();
}

void runRoundtripTest(TestCase testCase) throws Exception {
String result;
try {
result = new PackageURL(testCase.input.purl).canonicalize().toString();
} catch (Exception e) {
assertTrue(testCase.expected_failure, "Unexpected failure: " + e.getMessage());
return;
}
assertFalse(testCase.expected_failure, "Expected failure but parsing succeeded");

assertEquals(result, testCase.expected_output.purl);

}

void runBuildTest(TestCase testCase) throws Exception {
PurlComponents input = testCase.input.components;
String result;
try {
result = new PackageURL(input.type, input.namespace, input.name, input.version, input.qualifiers,
input.subpath).canonicalize().toString();
} catch (Exception e) {
assertTrue(testCase.expected_failure, "Unexpected failure: " + e.getMessage());
return;
}

assertFalse(testCase.expected_failure, "Expected failure but build succeeded");
assertEquals(result, testCase.expected_output.purl);
}

void runParseTest(TestCase testCase) throws Exception {
PackageURL result;
try {
result = new PackageURL(testCase.input.purl);
} catch (Exception e) {
assertTrue(testCase.expected_failure, "Unexpected failure: " + e.getMessage());
return;
}
assertFalse(testCase.expected_failure, "Expected failure but parsing succeeded");

PurlComponents expected = testCase.expected_output.components;
result.canonicalize();

assertEquals(expected.type, result.getType(), "Type mismatch");
assertEquals(expected.namespace, result.getNamespace(), "Namespace mismatch");
assertEquals(expected.name, result.getName(), "Name mismatch");
assertEquals(expected.version, result.getVersion(), "Version mismatch");
assertEquals(expected.subpath, result.getSubpath(), "Subpath mismatch");

assertEquals(
expected.qualifiers != null ? expected.qualifiers : Collections.emptyMap(),
result.getQualifiers(),
"Qualifiers mismatch");
}

@ParameterizedTest(name = "{0}")
@MethodSource("collectTestCases")
void runTest(TestCase testCase) throws Exception {
switch (testCase.test_type) {
case "roundtrip":
runRoundtripTest(testCase);
break;
case "build":
runBuildTest(testCase);
break;
case "parse":
runParseTest(testCase);
break;
default:
throw new IllegalArgumentException("Unknown test_type: " + testCase.test_type);
}

}

}
1 change: 1 addition & 0 deletions src/test/resources/purl-spec
Submodule purl-spec added at 414fef
Loading