1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46 public abstract class AbstractJxrTestCase extends AbstractMojoTestCase {
47 private ArtifactStubFactory artifactStubFactory;
48
49
50
51
52 private MavenProject testMavenProject;
53
54 @Override
55 protected void setUp() throws Exception {
56
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
70
71
72
73 protected MavenProject getTestMavenProject() {
74 return testMavenProject;
75 }
76
77
78
79
80
81
82
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
98
99
100
101
102
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
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 }