View Javadoc
1   package org.apache.maven.report.projectinfo;
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 org.apache.maven.plugin.testing.AbstractMojoTestCase;
23  import org.apache.maven.profiles.DefaultProfileManager;
24  import org.apache.maven.profiles.ProfileManager;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.project.MavenProjectBuilder;
27  import org.codehaus.plexus.i18n.I18N;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.util.Locale;
34  
35  /**
36   * Abstract class to test reports generation with <a href="http://www.httpunit.org/">HTTPUnit</a> framework.
37   *
38   * @author Edwin Punzalan
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   * @version $Id$
41   */
42  public abstract class AbstractProjectInfoTestCase
43      extends AbstractMojoTestCase
44  {
45      /**
46       * The default locale is English.
47       */
48      protected static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
49  
50      /**
51       * The current project to be test.
52       */
53      private MavenProject testMavenProject;
54  
55      /**
56       * The I18N plexus component.
57       */
58      private I18N i18n;
59  
60      @Override
61      protected void setUp()
62          throws Exception
63      {
64          // required for mojo lookups to work
65          super.setUp();
66  
67          i18n = (I18N) getContainer().lookup( I18N.ROLE );
68  
69          File f = new File( getBasedir(), "target/local-repo/" );
70          f.mkdirs();
71  
72          // Set the default Locale
73          Locale.setDefault( DEFAULT_LOCALE );
74      }
75  
76      protected InputStream getCustomConfiguration()
77          throws Exception
78      {
79          // Allow sub classes to have their own configuration...
80          if ( super.getConfiguration() == null )
81          {
82              String className = AbstractProjectInfoTestCase.class.getName();
83  
84              String config = className.substring( className.lastIndexOf( "." ) + 1 ) + ".xml";
85  
86              return AbstractProjectInfoTestCase.class.getResourceAsStream( config );
87          }
88  
89          return null;
90      }
91  
92      @Override
93      protected void tearDown()
94          throws Exception
95      {
96          super.tearDown();
97      }
98  
99      /**
100      * Gets a trimmed String for the given key from the resource bundle defined by Plexus.
101      *
102      * @param key the key for the desired string
103      * @return the string for the given key
104      */
105     protected String getString( String key )
106     {
107         if ( StringUtils.isEmpty( key ) )
108         {
109             throw new IllegalArgumentException( "The key cannot be empty" );
110         }
111 
112         return i18n.getString( key, Locale.getDefault() ).trim();
113     }
114 
115     /**
116      * Gets a fully qualified title as generated by Doxia 1.6
117      *
118      * @param name the name to prepare
119      * @param title the title to prepare
120      * @return the prepared title as per Doxia 1.6
121      * @since 2.8
122      */
123     protected String prepareTitle( String name, String title )
124     {
125         if ( StringUtils.isEmpty( name ) )
126         {
127             throw new IllegalArgumentException( "The name cannot be empty" );
128         }
129 
130         if ( StringUtils.isEmpty( title ) )
131         {
132             throw new IllegalArgumentException( "The title cannot be empty" );
133         }
134 
135         return String.format( "%s \u2013 %s", name, title );
136     }
137 
138     /**
139      * Get the current Maven project
140      *
141      * @return the maven project
142      */
143     protected MavenProject getTestMavenProject()
144     {
145         return testMavenProject;
146     }
147 
148     /**
149      * Get the generated report as file in the test maven project.
150      *
151      * @param name the name of the report.
152      * @return the generated report as file
153      * @throws IOException if the return file doesnt exist
154      */
155     protected File getGeneratedReport( String name )
156         throws IOException
157     {
158         String outputDirectory = getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
159 
160         File report = new File( outputDirectory, name );
161         if ( !report.exists() )
162         {
163             throw new IOException( "File not found. Attempted :" + report );
164         }
165 
166         return report;
167     }
168 
169     /**
170      * Generate the report and return the generated file
171      *
172      * @param goal the mojo goal.
173      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
174      * @return the generated HTML file
175      * @throws Exception if any
176      */
177     protected File generateReport( String goal, String pluginXml )
178         throws Exception
179     {
180         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
181         AbstractProjectInfoReport mojo  = createReportMojo( goal, pluginXmlFile );
182         return generateReport( mojo, pluginXmlFile);
183     }
184 
185     protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
186         throws Exception
187     {
188         AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo( goal, pluginXmlFile );
189         assertNotNull( "Mojo found.", mojo );
190 
191         setVariableValueToObject( mojo, "remoteRepositories", mojo.project.getRemoteArtifactRepositories() );
192         return mojo;
193     }
194 
195     protected File generateReport( AbstractProjectInfoReport mojo, File pluginXmlFile )
196         throws Exception
197     {
198         mojo.execute();
199 
200         MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
201         ProfileManager profileManager = new DefaultProfileManager( getContainer(), null, null );
202 
203         assertNotNull( "Local repository", mojo.localRepository );
204         testMavenProject = builder.buildWithDependencies( pluginXmlFile, mojo.localRepository, profileManager );
205 
206         File outputDir = mojo.getReportOutputDirectory();
207         String filename = mojo.getOutputName() + ".html";
208 
209         return new File( outputDir, filename );
210     }
211 
212 
213 }