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