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.execution.MavenSession;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.plugin.LegacySupport;
31  import org.apache.maven.plugin.MojoExecution;
32  import org.apache.maven.plugin.descriptor.MojoDescriptor;
33  import org.apache.maven.plugin.descriptor.PluginDescriptor;
34  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35  import org.apache.maven.plugin.testing.ArtifactStubFactory;
36  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
37  import org.apache.maven.project.DefaultProjectBuildingRequest;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.project.ProjectBuilder;
40  import org.apache.maven.project.ProjectBuildingRequest;
41  import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils;
42  import org.apache.maven.report.projectinfo.stubs.DependencyArtifactStubFactory;
43  import org.codehaus.plexus.i18n.I18N;
44  import org.eclipse.aether.DefaultRepositorySystemSession;
45  import org.eclipse.aether.RepositorySystem;
46  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
47  import org.eclipse.aether.repository.LocalRepository;
48  
49  /**
50   * Abstract class to test reports generation with <a href="http://www.httpunit.org/">HTTPUnit</a> framework.
51   *
52   * @author Edwin Punzalan
53   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
54   * @version $Id$
55   */
56  public abstract class AbstractProjectInfoTestCase extends AbstractMojoTestCase {
57      private ArtifactStubFactory artifactStubFactory;
58  
59      /**
60       * The current project to be test.
61       */
62      private MavenProject testMavenProject;
63  
64      /**
65       * The I18N plexus component.
66       */
67      private I18N i18n;
68  
69      @Override
70      protected void setUp() throws Exception {
71          // required for mojo lookups to work
72          super.setUp();
73  
74          HttpUnitOptions.setScriptingEnabled(false);
75  
76          i18n = getContainer().lookup(I18N.class);
77          setVariableValueToObject(i18n, "defaultBundleName", "project-info-reports");
78  
79          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
80          artifactStubFactory.getWorkingDir().mkdirs();
81      }
82  
83      @Override
84      protected void tearDown() throws Exception {
85          super.tearDown();
86      }
87  
88      /**
89       * Gets a trimmed String for the given key from the resource bundle defined by Plexus.
90       *
91       * @param key the key for the desired string
92       * @return the string for the given key
93       */
94      protected String getString(String key) {
95          if (key == null || key.isEmpty()) {
96              throw new IllegalArgumentException("The key cannot be empty");
97          }
98  
99          return i18n.getString(key, SiteTool.DEFAULT_LOCALE).trim();
100     }
101 
102     /**
103      * Gets a fully qualified title as generated by Doxia Sitetools
104      *
105      * @param projectTitle the project title to prepare
106      * @param shortTitle the short title to prepare
107      * @return the prepared title as per Doxia Sitetools
108      * @since 2.8
109      */
110     protected String prepareTitle(String projectTitle, String shortTitle) {
111         if (projectTitle == null || projectTitle.isEmpty()) {
112             throw new IllegalArgumentException("The name cannot be empty");
113         }
114 
115         if (shortTitle == null || shortTitle.isEmpty()) {
116             throw new IllegalArgumentException("The title cannot be empty");
117         }
118 
119         return String.format("%s \u2013 %s", shortTitle, projectTitle);
120     }
121 
122     /**
123      * Get the current Maven project
124      *
125      * @return the maven project
126      */
127     protected MavenProject getTestMavenProject() {
128         return testMavenProject;
129     }
130 
131     /**
132      * Get the generated report as file in the test maven project.
133      *
134      * @param name the name of the report.
135      * @return the generated report as file
136      * @throws IOException if the return file doesnt exist
137      */
138     protected File getGeneratedReport(String name) throws IOException {
139         String outputDirectory =
140                 getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
141 
142         File report = new File(outputDirectory, name);
143         if (!report.exists()) {
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 the mojo goal.
154      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
155      * @return the generated HTML file
156      * @throws Exception if any
157      */
158     protected File generateReport(String goal, String pluginXml) throws Exception {
159         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
160         AbstractProjectInfoReport mojo = createReportMojo(goal, pluginXmlFile);
161         return generateReport(mojo, pluginXmlFile);
162     }
163 
164     protected AbstractProjectInfoReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
165         MavenSession mavenSession = newMavenSession(new MavenProjectStub());
166 
167         getContainer()
168                 .addComponent(
169                         new RepositoryUtils(
170                                 lookup(ProjectBuilder.class), lookup(RepositorySystem.class), () -> mavenSession),
171                         RepositoryUtils.class,
172                         null);
173 
174         AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo(goal, pluginXmlFile);
175         assertNotNull("Mojo not found.", mojo);
176 
177         LegacySupport legacySupport = lookup(LegacySupport.class);
178         legacySupport.setSession(mavenSession);
179 
180         DefaultRepositorySystemSession repoSession =
181                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
182         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
183                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
184 
185         List<MavenProject> reactorProjects =
186                 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
187 
188         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
189         setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
190         setVariableValueToObject(mojo, "session", legacySupport.getSession());
191         setVariableValueToObject(mojo, "repoSession", legacySupport.getRepositorySession());
192         setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
193         setVariableValueToObject(mojo, "remoteProjectRepositories", project.getRemoteProjectRepositories());
194         setVariableValueToObject(mojo, "remoteRepositories", project.getRemoteArtifactRepositories());
195         setVariableValueToObject(mojo, "pluginRepositories", project.getPluginArtifactRepositories());
196         setVariableValueToObject(mojo, "siteDirectory", new File(project.getBasedir(), "src/site"));
197         return mojo;
198     }
199 
200     protected File generateReport(AbstractProjectInfoReport mojo, File pluginXmlFile) throws Exception {
201         mojo.execute();
202 
203         ProjectBuilder builder = lookup(ProjectBuilder.class);
204 
205         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
206         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
207 
208         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
209 
210         File outputDir = mojo.getReportOutputDirectory();
211         String filename = mojo.getOutputPath() + ".html";
212 
213         return new File(outputDir, filename);
214     }
215 
216     private MojoExecution getMockMojoExecution() {
217         MojoDescriptor md = new MojoDescriptor();
218         md.setGoal(getGoal());
219 
220         MojoExecution me = new MojoExecution(md);
221 
222         PluginDescriptor pd = new PluginDescriptor();
223         Plugin p = new Plugin();
224         p.setGroupId("org.apache.maven.plugins");
225         p.setArtifactId("maven-project-info-reports-plugin");
226         pd.setPlugin(p);
227         md.setPluginDescriptor(pd);
228 
229         return me;
230     }
231 
232     protected abstract String getGoal();
233 }