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