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