001    /*
002     * #%L
003     * License Maven Plugin
004     *
005     * $Id: LicenseSummaryTest.java 13529 2011-02-07 11:05:33Z tchemit $
006     * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/test/java/org/codehaus/mojo/license/LicenseSummaryTest.java $
007     * %%
008     * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009     * %%
010     * This program is free software: you can redistribute it and/or modify
011     * it under the terms of the GNU Lesser General Public License as
012     * published by the Free Software Foundation, either version 3 of the
013     * License, or (at your option) any later version.
014     *
015     * This program is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Lesser Public License for more details.
019     *
020     * You should have received a copy of the GNU General Lesser Public
021     * License along with this program.  If not, see
022     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023     * #L%
024     */
025    package org.codehaus.mojo.license;
026    
027    import org.apache.maven.model.License;
028    import org.codehaus.mojo.license.model.ProjectLicenseInfo;
029    import org.junit.Assert;
030    import org.junit.Test;
031    import org.xml.sax.SAXException;
032    
033    import javax.xml.parsers.ParserConfigurationException;
034    import javax.xml.transform.TransformerException;
035    import javax.xml.transform.TransformerFactoryConfigurationError;
036    import java.io.File;
037    import java.io.FileInputStream;
038    import java.io.IOException;
039    import java.util.ArrayList;
040    import java.util.List;
041    
042    /**
043     * @since 1.0
044     */
045    public class LicenseSummaryTest
046    {
047    
048        /**
049         * Test reading the license summary xml file into ProjectLicenseInfo objects
050         */
051        @Test
052        public void testReadLicenseSummary()
053            throws IOException, SAXException, ParserConfigurationException
054        {
055            File licenseSummaryFile = new File( "src/test/resources/license-summary-test.xml" );
056            Assert.assertTrue( licenseSummaryFile.exists() );
057            FileInputStream fis = new FileInputStream( licenseSummaryFile );
058            List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
059            fis.close();
060            ProjectLicenseInfo dep = list.get( 0 );
061            Assert.assertEquals( "org.codehaus.mojo", dep.getGroupId() );
062            Assert.assertEquals( "junk", dep.getArtifactId() );
063            Assert.assertEquals( "1.1", dep.getVersion() );
064    
065        }
066    
067        /**
068         * Test writing license information to a license.xml file and then read this file
069         * back in to make sure it's ok.
070         */
071        @Test
072        public void testWriteReadLicenseSummary()
073            throws IOException, SAXException, ParserConfigurationException, TransformerFactoryConfigurationError,
074            TransformerException
075        {
076            List<ProjectLicenseInfo> licSummary = new ArrayList<ProjectLicenseInfo>();
077            ProjectLicenseInfo dep1 = new ProjectLicenseInfo( "org.test", "test1", "1.0" );
078            ProjectLicenseInfo dep2 = new ProjectLicenseInfo( "org.test", "test2", "2.0" );
079    
080            License lic = new License();
081            lic.setName( "lgpl" );
082            lic.setUrl( "http://www.gnu.org/licenses/lgpl-3.0.txt" );
083            lic.setComments( "lgpl version 3.0" );
084            dep1.addLicense( lic );
085            dep2.addLicense( lic );
086    
087            licSummary.add( dep1 );
088            licSummary.add( dep2 );
089    
090            File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" );
091            // File licenseSummaryFile = new File( "src/test/resources/license-summary-test-2.xml" );
092            LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile );
093    
094            Assert.assertTrue( licenseSummaryFile.exists() );
095            FileInputStream fis = new FileInputStream( licenseSummaryFile );
096            List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
097            fis.close();
098            ProjectLicenseInfo dep = list.get( 0 );
099            Assert.assertEquals( "org.test", dep.getGroupId() );
100            Assert.assertEquals( "test1", dep.getArtifactId() );
101            Assert.assertEquals( "1.0", dep.getVersion() );
102    
103        }
104    }