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  
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.eclipse.aether.DefaultRepositorySystemSession;
34  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
35  import org.eclipse.aether.repository.LocalRepository;
36  
37  /**
38   * Abstract class to test reports generation.
39   */
40  public abstract class AbstractJxrTestCase extends AbstractMojoTestCase {
41      private ArtifactStubFactory artifactStubFactory;
42  
43      /**
44       * The current project to be test.
45       */
46      private MavenProject testMavenProject;
47  
48      @Override
49      protected void setUp() throws Exception {
50          // required for mojo lookups to work
51          super.setUp();
52  
53          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
54          artifactStubFactory.getWorkingDir().mkdirs();
55      }
56  
57      @Override
58      protected void tearDown() throws Exception {
59          super.tearDown();
60      }
61  
62      /**
63       * Gets the current Maven project.
64       *
65       * @return the maven project
66       */
67      protected MavenProject getTestMavenProject() {
68          return testMavenProject;
69      }
70  
71      /**
72       * Gets the generated report as file in the test maven project.
73       *
74       * @param name the name of the report.
75       * @return the generated report as file
76       * @throws IOException if the return file doesnt exist
77       */
78      protected File getGeneratedReport(String name) throws IOException {
79          String outputDirectory =
80                  getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();
81  
82          File report = new File(outputDirectory, name);
83          if (!report.exists()) {
84              throw new IOException("File not found. Attempted: " + report);
85          }
86  
87          return report;
88      }
89  
90      /**
91       * Generate the report and return the generated file
92       *
93       * @param goal the mojo goal.
94       * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
95       * @return the generated HTML file
96       * @throws Exception if any
97       */
98      protected File generateReport(String goal, String pluginXml) throws Exception {
99          File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
100         AbstractJxrReport mojo = createReportMojo(goal, pluginXmlFile);
101         return generateReport(mojo, pluginXmlFile);
102     }
103 
104     protected AbstractJxrReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
105         AbstractJxrReport mojo = (AbstractJxrReport) lookupMojo(goal, pluginXmlFile);
106         assertNotNull("Mojo not found.", mojo);
107 
108         LegacySupport legacySupport = lookup(LegacySupport.class);
109         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
110         DefaultRepositorySystemSession repoSession =
111                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
112         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
113                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
114 
115         setVariableValueToObject(mojo, "session", legacySupport.getSession());
116         setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
117         return mojo;
118     }
119 
120     protected File generateReport(AbstractJxrReport mojo, File pluginXmlFile) throws Exception {
121         mojo.execute();
122 
123         ProjectBuilder builder = lookup(ProjectBuilder.class);
124 
125         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
126         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
127 
128         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
129 
130         File outputDir = mojo.getReportOutputDirectory();
131         String filename = mojo.getOutputName() + ".html";
132 
133         return new File(outputDir, filename);
134     }
135 
136     /**
137      * Read the contents of the specified file object into a string
138      */
139     protected String readFile(File xrefTestDir, String fileName) throws IOException {
140         return new String(Files.readAllBytes(xrefTestDir.toPath().resolve(fileName)));
141     }
142 }