Skip to content

Commit b9902aa

Browse files
authored
Merge pull request #198 from AlexanderYukhanov/master
Issue 197. Fix handling of optional package fields
2 parents 20c6a0b + 095e801 commit b9902aa

2 files changed

Lines changed: 44 additions & 51 deletions

File tree

src/main/java/org/spdx/library/model/SpdxPackage.java

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,6 @@ public Optional<SpdxPackageVerificationCode> getPackageVerificationCode() throws
383383
* @throws InvalidSPDXAnalysisException
384384
*/
385385
public SpdxPackage setPackageVerificationCode(SpdxPackageVerificationCode verificationCode) throws InvalidSPDXAnalysisException {
386-
if (strict && Objects.isNull(verificationCode) && isFilesAnalyzed()) {
387-
throw new InvalidSPDXAnalysisException("Can not set required verificationCode to null");
388-
}
389386
setPropertyValue(SpdxConstants.PROP_PACKAGE_VERIFICATION_CODE, verificationCode);
390387
return this;
391388
}
@@ -606,19 +603,13 @@ protected List<String> _verify(Set<String> verifiedIds, String specVersion) {
606603

607604
// files depends on if the filesAnalyzed flag
608605
try {
609-
if (getFiles().size() == 0) {
610-
if (filesAnalyzed) {
611-
retval.add("Missing required package files for "+pkgName);
612-
}
613-
} else {
614-
if (!filesAnalyzed) {
615-
retval.add("Warning: Found analyzed files for package "+pkgName+" when analyzedFiles is set to false.");
616-
}
617-
for (SpdxFile file:getFiles()) {
618-
List<String> verify = file.verify(verifiedIds, specVersion);
619-
addNameToWarnings(verify);
620-
retval.addAll(verify);
621-
}
606+
if (getFiles().size() != 0 && !filesAnalyzed) {
607+
retval.add("Warning: Found analyzed files for package " + pkgName + " when analyzedFiles is set to false.");
608+
}
609+
for (SpdxFile file:getFiles()) {
610+
List<String> verify = file.verify(verifiedIds, specVersion);
611+
addNameToWarnings(verify);
612+
retval.addAll(verify);
622613
}
623614
} catch (InvalidSPDXAnalysisException e) {
624615
retval.add("Invalid package files: "+e.getMessage());
@@ -627,11 +618,11 @@ protected List<String> _verify(Set<String> verifiedIds, String specVersion) {
627618
// verification code
628619
try {
629620
Optional<SpdxPackageVerificationCode> verificationCode = this.getPackageVerificationCode();
630-
if (!verificationCode.isPresent() && filesAnalyzed) {
631-
retval.add("Missing required package verification code for package " + pkgName);
632-
} else if (verificationCode.isPresent() && !verificationCode.get().getValue().isEmpty() && !filesAnalyzed) {
621+
if (verificationCode.isPresent()
622+
&& !verificationCode.get().getValue().isEmpty()
623+
&& !filesAnalyzed) {
633624
retval.add("Verification code must not be included when files not analyzed.");
634-
} else if (filesAnalyzed) {
625+
} else if (filesAnalyzed && verificationCode.isPresent()) {
635626
List<String> verify = verificationCode.get().verify(verifiedIds, specVersion);
636627
addNameToWarnings(verify);
637628
retval.addAll(verify);
@@ -730,32 +721,30 @@ protected List<String> _verify(Set<String> verifiedIds, String specVersion) {
730721
}
731722

732723
private void verifyLicenseInfosInFiles(Collection<AnyLicenseInfo> licenseInfoFromFiles,
733-
boolean filesAnalyzed, String pkgName, Set<String> verifiedIds, List<String> retval, String specVersion) {
734-
if (licenseInfoFromFiles.size() == 0 && filesAnalyzed) {
735-
if (Version.versionLessThan(specVersion, Version.TWO_POINT_THREE_VERSION)) {
736-
retval.add("Missing required license information from files for "+pkgName);
737-
}
738-
} else {
739-
boolean foundNonSimpleLic = false;
740-
for (AnyLicenseInfo lic:licenseInfoFromFiles) {
741-
List<String> verify = lic.verify(verifiedIds, specVersion);
742-
addNameToWarnings(verify);
743-
retval.addAll(verify);
744-
if (!(lic instanceof SimpleLicensingInfo ||
745-
lic instanceof SpdxNoAssertionLicense ||
746-
lic instanceof SpdxNoneLicense ||
747-
lic instanceof OrLaterOperator ||
748-
lic instanceof WithExceptionOperator)) {
749-
foundNonSimpleLic = true;
750-
}
751-
}
752-
if (foundNonSimpleLic) {
753-
retval.add("license info from files contains complex licenses for "+pkgName);
754-
}
755-
}
756-
}
724+
boolean filesAnalyzed, String pkgName, Set<String> verifiedIds, List<String> retval, String specVersion) {
725+
if (licenseInfoFromFiles.size() != 0 && !filesAnalyzed) {
726+
retval.add("License information from files must not be included when files not analyzed. Package " + pkgName);
727+
} else {
728+
boolean foundNonSimpleLic = false;
729+
for (AnyLicenseInfo lic:licenseInfoFromFiles) {
730+
List<String> verify = lic.verify(verifiedIds, specVersion);
731+
addNameToWarnings(verify);
732+
retval.addAll(verify);
733+
if (!(lic instanceof SimpleLicensingInfo ||
734+
lic instanceof SpdxNoAssertionLicense ||
735+
lic instanceof SpdxNoneLicense ||
736+
lic instanceof OrLaterOperator ||
737+
lic instanceof WithExceptionOperator)) {
738+
foundNonSimpleLic = true;
739+
}
740+
}
741+
if (foundNonSimpleLic) {
742+
retval.add("license info from files contains complex licenses for "+pkgName);
743+
}
744+
}
745+
}
757746

758-
@Override
747+
@Override
759748
public int compareTo(SpdxPackage pkg) {
760749
// sort order is determined by the name and the version
761750

src/test/java/org/spdx/library/model/SpdxPackageTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,20 @@ public void testVerify() throws InvalidSPDXAnalysisException {
221221
pkg.setStrict(false);
222222
List<String> result = pkg.verify();
223223
assertEquals(0, result.size());
224-
// verification code
224+
// verification code is optional
225225
pkg.setPackageVerificationCode(null);
226-
assertEquals(1, pkg.verify().size());
226+
assertEquals(0, pkg.verify().size());
227227

228-
// Make sure no files are allowed when filesAnalyzed is false
228+
// Make sure no files and no licenses from files are allowed when filesAnalyzed is false
229229
pkg.setFilesAnalyzed(false);
230-
assertEquals(1, pkg.verify().size());
231-
232-
//Make sure we're valid with no files and no verification code when filesAnalyzed = false.
230+
assertEquals(2, pkg.verify().size());
231+
232+
// Make sure no licenses are allowed when filesAnalyzed = false
233233
pkg.getFiles().clear();
234+
assertEquals(1, pkg.verify().size());
235+
236+
// Make sure we're valid with no files and no licenses and no verification code when filesAnalyzed = false
237+
pkg.getLicenseInfoFromFiles().clear();
234238
assertEquals(0, pkg.verify().size());
235239
}
236240

0 commit comments

Comments
 (0)