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.StringWriter;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.model.Developer;
27  import org.codehaus.plexus.PlexusTestCase;
28  import org.codehaus.plexus.i18n.I18N;
29  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
30  import org.codehaus.plexus.util.xml.XMLWriter;
31  
32  /**
33   * Test {@link DoapUtil} class.
34   *
35   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
36   * @version $Id: DoapUtilTest.html 815332 2012-05-01 21:25:03Z hboutemy $
37   */
38  public class DoapUtilTest
39      extends PlexusTestCase
40  {
41      /** {@inheritDoc} */
42      protected void setUp()
43          throws Exception
44      {
45          super.setUp();
46      }
47  
48      /** {@inheritDoc} */
49      protected void tearDown()
50          throws Exception
51      {
52          super.tearDown();
53      }
54  
55      /**
56       * Test method for {@link DoapUtil#writeElement(XMLWriter, String, String, String).
57       *
58       * @throws Exception if any
59       */
60      public void testWriteElementXMLWriterStringStringString()
61          throws Exception
62      {
63          StringWriter w = new StringWriter();
64          XMLWriter writer = new PrettyPrintXMLWriter( w );
65          DoapUtil.writeElement( writer, "name", "value" );
66          w.close();
67          assertEquals( w.toString(), "<name>value</name>" );
68  
69          w = new StringWriter();
70          writer = new PrettyPrintXMLWriter( w );
71          try
72          {
73              DoapUtil.writeElement( writer, null, null );
74              assertTrue( "Null not catched", false );
75          }
76          catch ( IllegalArgumentException e )
77          {
78              assertTrue( "IllegalArgumentException catched", true );
79          }
80          finally
81          {
82              w.close();
83          }
84      }
85  
86      /**
87       * Test method for {@link DoapUtil#writeRdfResourceElement(XMLWriter, String, String).
88       *
89       * @throws Exception if any
90       */
91      public void testWriteRdfResourceElement()
92          throws Exception
93      {
94          StringWriter w = new StringWriter();
95          XMLWriter writer = new PrettyPrintXMLWriter( w );
96          DoapUtil.writeRdfResourceElement( writer, "name", "value" );
97          w.close();
98          assertEquals( w.toString(), "<name " + DoapUtil.RDF_RESOURCE + "=\"value\"/>" );
99  
100         w = new StringWriter();
101         writer = new PrettyPrintXMLWriter( w );
102         try
103         {
104             DoapUtil.writeRdfResourceElement( writer, null, null );
105             assertTrue( "Null not catched", false );
106         }
107         catch ( IllegalArgumentException e )
108         {
109             assertTrue( "IllegalArgumentException catched", true );
110         }
111         finally
112         {
113             w.close();
114         }
115     }
116 
117     /**
118      * Test method for:
119      * {@link DoapUtil#getDevelopersOrContributorsWithDeveloperRole(I18N, List)}
120      * {@link DoapUtil#getDevelopersOrContributorsWithDocumenterRole(I18N, List)}
121      * {@link DoapUtil#getDevelopersOrContributorsWithHelperRole(I18N, List)}
122      * {@link DoapUtil#getDevelopersOrContributorsWithMaintainerRole(I18N, List)}
123      * {@link DoapUtil#getDevelopersOrContributorsWithTesterRole(I18N, List)}
124      * {@link DoapUtil#getDevelopersOrContributorsWithTranslatorRole(I18N, List)}
125      * {@link DoapUtil#getDevelopersOrContributorsWithUnknownRole(I18N, List)}
126      *
127      * @throws Exception if any
128      */
129     public void testDevelopersOrContributorsByDoapRoles()
130         throws Exception
131     {
132         I18N i18n = (I18N) getContainer().lookup( I18N.ROLE );
133         assertNotNull( i18n );
134         assertNotNull( i18n.getBundle() );
135 
136         List developersOrContributors = new ArrayList();
137 
138         // One role
139         Developer dev = new Developer();
140         dev.setId( "dev1" );
141         dev.addRole( "maintainer" );
142 
143         developersOrContributors.add( dev );
144 
145         assertTrue( DoapUtil.getDevelopersOrContributorsWithDeveloperRole( i18n, developersOrContributors ).isEmpty() );
146         assertTrue( DoapUtil.getDevelopersOrContributorsWithDocumenterRole( i18n, developersOrContributors ).isEmpty() );
147         assertTrue( DoapUtil.getDevelopersOrContributorsWithHelperRole( i18n, developersOrContributors ).isEmpty() );
148         assertFalse( DoapUtil.getDevelopersOrContributorsWithMaintainerRole( i18n, developersOrContributors ).isEmpty() );
149         assertTrue( DoapUtil.getDevelopersOrContributorsWithTesterRole( i18n, developersOrContributors ).isEmpty() );
150         assertTrue( DoapUtil.getDevelopersOrContributorsWithTranslatorRole( i18n, developersOrContributors ).isEmpty() );
151         assertTrue( DoapUtil.getDevelopersOrContributorsWithUnknownRole( i18n, developersOrContributors ).isEmpty() );
152 
153         // Several roles
154         developersOrContributors.clear();
155 
156         dev = new Developer();
157         dev.setId( "dev1" );
158         dev.addRole( " MAINTAINER" );
159         dev.addRole( "tesTER " );
160         dev.addRole( "blabla" );
161         dev.addRole( "translato r" );
162 
163         developersOrContributors.add( dev );
164 
165         assertTrue( DoapUtil.getDevelopersOrContributorsWithDeveloperRole( i18n, developersOrContributors ).isEmpty() );
166         assertTrue( DoapUtil.getDevelopersOrContributorsWithDocumenterRole( i18n, developersOrContributors ).isEmpty() );
167         assertTrue( DoapUtil.getDevelopersOrContributorsWithHelperRole( i18n, developersOrContributors ).isEmpty() );
168         assertFalse( DoapUtil.getDevelopersOrContributorsWithMaintainerRole( i18n, developersOrContributors ).isEmpty() );
169         assertFalse( DoapUtil.getDevelopersOrContributorsWithTesterRole( i18n, developersOrContributors ).isEmpty() );
170         assertTrue( DoapUtil.getDevelopersOrContributorsWithTranslatorRole( i18n, developersOrContributors ).isEmpty() );
171         assertFalse( DoapUtil.getDevelopersOrContributorsWithUnknownRole( i18n, developersOrContributors ).isEmpty() );
172     }
173 }