View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.doap;
20  
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.doap.options.DoapArtifact;
26  import org.apache.maven.plugin.doap.options.DoapOptions;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.util.IOUtil;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * Test {@link DoapMojo} class.
34   *
35   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
36   */
37  public class DoapMojoTest extends AbstractMojoTestCase {
38      @Override
39      protected void setUp() throws Exception {
40          super.setUp();
41      }
42  
43      @Override
44      protected void tearDown() throws Exception {
45          super.tearDown();
46      }
47  
48      /**
49       * Verify the generation of a pure DOAP file.
50       *
51       * @throws Exception if any
52       */
53      public void testGeneratedDoap() throws Exception {
54          File pluginXmlFile = new File(
55                  getBasedir(), "src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml");
56          DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
57          assertNotNull("Mojo found.", mojo);
58  
59          MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
60          assertNotNull(mavenProject);
61  
62          // Set some Mojo parameters
63          setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
64          setVariableValueToObject(mojo, "about", mavenProject.getUrl());
65  
66          mojo.execute();
67  
68          File doapFile = new File(getBasedir(), "target/test/unit/doap-configuration/doap-configuration.rdf");
69          assertTrue("Doap File was not generated!", doapFile.exists());
70  
71          String readed = readFile(doapFile);
72  
73          // Validate
74  
75          // Pure DOAP
76          assertTrue(readed.contains("<rdf:RDF xml:lang=\"en\" xmlns=\"http://usefulinc.com/ns/doap#\" "
77                  + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
78                  + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">"));
79          assertTrue(readed.contains("<Project rdf:about=\"" + mavenProject.getUrl() + "\">"));
80          assertTrue(readed.contains("<description xml:lang=\"en\">Test the DOAP plugin</description>"));
81          assertTrue(readed.contains("<shortdesc xml:lang=\"en\">Test the DOAP plugin</shortdesc>"));
82          assertTrue(readed.contains("<homepage rdf:resource=\"" + mavenProject.getUrl() + "\"/>"));
83          assertTrue(readed.contains("<category>library</category>"));
84          assertTrue(readed.contains("<created>2008-01-01</created>"));
85          assertTrue(readed.contains("<name>" + mavenProject.getName() + "</name>"));
86          assertTrue(readed.contains("<download-page rdf:resource=\"http://foo.org/download.html\"/>"));
87          assertTrue(readed.contains("<programming-language>Java</programming-language>"));
88          assertTrue(readed.contains("<bug-database rdf:resource=\"http://jira.codehaus.org/browse/MDOAPTEST\"/>"));
89          assertTrue(readed.contains("<license rdf:resource=\"http://www.apache.org/licenses/LICENSE-2.0.txt\"/>"));
90          assertTrue(readed.contains("<SVNRepository>"));
91          assertTrue(
92                  readed.contains(
93                          "<location rdf:resource=\"http://svn.foo.org/repos/asf/maven/plugins/trunk/maven-doap-plugin/src/test/resources/unit/doap-configuration\"/>"));
94          assertTrue(
95                  readed.contains(
96                          "<browse rdf:resource=\"http://svn.foo.org/viewvc/maven/plugins/trunk/maven-doap-plugin/src/test/resources/unit/doap-configuration\"/>"));
97          assertTrue(
98                  readed.contains(
99                          "<location rdf:resource=\"https://svn.foo.org/repos/asf/maven/plugins/trunk/maven-doap-plugin/src/test/resources/unit/doap-configuration\"/>"));
100 
101         // conf
102         assertTrue(readed.contains("<audience>developers</audience>"));
103         assertTrue(readed.contains("<blog rdf:resource=\"http://myblog.foo.org\"/>"));
104         assertTrue(readed.contains("<implements>JSR-foo</implements>"));
105         assertTrue(readed.contains("<language>en</language>"));
106         assertTrue(readed.contains("<language>fr</language>"));
107         assertTrue(readed.contains("<old-homepage rdf:resource=\"http://old.foo.org\"/>"));
108         assertTrue(readed.contains("<os>windows</os>"));
109         assertTrue(readed.contains("<os>linux</os>"));
110         assertTrue(readed.contains("<os>mac</os>"));
111         assertTrue(readed.contains("<platform>java</platform>"));
112         assertTrue(readed.contains("<platform>firefox</platform>"));
113         assertTrue(readed.contains("<screenshots rdf:resource=\"" + mavenProject.getUrl() + "/screenshots.html\"/>"));
114         assertTrue(readed.contains("<service-endpoint rdf:resource=\"http://webservice.foo.org\"/>"));
115         assertTrue(readed.contains("<wiki rdf:resource=\"http://wiki.foo.org\"/>"));
116 
117         // ASF ext
118         assertFalse(readed.contains("<asfext:pmc rdf:resource=\"" + mavenProject.getUrl() + "\"/>"));
119         assertFalse(readed.contains("<asfext:name>" + mavenProject.getName() + "</name>"));
120 
121         // Developers and Organizations
122         assertTrue(readed.contains("<maintainer>"));
123         assertTrue(readed.contains("<foaf:Person rdf:nodeID=\"b"));
124         assertTrue(readed.contains("<foaf:name>Jane Doe</foaf:name>"));
125         assertTrue(readed.contains("<foaf:Organization>"));
126         assertTrue(readed.contains("<foaf:homepage rdf:resource=\"http://www.example.org\"/>"));
127         assertTrue(readed.contains("<foaf:member rdf:nodeID=\"b"));
128     }
129 
130     /**
131      * @throws Exception if any
132      */
133     public void testLangParameter() throws Exception {
134         File pluginXmlFile = new File(
135                 getBasedir(), "src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml");
136         DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
137         assertNotNull("Mojo found.", mojo);
138 
139         MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
140         assertNotNull(mavenProject);
141 
142         // check invalid lang
143         setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
144         setVariableValueToObject(mojo, "lang", "foo");
145         try {
146             mojo.execute();
147             assertTrue("No lang checked", false);
148         } catch (Exception e) {
149             assertTrue(true);
150         }
151     }
152 
153     /**
154      * @throws Exception if any
155      */
156     public void testAboutParameter() throws Exception {
157         File pluginXmlFile = new File(
158                 getBasedir(), "src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml");
159         DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
160         assertNotNull("Mojo found.", mojo);
161 
162         MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
163         assertNotNull(mavenProject);
164 
165         // check invalid lang
166         setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
167         setVariableValueToObject(mojo, "about", "foo");
168         try {
169             mojo.execute();
170         } catch (Exception e) {
171             assertTrue(true);
172         }
173     }
174 
175     /**
176      * Verify the generation of a DOAP file from an artifact.
177      *
178      * @throws Exception if any
179      */
180     public void testGeneratedDoapArtifact() throws Exception {
181         File pluginXmlFile = new File(
182                 getBasedir(), "src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml");
183         DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
184         assertNotNull("Mojo found.", mojo);
185 
186         MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
187         assertNotNull(mavenProject);
188 
189         // Set some Mojo parameters
190         setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
191         setVariableValueToObject(mojo, "about", mavenProject.getUrl());
192         DoapOptions doapOptions = (DoapOptions) getVariableValueFromObject(mojo, "doapOptions");
193         doapOptions.setDescription("Common Utilities");
194         doapOptions.setShortdesc("Common Utilities");
195         doapOptions.setDownloadPage("http://plexus.codehaus.org/download-binaries.html");
196         setVariableValueToObject(mojo, "doapOptions", doapOptions);
197 
198         DoapArtifact artifact = new DoapArtifact();
199         artifact.setGroupId("org.codehaus.plexus");
200         artifact.setArtifactId("plexus-utils");
201         artifact.setVersion("1.5.5");
202         setVariableValueToObject(mojo, "artifact", artifact);
203         setVariableValueToObject(mojo, "outputDirectory", "target/test/unit/doap-configuration/");
204 
205         mojo.execute();
206 
207         File doapFile = new File(getBasedir(), "target/test/unit/doap-configuration/doap_plexus-utils.rdf");
208         assertTrue("Doap File was not generated!", doapFile.exists());
209 
210         String readed = readFile(doapFile);
211 
212         // Validate
213 
214         // Pure DOAP
215         assertTrue(readed.contains("<rdf:RDF xml:lang=\"en\" xmlns=\"http://usefulinc.com/ns/doap#\" "
216                 + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
217                 + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">"));
218         assertTrue(readed.contains("<Project rdf:about=\"http://plexus.codehaus.org/plexus-utils\">"));
219         assertTrue(readed.contains("<name>Plexus Common Utilities</name>"));
220         assertTrue(readed.contains("<description xml:lang=\"en\">Common Utilities</description>"));
221         assertTrue(readed.contains("<shortdesc xml:lang=\"en\">Common Utilities</shortdesc>"));
222         assertTrue(readed.contains("<created>2001-01-01</created>"));
223         assertTrue(
224                 readed.contains("<download-page rdf:resource=\"http://plexus.codehaus.org/download-binaries.html\"/>"));
225         assertTrue(readed.contains("<programming-language>Java</programming-language>"));
226         assertTrue(readed.contains("<bug-database rdf:resource=\"http://jira.codehaus.org/browse/PLXUTILS\"/>"));
227         assertTrue(readed.contains("<license rdf:resource=\"http://www.apache.org/licenses/LICENSE-2.0.txt\"/>"));
228         assertTrue(readed.contains("<SVNRepository>"));
229         assertTrue(readed.contains(
230                 "<location rdf:resource=\"http://svn.codehaus.org/plexus/plexus-utils/tags/plexus-utils-1.5.5\"/>"));
231         assertTrue(
232                 readed.contains(
233                         "<browse rdf:resource=\"http://fisheye.codehaus.org/browse/plexus/plexus-utils/tags/plexus-utils-1.5.5\"/>"));
234 
235         // conf
236         assertTrue(readed.contains("<audience>developers</audience>"));
237         assertTrue(readed.contains("<blog rdf:resource=\"http://myblog.foo.org\"/>"));
238         assertTrue(readed.contains("<implements>JSR-foo</implements>"));
239         assertTrue(readed.contains("<language>en</language>"));
240         assertTrue(readed.contains("<language>fr</language>"));
241         assertTrue(readed.contains("<old-homepage rdf:resource=\"http://old.foo.org\"/>"));
242         assertTrue(readed.contains("<os>windows</os>"));
243         assertTrue(readed.contains("<os>linux</os>"));
244         assertTrue(readed.contains("<os>mac</os>"));
245         assertTrue(readed.contains("<platform>java</platform>"));
246         assertTrue(readed.contains(
247                 "<screenshots rdf:resource=\"http://plexus.codehaus.org/plexus-utils/screenshots.html\"/>"));
248         assertTrue(readed.contains("<service-endpoint rdf:resource=\"http://webservice.foo.org\"/>"));
249         assertTrue(readed.contains("<wiki rdf:resource=\"http://wiki.foo.org\"/>"));
250     }
251 
252     /**
253      * Verify the generation of a DOAP file from a minimalist artifact.
254      *
255      * @throws Exception if any
256      */
257     public void testGeneratedDoapArtifactMinimalist() throws Exception {
258         File pluginXmlFile = new File(
259                 getBasedir(), "src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml");
260         DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
261         assertNotNull("Mojo found.", mojo);
262 
263         MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
264         assertNotNull(mavenProject);
265 
266         // Set some Mojo parameters
267         setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
268         setVariableValueToObject(mojo, "about", "foo");
269         DoapOptions doapOptions = new DoapOptions();
270         doapOptions.setName("XStream");
271         doapOptions.setDescription("XStream is a simple library to serialize objects to XML and back again.");
272         doapOptions.setShortdesc("XML Serializer");
273         doapOptions.setHomepage("http://xstream.codehaus.org/");
274         doapOptions.setDownloadPage("http://xstream.codehaus.org/download.html");
275         doapOptions.setBugDatabase("http://jira.codehaus.org/browse/XSTR");
276         doapOptions.setLicense("http://xstream.codehaus.org/license.html");
277         doapOptions.setScmDeveloper("http://svn.codehaus.org/xstream/trunk/xstream");
278         doapOptions.setMailingList("http://xstream.codehaus.org/list-user.html");
279         doapOptions.setCreated("2000-01-01");
280         setVariableValueToObject(mojo, "doapOptions", doapOptions);
281 
282         DoapArtifact artifact = new DoapArtifact();
283         artifact.setGroupId("xstream");
284         artifact.setArtifactId("xstream");
285         artifact.setVersion("1.1");
286         setVariableValueToObject(mojo, "artifact", artifact);
287         setVariableValueToObject(mojo, "outputDirectory", "target/test/unit/doap-configuration/");
288 
289         mojo.execute();
290 
291         File doapFile = new File(getBasedir(), "target/test/unit/doap-configuration/doap_xstream.rdf");
292         assertTrue("Doap File was not generated!", doapFile.exists());
293 
294         String readed = readFile(doapFile);
295 
296         // Validate
297 
298         // Pure DOAP
299         assertTrue(readed.contains("<rdf:RDF xml:lang=\"en\" xmlns=\"http://usefulinc.com/ns/doap#\" "
300                 + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
301                 + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">"));
302         assertTrue(readed.contains("<Project>"));
303         assertTrue(readed.contains("<name>XStream</name>"));
304         assertTrue(
305                 readed.contains(
306                         "<description xml:lang=\"en\">XStream is a simple library to serialize objects to XML and back again.</description>"));
307         assertTrue(readed.contains("<shortdesc xml:lang=\"en\">XML Serializer</shortdesc>"));
308         assertTrue(readed.contains("<created>2000-01-01</created>"));
309         assertTrue(readed.contains("<download-page rdf:resource=\"http://xstream.codehaus.org/download.html\"/>"));
310         assertTrue(readed.contains("<programming-language>Java</programming-language>"));
311         assertTrue(readed.contains("<bug-database rdf:resource=\"http://jira.codehaus.org/browse/XSTR\"/>"));
312         assertTrue(readed.contains("<license rdf:resource=\"http://xstream.codehaus.org/license.html\"/>"));
313         assertTrue(readed.contains("<Repository>"));
314         assertTrue(readed.contains("<location rdf:resource=\"http://svn.codehaus.org/xstream/trunk/xstream\"/>"));
315         assertTrue(readed.contains("<mailing-list rdf:resource=\"http://xstream.codehaus.org/list-user.html\"/>"));
316 
317         // conf
318         assertFalse(readed.contains("<audience>"));
319         assertFalse(readed.contains("<blog rdf:resource="));
320         assertFalse(readed.contains("<implements>"));
321         assertFalse(readed.contains("<language>"));
322         assertFalse(readed.contains("<old-homepage rdf:resource="));
323         assertFalse(readed.contains("<os>"));
324         assertFalse(readed.contains("<platform>"));
325         assertFalse(readed.contains("<screenshots rdf:resource="));
326         assertFalse(readed.contains("<service-endpoint rdf:resource="));
327         assertFalse(readed.contains("<wiki rdf:resource="));
328     }
329 
330     /**
331      * Verify the generation of a DOAP file with ASF extension.
332      *
333      * @throws Exception if any
334      */
335     public void testGeneratedDoapForASF() throws Exception {
336         File pluginXmlFile = new File(
337                 getBasedir(),
338                 "src/test/resources/unit/asf-doap-configuration/asf-doap-configuration-plugin-config.xml");
339         DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
340         assertNotNull("Mojo found.", mojo);
341 
342         MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
343         assertNotNull(mavenProject);
344 
345         // Set some Mojo parameters
346         setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
347         setVariableValueToObject(mojo, "about", mavenProject.getUrl());
348 
349         mojo.execute();
350 
351         File doapFile = new File(getBasedir(), "target/test/unit/asf-doap-configuration/asf-doap-configuration.rdf");
352         assertTrue("Doap File was not generated!", doapFile.exists());
353 
354         String readed = readFile(doapFile);
355 
356         // Validate
357 
358         // ASF DOAP
359         assertTrue(readed.contains("<rdf:RDF xml:lang=\"en\" xmlns=\"http://usefulinc.com/ns/doap#\" "
360                 + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
361                 + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\" "
362                 + "xmlns:asfext=\"http://projects.apache.org/ns/asfext#\">"));
363         if (StringUtils.isNotEmpty(mavenProject.getUrl())) {
364             assertTrue(readed.contains("<Project rdf:about=\"" + mavenProject.getUrl() + "\">"));
365             assertTrue(readed.contains("<homepage rdf:resource=\"" + mavenProject.getUrl() + "\"/>"));
366         }
367         assertTrue(readed.contains("<name>Apache " + mavenProject.getName() + "</name>"));
368         assertTrue(readed.contains("<programming-language>Java</programming-language>"));
369         assertTrue(readed.contains("<category rdf:resource=\"http://projects.apache.org/category/library\"/>"));
370 
371         // ASF ext
372         assertTrue(readed.contains("<asfext:pmc rdf:resource=\"" + mavenProject.getUrl() + "\"/>"));
373         assertTrue(readed.contains("<asfext:name>Apache " + mavenProject.getName() + "</asfext:name>"));
374         assertTrue(readed.contains("<asfext:charter>"));
375         assertTrue(readed.contains("<asfext:chair>"));
376     }
377 
378     /**
379      * Verify the generation of a DOAP file with extra extension.
380      *
381      * @throws Exception if any
382      */
383     public void testGeneratedExtraDoap() throws Exception {
384         File pluginXmlFile = new File(
385                 getBasedir(), "src/test/resources/unit/doap-configuration/doap-extra-configuration-plugin-config.xml");
386         DoapMojo mojo = (DoapMojo) lookupMojo("generate", pluginXmlFile);
387         assertNotNull("Mojo found.", mojo);
388 
389         MavenProject mavenProject = (MavenProject) getVariableValueFromObject(mojo, "project");
390         assertNotNull(mavenProject);
391 
392         // Set some Mojo parameters
393         setVariableValueToObject(mojo, "remoteRepositories", mavenProject.getRemoteArtifactRepositories());
394         setVariableValueToObject(mojo, "about", mavenProject.getUrl());
395 
396         mojo.execute();
397 
398         File doapFile = new File(getBasedir(), "target/test/unit/doap-configuration/doap-extra-configuration.rdf");
399         assertTrue("Doap File was not generated!", doapFile.exists());
400 
401         String readed = readFile(doapFile);
402 
403         assertTrue(readed.contains("<ciManagement rdf:resource=\"http://ci.foo.org\"/>"));
404         assertTrue(readed.contains("<asfext:status>active</asfext:status>"));
405         assertTrue(readed.contains("<labs:status>active</labs:status>"));
406     }
407 
408     private String readFile(File file) throws IOException {
409         try (FileReader reader = new FileReader(file)) {
410             return IOUtil.toString(reader);
411         }
412     }
413 }