1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
41
42
43
44
45
46 public abstract class AbstractProjectInfoTestCase extends AbstractMojoTestCase {
47 private ArtifactStubFactory artifactStubFactory;
48
49
50
51
52 protected static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
53
54
55
56
57 private MavenProject testMavenProject;
58
59
60
61
62 private I18N i18n;
63
64 @Override
65 protected void setUp() throws Exception {
66
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
76 Locale.setDefault(DEFAULT_LOCALE);
77 }
78
79 @Override
80 protected void tearDown() throws Exception {
81 super.tearDown();
82 }
83
84
85
86
87
88
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
100
101
102
103
104
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
120
121
122
123 protected MavenProject getTestMavenProject() {
124 return testMavenProject;
125 }
126
127
128
129
130
131
132
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
148
149
150
151
152
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 }