View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    *
5    * $Id: LicenseSummaryTest.java 13529 2011-02-07 11:05:33Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/test/java/org/codehaus/mojo/license/LicenseSummaryTest.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as
12   * published by the Free Software Foundation, either version 3 of the
13   * License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   *
20   * You should have received a copy of the GNU General Lesser Public
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  package org.codehaus.mojo.license;
26  
27  import org.apache.maven.model.License;
28  import org.codehaus.mojo.license.model.ProjectLicenseInfo;
29  import org.junit.Assert;
30  import org.junit.Test;
31  import org.xml.sax.SAXException;
32  
33  import javax.xml.parsers.ParserConfigurationException;
34  import javax.xml.transform.TransformerException;
35  import javax.xml.transform.TransformerFactoryConfigurationError;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.IOException;
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * @since 1.0
44   */
45  public class LicenseSummaryTest
46  {
47  
48      /**
49       * Test reading the license summary xml file into ProjectLicenseInfo objects
50       */
51      @Test
52      public void testReadLicenseSummary()
53          throws IOException, SAXException, ParserConfigurationException
54      {
55          File licenseSummaryFile = new File( "src/test/resources/license-summary-test.xml" );
56          Assert.assertTrue( licenseSummaryFile.exists() );
57          FileInputStream fis = new FileInputStream( licenseSummaryFile );
58          List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
59          fis.close();
60          ProjectLicenseInfo dep = list.get( 0 );
61          Assert.assertEquals( "org.codehaus.mojo", dep.getGroupId() );
62          Assert.assertEquals( "junk", dep.getArtifactId() );
63          Assert.assertEquals( "1.1", dep.getVersion() );
64  
65      }
66  
67      /**
68       * Test writing license information to a license.xml file and then read this file
69       * back in to make sure it's ok.
70       */
71      @Test
72      public void testWriteReadLicenseSummary()
73          throws IOException, SAXException, ParserConfigurationException, TransformerFactoryConfigurationError,
74          TransformerException
75      {
76          List<ProjectLicenseInfo> licSummary = new ArrayList<ProjectLicenseInfo>();
77          ProjectLicenseInfo dep1 = new ProjectLicenseInfo( "org.test", "test1", "1.0" );
78          ProjectLicenseInfo dep2 = new ProjectLicenseInfo( "org.test", "test2", "2.0" );
79  
80          License lic = new License();
81          lic.setName( "lgpl" );
82          lic.setUrl( "http://www.gnu.org/licenses/lgpl-3.0.txt" );
83          lic.setComments( "lgpl version 3.0" );
84          dep1.addLicense( lic );
85          dep2.addLicense( lic );
86  
87          licSummary.add( dep1 );
88          licSummary.add( dep2 );
89  
90          File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" );
91          // File licenseSummaryFile = new File( "src/test/resources/license-summary-test-2.xml" );
92          LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile );
93  
94          Assert.assertTrue( licenseSummaryFile.exists() );
95          FileInputStream fis = new FileInputStream( licenseSummaryFile );
96          List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
97          fis.close();
98          ProjectLicenseInfo dep = list.get( 0 );
99          Assert.assertEquals( "org.test", dep.getGroupId() );
100         Assert.assertEquals( "test1", dep.getArtifactId() );
101         Assert.assertEquals( "1.0", dep.getVersion() );
102 
103     }
104 }