@@ -608,9 +608,21 @@ public void testConvertAndStoreSpdxDocument() throws InvalidSPDXAnalysisExceptio
608608 assertEquals (annotationComment , resultAnnotation .getStatement ().get ());
609609
610610 Element [] rootElements = result .getRootElements ().toArray (new Element [result .getRootElements ().size ()]);
611- assertEquals (1 , rootElements .length );
612- assertTrue (rootElements [0 ] instanceof SpdxPackage );
613- SpdxPackage resultPkg = (SpdxPackage )rootElements [0 ];
611+ assertEquals (2 , rootElements .length );
612+ SpdxPackage resultPkg = null ;
613+ CustomLicense rootCustomLicense = null ;
614+ for (Element rootElement :rootElements ) {
615+ if (rootElement instanceof SpdxPackage ) {
616+ resultPkg = (SpdxPackage )rootElement ;
617+ } else if (rootElement instanceof CustomLicense ) {
618+ rootCustomLicense = (CustomLicense )rootElement ;
619+ } else {
620+ fail ("Unexpected root element type " +rootElement .getClass ().getName ());
621+ }
622+ }
623+ assertTrue (Objects .nonNull (resultPkg ));
624+ assertTrue (Objects .nonNull (rootCustomLicense ));
625+ assertEquals (extractedLicName , rootCustomLicense .getName ().get ());
614626 assertEquals (pkgName , resultPkg .getName ().get ());
615627 assertEquals (pkgDownloadLocation , resultPkg .getDownloadLocation ().get ());
616628 assertEquals (pkgCopyright , resultPkg .getCopyrightText ().get ());
@@ -722,6 +734,85 @@ public void testConvertAndStoreSpdxDocumentOrphanPackage() throws InvalidSPDXAna
722734 assertTrue (resultPackageNames .contains (describedPkgName ));
723735 assertTrue (resultPackageNames .contains (orphanPkgName ));
724736 }
737+
738+ /**
739+ * Test method for {@link org.spdx.library.conversion.Spdx2to3Converter#convertAndStore(org.spdx.library.model.v2.SpdxDocument)}.
740+ * Verifies that extracted licensing info, listed licenses, and listed license exceptions stored in the
741+ * document's model store but not referenced by any package, file, or snippet are still converted and
742+ * added as root elements rather than silently dropped.
743+ * @throws InvalidSPDXAnalysisException
744+ */
745+ @ Test
746+ public void testConvertAndStoreSpdxDocumentOrphanLicenses () throws InvalidSPDXAnalysisException {
747+ String pkgName = "package" ;
748+ String extractedLicName = "Extracted License Name" ;
749+ String extractedLicText = "Extracted license text" ;
750+ String orphanLicenseId = "Apache-2.0" ;
751+ String orphanExceptionId = "Classpath-exception-2.0" ;
752+
753+ org .spdx .library .model .v2 .SpdxDocument doc = new org .spdx .library .model .v2 .SpdxDocument (fromModelStore , DOCUMENT_URI , copyManager , true );
754+ doc .setCreationInfo (doc .createCreationInfo (Arrays .asList (new String [] {SpdxConstantsCompatV2 .CREATOR_PREFIX_TOOL + "test" }),
755+ "2010-01-29T18:30:22Z" ));
756+ org .spdx .library .model .v2 .license .AnyLicenseInfo dataLicense =
757+ LicenseInfoFactory .parseSPDXLicenseStringCompatV2 ("CC0-1.0" , fromModelStore , DOCUMENT_URI , copyManager );
758+ doc .setDataLicense (dataLicense );
759+
760+ org .spdx .library .model .v2 .license .AnyLicenseInfo noAssertion =
761+ new org .spdx .library .model .v2 .license .SpdxNoAssertionLicense ();
762+ org .spdx .library .model .v2 .SpdxPackage pkg = doc .createPackage (fromModelStore .getNextId (IdType .SpdxId ),
763+ pkgName , noAssertion , "copyright" , noAssertion )
764+ .setFilesAnalyzed (false )
765+ .setDownloadLocation ("NOASSERTION" )
766+ .build ();
767+ doc .setDocumentDescribes (Arrays .asList (new org .spdx .library .model .v2 .SpdxItem [] {pkg }));
768+
769+ // extracted license info stored at the document level but not referenced by the package
770+ org .spdx .library .model .v2 .license .ExtractedLicenseInfo extractedLicense =
771+ new org .spdx .library .model .v2 .license .ExtractedLicenseInfo (fromModelStore , DOCUMENT_URI ,
772+ fromModelStore .getNextId (IdType .LicenseRef ), copyManager , true );
773+ extractedLicense .setExtractedText (extractedLicText );
774+ extractedLicense .setName (extractedLicName );
775+ doc .setExtractedLicenseInfos (Arrays .asList (new org .spdx .library .model .v2 .license .ExtractedLicenseInfo [] {extractedLicense }));
776+
777+ // listed license and listed license exception stored in the document's model store but not
778+ // referenced by the package, a relationship, or documentDescribes
779+ org .spdx .library .model .v2 .license .SpdxListedLicense orphanLicense =
780+ new org .spdx .library .model .v2 .license .SpdxListedLicense (fromModelStore ,
781+ SpdxConstantsCompatV2 .LISTED_LICENSE_NAMESPACE_PREFIX , orphanLicenseId , copyManager , true );
782+ org .spdx .library .model .v2 .license .ListedLicenseException orphanException =
783+ new org .spdx .library .model .v2 .license .ListedLicenseException (fromModelStore ,
784+ SpdxConstantsCompatV2 .LISTED_LICENSE_NAMESPACE_PREFIX , orphanExceptionId , copyManager , true );
785+
786+ Spdx2to3Converter converter = new Spdx2to3Converter (toModelStore , copyManager , defaultCreationInfo ,
787+ SpdxModelFactory .getLatestSpecVersion (), DEFAULT_PREFIX , true );
788+ SpdxDocument result = converter .convertAndStore (doc );
789+
790+ Element [] rootElements = result .getRootElements ().toArray (new Element [result .getRootElements ().size ()]);
791+ assertEquals (4 , rootElements .length );
792+ boolean foundPkg = false ;
793+ boolean foundCustomLicense = false ;
794+ boolean foundListedLicense = false ;
795+ boolean foundListedException = false ;
796+ for (Element rootElement :rootElements ) {
797+ if (rootElement instanceof SpdxPackage ) {
798+ foundPkg = true ;
799+ assertEquals (pkgName , ((SpdxPackage )rootElement ).getName ().get ());
800+ } else if (rootElement instanceof CustomLicense ) {
801+ foundCustomLicense = true ;
802+ assertEquals (extractedLicName , ((CustomLicense )rootElement ).getName ().get ());
803+ } else if (rootElement instanceof ListedLicense ) {
804+ foundListedLicense = true ;
805+ } else if (rootElement instanceof ListedLicenseException ) {
806+ foundListedException = true ;
807+ } else {
808+ fail ("Unexpected root element type " +rootElement .getClass ().getName ());
809+ }
810+ }
811+ assertTrue (foundPkg );
812+ assertTrue (foundCustomLicense );
813+ assertTrue (foundListedLicense );
814+ assertTrue (foundListedException );
815+ }
725816
726817 @ Test
727818 public void testConvertAndStoreExternalDocRef () throws InvalidSPDXAnalysisException {
0 commit comments