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: AbstractProjectInfoTestCase.java 1401610 2012-10-24 10:27:03Z krosenvold $
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      @Override
77      protected InputStream getCustomConfiguration()
78          throws Exception
79      {
80          // Allow sub classes to have their own configuration...
81          if ( super.getConfiguration() == null )
82          {
83              String className = AbstractProjectInfoTestCase.class.getName();
84  
85              String config = className.substring( className.lastIndexOf( "." ) + 1 ) + ".xml";
86  
87              return AbstractProjectInfoTestCase.class.getResourceAsStream( config );
88          }
89  
90          return null;
91      }
92  
93      @Override
94      protected void tearDown()
95          throws Exception
96      {
97          super.tearDown();
98      }
99  
100     /**
101      * Gets a trimmed String for the given key from the resource bundle defined by Plexus.
102      *
103      * @param key the key for the desired string
104      * @return the string for the given key
105      */
106     protected String getString( String key )
107     {
108         if ( StringUtils.isEmpty( key ) )
109         {
110             throw new IllegalArgumentException( "The key cannot be empty" );
111         }
112 
113         return i18n.getString( key, Locale.getDefault() ).trim();
114     }
115 
116     /**
117      * Get the current Maven project
118      *
119      * @return the maven project
120      */
121     protected MavenProject getTestMavenProject()
122     {
123         return testMavenProject;
124     }
125 
126     /**
127      * Get the generated report as file in the test maven project.
128      *
129      * @param name the name of the report.
130      * @return the generated report as file
131      * @throws IOException if the return file doesnt exist
132      */
133     protected File getGeneratedReport( String name )
134         throws IOException
135     {
136         String outputDirectory = getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
137 
138         File report = new File( outputDirectory, name );
139         if ( !report.exists() )
140         {
141             throw new IOException( "File not found. Attempted :" + report );
142         }
143 
144         return report;
145     }
146 
147     /**
148      * Generate the report and return the generated file
149      *
150      * @param goal the mojo goal.
151      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
152      * @return the generated HTML file
153      * @throws Exception if any
154      */
155     protected File generateReport( String goal, String pluginXml )
156         throws Exception
157     {
158         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
159         AbstractProjectInfoReport mojo  = createReportMojo( goal, pluginXmlFile );
160         return generateReport( mojo, pluginXmlFile);
161     }
162 
163     protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
164         throws Exception
165     {
166         AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo( goal, pluginXmlFile );
167         assertNotNull( "Mojo found.", mojo );
168 
169         setVariableValueToObject( mojo, "remoteRepositories", mojo.project.getRemoteArtifactRepositories() );
170         return mojo;
171     }
172 
173     protected File generateReport( AbstractProjectInfoReport mojo, File pluginXmlFile )
174         throws Exception
175     {
176         mojo.execute();
177 
178         MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
179         ProfileManager profileManager = new DefaultProfileManager( getContainer(), null, null );
180 
181         testMavenProject = builder.buildWithDependencies( pluginXmlFile, mojo.localRepository, profileManager );
182 
183         File outputDir = mojo.getReportOutputDirectory();
184         String filename = mojo.getOutputName() + ".html";
185 
186         return new File( outputDir, filename );
187     }
188 
189 
190 }