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.eclipse.aether.DefaultRepositorySystemSession;
36  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
37  import org.eclipse.aether.repository.LocalRepository;
38  
39  /**
40   * Abstract class to test reports generation with <a href="http://www.httpunit.org/">HTTPUnit</a> framework.
41   *
42   * @author Edwin Punzalan
43   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
44   * @version $Id$
45   */
46  public abstract class AbstractProjectInfoTestCase extends AbstractMojoTestCase {
47      private ArtifactStubFactory artifactStubFactory;
48  
49      /**
50       * The default locale is English.
51       */
52      protected static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
53  
54      /**
55       * The current project to be test.
56       */
57      private MavenProject testMavenProject;
58  
59      /**
60       * The I18N plexus component.
61       */
62      private I18N i18n;
63  
64      @Override
65      protected void setUp() throws Exception {
66          // required for mojo lookups to work
67          super.setUp();
68  
69          i18n = getContainer().lookup(I18N.class);
70          setVariableValueToObject(i18n, "defaultBundleName", "project-info-reports");
71  
72          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
73          artifactStubFactory.getWorkingDir().mkdirs();
74  
75          // Set the default Locale
76          Locale.setDefault(DEFAULT_LOCALE);
77      }
78  
79      @Override
80      protected void tearDown() throws Exception {
81          super.tearDown();
82      }
83  
84      /**
85       * Gets a trimmed String for the given key from the resource bundle defined by Plexus.
86       *
87       * @param key the key for the desired string
88       * @return the string for the given key
89       */
90      protected String getString(String key) {
91          if (key == null || key.isEmpty()) {
92              throw new IllegalArgumentException("The key cannot be empty");
93          }
94  
95          return i18n.getString(key, Locale.getDefault()).trim();
96      }
97  
98      /**
99       * Gets a fully qualified title as generated by Doxia 1.6
100      *
101      * @param name the name to prepare
102      * @param title the title to prepare
103      * @return the prepared title as per Doxia 1.6
104      * @since 2.8
105      */
106     protected String prepareTitle(String name, String title) {
107         if (name == null || name.isEmpty()) {
108             throw new IllegalArgumentException("The name cannot be empty");
109         }
110 
111         if (title == null || title.isEmpty()) {
112             throw new IllegalArgumentException("The title cannot be empty");
113         }
114 
115         return String.format("%s \u2013 %s", name, title);
116     }
117 
118     /**
119      * Get the current Maven project
120      *
121      * @return the maven project
122      */
123     protected MavenProject getTestMavenProject() {
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) throws IOException {
135         String outputDirectory =
136                 getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
137 
138         File report = new File(outputDirectory, name);
139         if (!report.exists()) {
140             throw new IOException("File not found. Attempted: " + report);
141         }
142 
143         return report;
144     }
145 
146     /**
147      * Generate the report and return the generated file
148      *
149      * @param goal the mojo goal.
150      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
151      * @return the generated HTML file
152      * @throws Exception if any
153      */
154     protected File generateReport(String goal, String pluginXml) throws Exception {
155         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
156         AbstractProjectInfoReport mojo = createReportMojo(goal, pluginXmlFile);
157         return generateReport(mojo, pluginXmlFile);
158     }
159 
160     protected AbstractProjectInfoReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
161         AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo(goal, pluginXmlFile);
162         assertNotNull("Mojo not found.", mojo);
163 
164         LegacySupport legacySupport = lookup(LegacySupport.class);
165         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
166         DefaultRepositorySystemSession repoSession =
167                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
168         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
169                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
170 
171         setVariableValueToObject(mojo, "session", legacySupport.getSession());
172         setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
173         setVariableValueToObject(mojo, "pluginRepositories", mojo.getProject().getPluginArtifactRepositories());
174         return mojo;
175     }
176 
177     protected File generateReport(AbstractProjectInfoReport mojo, File pluginXmlFile) throws Exception {
178         mojo.execute();
179 
180         ProjectBuilder builder = lookup(ProjectBuilder.class);
181 
182         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
183         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
184 
185         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
186 
187         File outputDir = mojo.getReportOutputDirectory();
188         String filename = mojo.getOutputName() + ".html";
189 
190         return new File(outputDir, filename);
191     }
192 }