View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.license.model;
17  
18  import org.junit.Assert;
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  import java.io.IOException;
23  import java.net.URL;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  /**
28   * Tests {@link LicenseStore}.
29   *
30   * @author tchemit <chemit@codelutin.com>
31   * @since 1.0
32   */
33  public class LicenseStoreTest
34  {
35  
36      public static final List<String> DEFAULT_LICENSES =
37          Arrays.asList( "agpl_v3", "apache_v2", "cddl_v1", "fdl_v1_3", "gpl_v1", "gpl_v2", "gpl_v3", "lgpl_v2_1",
38                         "lgpl_v3", "mit" );
39  
40      public static final List<String> NEW_LICENSES = Arrays.asList( "license1", "license2" );
41  
42      protected LicenseStore store;
43  
44      @Before
45      public void setUp()
46      {
47          store = null;
48      }
49  
50      @Test
51      public void testJarRepository()
52          throws IOException
53      {
54  
55          store = new LicenseStore();
56          store.init();
57  
58          List<LicenseRepository> repositories = store.getRepositories();
59          Assert.assertNotNull( repositories );
60          Assert.assertEquals( 1, repositories.size() );
61          LicenseRepository repository = repositories.get( 0 );
62  
63          License[] licenses1 = repository.getLicenses();
64          License[] licenses = store.getLicenses();
65          Assert.assertNotNull( licenses );
66          Assert.assertNotNull( licenses1 );
67          Assert.assertEquals( DEFAULT_LICENSES.size(), licenses.length );
68          Assert.assertEquals( DEFAULT_LICENSES.size(), licenses1.length );
69  
70          for ( String licenseName : DEFAULT_LICENSES )
71          {
72              License license = repository.getLicense( licenseName );
73              License license1 = store.getLicense( licenseName );
74              Assert.assertNotNull( license );
75              Assert.assertNotNull( license1 );
76              Assert.assertEquals( license, license1 );
77          }
78  
79          for ( String licenseName : store.getLicenseNames() )
80          {
81              Assert.assertTrue( DEFAULT_LICENSES.contains( licenseName ) );
82          }
83      }
84  
85      @Test
86      public void testOtherJarRepository()
87          throws IOException
88      {
89  
90          URL baseURL = getClass().getResource( "/newRepository" );
91          LicenseRepository jarRepository = new LicenseRepository();
92          jarRepository.setBaseURL( baseURL );
93  
94          store = new LicenseStore();
95          store.addRepository( jarRepository );
96          store.init();
97          List<LicenseRepository> repositories = store.getRepositories();
98          Assert.assertNotNull( repositories );
99          Assert.assertEquals( 1, repositories.size() );
100         LicenseRepository repository = repositories.get( 0 );
101 
102         License[] licenses1 = repository.getLicenses();
103         License[] licenses = store.getLicenses();
104         Assert.assertNotNull( licenses );
105         Assert.assertNotNull( licenses1 );
106         Assert.assertEquals( licenses1.length, 2 );
107         Assert.assertEquals( licenses1.length, licenses.length );
108 
109         for ( String licenseName : NEW_LICENSES )
110         {
111             License license = repository.getLicense( licenseName );
112             License license1 = store.getLicense( licenseName );
113             Assert.assertNotNull( license );
114             Assert.assertNotNull( license1 );
115             Assert.assertEquals( license, license1 );
116         }
117 
118         for ( String licenseName : store.getLicenseNames() )
119         {
120             Assert.assertTrue( NEW_LICENSES.contains( licenseName ) );
121         }
122     }
123 }