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.plugin.jxr;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.maven.model.Plugin;
28  import org.apache.maven.plugin.LegacySupport;
29  import org.apache.maven.plugin.MojoExecution;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
33  import org.apache.maven.plugin.testing.ArtifactStubFactory;
34  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
35  import org.apache.maven.project.DefaultProjectBuildingRequest;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.ProjectBuilder;
38  import org.apache.maven.project.ProjectBuildingRequest;
39  import org.eclipse.aether.DefaultRepositorySystemSession;
40  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
41  import org.eclipse.aether.repository.LocalRepository;
42  
43  /**
44   * Abstract class to test reports generation.
45   */
46  public abstract class AbstractJxrTestCase extends AbstractMojoTestCase {
47      private ArtifactStubFactory artifactStubFactory;
48  
49      /**
50       * The current project to be test.
51       */
52      private MavenProject testMavenProject;
53  
54      @Override
55      protected void setUp() throws Exception {
56          // required for mojo lookups to work
57          super.setUp();
58  
59          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
60          artifactStubFactory.getWorkingDir().mkdirs();
61      }
62  
63      @Override
64      protected void tearDown() throws Exception {
65          super.tearDown();
66      }
67  
68      /**
69       * Gets the current Maven project.
70       *
71       * @return the maven project
72       */
73      protected MavenProject getTestMavenProject() {
74          return testMavenProject;
75      }
76  
77      /**
78       * Gets the generated report as file in the test maven project.
79       *
80       * @param name the name of the report.
81       * @return the generated report as file
82       * @throws IOException if the return file doesnt exist
83       */
84      protected File getGeneratedReport(String name) throws IOException {
85          String outputDirectory =
86                  getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();
87  
88          File report = new File(outputDirectory, name);
89          if (!report.exists()) {
90              throw new IOException("File not found. Attempted: " + report);
91          }
92  
93          return report;
94      }
95  
96      /**
97       * Generate the report and return the generated file
98       *
99       * @param goal the mojo goal.
100      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
101      * @return the generated HTML file
102      * @throws Exception if any
103      */
104     protected File generateReport(String goal, String pluginXml) throws Exception {
105         File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
106         AbstractJxrReport mojo = createReportMojo(goal, pluginXmlFile);
107         return generateReport(mojo, pluginXmlFile);
108     }
109 
110     protected AbstractJxrReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
111         AbstractJxrReport mojo = (AbstractJxrReport) lookupMojo(goal, pluginXmlFile);
112         assertNotNull("Mojo not found.", mojo);
113 
114         LegacySupport legacySupport = lookup(LegacySupport.class);
115         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
116         DefaultRepositorySystemSession repoSession =
117                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
118         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
119                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
120 
121         List<MavenProject> reactorProjects =
122                 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
123 
124         setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
125         setVariableValueToObject(mojo, "session", legacySupport.getSession());
126         setVariableValueToObject(mojo, "repoSession", legacySupport.getRepositorySession());
127         setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
128         setVariableValueToObject(
129                 mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
130         setVariableValueToObject(
131                 mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
132         return mojo;
133     }
134 
135     protected File generateReport(AbstractJxrReport mojo, File pluginXmlFile) throws Exception {
136         mojo.execute();
137 
138         ProjectBuilder builder = lookup(ProjectBuilder.class);
139 
140         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
141         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
142 
143         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
144 
145         File outputDir = mojo.getReportOutputDirectory();
146         String filename = mojo.getOutputName() + ".html";
147 
148         return new File(outputDir, filename);
149     }
150 
151     /**
152      * Read the contents of the specified file object into a string
153      */
154     protected String readFile(File xrefTestDir, String fileName) throws IOException {
155         return new String(Files.readAllBytes(xrefTestDir.toPath().resolve(fileName)));
156     }
157 
158     private MojoExecution getMockMojoExecution() {
159         MojoDescriptor md = new MojoDescriptor();
160         md.setGoal(getGoal());
161 
162         MojoExecution me = new MojoExecution(md);
163 
164         PluginDescriptor pd = new PluginDescriptor();
165         Plugin p = new Plugin();
166         p.setGroupId("org.apache.maven.plugins");
167         p.setArtifactId("maven-jxr-plugin");
168         pd.setPlugin(p);
169         md.setPluginDescriptor(pd);
170 
171         return me;
172     }
173 
174     protected abstract String getGoal();
175 }