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.Locale;
24  
25  import org.apache.maven.plugin.LegacySupport;
26  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
27  import org.apache.maven.plugin.testing.ArtifactStubFactory;
28  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
29  import org.apache.maven.project.DefaultProjectBuildingRequest;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.ProjectBuilder;
32  import org.apache.maven.project.ProjectBuildingRequest;
33  import org.apache.maven.report.projectinfo.stubs.DependencyArtifactStubFactory;
34  import org.codehaus.plexus.i18n.I18N;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.eclipse.aether.DefaultRepositorySystemSession;
37  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
38  import org.eclipse.aether.repository.LocalRepository;
39  
40  /**
41   * Abstract class to test reports generation with <a href="http://www.httpunit.org/">HTTPUnit</a> framework.
42   *
43   * @author Edwin Punzalan
44   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
45   * @version $Id$
46   */
47  public abstract class AbstractProjectInfoTestCase extends AbstractMojoTestCase {
48      private ArtifactStubFactory artifactStubFactory;
49  
50      /**
51       * The default locale is English.
52       */
53      protected static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
54  
55      /**
56       * The current project to be test.
57       */
58      private MavenProject testMavenProject;
59  
60      /**
61       * The I18N plexus component.
62       */
63      private I18N i18n;
64  
65      @Override
66      protected void setUp() throws Exception {
67          // required for mojo lookups to work
68          super.setUp();
69  
70          i18n = getContainer().lookup(I18N.class);
71          setVariableValueToObject(i18n, "defaultBundleName", "project-info-reports");
72  
73          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
74          artifactStubFactory.getWorkingDir().mkdirs();
75  
76          // Set the default Locale
77          Locale.setDefault(DEFAULT_LOCALE);
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 (StringUtils.isEmpty(key)) {
93              throw new IllegalArgumentException("The key cannot be empty");
94          }
95  
96          return i18n.getString(key, Locale.getDefault()).trim();
97      }
98  
99      /**
100      * Gets a fully qualified title as generated by Doxia 1.6
101      *
102      * @param name the name to prepare
103      * @param title the title to prepare
104      * @return the prepared title as per Doxia 1.6
105      * @since 2.8
106      */
107     protected String prepareTitle(String name, String title) {
108         if (StringUtils.isEmpty(name)) {
109             throw new IllegalArgumentException("The name cannot be empty");
110         }
111 
112         if (StringUtils.isEmpty(title)) {
113             throw new IllegalArgumentException("The title cannot be empty");
114         }
115 
116         return String.format("%s \u2013 %s", name, title);
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 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         setVariableValueToObject(mojo, "session", legacySupport.getSession());
173         setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
174         setVariableValueToObject(mojo, "pluginRepositories", mojo.getProject().getPluginArtifactRepositories());
175         return mojo;
176     }
177 
178     protected File generateReport(AbstractProjectInfoReport mojo, File pluginXmlFile) throws Exception {
179         mojo.execute();
180 
181         ProjectBuilder builder = lookup(ProjectBuilder.class);
182 
183         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
184         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
185 
186         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
187 
188         File outputDir = mojo.getReportOutputDirectory();
189         String filename = mojo.getOutputName() + ".html";
190 
191         return new File(outputDir, filename);
192     }
193 }