View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import com.meterware.httpunit.HttpUnitOptions;
27  import org.apache.maven.doxia.tools.SiteTool;
28  import org.apache.maven.model.Plugin;
29  import org.apache.maven.plugin.LegacySupport;
30  import org.apache.maven.plugin.MojoExecution;
31  import org.apache.maven.plugin.descriptor.MojoDescriptor;
32  import org.apache.maven.plugin.descriptor.PluginDescriptor;
33  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
34  import org.apache.maven.plugin.testing.ArtifactStubFactory;
35  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
36  import org.apache.maven.project.DefaultProjectBuildingRequest;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.project.ProjectBuilder;
39  import org.apache.maven.project.ProjectBuildingRequest;
40  import org.apache.maven.report.projectinfo.stubs.DependencyArtifactStubFactory;
41  import org.codehaus.plexus.i18n.I18N;
42  import org.eclipse.aether.DefaultRepositorySystemSession;
43  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
44  import org.eclipse.aether.repository.LocalRepository;
45  
46  /**
47   * Abstract class to test reports generation with <a href="http://www.httpunit.org/">HTTPUnit</a> framework.
48   *
49   * @author Edwin Punzalan
50   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
51   * @version $Id$
52   */
53  public abstract class AbstractProjectInfoTestCase extends AbstractMojoTestCase {
54      private ArtifactStubFactory artifactStubFactory;
55  
56      /**
57       * The current project to be test.
58       */
59      private MavenProject testMavenProject;
60  
61      /**
62       * The I18N plexus component.
63       */
64      private I18N i18n;
65  
66      @Override
67      protected void setUp() throws Exception {
68          // required for mojo lookups to work
69          super.setUp();
70  
71          HttpUnitOptions.setScriptingEnabled(false);
72  
73          i18n = getContainer().lookup(I18N.class);
74          setVariableValueToObject(i18n, "defaultBundleName", "project-info-reports");
75  
76          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
77          artifactStubFactory.getWorkingDir().mkdirs();
78      }
79  
80      @Override
81      protected void tearDown() throws Exception {
82          super.tearDown();
83      }
84  
85      /**
86       * Gets a trimmed String for the given key from the resource bundle defined by Plexus.
87       *
88       * @param key the key for the desired string
89       * @return the string for the given key
90       */
91      protected String getString(String key) {
92          if (key == null || key.isEmpty()) {
93              throw new IllegalArgumentException("The key cannot be empty");
94          }
95  
96          return i18n.getString(key, SiteTool.DEFAULT_LOCALE).trim();
97      }
98  
99      /**
100      * Gets a fully qualified title as generated by Doxia Sitetools
101      *
102      * @param projectTitle the project title to prepare
103      * @param shortTitle the short title to prepare
104      * @return the prepared title as per Doxia Sitetools
105      * @since 2.8
106      */
107     protected String prepareTitle(String projectTitle, String shortTitle) {
108         if (projectTitle == null || projectTitle.isEmpty()) {
109             throw new IllegalArgumentException("The name cannot be empty");
110         }
111 
112         if (shortTitle == null || shortTitle.isEmpty()) {
113             throw new IllegalArgumentException("The title cannot be empty");
114         }
115 
116         return String.format("%s \u2013 %s", shortTitle, projectTitle);
117     }
118 
119     /**
120      * Get the current Maven project
121      *
122      * @return the maven project
123      */
124     protected MavenProject getTestMavenProject() {
125         return testMavenProject;
126     }
127 
128     /**
129      * Get the generated report as file in the test maven project.
130      *
131      * @param name the name of the report.
132      * @return the generated report as file
133      * @throws IOException if the return file doesnt exist
134      */
135     protected File getGeneratedReport(String name) throws IOException {
136         String outputDirectory =
137                 getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
138 
139         File report = new File(outputDirectory, name);
140         if (!report.exists()) {
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) throws Exception {
156         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
157         AbstractProjectInfoReport mojo = createReportMojo(goal, pluginXmlFile);
158         return generateReport(mojo, pluginXmlFile);
159     }
160 
161     protected AbstractProjectInfoReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
162         AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo(goal, pluginXmlFile);
163         assertNotNull("Mojo not found.", mojo);
164 
165         LegacySupport legacySupport = lookup(LegacySupport.class);
166         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
167         DefaultRepositorySystemSession repoSession =
168                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
169         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
170                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
171 
172         List<MavenProject> reactorProjects =
173                 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
174 
175         setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
176         setVariableValueToObject(mojo, "session", legacySupport.getSession());
177         setVariableValueToObject(mojo, "repoSession", legacySupport.getRepositorySession());
178         setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
179         setVariableValueToObject(
180                 mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
181         setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
182         setVariableValueToObject(mojo, "pluginRepositories", mojo.getProject().getPluginArtifactRepositories());
183         setVariableValueToObject(
184                 mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
185         return mojo;
186     }
187 
188     protected File generateReport(AbstractProjectInfoReport mojo, File pluginXmlFile) throws Exception {
189         mojo.execute();
190 
191         ProjectBuilder builder = lookup(ProjectBuilder.class);
192 
193         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
194         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
195 
196         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
197 
198         File outputDir = mojo.getReportOutputDirectory();
199         String filename = mojo.getOutputName() + ".html";
200 
201         return new File(outputDir, filename);
202     }
203 
204     private MojoExecution getMockMojoExecution() {
205         MojoDescriptor md = new MojoDescriptor();
206         md.setGoal(getGoal());
207 
208         MojoExecution me = new MojoExecution(md);
209 
210         PluginDescriptor pd = new PluginDescriptor();
211         Plugin p = new Plugin();
212         p.setGroupId("org.apache.maven.plugins");
213         p.setArtifactId("maven-project-info-reports-plugin");
214         pd.setPlugin(p);
215         md.setPluginDescriptor(pd);
216 
217         return me;
218     }
219 
220     protected abstract String getGoal();
221 }