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.FileReader;
24  import java.io.IOException;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
30  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
31  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
32  import org.apache.maven.project.MavenProject;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  /**
37   * Test {@link DoapMojo} class.
38   *
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   * @version $Id: DoapMojoTest.html 815332 2012-05-01 21:25:03Z hboutemy $
41   */
42  public class DoapMojoTest
43      extends AbstractMojoTestCase
44  {
45      /** {@inheritDoc} */
46      protected void setUp()
47          throws Exception
48      {
49          super.setUp();
50      }
51  
52      /** {@inheritDoc} */
53      protected void tearDown()
54          throws Exception
55      {
56          super.tearDown();
57      }
58  
59      /**
60       * Verify the generation of a pure DOAP file.
61       *
62       * @throws Exception if any
63       */
64      public void testGeneratedDoap()
65          throws Exception
66      {
67          File pluginXmlFile =
68              new File( getBasedir(),
69                        "src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml" );
70          DoapMojo mojo = (DoapMojo) lookupMojo( "generate", pluginXmlFile );
71          assertNotNull( "Mojo found.", mojo );
72  
73          MavenProject mavenProject = (MavenProject) getVariableValueFromObject( mojo, "project" );
74          assertNotNull( mavenProject );
75  
76          // Set some Mojo parameters
77          setVariableValueToObject( mojo, "remoteRepositories", getRemoteRepositories() );
78          setVariableValueToObject( mojo, "about", mavenProject.getUrl() );
79  
80          mojo.execute();
81  
82          File doapFile = new File( getBasedir(), "target/test/unit/doap-configuration/doap-configuration.rdf" );
83          assertTrue( "Doap File was not generated!", doapFile.exists() );
84  
85          String readed = readFile( doapFile );
86  
87          // Validate
88  
89          // Pure DOAP
90          assertTrue( readed.indexOf( "<rdf:RDF xml:lang=\"en\" xmlns=\"http://usefulinc.com/ns/doap#\" "
91              + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
92              + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">" ) != -1 );
93          if ( StringUtils.isNotEmpty( mavenProject.getUrl() ) )
94          {
95              assertTrue( readed.indexOf( "<Project rdf:about=\"" + mavenProject.getUrl() + "\">" ) != -1 );
96              assertTrue( readed.indexOf( "<homepage rdf:resource=\"" + mavenProject.getUrl() + "\"/>" ) != -1 );
97          }
98          assertTrue( readed.indexOf( "<name rdf:resource=\"" + mavenProject.getName() + "\"/>" ) != -1 );
99          assertTrue( readed.indexOf( "<programming-language rdf:resource=\"java\"/>" ) != -1 );
100 
101         // ASF ext
102         assertFalse( readed.indexOf( "<asfext:pmc rdf:resource=\"" + mavenProject.getUrl() + "\"/>" ) != -1 );
103         assertFalse( readed.indexOf( "<asfext:name rdf:resource=\"" + mavenProject.getName() + "\"/>" ) != -1 );
104     }
105 
106     /**
107      * Verify the generation of a DOAP file with ASF extension.
108      *
109      * @throws Exception if any
110      */
111     public void testGeneratedDoapForASF()
112         throws Exception
113     {
114         File pluginXmlFile =
115             new File( getBasedir(),
116                       "src/test/resources/unit/asf-doap-configuration/asf-doap-configuration-plugin-config.xml" );
117         DoapMojo mojo = (DoapMojo) lookupMojo( "generate", pluginXmlFile );
118         assertNotNull( "Mojo found.", mojo );
119 
120         MavenProject mavenProject = (MavenProject) getVariableValueFromObject( mojo, "project" );
121         assertNotNull( mavenProject );
122 
123         // Set some Mojo parameters
124         setVariableValueToObject( mojo, "remoteRepositories", getRemoteRepositories() );
125         setVariableValueToObject( mojo, "about", mavenProject.getUrl() );
126 
127         mojo.execute();
128 
129         File doapFile =
130             new File( getBasedir(), "target/test/unit/asf-doap-configuration/asf-doap-configuration.rdf" );
131         assertTrue( "Doap File was not generated!", doapFile.exists() );
132 
133         String readed = readFile( doapFile );
134 
135         // Validate
136 
137         // Pure DOAP
138         assertTrue( readed.indexOf( "<rdf:RDF xml:lang=\"en\" xmlns=\"http://usefulinc.com/ns/doap#\" "
139             + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
140             + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\" "
141             + "xmlns:asfext=\"http://projects.apache.org/ns/asfext#\">" ) != -1 );
142         if ( StringUtils.isNotEmpty( mavenProject.getUrl() ) )
143         {
144             assertTrue( readed.indexOf( "<Project rdf:about=\"" + mavenProject.getUrl() + "\">" ) != -1 );
145             assertTrue( readed.indexOf( "<homepage rdf:resource=\"" + mavenProject.getUrl() + "\"/>" ) != -1 );
146         }
147         assertTrue( readed.indexOf( "<name rdf:resource=\"Apache " + mavenProject.getName() + "\"/>" ) != -1 );
148         assertTrue( readed.indexOf( "<programming-language rdf:resource=\"java\"/>" ) != -1 );
149 
150         // ASF ext
151         assertTrue( readed.indexOf( "<asfext:pmc rdf:resource=\"" + mavenProject.getUrl() + "\"/>" ) != -1 );
152         assertTrue( readed.indexOf( "<asfext:name rdf:resource=\"Apache " + mavenProject.getName() + "\"/>" ) != -1 );
153     }
154 
155     /**
156      * @param file
157      * @return
158      * @throws IOException if any
159      */
160     private String readFile( File file )
161         throws IOException
162     {
163         String result = null;
164 
165         FileReader reader = null;
166         try
167         {
168             // platform encoding
169             reader = new FileReader( file );
170 
171             result = IOUtil.toString( reader );
172         }
173         finally
174         {
175             IOUtil.close( reader );
176         }
177 
178         return result;
179     }
180 
181     /**
182      * @return remote repo
183      */
184     private static List getRemoteRepositories()
185     {
186         ArtifactRepository repository =
187             new DefaultArtifactRepository( "central", "http://repo1.maven.org/maven2",
188                                            new DefaultRepositoryLayout() );
189         return Collections.singletonList( repository );
190     }
191 }