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.StringWriter;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.maven.model.Contributor;
28  import org.apache.maven.model.Developer;
29  import org.apache.maven.model.License;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.PlexusTestCase;
32  import org.codehaus.plexus.i18n.I18N;
33  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
34  import org.codehaus.plexus.util.xml.XMLWriter;
35  
36  /**
37   * Test {@link DoapUtil} class.
38   *
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   */
41  public class DoapUtilTest extends PlexusTestCase {
42      /** {@inheritDoc} */
43      protected void setUp() throws Exception {
44          super.setUp();
45      }
46  
47      /** {@inheritDoc} */
48      protected void tearDown() throws Exception {
49          super.tearDown();
50      }
51  
52      /**
53       * Test method for {@link DoapUtil#writeElement(XMLWriter, String, String, String)}.
54       *
55       * @throws Exception if any
56       */
57      public void testWriteElement() throws Exception {
58          StringWriter w = new StringWriter();
59          XMLWriter writer = new PrettyPrintXMLWriter(w);
60          DoapUtil.writeElement(writer, null, "name", "value");
61          w.close();
62          assertEquals(w.toString(), "<name>value</name>");
63  
64          w = new StringWriter();
65          writer = new PrettyPrintXMLWriter(w);
66          try {
67              DoapUtil.writeElement(writer, null, null, null);
68              assertTrue("Null not catched", false);
69          } catch (IllegalArgumentException e) {
70              assertTrue("IllegalArgumentException catched", true);
71          } finally {
72              w.close();
73          }
74      }
75  
76      /**
77       * Test method for {@link DoapUtil#writeRdfResourceElement(XMLWriter, String, String, String)}.
78       *
79       * @throws Exception if any
80       */
81      public void testWriteRdfResourceElement() throws Exception {
82          StringWriter w = new StringWriter();
83          XMLWriter writer = new PrettyPrintXMLWriter(w);
84          DoapUtil.writeRdfResourceElement(writer, null, "name", "value");
85          w.close();
86          assertEquals(w.toString(), "<name " + DoapUtil.RDF_RESOURCE + "=\"value\"/>");
87  
88          w = new StringWriter();
89          writer = new PrettyPrintXMLWriter(w);
90          try {
91              DoapUtil.writeRdfResourceElement(writer, null, null, null);
92              assertTrue("Null not catched", false);
93          } catch (IllegalArgumentException e) {
94              assertTrue("IllegalArgumentException catched", true);
95          } finally {
96              w.close();
97          }
98      }
99  
100     /**
101      * Test method for:
102      * {@link DoapUtil#getContributorsWithDeveloperRole(I18N, List)}
103      * {@link DoapUtil#getContributorsWithDocumenterRole(I18N, List)}
104      * {@link DoapUtil#getContributorsWithHelperRole(I18N, List)}
105      * {@link DoapUtil#getContributorsWithMaintainerRole(I18N, List)}
106      * {@link DoapUtil#getContributorsWithTesterRole(I18N, List)}
107      * {@link DoapUtil#getContributorsWithTranslatorRole(I18N, List)}
108      * {@link DoapUtil#getContributorsWithUnknownRole(I18N, List)}
109      *
110      * @throws Exception if any
111      */
112     public void testDevelopersOrContributorsByDoapRoles() throws Exception {
113         I18N i18n = (I18N) getContainer().lookup(I18N.ROLE);
114         assertNotNull(i18n);
115         assertNotNull(i18n.getBundle());
116 
117         List<Contributor> developersOrContributors = new ArrayList<>();
118 
119         // One role
120         Developer dev = new Developer();
121         dev.setId("dev1");
122         dev.addRole("maintainer");
123 
124         developersOrContributors.add(dev);
125 
126         assertTrue(DoapUtil.getContributorsWithDeveloperRole(i18n, developersOrContributors)
127                 .isEmpty());
128         assertTrue(DoapUtil.getContributorsWithDocumenterRole(i18n, developersOrContributors)
129                 .isEmpty());
130         assertTrue(DoapUtil.getContributorsWithHelperRole(i18n, developersOrContributors)
131                 .isEmpty());
132         assertFalse(DoapUtil.getContributorsWithMaintainerRole(i18n, developersOrContributors)
133                 .isEmpty());
134         assertTrue(DoapUtil.getContributorsWithTesterRole(i18n, developersOrContributors)
135                 .isEmpty());
136         assertTrue(DoapUtil.getContributorsWithTranslatorRole(i18n, developersOrContributors)
137                 .isEmpty());
138         assertTrue(DoapUtil.getContributorsWithUnknownRole(i18n, developersOrContributors)
139                 .isEmpty());
140 
141         // Several roles
142         developersOrContributors.clear();
143 
144         dev = new Developer();
145         dev.setId("dev1");
146         dev.addRole(" MAINTAINER");
147         dev.addRole("tesTER ");
148         dev.addRole("blabla");
149         dev.addRole("translato r");
150 
151         developersOrContributors.add(dev);
152 
153         assertTrue(DoapUtil.getContributorsWithDeveloperRole(i18n, developersOrContributors)
154                 .isEmpty());
155         assertTrue(DoapUtil.getContributorsWithDocumenterRole(i18n, developersOrContributors)
156                 .isEmpty());
157         assertTrue(DoapUtil.getContributorsWithHelperRole(i18n, developersOrContributors)
158                 .isEmpty());
159         assertFalse(DoapUtil.getContributorsWithMaintainerRole(i18n, developersOrContributors)
160                 .isEmpty());
161         assertFalse(DoapUtil.getContributorsWithTesterRole(i18n, developersOrContributors)
162                 .isEmpty());
163         assertTrue(DoapUtil.getContributorsWithTranslatorRole(i18n, developersOrContributors)
164                 .isEmpty());
165         assertFalse(DoapUtil.getContributorsWithUnknownRole(i18n, developersOrContributors)
166                 .isEmpty());
167 
168         // Skip emeritus role
169         developersOrContributors.clear();
170 
171         dev = new Developer();
172         dev.setId("dev1");
173         dev.addRole("maintainer");
174         dev.addRole("unknown");
175 
176         developersOrContributors.add(dev);
177 
178         int sizeBeforeEmeritus = DoapUtil.getContributorsWithUnknownRole(i18n, developersOrContributors)
179                 .size();
180         dev.addRole(" Emeritus");
181 
182         assertTrue(DoapUtil.getContributorsWithUnknownRole(i18n, developersOrContributors)
183                         .size()
184                 == sizeBeforeEmeritus);
185     }
186 
187     /**
188      * Test method for:
189      * {@link DoapUtil#validate(java.io.File)}
190      *
191      * @throws Exception if any
192      */
193     public void testValidate() throws Exception {
194         File doapFile = new File(getBasedir(), "src/test/resources/generated-doap-1.0.rdf");
195         assertFalse(DoapUtil.validate(doapFile).isEmpty());
196     }
197 
198     /**
199      * Test method for:
200      * {@link DoapUtil#interpolate(String, MavenProject, org.apache.maven.settings.Settings)}
201      *
202      * @throws Exception if any
203      */
204     public void testInterpolate() throws Exception {
205         License license = new License();
206         license.setName("licenseName");
207         license.setUrl("licenseUrl");
208 
209         List<Developer> developers = new ArrayList<>();
210         Developer developer1 = new Developer();
211         developer1.setId("id1");
212         developer1.setName("developerName1");
213         developers.add(developer1);
214         Developer developer2 = new Developer();
215         developer2.setId("id1");
216         developer2.setName("developerName2");
217         developers.add(developer2);
218 
219         MavenProject project = new MavenProject();
220         project.setName("projectName");
221         project.setDescription("projectDescription");
222         project.setLicenses(Collections.singletonList(license));
223         project.setDevelopers(developers);
224         project.getProperties().put("myKey", "myValue");
225 
226         assertEquals(DoapUtil.interpolate("${project.name}", project, null), "projectName");
227         assertEquals(DoapUtil.interpolate("my name is ${project.name}", project, null), "my name is projectName");
228         assertEquals(
229                 DoapUtil.interpolate("my name is ${project.invalid}", project, null), "my name is ${project.invalid}");
230         assertEquals(DoapUtil.interpolate("${pom.description}", project, null), "projectDescription");
231         assertNull(DoapUtil.interpolate("${project.licenses.name}", project, null));
232         assertEquals(DoapUtil.interpolate("${project.licenses[0].name}", project, null), "licenseName");
233         assertNull(DoapUtil.interpolate("${project.licenses[1].name}", project, null));
234         assertNotNull(DoapUtil.interpolate("${project.developers}", project, null));
235         assertEquals(DoapUtil.interpolate("${project.developers[0].name}", project, null), "developerName1");
236         assertEquals(DoapUtil.interpolate("${project.developers[1].name}", project, null), "developerName2");
237         assertEquals(DoapUtil.interpolate("${myKey}", project, null), "myValue");
238     }
239 }