View Javadoc
1   package org.apache.maven.plugin.doap;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.StringWriter;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.maven.model.Contributor;
29  import org.apache.maven.model.Developer;
30  import org.apache.maven.model.License;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.codehaus.plexus.i18n.I18N;
34  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35  import org.codehaus.plexus.util.xml.XMLWriter;
36  
37  /**
38   * Test {@link DoapUtil} class.
39   *
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   * @version $Id: DoapUtilTest.java 1058121 2011-01-12 13:12:38Z vsiveton $
42   */
43  public class DoapUtilTest
44      extends PlexusTestCase
45  {
46      /** {@inheritDoc} */
47      protected void setUp()
48          throws Exception
49      {
50          super.setUp();
51      }
52  
53      /** {@inheritDoc} */
54      protected void tearDown()
55          throws Exception
56      {
57          super.tearDown();
58      }
59  
60      /**
61       * Test method for {@link DoapUtil#writeElement(XMLWriter, String, String, String)}.
62       *
63       * @throws Exception if any
64       */
65      public void testWriteElement()
66          throws Exception
67      {
68          StringWriter w = new StringWriter();
69          XMLWriter writer = new PrettyPrintXMLWriter( w );
70          DoapUtil.writeElement( writer, null, "name", "value" );
71          w.close();
72          assertEquals( w.toString(), "<name>value</name>" );
73  
74          w = new StringWriter();
75          writer = new PrettyPrintXMLWriter( w );
76          try
77          {
78              DoapUtil.writeElement( writer, null, null, null );
79              assertTrue( "Null not catched", false );
80          }
81          catch ( IllegalArgumentException e )
82          {
83              assertTrue( "IllegalArgumentException catched", true );
84          }
85          finally
86          {
87              w.close();
88          }
89      }
90  
91      /**
92       * Test method for {@link DoapUtil#writeRdfResourceElement(XMLWriter, String, String)}.
93       *
94       * @throws Exception if any
95       */
96      public void testWriteRdfResourceElement()
97          throws Exception
98      {
99          StringWriter w = new StringWriter();
100         XMLWriter writer = new PrettyPrintXMLWriter( w );
101         DoapUtil.writeRdfResourceElement( writer, null, "name", "value" );
102         w.close();
103         assertEquals( w.toString(), "<name " + DoapUtil.RDF_RESOURCE + "=\"value\"/>" );
104 
105         w = new StringWriter();
106         writer = new PrettyPrintXMLWriter( w );
107         try
108         {
109             DoapUtil.writeRdfResourceElement( writer, null, null, null );
110             assertTrue( "Null not catched", false );
111         }
112         catch ( IllegalArgumentException e )
113         {
114             assertTrue( "IllegalArgumentException catched", true );
115         }
116         finally
117         {
118             w.close();
119         }
120     }
121 
122     /**
123      * Test method for:
124      * {@link DoapUtil#getContributorsWithDeveloperRole(I18N, List)}
125      * {@link DoapUtil#getContributorsWithDocumenterRole(I18N, List)}
126      * {@link DoapUtil#getContributorsWithHelperRole(I18N, List)}
127      * {@link DoapUtil#getContributorsWithMaintainerRole(I18N, List)}
128      * {@link DoapUtil#getContributorsWithTesterRole(I18N, List)}
129      * {@link DoapUtil#getContributorsWithTranslatorRole(I18N, List)}
130      * {@link DoapUtil#getContributorsWithUnknownRole(I18N, List)}
131      *
132      * @throws Exception if any
133      */
134     public void testDevelopersOrContributorsByDoapRoles()
135         throws Exception
136     {
137         I18N i18n = (I18N) getContainer().lookup( I18N.ROLE );
138         assertNotNull( i18n );
139         assertNotNull( i18n.getBundle() );
140 
141         List<Contributor> developersOrContributors = new ArrayList<Contributor>();
142 
143         // One role
144         Developer dev = new Developer();
145         dev.setId( "dev1" );
146         dev.addRole( "maintainer" );
147 
148         developersOrContributors.add( dev );
149 
150         assertTrue( DoapUtil.getContributorsWithDeveloperRole( i18n, developersOrContributors ).isEmpty() );
151         assertTrue( DoapUtil.getContributorsWithDocumenterRole( i18n, developersOrContributors ).isEmpty() );
152         assertTrue( DoapUtil.getContributorsWithHelperRole( i18n, developersOrContributors ).isEmpty() );
153         assertFalse( DoapUtil.getContributorsWithMaintainerRole( i18n, developersOrContributors ).isEmpty() );
154         assertTrue( DoapUtil.getContributorsWithTesterRole( i18n, developersOrContributors ).isEmpty() );
155         assertTrue( DoapUtil.getContributorsWithTranslatorRole( i18n, developersOrContributors ).isEmpty() );
156         assertTrue( DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors ).isEmpty() );
157 
158         // Several roles
159         developersOrContributors.clear();
160 
161         dev = new Developer();
162         dev.setId( "dev1" );
163         dev.addRole( " MAINTAINER" );
164         dev.addRole( "tesTER " );
165         dev.addRole( "blabla" );
166         dev.addRole( "translato r" );
167 
168         developersOrContributors.add( dev );
169 
170         assertTrue( DoapUtil.getContributorsWithDeveloperRole( i18n, developersOrContributors ).isEmpty() );
171         assertTrue( DoapUtil.getContributorsWithDocumenterRole( i18n, developersOrContributors ).isEmpty() );
172         assertTrue( DoapUtil.getContributorsWithHelperRole( i18n, developersOrContributors ).isEmpty() );
173         assertFalse( DoapUtil.getContributorsWithMaintainerRole( i18n, developersOrContributors ).isEmpty() );
174         assertFalse( DoapUtil.getContributorsWithTesterRole( i18n, developersOrContributors ).isEmpty() );
175         assertTrue( DoapUtil.getContributorsWithTranslatorRole( i18n, developersOrContributors ).isEmpty() );
176         assertFalse( DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors ).isEmpty() );
177 
178         // Skip emeritus role
179         developersOrContributors.clear();
180 
181         dev = new Developer();
182         dev.setId( "dev1" );
183         dev.addRole( "maintainer" );
184         dev.addRole( "unknown" );
185 
186         developersOrContributors.add( dev );
187 
188         int sizeBeforeEmeritus = DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors).size();
189         dev.addRole( " Emeritus" );
190 
191         assertTrue( DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors).size() == sizeBeforeEmeritus );
192 
193     }
194 
195     /**
196      * Test method for:
197      * {@link DoapUtil#validate(java.io.File)}
198      *
199      * @throws Exception if any
200      */
201     public void testValidate()
202         throws Exception
203     {
204         File doapFile = new File( getBasedir(), "src/test/resources/generated-doap-1.0.rdf" );
205         assertFalse( DoapUtil.validate( doapFile ).isEmpty() );
206     }
207 
208     /**
209      * Test method for:
210      * {@link DoapUtil#interpolate(String, MavenProject, org.apache.maven.settings.Settings)}
211      *
212      * @throws Exception if any
213      */
214     public void testInterpolate()
215         throws Exception
216     {
217         License license = new License();
218         license.setName( "licenseName" );
219         license.setUrl( "licenseUrl" );
220 
221         List<Developer> developers = new ArrayList<Developer>();
222         Developer developer1 = new Developer();
223         developer1.setId( "id1" );
224         developer1.setName( "developerName1" );
225         developers.add( developer1 );
226         Developer developer2 = new Developer();
227         developer2.setId( "id1" );
228         developer2.setName( "developerName2" );
229         developers.add( developer2 );
230 
231         MavenProject project = new MavenProject();
232         project.setName( "projectName" );
233         project.setDescription( "projectDescription" );
234         project.setLicenses( Collections.singletonList( license ) );
235         project.setDevelopers( developers );
236         project.getProperties().put( "myKey", "myValue" );
237 
238         assertEquals( DoapUtil.interpolate( "${project.name}", project, null ), "projectName" );
239         assertEquals( DoapUtil.interpolate( "my name is ${project.name}", project, null ), "my name is projectName" );
240         assertEquals( DoapUtil.interpolate( "my name is ${project.invalid}", project, null ), "my name is ${project.invalid}" );
241         assertEquals( DoapUtil.interpolate( "${pom.description}", project, null ), "projectDescription" );
242         assertNull( DoapUtil.interpolate( "${project.licenses.name}", project, null ) );
243         assertEquals( DoapUtil.interpolate( "${project.licenses[0].name}", project, null ), "licenseName" );
244         assertNull( DoapUtil.interpolate( "${project.licenses[1].name}", project, null ) );
245         assertNotNull( DoapUtil.interpolate( "${project.developers}", project, null ) );
246         assertEquals( DoapUtil.interpolate( "${project.developers[0].name}", project, null ), "developerName1" );
247         assertEquals( DoapUtil.interpolate( "${project.developers[1].name}", project, null ), "developerName2" );
248         assertEquals( DoapUtil.interpolate( "${myKey}", project, null ), "myValue" );
249     }
250 }