1   /*
2    * Copyright 2006 Eric Ballet Baz
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  
17  package org.apache.maven.author;
18  
19  import java.io.File;
20  import java.lang.reflect.Constructor;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  import junit.framework.TestCase;
30  
31  import org.apache.commons.jelly.tags.ant.FileScanner;
32  import org.apache.tools.ant.Project;
33  import org.apache.tools.ant.types.FileSet;
34  
35  /**
36   * Unit test for the class FilesByAuthorProvider.
37   *
38   * @author Eric Ballet Baz
39   */
40  public class TestFilesByAuthorProvider extends TestCase {
41  
42      private static final String[] AUTHOR_RESOLVER_CLASSNAMES = new String[] {
43          "org.apache.maven.author.resolver.JavadocAuthorResolver",
44          "org.apache.maven.author.resolver.SCMAuthorResolver"
45      };
46  
47      /**
48       * Test the default constructor.
49       */
50      public final void testDefaultConstructor() throws Exception {
51          Constructor constructor = FilesByAuthorProvider.class.getDeclaredConstructor(null);
52          assertFalse(constructor.isAccessible());
53          constructor.setAccessible(true);
54          assertNotNull(constructor.newInstance(null));
55      }
56  
57      /**
58       * Test method resolveAuthors, using null as both arguments.
59       */
60      public final void testResolveAuthorsWithBothArgumentsNull() {
61          try {
62              FilesByAuthorProvider.resolveAuthors(null, null);
63              fail("An IllegalArgumentException should have been thrown");
64  
65          } catch (IllegalArgumentException e) {
66              // Ok !
67              assertNotNull(e);
68  
69          } catch (Exception e) {
70              fail("An IllegalArgumentException should have been thrown");
71          }
72      }
73  
74      /**
75       * Test method resolveAuthors, using null as the first argument.
76       */
77      public final void testResolveAuthorsWithFirstArgumentNull() {
78          try {
79              FilesByAuthorProvider.resolveAuthors(null, new ArrayList(0));
80              fail("An IllegalArgumentException should have been thrown");
81  
82          } catch (IllegalArgumentException e) {
83              // Ok !
84              assertNotNull(e);
85  
86          } catch (Exception e) {
87              fail("An IllegalArgumentException should have been thrown");
88          }
89      }
90  
91      /**
92       * Test method resolveAuthors, using null as the second argument.
93       */
94      public final void testResolveAuthorsWithSecondArgumentNull() {
95          try {
96              FilesByAuthorProvider.resolveAuthors(new FileScanner(), null);
97              fail("An IllegalArgumentException should have been thrown");
98          } catch (IllegalArgumentException e) {
99              // Ok !
100             assertNotNull(e);
101 
102         } catch (Exception e) {
103             fail("An IllegalArgumentException should have been thrown");
104         }
105     }
106 
107     /**
108      * Test method resolveAuthors, using empty values as both arguments.
109      */
110     public final void testResolveAuthorsWithEmptyArguments() throws Exception {
111         Map map = FilesByAuthorProvider.resolveAuthors(new FileScanner(), new ArrayList(0));
112         assertTrue("No author should have been found", map.isEmpty());
113     }
114 
115     /**
116      * Test method resolveAuthors on a file without author.
117      */
118     public final void testResolveAuthorWithoutAuthor() throws Exception {
119         String fileName = "SampleFileWithoutJavadocAuthor.java";
120         URL file = getClass().getClassLoader().getResource(fileName);
121         assertNotNull(file);
122 
123         Map filesByAuthorMap = resolveAuthors(new File(file.getPath()).getParentFile(), fileName);
124         assertEquals(1, filesByAuthorMap.size());
125         Iterator it = filesByAuthorMap.entrySet().iterator();
126         assertExpectedEntry((Map.Entry) it.next(), "", new URL[] {file});
127     }
128 
129     /**
130      * Test method resolveAuthors on a file with one author.
131      */
132     public final void testResolveAuthorWithOneAuthor() throws Exception {
133         String fileName = "SampleFileWithOneJavadocAuthor.java";
134         URL file = getClass().getClassLoader().getResource(fileName);
135         assertNotNull(file);
136 
137         Map filesByAuthorMap = resolveAuthors(new File(file.getPath()).getParentFile(), fileName);
138         assertEquals(1, filesByAuthorMap.size());
139         Iterator it = filesByAuthorMap.entrySet().iterator();
140         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 1", new URL[] {file});
141     }
142 
143     /**
144      * Test method resolveAuthors on a file with three authors.
145      */
146     public final void testResolveAuthorWithThreeAuthors() throws Exception {
147         String fileName = "SampleFileWithThreeJavadocAuthors.java";
148         URL file = getClass().getClassLoader().getResource(fileName);
149         assertNotNull(file);
150 
151         Map filesByAuthorMap = resolveAuthors(new File(file.getPath()).getParentFile(), fileName);
152         assertEquals(3, filesByAuthorMap.size());
153 
154         // Results must be sorted
155         Iterator it = filesByAuthorMap.entrySet().iterator();
156         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 1", new URL[] {file});
157         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 2", new URL[] {file});
158         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 3", new URL[] {file});
159     }
160 
161     /**
162      * Test method resolveAuthors on a few files.
163      */
164     public final void testResolveAuthorOnMultipleFiles() throws Exception {
165         String fileName1 = "SampleFileWithoutJavadocAuthor.java";
166         String fileName2 = "SampleFileWithOneJavadocAuthor.java";
167         String fileName3 = "SampleFileWithThreeJavadocAuthors.java";
168 
169         URL file = getClass().getClassLoader().getResource(fileName1);
170         assertNotNull(file);
171 
172         Map filesByAuthorMap = resolveAuthors(new File(file.getPath()).getParentFile(), fileName1 + "," + fileName2 + "," + fileName3);
173         assertEquals(4, filesByAuthorMap.size());
174 
175         // Results must be sorted
176         Iterator it = filesByAuthorMap.entrySet().iterator();
177         assertExpectedEntry((Map.Entry) it.next(), "", new URL[] {getClass().getClassLoader().getResource(fileName1)});
178         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 1", new URL[] {getClass().getClassLoader().getResource(fileName2), getClass().getClassLoader().getResource(fileName3)});
179         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 2", new URL[] {getClass().getClassLoader().getResource(fileName3)});
180         assertExpectedEntry((Map.Entry) it.next(), "Sample Author 3", new URL[] {getClass().getClassLoader().getResource(fileName3)});
181     }
182 
183     private Map resolveAuthors(final File directory, final String includes)
184         throws ClassNotFoundException, InstantiationException, IllegalAccessException {
185 
186         FileSet fileSet = new FileSet();
187         fileSet.setDir(directory);
188         fileSet.setIncludes(includes);
189 
190         FileScanner fileScanner = new FileScanner();
191         fileScanner.addFileset(fileSet);
192         fileScanner.setProject(new Project());
193 
194         return FilesByAuthorProvider.resolveAuthors(fileScanner, Arrays.asList(AUTHOR_RESOLVER_CLASSNAMES));
195     }
196 
197     private void assertExpectedEntry(final Map.Entry entry, final String author, final URL[] files) throws MalformedURLException {
198         assertNotNull(files);
199 
200         // Test key
201         assertNotNull(entry);
202         assertTrue(entry.getKey() instanceof String);
203         assertEquals(author, entry.getKey());
204 
205         // Test value
206         assertNotNull(entry.getValue());
207         assertTrue(entry.getValue() instanceof Collection);
208         Collection value = (Collection) entry.getValue();
209         assertEquals(files.length, value.size());
210         Iterator it = value.iterator();
211         for (int i = 0; i < files.length; i++) {
212             assertEquals(files[i], new URL("file:/" + (String) it.next()));
213         }
214     }
215 }